module Network.Mux.DeltaQ.TraceStats
  ( step
  , OneWayDeltaQSample (..)
  , constructSample
  , StatsA
  , initialStatsA
  ) where

import           Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import           Data.Maybe
import           Data.Word (Word32)

import           Control.Monad.Class.MonadTime
import           Network.Mux.DeltaQ.TraceStatsSupport
import           Network.Mux.DeltaQ.TraceTypes
import           Network.Mux.Types

-- the per observation procesing step
step :: RemoteClockModel        -- ^ Remote clock timestamp
     -> Time                    -- ^ Local clock timestamp
     -> Int                     -- ^ the number of octets in the
                                --   observed outcome
     -> StatsA                  -- ^ accumulation state
     -> (Maybe OneWayDeltaQSample, StatsA)
step :: RemoteClockModel
-> Time -> Int -> StatsA -> (Maybe OneWayDeltaQSample, StatsA)
step RemoteClockModel
remoteTS Time
localTS Int
obsSize StatsA
s
 | Maybe (Word32, Time) -> Bool
forall a. Maybe a -> Bool
isNothing (StatsA -> Maybe (Word32, Time)
referenceTimePoint StatsA
s) -- first observation this sample period
   = RemoteClockModel
-> Time -> Int -> StatsA -> (Maybe OneWayDeltaQSample, StatsA)
step RemoteClockModel
remoteTS Time
localTS Int
obsSize
          (StatsA
s { referenceTimePoint :: Maybe (Word32, Time)
referenceTimePoint = (Word32, Time) -> Maybe (Word32, Time)
forall a. a -> Maybe a
Just ((Word32, Time) -> Maybe (Word32, Time))
-> (Word32, Time) -> Maybe (Word32, Time)
forall a b. (a -> b) -> a -> b
$! (RemoteClockModel -> Word32
unRemoteClockModel RemoteClockModel
remoteTS, Time
localTS)
             , nextSampleAt :: Time
nextSampleAt       = DiffTime
sampleInterval DiffTime -> Time -> Time
`addTime` Time
localTS
             , timeLastObs :: Time
timeLastObs        = Time
localTS -- for single observation in sample case
             })

 | Time
localTS Time -> Time -> Bool
forall a. Ord a => a -> a -> Bool
<= StatsA -> Time
nextSampleAt StatsA
s    -- still in a sample period
  = let refTimePoint :: (Word32, Time)
refTimePoint = case StatsA -> Maybe (Word32, Time)
referenceTimePoint StatsA
s of
          Just (Word32, Time)
a  -> (Word32, Time)
a
          Maybe (Word32, Time)
Nothing -> [Char] -> (Word32, Time)
forall a. HasCallStack => [Char] -> a
error [Char]
"step: missing referenceTimePoint"
        transitTime :: SISec
transitTime       = (Word32, Time) -> RemoteClockModel -> Time -> SISec
calcTransitTime (Word32, Time)
refTimePoint RemoteClockModel
remoteTS Time
localTS
    in (Maybe OneWayDeltaQSample
forall a. Maybe a
Nothing, StatsA -> Time -> Int -> SISec -> StatsA
recordObservation StatsA
s Time
localTS Int
obsSize SISec
transitTime)
 | Bool
otherwise                    -- need to start next sample period
  = let sample :: OneWayDeltaQSample
sample  = StatsA -> OneWayDeltaQSample
constructSample StatsA
s
        (Maybe OneWayDeltaQSample
_, StatsA
s') = RemoteClockModel
-> Time -> Int -> StatsA -> (Maybe OneWayDeltaQSample, StatsA)
step RemoteClockModel
remoteTS Time
localTS Int
obsSize StatsA
initialStatsA
    in (OneWayDeltaQSample -> Maybe OneWayDeltaQSample
forall a. a -> Maybe a
Just OneWayDeltaQSample
sample, StatsA
s')

-- Calculate the transit time by transforming the remotely reported
-- emit time into local clock domain then calculating differences.
calcTransitTime :: (Word32, Time)
                -> RemoteClockModel
                -> Time
                -> SISec
calcTransitTime :: (Word32, Time) -> RemoteClockModel -> Time -> SISec
calcTransitTime (Word32
remoteRefTS, Time
localRefTS) RemoteClockModel
remoteTS' Time
localTS
  = let remoteTS :: Word32
remoteTS
          = RemoteClockModel -> Word32
unRemoteClockModel RemoteClockModel
remoteTS'
        remoteClockDiffAsTimeDiff :: Word32 -> DiffTime
remoteClockDiffAsTimeDiff
          = (DiffTime
remoteClockPrecision DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
*) (DiffTime -> DiffTime)
-> (Word32 -> DiffTime) -> Word32 -> DiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> DiffTime
forall a. Fractional a => Rational -> a
fromRational (Rational -> DiffTime)
-> (Word32 -> Rational) -> Word32 -> DiffTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral
        correctedEmitTime :: Time
correctedEmitTime
          | Word32
remoteTS Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word32
remoteRefTS
          = (Word32 -> DiffTime
remoteClockDiffAsTimeDiff (Word32 -> DiffTime) -> Word32 -> DiffTime
forall a b. (a -> b) -> a -> b
$ Word32
remoteTS Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
remoteRefTS)
            DiffTime -> Time -> Time
`addTime` Time
localRefTS
          | Bool
otherwise -- wrap has occurred
          = (Word32 -> DiffTime
remoteClockDiffAsTimeDiff (Word32 -> DiffTime) -> Word32 -> DiffTime
forall a b. (a -> b) -> a -> b
$ Word32
forall a. Bounded a => a
maxBound Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- (Word32
remoteRefTS Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
remoteTS))
            DiffTime -> Time -> Time
`addTime` Time
localRefTS
    in Float -> SISec
S (Float -> SISec) -> Float -> SISec
forall a b. (a -> b) -> a -> b
$! Rational -> Float
forall a. Fractional a => Rational -> a
fromRational (DiffTime -> Rational
forall a. Real a => a -> Rational
toRational (Time
localTS Time -> Time -> DiffTime
`diffTime` Time
correctedEmitTime))

recordObservation :: StatsA -> Time -> Int -> SISec -> StatsA
recordObservation :: StatsA -> Time -> Int -> SISec -> StatsA
recordObservation StatsA
s Time
obsTime Int
obsSize SISec
transitTime
  = let f :: Maybe PerSizeRecord -> Maybe PerSizeRecord
f Maybe PerSizeRecord
Nothing  = PerSizeRecord -> Maybe PerSizeRecord
forall a. a -> Maybe a
Just (PerSizeRecord -> Maybe PerSizeRecord)
-> PerSizeRecord -> Maybe PerSizeRecord
forall a b. (a -> b) -> a -> b
$! SISec -> PerSizeRecord
makePerSizeRecord SISec
transitTime
        f (Just PerSizeRecord
a) = PerSizeRecord -> Maybe PerSizeRecord
forall a. a -> Maybe a
Just (PerSizeRecord -> Maybe PerSizeRecord)
-> PerSizeRecord -> Maybe PerSizeRecord
forall a b. (a -> b) -> a -> b
$! SISec -> PerSizeRecord
makePerSizeRecord SISec
transitTime PerSizeRecord -> PerSizeRecord -> PerSizeRecord
forall a. Semigroup a => a -> a -> a
<> PerSizeRecord
a
    in StatsA
s { timeLastObs :: Time
timeLastObs     = Time
obsTime
         , numObservations :: Int
numObservations = Int -> Int
forall a. Enum a => a -> a
succ (StatsA -> Int
numObservations StatsA
s)
         , observables :: IntMap PerSizeRecord
observables     = (Maybe PerSizeRecord -> Maybe PerSizeRecord)
-> Int -> IntMap PerSizeRecord -> IntMap PerSizeRecord
forall a. (Maybe a -> Maybe a) -> Int -> IntMap a -> IntMap a
IM.alter Maybe PerSizeRecord -> Maybe PerSizeRecord
f Int
obsSize (StatsA -> IntMap PerSizeRecord
observables StatsA
s)
         }

-- This might benefit from some strictness analysis, what are the
-- likely usage patterns?, do we want a single use collapse the
-- collective set of thunks or not?
--
-- There is the issue of "bias" (in its statistical meaning) here. The
-- approach here is pragmatic, we are going to use the uncorrected
-- sample standard deviation here as it has a defined solution for a
-- single sample.
--
-- Given that the consumer of this statistic also has access to the
-- population size, they could reconstruct the underlying measures and
-- take it from there.
--
-- We return `NaN` for the appropraiate satistics when the population
-- is empty.
constructSample :: StatsA -> OneWayDeltaQSample
constructSample :: StatsA -> OneWayDeltaQSample
constructSample StatsA
sa = OneWaySample :: Double
-> Int
-> Int
-> Double
-> Double
-> Double
-> Double
-> [Char]
-> OneWayDeltaQSample
OneWaySample
  { duration :: Double
duration       = Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Rational -> Double)
-> (DiffTime -> Rational) -> DiffTime -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DiffTime -> Rational
forall a. Real a => a -> Rational
toRational (DiffTime -> Double) -> DiffTime -> Double
forall a b. (a -> b) -> a -> b
$
                     DiffTime
-> ((Word32, Time) -> DiffTime) -> Maybe (Word32, Time) -> DiffTime
forall b a. b -> (a -> b) -> Maybe a -> b
maybe DiffTime
0 (\(Word32
_,Time
a) -> StatsA -> Time
timeLastObs StatsA
sa Time -> Time -> DiffTime
`diffTime` Time
a)
                             (StatsA -> Maybe (Word32, Time)
referenceTimePoint StatsA
sa)
  , sumPackets :: Int
sumPackets     = Int
population
  , sumTotalSDU :: Int
sumTotalSDU    = Int
totalSDUOctets
  , estDeltaQS :: Double
estDeltaQS     = Double -> Double
normCheck Double
dQSEst
  , estDeltaQVMean :: Double
estDeltaQVMean = Double -> Double
normCheck (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ Double
vSum Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
pop
  , estDeltaQVVar :: Double
estDeltaQVVar  = Double -> Double
normCheck (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ (Double
vSum2 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
vSum Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
vSum Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
pop) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
pop
  , estR :: Double
estR           = Double -> Double
normCheck Double
rEst
  , sizeDist :: [Char]
sizeDist       = [(Int, Int, Float)] -> [Char]
forall a. Show a => a -> [Char]
show [ (Int
a,PerSizeRecord -> Int
count PerSizeRecord
b, let S Float
mtt = PerSizeRecord -> SISec
minTransitTime PerSizeRecord
b in Float
mtt)
                          | (Int
a, PerSizeRecord
b) <- IntMap PerSizeRecord -> [(Int, PerSizeRecord)]
forall a. IntMap a -> [(Int, a)]
IM.toAscList (StatsA -> IntMap PerSizeRecord
observables StatsA
sa)
                          , PerSizeRecord -> Int
count PerSizeRecord
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0]
  }
  where
    -- the sample population size
    population :: Int
population = StatsA -> Int
numObservations StatsA
sa
    pop :: Double
pop        = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
population
    -- Handle the empty population condition
    normCheck :: Double -> Double
normCheck Double
x
      | IntMap PerSizeRecord -> Int
forall a. IntMap a -> Int
IM.size (StatsA -> IntMap PerSizeRecord
observables StatsA
sa) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = Double
x
      | Bool
otherwise                    = Double
nan
    -- gather the data for the G,S estimations
    (Int
totalSDUOctets, [(Int, SISec)]
minSRev)
      = (Int
 -> PerSizeRecord -> (Int, [(Int, SISec)]) -> (Int, [(Int, SISec)]))
-> (Int, [(Int, SISec)])
-> IntMap PerSizeRecord
-> (Int, [(Int, SISec)])
forall a b. (Int -> a -> b -> b) -> b -> IntMap a -> b
IM.foldrWithKey Int
-> PerSizeRecord -> (Int, [(Int, SISec)]) -> (Int, [(Int, SISec)])
accum (Int
0, []) (IntMap PerSizeRecord -> (Int, [(Int, SISec)]))
-> IntMap PerSizeRecord -> (Int, [(Int, SISec)])
forall a b. (a -> b) -> a -> b
$ StatsA -> IntMap PerSizeRecord
observables StatsA
sa
    accum :: Int
-> PerSizeRecord -> (Int, [(Int, SISec)]) -> (Int, [(Int, SISec)])
accum Int
nOctets PerSizeRecord
psr (Int
sumSize, [(Int, SISec)]
minS)
      = ( Int
sumSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (PerSizeRecord -> Int
count PerSizeRecord
psr) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
nOctets
        , (Int
nOctets, PerSizeRecord -> SISec
minTransitTime PerSizeRecord
psr) (Int, SISec) -> [(Int, SISec)] -> [(Int, SISec)]
forall a. a -> [a] -> [a]
: [(Int, SISec)]
minS)
    -- fit a line to get the G,S estimation
    (Double
dQGEst, Double
dQSEst, Double
rEst) = [(Int, SISec)] -> (Double, Double, Double)
estimateGS [(Int, SISec)]
minSRev
    -- normalise all the observations
    normalisedObservations :: IntMap PerSizeRecord
normalisedObservations
      = let norm :: a -> SISec
norm a
n = Float -> SISec
S (Float -> SISec) -> (Double -> Float) -> Double -> SISec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Float
forall a. Fractional a => Rational -> a
fromRational (Rational -> Float) -> (Double -> Rational) -> Double -> Float
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Rational
forall a. Real a => a -> Rational
toRational
                     (Double -> SISec) -> Double -> SISec
forall a b. (a -> b) -> a -> b
$ Double
dQGEst Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (a -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n) Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
dQSEst
        in (Int -> PerSizeRecord -> PerSizeRecord)
-> IntMap PerSizeRecord -> IntMap PerSizeRecord
forall a b. (Int -> a -> b) -> IntMap a -> IntMap b
IM.mapWithKey (\Int
k -> SISec -> PerSizeRecord -> PerSizeRecord
normalisePSR (Int -> SISec
forall a. Integral a => a -> SISec
norm Int
k)) (StatsA -> IntMap PerSizeRecord
observables StatsA
sa)
    -- calculate the total population V stats
    (Double
vSum, Double
vSum2)
      = let S  Float
v  = SISec
vSum'
            S2 Float
v2 = SISec2
vSum2'
        in (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Rational -> Double) -> (Float -> Rational) -> Float -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Rational
forall a. Real a => a -> Rational
toRational (Float -> Double) -> Float -> Double
forall a b. (a -> b) -> a -> b
$ Float
v, Rational -> Double
forall a. Fractional a => Rational -> a
fromRational (Rational -> Double) -> (Float -> Rational) -> Float -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Rational
forall a. Real a => a -> Rational
toRational (Float -> Double) -> Float -> Double
forall a b. (a -> b) -> a -> b
$ Float
v2)
    (SISec
vSum', SISec2
vSum2')
      = (PerSizeRecord -> (SISec, SISec2) -> (SISec, SISec2))
-> (SISec, SISec2) -> IntMap PerSizeRecord -> (SISec, SISec2)
forall a b. (a -> b -> b) -> b -> IntMap a -> b
IM.foldr PerSizeRecord -> (SISec, SISec2) -> (SISec, SISec2)
vCalc (SISec
0,SISec2
0) IntMap PerSizeRecord
normalisedObservations
    vCalc :: PerSizeRecord -> (SISec, SISec2) -> (SISec, SISec2)
vCalc PerSizeRecord
psr (SISec
x, SISec2
x2)
      = (SISec
x SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
+ PerSizeRecord -> SISec
sumTransitTime PerSizeRecord
psr, SISec2
x2 SISec2 -> SISec2 -> SISec2
forall a. Num a => a -> a -> a
+ PerSizeRecord -> SISec2
sumTransitTimeSq PerSizeRecord
psr)

-- | One way measurement for interval. Note that the fields are lazy
--   here so that only calcuation necessary to satisfy strictness of
--   use occurs.
data OneWayDeltaQSample = OneWaySample
  { OneWayDeltaQSample -> Double
duration       :: Double -- SI Seconds of activity captured
  , OneWayDeltaQSample -> Int
sumPackets     :: Int
  , OneWayDeltaQSample -> Int
sumTotalSDU    :: Int
  , OneWayDeltaQSample -> Double
estDeltaQS     :: Double -- octets per second
  , OneWayDeltaQSample -> Double
estDeltaQVMean :: Double -- SI Seconds
  , OneWayDeltaQSample -> Double
estDeltaQVVar  :: Double
  , OneWayDeltaQSample -> Double
estR           :: Double -- R estimate
  , OneWayDeltaQSample -> [Char]
sizeDist       :: String -- temporary to show size distribution
  }

-- | Statistics accumulator. Strict evaluation used to keep the memory
--   footprint strictly bounded.
data StatsA = StatsA
  { -- per sample
    StatsA -> Maybe (Word32, Time)
referenceTimePoint :: !(Maybe (Word32, Time))
  , StatsA -> Time
nextSampleAt       :: !Time
    -- per observation
  , StatsA -> Int
numObservations    :: !Int
  , StatsA -> Time
timeLastObs        :: !Time
  , StatsA -> IntMap PerSizeRecord
observables        :: !(IntMap PerSizeRecord)
  }

-- This _may_ not be the best representation, but it does appear to be
-- an adequate one. There are known issues with numerical stabilty for
-- this representation approach in floating point arithmetic where the
-- values being measured are "large" and the variablity in the sampled
-- population is "small" (i.e the inherent rounding effect of floating
-- point arithmetic has an effect).
--
-- This is very unlikely to cause an issue here as:
--
--   a) the modulo model of the RemoteClockModel (and hence the
--   establishment of a clock reference offset for a given sample)
--   means that we are only ever recording differences - thus any
--   absolute clock differences get factored out.
--
--   b) the transit delay for a measurement will be small, (probablly
--   not even credible) worst case ~10^3 / 10^4 seconds, the finite
--   mantissa of IEEE754
--   (https://en.wikipedia.org/wiki/IEEE_754#Representation_and_encoding_in_memory)
--   even for 32 bit representation (24bits / 7.22 decimal digits)
--   represents an bound on the inherent measured population
--   variability.
--
--   c) longer term clock drift is covered here by the restablishing
--   of the clock reference offsets every sampling period. Given a
--   reasonable sampling period (say 10 seconds) clock drift ( <
--   100ppm) can't amount to a significant error over such a period.
--
-- To conclude, reasonable model of delay is < 1second, the precision
-- of delay measurement is 10^-6 - this all fits nicely withing a
-- IEEE754 32bit representation with its 7.22 decimal digit
-- mantissa. Haskell `Float`s are adequate for this purpose.

data PerSizeRecord = PSR
  { PerSizeRecord -> SISec
minTransitTime   :: !SISec
  , PerSizeRecord -> Int
count            :: !Int
  , PerSizeRecord -> SISec
sumTransitTime   :: !SISec
  , PerSizeRecord -> SISec2
sumTransitTimeSq :: !SISec2
  }

instance Semigroup PerSizeRecord where
  PerSizeRecord
a <> :: PerSizeRecord -> PerSizeRecord -> PerSizeRecord
<> PerSizeRecord
b = PSR :: SISec -> Int -> SISec -> SISec2 -> PerSizeRecord
PSR { minTransitTime :: SISec
minTransitTime   = (PerSizeRecord -> SISec
minTransitTime PerSizeRecord
a) SISec -> SISec -> SISec
forall a. Ord a => a -> a -> a
`min` (PerSizeRecord -> SISec
minTransitTime PerSizeRecord
b)
               , count :: Int
count            = PerSizeRecord -> Int
count PerSizeRecord
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ PerSizeRecord -> Int
count PerSizeRecord
b
               , sumTransitTime :: SISec
sumTransitTime   = PerSizeRecord -> SISec
sumTransitTime PerSizeRecord
a SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
+ PerSizeRecord -> SISec
sumTransitTime PerSizeRecord
b
               , sumTransitTimeSq :: SISec2
sumTransitTimeSq = PerSizeRecord -> SISec2
sumTransitTimeSq PerSizeRecord
a SISec2 -> SISec2 -> SISec2
forall a. Num a => a -> a -> a
+ PerSizeRecord -> SISec2
sumTransitTimeSq PerSizeRecord
b
               }

-- | Normalise given the calculated G,S for the size
normalisePSR :: SISec -> PerSizeRecord -> PerSizeRecord
normalisePSR :: SISec -> PerSizeRecord -> PerSizeRecord
normalisePSR SISec
norm PerSizeRecord
psr
  = let adj :: SISec
adj  = (Int -> SISec
forall a b. (Integral a, Num b) => a -> b
fromIntegral (PerSizeRecord -> Int
count PerSizeRecord
psr) SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
* SISec
norm)
        stt' :: SISec
stt' = (PerSizeRecord -> SISec
sumTransitTime PerSizeRecord
psr) SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
- SISec
adj
        ttt :: SISec -> SISec -> SISec2
ttt (S Float
a) (S Float
b)
             = Float -> SISec2
S2 (Float -> SISec2) -> Float -> SISec2
forall a b. (a -> b) -> a -> b
$ Float
a Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
b
    in PerSizeRecord
psr { minTransitTime :: SISec
minTransitTime   = PerSizeRecord -> SISec
minTransitTime   PerSizeRecord
psr SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
- SISec
norm
           , sumTransitTime :: SISec
sumTransitTime   = SISec
stt'
           , sumTransitTimeSq :: SISec2
sumTransitTimeSq = PerSizeRecord -> SISec2
sumTransitTimeSq PerSizeRecord
psr
                                SISec2 -> SISec2 -> SISec2
forall a. Num a => a -> a -> a
- SISec
norm SISec -> SISec -> SISec2
`ttt` (SISec
2 SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
* SISec
stt' SISec -> SISec -> SISec
forall a. Num a => a -> a -> a
+ SISec
norm)
           }


-- | Initial StatsA
initialStatsA :: StatsA
initialStatsA :: StatsA
initialStatsA = StatsA :: Maybe (Word32, Time)
-> Time -> Int -> Time -> IntMap PerSizeRecord -> StatsA
StatsA
  { referenceTimePoint :: Maybe (Word32, Time)
referenceTimePoint = Maybe (Word32, Time)
forall a. Maybe a
Nothing
  , nextSampleAt :: Time
nextSampleAt       = Time
noTime
  , numObservations :: Int
numObservations    = Int
0
  , timeLastObs :: Time
timeLastObs        = Time
noTime
  , observables :: IntMap PerSizeRecord
observables        = IntMap PerSizeRecord
forall a. IntMap a
IM.empty
  }
  where
    noTime :: Time
noTime = DiffTime -> Time
Time DiffTime
0

makePerSizeRecord :: SISec -> PerSizeRecord
makePerSizeRecord :: SISec -> PerSizeRecord
makePerSizeRecord SISec
tt = PSR :: SISec -> Int -> SISec -> SISec2 -> PerSizeRecord
PSR
  { minTransitTime :: SISec
minTransitTime   = SISec
tt
  , count :: Int
count            = Int
1
  , sumTransitTime :: SISec
sumTransitTime   = SISec
tt
  , sumTransitTimeSq :: SISec2
sumTransitTimeSq = SISec -> SISec2
squareSISec SISec
tt
  }

-- May want to make this a configuration variable

-- NOTE this interval must be less than the wrap around time of the
-- `RemoteClockModel`. The remote clock model has a precision of
-- `remoteClockPrecision`.
sampleInterval :: DiffTime
sampleInterval :: DiffTime
sampleInterval = DiffTime -> DiffTime
check DiffTime
10
  where
    check :: DiffTime -> DiffTime
check DiffTime
n
     | DiffTime
n DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
> DiffTime
0 Bool -> Bool -> Bool
&& DiffTime
n DiffTime -> DiffTime -> Bool
forall a. Ord a => a -> a -> Bool
< DiffTime
wrapInterval
       = DiffTime
n
     | Bool
otherwise
       = [Char] -> DiffTime
forall a. HasCallStack => [Char] -> a
error [Char]
"Infeasible sampleInterval"
    wrapInterval :: DiffTime
wrapInterval
      = DiffTime
remoteClockPrecision DiffTime -> DiffTime -> DiffTime
forall a. Num a => a -> a -> a
* (Word32 -> DiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32 -> DiffTime) -> Word32 -> DiffTime
forall a b. (a -> b) -> a -> b
$ RemoteClockModel -> Word32
unRemoteClockModel RemoteClockModel
forall a. Bounded a => a
maxBound)

nan :: Double
nan :: Double
nan = Double
0Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
0