\subsection{Cardano.BM.Data.Counter}
\label{code:Cardano.BM.Data.Counter}

%if style == newcode
\begin{code}
{-# LANGUAGE DeriveAnyClass     #-}
{-# LANGUAGE DeriveGeneric      #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Cardano.BM.Data.Counter
  ( Counter (..)
  , CounterType (..)
  , CounterState (..)
  , Platform (..)
  , PlatformCode (..)
  , diffCounters
  , nameCounter
  )
  where

import           Data.Aeson (FromJSON (..), ToJSON, toEncoding,
                     toJSON)
import qualified Data.HashMap.Strict as HM
import           Data.Maybe (catMaybes)
import           Data.Text (Text)
import           Data.Time.Units (Microsecond, toMicroseconds)
import           GHC.Generics (Generic)

import           Cardano.BM.Data.Aggregated (Measurable (..))

\end{code}
%endif


\subsubsection{Counter}\label{code:Counter}\index{Counter}\label{code:CounterType}\index{CounterType}
\begin{code}
data Counter = Counter
               { Counter -> CounterType
cType  :: !CounterType
               , Counter -> Text
cName  :: !Text
               , Counter -> Measurable
cValue :: !Measurable
               }
               deriving (Int -> Counter -> ShowS
[Counter] -> ShowS
Counter -> String
(Int -> Counter -> ShowS)
-> (Counter -> String) -> ([Counter] -> ShowS) -> Show Counter
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Counter] -> ShowS
$cshowList :: [Counter] -> ShowS
show :: Counter -> String
$cshow :: Counter -> String
showsPrec :: Int -> Counter -> ShowS
$cshowsPrec :: Int -> Counter -> ShowS
Show, Counter -> Counter -> Bool
(Counter -> Counter -> Bool)
-> (Counter -> Counter -> Bool) -> Eq Counter
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Counter -> Counter -> Bool
$c/= :: Counter -> Counter -> Bool
== :: Counter -> Counter -> Bool
$c== :: Counter -> Counter -> Bool
Eq, (forall x. Counter -> Rep Counter x)
-> (forall x. Rep Counter x -> Counter) -> Generic Counter
forall x. Rep Counter x -> Counter
forall x. Counter -> Rep Counter x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Counter x -> Counter
$cfrom :: forall x. Counter -> Rep Counter x
Generic, [Counter] -> Encoding
[Counter] -> Value
Counter -> Encoding
Counter -> Value
(Counter -> Value)
-> (Counter -> Encoding)
-> ([Counter] -> Value)
-> ([Counter] -> Encoding)
-> ToJSON Counter
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Counter] -> Encoding
$ctoEncodingList :: [Counter] -> Encoding
toJSONList :: [Counter] -> Value
$ctoJSONList :: [Counter] -> Value
toEncoding :: Counter -> Encoding
$ctoEncoding :: Counter -> Encoding
toJSON :: Counter -> Value
$ctoJSON :: Counter -> Value
ToJSON, Value -> Parser [Counter]
Value -> Parser Counter
(Value -> Parser Counter)
-> (Value -> Parser [Counter]) -> FromJSON Counter
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Counter]
$cparseJSONList :: Value -> Parser [Counter]
parseJSON :: Value -> Parser Counter
$cparseJSON :: Value -> Parser Counter
FromJSON)

data CounterType = MonotonicClockTime
                 | MemoryCounter
                 | SysInfo
                 | StatInfo
                 | IOCounter
                 | NetCounter
                 | RTSStats
                   deriving (CounterType -> CounterType -> Bool
(CounterType -> CounterType -> Bool)
-> (CounterType -> CounterType -> Bool) -> Eq CounterType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CounterType -> CounterType -> Bool
$c/= :: CounterType -> CounterType -> Bool
== :: CounterType -> CounterType -> Bool
$c== :: CounterType -> CounterType -> Bool
Eq, Int -> CounterType -> ShowS
[CounterType] -> ShowS
CounterType -> String
(Int -> CounterType -> ShowS)
-> (CounterType -> String)
-> ([CounterType] -> ShowS)
-> Show CounterType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CounterType] -> ShowS
$cshowList :: [CounterType] -> ShowS
show :: CounterType -> String
$cshow :: CounterType -> String
showsPrec :: Int -> CounterType -> ShowS
$cshowsPrec :: Int -> CounterType -> ShowS
Show, (forall x. CounterType -> Rep CounterType x)
-> (forall x. Rep CounterType x -> CounterType)
-> Generic CounterType
forall x. Rep CounterType x -> CounterType
forall x. CounterType -> Rep CounterType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CounterType x -> CounterType
$cfrom :: forall x. CounterType -> Rep CounterType x
Generic, [CounterType] -> Encoding
[CounterType] -> Value
CounterType -> Encoding
CounterType -> Value
(CounterType -> Value)
-> (CounterType -> Encoding)
-> ([CounterType] -> Value)
-> ([CounterType] -> Encoding)
-> ToJSON CounterType
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [CounterType] -> Encoding
$ctoEncodingList :: [CounterType] -> Encoding
toJSONList :: [CounterType] -> Value
$ctoJSONList :: [CounterType] -> Value
toEncoding :: CounterType -> Encoding
$ctoEncoding :: CounterType -> Encoding
toJSON :: CounterType -> Value
$ctoJSON :: CounterType -> Value
ToJSON, Value -> Parser [CounterType]
Value -> Parser CounterType
(Value -> Parser CounterType)
-> (Value -> Parser [CounterType]) -> FromJSON CounterType
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [CounterType]
$cparseJSONList :: Value -> Parser [CounterType]
parseJSON :: Value -> Parser CounterType
$cparseJSON :: Value -> Parser CounterType
FromJSON)

instance ToJSON Microsecond where
    toJSON :: Microsecond -> Value
toJSON     = Integer -> Value
forall a. ToJSON a => a -> Value
toJSON     (Integer -> Value)
-> (Microsecond -> Integer) -> Microsecond -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Microsecond -> Integer
forall a. TimeUnit a => a -> Integer
toMicroseconds
    toEncoding :: Microsecond -> Encoding
toEncoding = Integer -> Encoding
forall a. ToJSON a => a -> Encoding
toEncoding (Integer -> Encoding)
-> (Microsecond -> Integer) -> Microsecond -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Microsecond -> Integer
forall a. TimeUnit a => a -> Integer
toMicroseconds

\end{code}

\subsubsection{Names of counters}\label{code:nameCounter}\index{nameCounter}
\begin{code}
nameCounter :: Counter -> Text
nameCounter :: Counter -> Text
nameCounter (Counter CounterType
MonotonicClockTime Text
_ Measurable
_) = Text
"Clock"
nameCounter (Counter CounterType
MemoryCounter      Text
_ Measurable
_) = Text
"Mem"
nameCounter (Counter CounterType
SysInfo            Text
_ Measurable
_) = Text
"Sys"
nameCounter (Counter CounterType
StatInfo           Text
_ Measurable
_) = Text
"Stat"
nameCounter (Counter CounterType
IOCounter          Text
_ Measurable
_) = Text
"IO"
nameCounter (Counter CounterType
NetCounter         Text
_ Measurable
_) = Text
"Net"
nameCounter (Counter CounterType
RTSStats           Text
_ Measurable
_) = Text
"RTS"

\end{code}

\subsubsection{CounterState}\label{code:CounterState}\index{CounterState}
\begin{code}
data CounterState = CounterState {
      CounterState -> [Counter]
csCounters   :: [Counter]
    }
    deriving (Int -> CounterState -> ShowS
[CounterState] -> ShowS
CounterState -> String
(Int -> CounterState -> ShowS)
-> (CounterState -> String)
-> ([CounterState] -> ShowS)
-> Show CounterState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CounterState] -> ShowS
$cshowList :: [CounterState] -> ShowS
show :: CounterState -> String
$cshow :: CounterState -> String
showsPrec :: Int -> CounterState -> ShowS
$cshowsPrec :: Int -> CounterState -> ShowS
Show, CounterState -> CounterState -> Bool
(CounterState -> CounterState -> Bool)
-> (CounterState -> CounterState -> Bool) -> Eq CounterState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CounterState -> CounterState -> Bool
$c/= :: CounterState -> CounterState -> Bool
== :: CounterState -> CounterState -> Bool
$c== :: CounterState -> CounterState -> Bool
Eq, (forall x. CounterState -> Rep CounterState x)
-> (forall x. Rep CounterState x -> CounterState)
-> Generic CounterState
forall x. Rep CounterState x -> CounterState
forall x. CounterState -> Rep CounterState x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CounterState x -> CounterState
$cfrom :: forall x. CounterState -> Rep CounterState x
Generic, [CounterState] -> Encoding
[CounterState] -> Value
CounterState -> Encoding
CounterState -> Value
(CounterState -> Value)
-> (CounterState -> Encoding)
-> ([CounterState] -> Value)
-> ([CounterState] -> Encoding)
-> ToJSON CounterState
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [CounterState] -> Encoding
$ctoEncodingList :: [CounterState] -> Encoding
toJSONList :: [CounterState] -> Value
$ctoJSONList :: [CounterState] -> Value
toEncoding :: CounterState -> Encoding
$ctoEncoding :: CounterState -> Encoding
toJSON :: CounterState -> Value
$ctoJSON :: CounterState -> Value
ToJSON, Value -> Parser [CounterState]
Value -> Parser CounterState
(Value -> Parser CounterState)
-> (Value -> Parser [CounterState]) -> FromJSON CounterState
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [CounterState]
$cparseJSONList :: Value -> Parser [CounterState]
parseJSON :: Value -> Parser CounterState
$cparseJSON :: Value -> Parser CounterState
FromJSON)

\end{code}

\subsubsection{Difference between counters}\label{code:diffCounters}\index{diffCounters}
\begin{code}
diffCounters :: [Counter] -> [Counter] -> [Counter]
diffCounters :: [Counter] -> [Counter] -> [Counter]
diffCounters [Counter]
openings [Counter]
closings =
    [Counter] -> [Counter] -> [Counter]
getCountersDiff [Counter]
openings [Counter]
closings
  where
    getCountersDiff :: [Counter]
                    -> [Counter]
                    -> [Counter]
    getCountersDiff :: [Counter] -> [Counter] -> [Counter]
getCountersDiff [Counter]
as [Counter]
bs =
        let
            getName :: Counter -> Text
getName Counter
counter = Counter -> Text
nameCounter Counter
counter Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Counter -> Text
cName Counter
counter

            asNames :: [Text]
asNames = (Counter -> Text) -> [Counter] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Counter -> Text
getName [Counter]
as
            aPairs :: [(Text, Counter)]
aPairs = [Text] -> [Counter] -> [(Text, Counter)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Text]
asNames [Counter]
as

            bsNames :: [Text]
bsNames = (Counter -> Text) -> [Counter] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Counter -> Text
getName [Counter]
bs
            bs' :: [(Text, Counter)]
bs' = [Text] -> [Counter] -> [(Text, Counter)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Text]
bsNames [Counter]
bs
            bPairs :: HashMap Text Counter
bPairs = [(Text, Counter)] -> HashMap Text Counter
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HM.fromList [(Text, Counter)]
bs'
        in
            [Maybe Counter] -> [Counter]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe Counter] -> [Counter]) -> [Maybe Counter] -> [Counter]
forall a b. (a -> b) -> a -> b
$ ((((Text, Counter) -> Maybe Counter)
 -> [(Text, Counter)] -> [Maybe Counter])
-> [(Text, Counter)]
-> ((Text, Counter) -> Maybe Counter)
-> [Maybe Counter]
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((Text, Counter) -> Maybe Counter)
-> [(Text, Counter)] -> [Maybe Counter]
forall a b. (a -> b) -> [a] -> [b]
map) [(Text, Counter)]
aPairs (((Text, Counter) -> Maybe Counter) -> [Maybe Counter])
-> ((Text, Counter) -> Maybe Counter) -> [Maybe Counter]
forall a b. (a -> b) -> a -> b
$ \(Text
name, Counter CounterType
_ Text
_ Measurable
startValue) ->
                case Text -> HashMap Text Counter -> Maybe Counter
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup Text
name HashMap Text Counter
bPairs of
                    Maybe Counter
Nothing       -> Maybe Counter
forall a. Maybe a
Nothing
                    Just Counter
counter  -> let endValue :: Measurable
endValue = Counter -> Measurable
cValue Counter
counter
                                     in Counter -> Maybe Counter
forall a. a -> Maybe a
Just Counter
counter {cValue :: Measurable
cValue = Measurable
endValue Measurable -> Measurable -> Measurable
forall a. Num a => a -> a -> a
- Measurable
startValue}

\end{code}

\subsubsection{Platform information}\label{code:PlatformCode}\index{PlatformCode}
\begin{code}
data Platform = UnknownPlatform | Linux | Darwin | Windows
                deriving (Int -> Platform -> ShowS
[Platform] -> ShowS
Platform -> String
(Int -> Platform -> ShowS)
-> (Platform -> String) -> ([Platform] -> ShowS) -> Show Platform
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Platform] -> ShowS
$cshowList :: [Platform] -> ShowS
show :: Platform -> String
$cshow :: Platform -> String
showsPrec :: Int -> Platform -> ShowS
$cshowsPrec :: Int -> Platform -> ShowS
Show, Platform -> Platform -> Bool
(Platform -> Platform -> Bool)
-> (Platform -> Platform -> Bool) -> Eq Platform
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Platform -> Platform -> Bool
$c/= :: Platform -> Platform -> Bool
== :: Platform -> Platform -> Bool
$c== :: Platform -> Platform -> Bool
Eq, Eq Platform
Eq Platform
-> (Platform -> Platform -> Ordering)
-> (Platform -> Platform -> Bool)
-> (Platform -> Platform -> Bool)
-> (Platform -> Platform -> Bool)
-> (Platform -> Platform -> Bool)
-> (Platform -> Platform -> Platform)
-> (Platform -> Platform -> Platform)
-> Ord Platform
Platform -> Platform -> Bool
Platform -> Platform -> Ordering
Platform -> Platform -> Platform
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Platform -> Platform -> Platform
$cmin :: Platform -> Platform -> Platform
max :: Platform -> Platform -> Platform
$cmax :: Platform -> Platform -> Platform
>= :: Platform -> Platform -> Bool
$c>= :: Platform -> Platform -> Bool
> :: Platform -> Platform -> Bool
$c> :: Platform -> Platform -> Bool
<= :: Platform -> Platform -> Bool
$c<= :: Platform -> Platform -> Bool
< :: Platform -> Platform -> Bool
$c< :: Platform -> Platform -> Bool
compare :: Platform -> Platform -> Ordering
$ccompare :: Platform -> Platform -> Ordering
$cp1Ord :: Eq Platform
Ord, Int -> Platform
Platform -> Int
Platform -> [Platform]
Platform -> Platform
Platform -> Platform -> [Platform]
Platform -> Platform -> Platform -> [Platform]
(Platform -> Platform)
-> (Platform -> Platform)
-> (Int -> Platform)
-> (Platform -> Int)
-> (Platform -> [Platform])
-> (Platform -> Platform -> [Platform])
-> (Platform -> Platform -> [Platform])
-> (Platform -> Platform -> Platform -> [Platform])
-> Enum Platform
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Platform -> Platform -> Platform -> [Platform]
$cenumFromThenTo :: Platform -> Platform -> Platform -> [Platform]
enumFromTo :: Platform -> Platform -> [Platform]
$cenumFromTo :: Platform -> Platform -> [Platform]
enumFromThen :: Platform -> Platform -> [Platform]
$cenumFromThen :: Platform -> Platform -> [Platform]
enumFrom :: Platform -> [Platform]
$cenumFrom :: Platform -> [Platform]
fromEnum :: Platform -> Int
$cfromEnum :: Platform -> Int
toEnum :: Int -> Platform
$ctoEnum :: Int -> Platform
pred :: Platform -> Platform
$cpred :: Platform -> Platform
succ :: Platform -> Platform
$csucc :: Platform -> Platform
Enum)
newtype PlatformCode = PlatformCode { PlatformCode -> Platform
platform :: Platform }

instance Show PlatformCode where
    show :: PlatformCode -> String
show (PlatformCode Platform
p) = Platform -> String
forall a. Show a => a -> String
show Platform
p
\end{code}