X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=diddohs.hs;h=6884d1462cd52cb50d96fb6fc36c7b7dbf42fba4;hb=95d1857e437aa09e1161be075c654c50afd91dd8;hp=f54ec2d91b77da4e7f845a0337615a229c3a8654;hpb=d38cc424b83a0240b466f41ec1fe7521aba2d64e;p=user%2Fhenk%2Fcode%2Fhaskell%2Fdiddohs.git diff --git a/diddohs.hs b/diddohs.hs index f54ec2d..6884d14 100644 --- a/diddohs.hs +++ b/diddohs.hs @@ -1,47 +1,114 @@ -import Control.Applicative( (<$>), (<*>) ) -import Data.DateTime( DateTime(..), parseDateTime, formatDateTime, startOfTime, diffSeconds ) -import Data.List.Split( splitOn ) -import Data.List( zipWith4, transpose ) -import Data.Maybe( fromJust, fromMaybe ) -import Data.Monoid( mempty ) -import Data.Time.Clock( secondsToDiffTime ) -import Options.Applicative( execParser, info, strOption, long ) +import Control.Applicative( (<$>) ) +import Control.Monad( unless ) +import Data.Time.Clock( UTCTime(..) ) +import Data.Time.LocalTime( TimeZone(), ZonedTime(..), zonedTimeToUTC, utcToZonedTime ) +import Diddo( Diddo(..), LogEntry(..), parseDiddoLogline, formatDiddo, logToDiddo, parseToZonedTime ) +import System.Console.GetOpt import System.Environment( getArgs ) -import HMSTime( HMSTime(..), secondsToHMS ) -import Diddo.Entry( DiddoEntry(..) ) +import System.Exit( exitSuccess, exitFailure ) +import System.IO( stderr, hPutStr ) +import qualified Data.Map as Map +import qualified Data.Text as T +import qualified Data.Text.IO as TIO -data DiddoOpts = DiddoOpts - { inDateFmt :: String - , inFile :: String - } +data Opt = Opt + { optVerbose :: Bool + , optVersion :: Bool + , optHelp :: Bool + , optInputFiles :: [String] + , optOutputFile :: String + , optInputFormat :: String + , optOutputFormat :: String + , optStartDate :: String + , optEndDate :: String + } -calculateDeltas :: [DateTime] -> [Integer] -calculateDeltas dateTimes = zipWith diffSeconds dateTimes ((startOfDay $ head dateTimes) : init dateTimes) +defaultOpts :: Opt +defaultOpts = Opt + { optVerbose = False + , optVersion = False + , optHelp = False + , optInputFiles = [] + , optOutputFile = "" + , optInputFormat = "%FT%T%z" + , optOutputFormat = "%FT%T%z" + , optStartDate = "" + , optEndDate = "" + } -parseDateTimeFormat :: String -> String -> DateTime -parseDateTimeFormat format = fromMaybe (error "Input data broken.") . parseDateTime format +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" + ] -startOfDay :: DateTime -> DateTime -startOfDay = fromJust . parseDateTime "%x" . formatDateTime "%x" +-- SECTION: Map of logentries to Map of Diddos +logentryMapToDiddoMap :: Map.Map UTCTime Diddo.LogEntry -> Map.Map UTCTime Diddo.Diddo +logentryMapToDiddoMap logmap = Map.mapWithKey toDddEntry logmap + where + toDddEntry key value = Diddo.logToDiddo (precedingTimestamp key) value + precedingTimestamp x = case Map.lookupLT x logmap of + Just (y,_) -> y + Nothing -> fst $ Map.findMin logmap +-- SECTION: Map of logentries to Map of DiddoEntries -mainWithOpts :: DiddoOpts -> IO () -mainWithOpts opts = - do - [ timeStrings - , entries - ] <- transpose . map (splitOn ";") . lines <$> readFile (inFile opts) +main :: IO () +main = do + -- 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 let - deltasHMS = map secondsToHMS $ calculateDeltas $ map (parseDateTimeFormat (inDateFmt opts)) timeStrings - diddos_summarized = zipWith4 DiddoEntry ("" : init timeStrings) timeStrings deltasHMS entries + inDateFmt = optInputFormat effectiveOptions + outDateFmt = optOutputFormat effectiveOptions - mapM_ print diddos_summarized + startDate = parseToZonedTime inDateFmt $ optStartDate effectiveOptions + endDate = parseToZonedTime inDateFmt $ optEndDate effectiveOptions + -- SECTION: option processing -main :: IO () -main = execParser opts >>= mainWithOpts - where - opts = info parser mempty - parser = DiddoOpts - <$> strOption ( long "indateform" ) - <*> strOption ( long "infile" ) + loglines <- case optInputFiles effectiveOptions of + files@(_:_) -> T.lines . T.concat <$> mapM TIO.readFile files + [] -> T.lines <$> TIO.getContents + + let + 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 + -- DEBUG + + mapM_ (TIO.putStrLn . snd) $ Map.toAscList $ Map.map (Diddo.formatDiddo outDateFmt) timestampDiddoMap