From c764ad420918ab8aecfd777a82d8d702b0993e7f Mon Sep 17 00:00:00 2001 From: Hendrik Jaeger Date: Sat, 15 Nov 2014 00:05:14 +0100 Subject: [PATCH] Changed format and structure a bit modified: src/Main.hs --- src/Main.hs | 60 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index 6884d14..24fefa7 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -11,7 +11,7 @@ import qualified Data.Map as Map import qualified Data.Text as T import qualified Data.Text.IO as TIO -data Opt = Opt +data Optionset = Optionset { optVerbose :: Bool , optVersion :: Bool , optHelp :: Bool @@ -23,8 +23,8 @@ data Opt = Opt , optEndDate :: String } -defaultOpts :: Opt -defaultOpts = Opt +defaultOpts :: Optionset +defaultOpts = Optionset { optVerbose = False , optVersion = False , optHelp = False @@ -36,34 +36,34 @@ defaultOpts = Opt , optEndDate = "" } -availableOptions :: [OptDescr (Opt -> IO Opt)] +availableOptions :: [OptDescr (Optionset -> IO Optionset)] availableOptions = [ Option ['h'] ["help"] (NoArg (\ _ -> putStrLn (usageInfo "Usage: diddohs [OPTION...]" availableOptions) >> exitSuccess)) "Display program help" , Option ['v'] ["verbose"] - (NoArg (\ opts -> return opts { optVerbose = True })) + (NoArg (\ optset -> return optset { optVerbose = True })) "More detailed output" , Option ['V'] ["version"] - (NoArg (\ opts -> return opts { optVersion = True })) + (NoArg (\ optset -> return optset { optVersion = True })) "Display program version" , Option ['f'] ["file"] - (ReqArg (\ arg opts -> return opts { optInputFiles = optInputFiles opts ++ [arg]}) "FILE" ) + (ReqArg (\ arg optset -> return optset { optInputFiles = optInputFiles optset ++ [arg]}) "FILE" ) "Read from FILE" , Option ['w'] ["output"] - (ReqArg (\ arg opts -> return opts { optOutputFile = arg }) "FILE") + (ReqArg (\ arg optset -> return optset { optOutputFile = arg }) "FILE") "Write to FILE" , Option ['i'] ["informat"] - (ReqArg (\ arg opts -> return opts { optInputFormat = arg }) "FORMAT") + (ReqArg (\ arg optset -> return optset { optInputFormat = arg }) "FORMAT") "Timeformat used in input" , Option ['o'] ["outformat"] - (ReqArg (\ arg opts -> return opts { optOutputFormat = arg }) "FORMAT") + (ReqArg (\ arg optset -> return optset { optOutputFormat = arg }) "FORMAT") "Timeformat used in output" , Option ['s'] ["start"] - (ReqArg (\ arg opts -> return opts { optStartDate = arg }) "DATE") + (ReqArg (\ arg optset -> return optset { optStartDate = arg }) "DATE") "Start of reporting period" , Option ['e'] ["end"] - (ReqArg (\ arg opts -> return opts { optEndDate = arg }) "DATE") + (ReqArg (\ arg optset -> return optset { optEndDate = arg }) "DATE") "End of reporting period" ] @@ -77,34 +77,44 @@ logentryMapToDiddoMap logmap = Map.mapWithKey toDddEntry logmap Nothing -> fst $ Map.findMin logmap -- SECTION: Map of logentries to Map of DiddoEntries +getLoglines :: [String] -> IO [T.Text] +getLoglines [] = T.lines <$> TIO.getContents +getLoglines files@(_:_) = T.lines . T.concat <$> mapM TIO.readFile files + main :: IO () main = do -- SECTION: option processing - (givenOptions,args,errs) <- getArgs >>= return . getOpt Permute availableOptions + (userOpts,args,errs) <- + getArgs >>= return . getOpt Permute availableOptions unless (null errs) $ do mapM_ (hPutStr stderr) errs exitFailure - effectiveOptions <- foldl (>>=) (return defaultOpts) givenOptions + opts <- foldl (>>=) (return defaultOpts) userOpts let - inDateFmt = optInputFormat effectiveOptions - outDateFmt = optOutputFormat effectiveOptions + inDateFmt = optInputFormat opts + outDateFmt = optOutputFormat opts - startDate = parseToZonedTime inDateFmt $ optStartDate effectiveOptions - endDate = parseToZonedTime inDateFmt $ optEndDate effectiveOptions + startDate = parseToZonedTime inDateFmt $ optStartDate opts + endDate = parseToZonedTime inDateFmt $ optEndDate opts -- SECTION: option processing - loglines <- case optInputFiles effectiveOptions of - files@(_:_) -> T.lines . T.concat <$> mapM TIO.readFile files - [] -> T.lines <$> TIO.getContents + loglines <- getLoglines $ optInputFiles opts let - timestampLogentryMap = Map.fromList $ map Diddo.parseDiddoLogline loglines - (_, _, startedTimestampLogentryMap) = Map.splitLookup (zonedTimeToUTC startDate) timestampLogentryMap - (endedTimestampLogentryMap, lastEntry, _) = Map.splitLookup (zonedTimeToUTC endDate) startedTimestampLogentryMap - timestampDiddoMap = logentryMapToDiddoMap timestampLogentryMap + timestampLogentryMap = + Map.fromList $ map Diddo.parseDiddoLogline loglines + + (_, _, startedTimestampLogentryMap) = + Map.splitLookup (zonedTimeToUTC startDate) timestampLogentryMap + + (endedTimestampLogentryMap, lastEntry, _) = + Map.splitLookup (zonedTimeToUTC endDate) startedTimestampLogentryMap + + timestampDiddoMap = + logentryMapToDiddoMap timestampLogentryMap -- DEBUG mapM_ putStrLn args -- 2.39.2