1- module Cardano.ReCon.Cli (Mode (.. ), CliOptions (.. ), opts ) where
1+ module Cardano.ReCon.Cli (Timeunit ( .. ), timeunitToMicrosecond , Mode (.. ), CliOptions (.. ), opts ) where
22
33
44import Control.Arrow ((>>>) )
@@ -20,6 +20,7 @@ readMode = eitherReader $ \case
2020 " online" -> Right Online
2121 _ -> Left " Expected either of: 'offline' or 'online'"
2222
23+
2324readBool :: ReadM Bool
2425readBool = eitherReader $ fmap toLower >>> \ case
2526 " true" -> Right True
@@ -31,6 +32,51 @@ readBool = eitherReader $ fmap toLower >>> \case
3132parseMode :: Parser Mode
3233parseMode = option readMode (long " mode" <> metavar " <offline|online>" <> help " mode" )
3334
35+ data Timeunit = Hour | Minute | Second | Millisecond | Microsecond deriving (Ord , Eq )
36+
37+ -- | Convert `Timeunit` to μs.
38+ timeunitToMicrosecond :: Timeunit -> Word -> Word
39+ timeunitToMicrosecond Hour = (3_600_000_000 * )
40+ timeunitToMicrosecond Minute = (60_000_000 * )
41+ timeunitToMicrosecond Second = (1_000_000 * )
42+ timeunitToMicrosecond Millisecond = (1_000 * )
43+ timeunitToMicrosecond Microsecond = id
44+
45+ instance Show Timeunit where
46+ show Hour = " hour"
47+ show Minute = " minute"
48+ show Second = " second"
49+ show Millisecond = " millisecond"
50+ show Microsecond = " microsecond"
51+
52+ readTimeunit :: ReadM Timeunit
53+ readTimeunit = eitherReader $ fmap toLower >>> \ case
54+ " hour" -> Right Hour
55+ " h" -> Right Hour
56+ " minute" -> Right Minute
57+ " min" -> Right Minute
58+ " m" -> Right Minute
59+ " second" -> Right Second
60+ " sec" -> Right Second
61+ " s" -> Right Second
62+ " millisecond" -> Right Millisecond
63+ " millisec" -> Right Millisecond
64+ " millis" -> Right Millisecond
65+ " ms" -> Right Millisecond
66+ " microsecond" -> Right Microsecond
67+ " microsec" -> Right Microsecond
68+ " micros" -> Right Microsecond
69+ " μs" -> Right Microsecond
70+ _ -> Left " Can't read a Timeunit"
71+
72+ parseTimeunit :: Parser Timeunit
73+ parseTimeunit = option readTimeunit $
74+ long " timeunit"
75+ <> metavar " <hour|minute|second|millisecond|microsecond>"
76+ <> showDefault
77+ <> value Second
78+ <> help " timeunit"
79+
3480parseEventDuration :: Parser Word
3581parseEventDuration = option auto (long " duration" <> metavar " INT" <> help " temporal event duration (μs)" )
3682
@@ -82,6 +128,7 @@ data CliOptions = CliOptions
82128 , context :: Maybe FilePath
83129 , enableProgressDumps :: Bool
84130 , enableSeekToEnd :: Bool
131+ , timeunit :: Timeunit
85132 }
86133
87134parseCliOptions :: Parser CliOptions
@@ -95,6 +142,7 @@ parseCliOptions = CliOptions
95142 <*> parseContext
96143 <*> parseDumpMetrics
97144 <*> parseSeekToEnd
145+ <*> parseTimeunit
98146
99147opts :: ParserInfo CliOptions
100148opts = info (parseCliOptions <**> helper)
0 commit comments