]> git.netwichtig.de Git - user/henk/code/haskell/diddohs.git/blobdiff - diddohs.hs
On branch master
[user/henk/code/haskell/diddohs.git] / diddohs.hs
index 7ee35cd3dad1b4bb50236cdd69cb778f08cd6ca8..0444e22aaec24fdc98ea57636160933adf4e7329 100644 (file)
-import Control.Applicative( (<$>), (<*>), liftA, liftA2 )
-import Data.DateTime( parseDateTime, startOfTime, diffSeconds, formatDateTime )
-import Data.List( zip4, zipWith4, transpose )
-import Data.List.Split( splitOn )
-import Data.Maybe( fromJust, fromMaybe )
-import Data.Time.Clock( secondsToDiffTime )
+import Control.Applicative( (<$>) )
+import Control.Monad( unless )
+import qualified Data.Map as Map
+import Data.Time.Clock( UTCTime(..) )
+import Diddo( DiddoEntry(..), LogEntry(..), parseDiddoLogline, formatDiddoEntry, timestamp, logToDiddoEntry )
+import System.Console.GetOpt
 import System.Environment( getArgs )
-import Text.Printf( printf )
+import System.Exit( exitSuccess, exitFailure )
+import System.IO( stderr, hPutStr )
 
-data HMSTime = HMSTime { hours :: Integer, minutes :: Integer, seconds :: Integer }
-instance Show HMSTime where
-  show (HMSTime h m s) = printf "%d:%02d:%02d" h m s
+data Opt = Opt
+    { optVerbose        :: Bool
+    , optVersion        :: Bool
+    , optHelp           :: Bool
+    , optInputFiles     :: [String]
+    , optOutputFile     :: String
+    , optInputFormat    :: String
+    , optOutputFormat   :: String
+    , optStartDate      :: String
+    , optEndDate        :: String
+    }
 
-secondsToHMS :: Integer -> HMSTime
-secondsToHMS seconds =  HMSTime h m s where
-                          (mLeft, s) = seconds `divMod` 60
-                          (h, m)     = mLeft `divMod` 60
+defaultOpts :: Opt
+defaultOpts = Opt
+    { optVerbose = False
+    , optVersion = False
+    , optHelp = False
+    , optInputFiles = []
+    , optOutputFile = ""
+    , optInputFormat = "%FT%T%z"
+    , optOutputFormat = "%FT%T%z"
+    , optStartDate = ""
+    , optEndDate = ""
+    }
 
-data DiddoEntry = DiddoEntry {  start :: String
-                                , finish :: String
-                                , delta :: HMSTime
-                                , entry :: String
-                             }
-instance Show DiddoEntry where
-  show (DiddoEntry start finish delta entry) = printf "%s;%s;%s;%s" start finish (show delta) entry
+availableOptions :: [OptDescr (Opt -> IO Opt)]
+availableOptions =
+    [ Option ['h']    ["help"]
+        (NoArg (\_ -> putStrLn (usageInfo "Usage: diddohs [OPTION...]" availableOptions) >> exitSuccess))
+        "Display program help"
+    , Option ['v']    ["verbose"]
+        (NoArg (\opts -> return opts { optVerbose = True }))
+        "More detailed output"
+    , Option ['V']    ["version"]
+        (NoArg (\opts -> return opts { optVersion = True }))
+        "Display program version"
+    , Option ['f']    ["file"]
+        (ReqArg (\arg opts -> return opts { optInputFiles = optInputFiles opts ++ [arg]}) "FILE" )
+        "Read from FILE"
+    , Option ['w']    ["output"]
+        (ReqArg (\arg opts -> return opts { optOutputFile = arg }) "FILE")
+        "Write to FILE"
+    , Option ['i']    ["informat"]
+        (ReqArg (\arg opts -> return opts { optInputFormat = arg }) "FORMAT")
+        "Timeformat used in input"
+    , Option ['o']    ["outformat"]
+        (ReqArg (\arg opts -> return opts { optOutputFormat = arg }) "FORMAT")
+        "Timeformat used in output"
+    , Option ['s']    ["start"]
+        (ReqArg (\arg opts -> return opts { optStartDate = arg }) "DATE")
+        "Start of reporting period"
+    , Option ['e']    ["end"]
+        (ReqArg (\arg opts -> return opts { optEndDate = arg }) "DATE")
+        "End of reporting period"
+    ]
+
+-- SECTION: Map of logentries to Map of DiddoEntries
+mapToDiddoEntries :: Map.Map UTCTime Diddo.LogEntry -> Map.Map UTCTime Diddo.DiddoEntry
+mapToDiddoEntries logmap = Map.mapWithKey toDddEntry logmap
+    where
+        toDddEntry key value = Diddo.logToDiddoEntry (preceedingTimestamp key) value
+        preceedingTimestamp x = case Map.lookupLT x logmap of
+            Just y          -> fst y
+            Nothing         -> fst $ Map.findMin logmap
+-- SECTION: Map of logentries to Map of DiddoEntries
 
 main :: IO ()
 main = do
-  logfile_name : timestring_format : _ <- getArgs
-  logfile_lines_split <- map (splitOn ";") . lines <$> readFile logfile_name
+    -- SECTION: option processing
+    (givenOptions,args,errs) <- getArgs >>= return . getOpt Permute availableOptions
+
+    unless (null errs) $ do
+        mapM_ (hPutStr stderr) errs
+        exitFailure
+
+    effectiveOptions <- foldl (>>=) (return defaultOpts) givenOptions
+    -- SECTION: option processing
+
+    dddLogEntries <-
+        map Diddo.parseDiddoLogline <$> case optInputFiles effectiveOptions of
+            files@(_:_)         -> lines . concat <$> mapM readFile files
+            []                  -> lines <$> getContents
+
+    let
+        dddLogEntryMap      = Map.fromList $ map (\diddo -> (Diddo.timestamp diddo, diddo)) dddLogEntries
+        diddoEntriesMap     = mapToDiddoEntries dddLogEntryMap
+        inDateFmt           = optInputFormat effectiveOptions
+        outDateFmt          = optOutputFormat effectiveOptions
 
-  let [timestrings_finish
-        , entries
-        ]                  = transpose logfile_lines_split
-      utcTimes_finish      = map (fromMaybe (error "Input data broken.") . parseDateTime timestring_format) timestrings_finish
-      entry_deltas_HMMSS   = zipWith (\x y -> secondsToHMS $ diffSeconds x y) utcTimes_finish (startOfTime : init utcTimes_finish)
-      diddos_summarized    = zipWith4 DiddoEntry ("" : init timestrings_finish) timestrings_finish entry_deltas_HMMSS entries
+    -- DEBUG
+    mapM_ putStrLn args
+    -- DEBUG
 
-  mapM_ print diddos_summarized
+    mapM_ (putStrLn . snd) $ Map.toAscList $ Map.map (Diddo.formatDiddoEntry outDateFmt) diddoEntriesMap