summaryrefslogtreecommitdiff
path: root/diddohs.hs
diff options
context:
space:
mode:
Diffstat (limited to 'diddohs.hs')
-rw-r--r--diddohs.hs77
1 files changed, 42 insertions, 35 deletions
diff --git a/diddohs.hs b/diddohs.hs
index 026ce16..6884d14 100644
--- a/diddohs.hs
+++ b/diddohs.hs
@@ -1,7 +1,8 @@
import Control.Applicative( (<$>) )
import Control.Monad( unless )
import Data.Time.Clock( UTCTime(..) )
-import Diddo( DiddoEntry(..), LogEntry(..), parseDiddoLogline, formatDiddoEntry, timestamp, logToDiddoEntry )
+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 System.Exit( exitSuccess, exitFailure )
@@ -24,56 +25,56 @@ data Opt = Opt
defaultOpts :: Opt
defaultOpts = Opt
- { optVerbose = False
- , optVersion = False
- , optHelp = False
- , optInputFiles = []
- , optOutputFile = ""
- , optInputFormat = "%FT%T%z"
- , optOutputFormat = "%FT%T%z"
- , optStartDate = ""
- , optEndDate = ""
+ { optVerbose = False
+ , optVersion = False
+ , optHelp = False
+ , optInputFiles = []
+ , optOutputFile = ""
+ , optInputFormat = "%FT%T%z"
+ , optOutputFormat = "%FT%T%z"
+ , optStartDate = ""
+ , optEndDate = ""
}
availableOptions :: [OptDescr (Opt -> IO Opt)]
availableOptions =
[ Option ['h'] ["help"]
- (NoArg (\_ -> putStrLn (usageInfo "Usage: diddohs [OPTION...]" availableOptions) >> exitSuccess))
+ (NoArg (\ _ -> putStrLn (usageInfo "Usage: diddohs [OPTION...]" availableOptions) >> exitSuccess))
"Display program help"
, Option ['v'] ["verbose"]
- (NoArg (\opts -> return opts { optVerbose = True }))
+ (NoArg (\ opts -> return opts { optVerbose = True }))
"More detailed output"
, Option ['V'] ["version"]
- (NoArg (\opts -> return opts { optVersion = True }))
+ (NoArg (\ opts -> return opts { optVersion = True }))
"Display program version"
, Option ['f'] ["file"]
- (ReqArg (\arg opts -> return opts { optInputFiles = optInputFiles opts ++ [arg]}) "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")
+ (ReqArg (\ arg opts -> return opts { optOutputFile = arg }) "FILE")
"Write to FILE"
, Option ['i'] ["informat"]
- (ReqArg (\arg opts -> return opts { optInputFormat = arg }) "FORMAT")
+ (ReqArg (\ arg opts -> return opts { optInputFormat = arg }) "FORMAT")
"Timeformat used in input"
, Option ['o'] ["outformat"]
- (ReqArg (\arg opts -> return opts { optOutputFormat = arg }) "FORMAT")
+ (ReqArg (\ arg opts -> return opts { optOutputFormat = arg }) "FORMAT")
"Timeformat used in output"
, Option ['s'] ["start"]
- (ReqArg (\arg opts -> return opts { optStartDate = arg }) "DATE")
+ (ReqArg (\ arg opts -> return opts { optStartDate = arg }) "DATE")
"Start of reporting period"
, Option ['e'] ["end"]
- (ReqArg (\arg opts -> return opts { optEndDate = arg }) "DATE")
+ (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
+-- 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.logToDiddoEntry (preceedingTimestamp key) value
- preceedingTimestamp x = case Map.lookupLT x logmap of
- Just y -> fst y
- Nothing -> fst $ Map.findMin logmap
+ 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
main :: IO ()
@@ -86,22 +87,28 @@ main = do
exitFailure
effectiveOptions <- foldl (>>=) (return defaultOpts) givenOptions
+
+ let
+ inDateFmt = optInputFormat effectiveOptions
+ outDateFmt = optOutputFormat effectiveOptions
+
+ startDate = parseToZonedTime inDateFmt $ optStartDate effectiveOptions
+ endDate = parseToZonedTime inDateFmt $ optEndDate effectiveOptions
-- SECTION: option processing
- dddLogEntries <-
- map Diddo.parseDiddoLogline <$> case optInputFiles effectiveOptions of
- files@(_:_) -> T.lines . T.concat <$> mapM TIO.readFile files
- [] -> T.lines <$> TIO.getContents
+ loglines <- case optInputFiles effectiveOptions of
+ files@(_:_) -> T.lines . T.concat <$> mapM TIO.readFile files
+ [] -> T.lines <$> TIO.getContents
let
- dddLogEntryMap = Map.fromList $ map (\diddo -> (Diddo.timestamp diddo, diddo)) dddLogEntries
- diddoEntriesMap = mapToDiddoEntries dddLogEntryMap
- inDateFmt = optInputFormat effectiveOptions
- outDateFmt = optOutputFormat effectiveOptions
+ 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.formatDiddoEntry outDateFmt) diddoEntriesMap
+ mapM_ (TIO.putStrLn . snd) $ Map.toAscList $ Map.map (Diddo.formatDiddo outDateFmt) timestampDiddoMap