{-# LANGUAGE OverloadedStrings #-}
-- Warning turned off for CI
{-# OPTIONS_GHC -Wwarn=unused-imports #-}
module Eventlog.Args
  (
    args
  , argsInfo
  , Args(..)
  , Option(..)
  , Sort(..)
  , defaultArgs
  ) where

import Options.Applicative
import Data.Text (Text)
-- Used for GHC 8.6.5
import Data.Semigroup ((<>))
import Control.Applicative (optional)

data Option
  = ShowVersion
  | Run Args

data Sort = Size | StdDev | Name | Gradient

data Args = Args
  {
    Args -> Sort
sorting      :: Sort
  , Args -> Bool
reversing    :: Bool
  , Args -> Int
nBands       :: Int
  , Args -> Maybe Int
detailedLimit :: Maybe Int
  , Args -> Bool
heapProfile  :: Bool
  , Args -> Bool
noIncludejs    :: Bool
  , Args -> Bool
json         :: Bool
  , Args -> Bool
noTraces     :: Bool
  , Args -> Bool
traceEvents  :: Bool -- ^ By default, only traceMarkers are included.
                         -- This option enables the inclusion of traceEvents.
  , Args -> Text
userColourScheme :: Text
  , Args -> Maybe Int
fixedYAxis :: Maybe Int
  , Args -> [Text]
includeStr :: [Text]
  , Args -> [Text]
excludeStr :: [Text]
  , Args -> Maybe String
outputFile :: Maybe String
  , Args -> [String]
files        :: [String]
  }

argParser :: Parser Option
argParser :: Parser Option
argParser = Args -> Option
Run forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Sort
-> Bool
-> Int
-> Maybe Int
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Text
-> Maybe Int
-> [Text]
-> [Text]
-> Maybe String
-> [String]
-> Args
Args
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Sort
parseSort
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"sort"
         forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"How to sort the bands.  One of: size (default), stddev, name, gradient."
         forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Sort
Size
         forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"FIELD" )
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"reverse"
         forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Reverse the order of bands." )
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. ReadM a -> Mod OptionFields a -> Parser a
option forall a. Read a => ReadM a
auto
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"bands"
         forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Maximum number of bands to draw (0 for unlimited)."
         forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Int
15
         forall a. Semigroup a => a -> a -> a
<> forall a (f :: * -> *). Show a => Mod f a
showDefault
         forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"COUNT" )
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall a b. (a -> b) -> a -> b
$ forall a. ReadM a -> Mod OptionFields a -> Parser a
option forall a. Read a => ReadM a
auto
          (forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"limit-detailed"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"The maximum number of bands to show in the detailed view."
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"N"))
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"heap-profile"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'p'
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Input files are .hp heap profiles.")
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
          (forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"no-include-js"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Fetch the javascript from a CDN rather than bundling it into the file.")
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"json"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'j'
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Output JSON")
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"no-traces"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Don't display traces on chart")
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Mod FlagFields Bool -> Parser Bool
switch
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"include-trace-events"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help (String
"Enables the inclusion of traces emitted using `traceEvent`"
                   forall a. [a] -> [a] -> [a]
++ String
", which should only be used for high-frequency events. "
                   forall a. [a] -> [a] -> [a]
++ String
"For low frequency events, use `traceMarker` instead.")
          forall a. Semigroup a => a -> a -> a
<> forall a (f :: * -> *). Show a => Mod f a
showDefault)
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. ReadM a -> Mod OptionFields a -> Parser a
option forall s. IsString s => ReadM s
str
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"colour-scheme"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasValue f => a -> Mod f a
value Text
"category20b"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"The name of the colour scheme. See the vega documentation (https://vega.github.io/vega/docs/schemes/#reference) for a complete list. Examples include \"category10\" \"dark2\" \"tableau10\". ")
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. ReadM a -> Mod OptionFields a -> Parser a
option (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Read a => ReadM a
auto)
          ( forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"y-axis"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasValue f => a -> Mod f a
value forall a. Maybe a
Nothing
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Fixed value for the maximum extent of the y-axis in bytes. This option is useful for comparing profiles together.")
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (forall a. ReadM a -> Mod OptionFields a -> Parser a
option forall s. IsString s => ReadM s
str
          (forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'i'
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"include"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help (String
"Specify the traces which should be included in the output. Only traces which contain SUBSTRING "
                    forall a. [a] -> [a] -> [a]
++ String
"in their name will be included. Multiple different traces can be included "
                    forall a. [a] -> [a] -> [a]
++ String
"with \"-i foo -i bar\".")
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"SUBSTRING"))
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (forall a. ReadM a -> Mod OptionFields a -> Parser a
option forall s. IsString s => ReadM s
str
          (forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'x'
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"exclude"
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help (String
"Specify the traces which should be excluded in the output. All traces which contain SUBSTRING "
                    forall a. [a] -> [a] -> [a]
++ String
"in their name will be excluded. Multiple different traces can be excluded "
                    forall a. [a] -> [a] -> [a]
++ String
"with \"-x foo -x bar\".")
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"SUBSTRING"))
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall a b. (a -> b) -> a -> b
$ forall a. ReadM a -> Mod OptionFields a -> Parser a
option forall s. IsString s => ReadM s
str
          (forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'o'
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Write the output to the given filename."
          forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"OUTFILE"))
      forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f [a]
some (forall a. ReadM a -> Mod ArgumentFields a -> Parser a
argument forall s. IsString s => ReadM s
str
          ( forall (f :: * -> *) a. String -> Mod f a
help String
"Eventlogs (FILE.eventlog will be converted to FILE.html)."
         forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => String -> Mod f a
metavar String
"FILES..." )))

parseSort :: ReadM Sort
parseSort :: ReadM Sort
parseSort = forall a. (String -> Either String a) -> ReadM a
eitherReader forall a b. (a -> b) -> a -> b
$ \String
s -> case String
s of
  String
"size" -> forall a b. b -> Either a b
Right Sort
Size
  String
"stddev" -> forall a b. b -> Either a b
Right Sort
StdDev
  String
"name" -> forall a b. b -> Either a b
Right Sort
Name
  String
"gradient" -> forall a b. b -> Either a b
Right Sort
Gradient
  String
_ -> forall a b. a -> Either a b
Left String
"expected one of: size, stddev, name"

args :: IO Option
args :: IO Option
args = forall a. ParserInfo a -> IO a
execParser ParserInfo Option
argsInfo


versionParser :: Parser Option
versionParser :: Parser Option
versionParser = forall a. a -> Mod FlagFields a -> Parser a
flag' Option
ShowVersion
  (  forall (f :: * -> *) a. HasName f => String -> Mod f a
long String
"version"
  forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'v'
  forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. String -> Mod f a
help String
"Show the version of eventlog2html" )

defaultArgs :: FilePath -> IO Option
defaultArgs :: String -> IO Option
defaultArgs String
fp = forall a. ParserResult a -> IO a
handleParseResult (forall a. ParserPrefs -> ParserInfo a -> [String] -> ParserResult a
execParserPure ParserPrefs
defaultPrefs ParserInfo Option
argsInfo [String
fp])


argsInfo :: ParserInfo Option
argsInfo :: ParserInfo Option
argsInfo = ParserInfo Option
opts
  where
    opts :: ParserInfo Option
opts = forall a. Parser a -> InfoMod a -> ParserInfo a
info ((Parser Option
argParser forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Option
versionParser) forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> forall a. Parser (a -> a)
helper)
      ( forall a. InfoMod a
fullDesc
     forall a. Semigroup a => a -> a -> a
<> forall a. String -> InfoMod a
progDesc String
"Convert eventlogs FILES.eventlog to interactive FILES.html"
     forall a. Semigroup a => a -> a -> a
<> forall a. String -> InfoMod a
header String
"eventlog2html - generate interactive html from eventlogs" )