{-# LANGUAGE GADTs               #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE TypeFamilies        #-}
module Experimenter.Eval.Csv
    ( writeCsvMeasure
    , Smoothing (..)
    , MeasureName
    ) where

import           Conduit                      as C
import           Control.Lens                 hiding (Cons, Over)
import           Control.Monad.Logger
import           Control.Monad.Reader
import           Data.Maybe                   (fromMaybe)
import qualified Data.Text                    as T
import           Data.Time.Clock              (diffUTCTime, getCurrentTime)
import qualified Database.Esqueleto           as E
import           System.Directory
import           System.FilePath.Posix
import           System.IO

import           Experimenter.DatabaseSetting
import           Experimenter.DB
import           Experimenter.Eval.Util
import           Experimenter.Experiment      (Phase (..))
import           Experimenter.Models
import           Experimenter.Result.Type
import           Experimenter.Util


data Smoothing = NoSmoothing | SmoothingMovAvg !Int

type MeasureName = T.Text

writeCsvMeasure :: DatabaseSetting -> Experiments a -> Smoothing -> [MeasureName] -> IO ()
writeCsvMeasure :: DatabaseSetting
-> Experiments a -> Smoothing -> [MeasureName] -> IO ()
writeCsvMeasure DatabaseSetting
dbSetup Experiments a
exps Smoothing
smoothing [MeasureName]
measures = (ResourceT IO () -> IO ())
-> DatabaseSetting
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
-> IO ()
forall (m1 :: * -> *) a (m :: * -> *).
MonadUnliftIO m1 =>
(m1 a -> m a)
-> DatabaseSetting -> ReaderT SqlBackend (LoggingT m1) a -> m a
runDBWithM ResourceT IO () -> IO ()
forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT DatabaseSetting
dbSetup (ReaderT SqlBackend (LoggingT (ResourceT IO)) () -> IO ())
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) () -> IO ()
forall a b. (a -> b) -> a -> b
$ (MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> [MeasureName] -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Experiments a
-> Smoothing
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a.
Experiments a
-> Smoothing
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
smoothAndWriteFile Experiments a
exps Smoothing
smoothing) [MeasureName]
measures

smoothAndWriteFile :: Experiments a -> Smoothing -> MeasureName -> DB IO ()
smoothAndWriteFile :: Experiments a
-> Smoothing
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
smoothAndWriteFile Experiments a
exps Smoothing
smoothing MeasureName
measureName = (Experiment a -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> [Experiment a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Experiments a
-> Smoothing
-> MeasureName
-> Experiment a
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a.
Experiments a
-> Smoothing
-> MeasureName
-> Experiment a
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
smoothAndWriteFileExp Experiments a
exps Smoothing
smoothing MeasureName
measureName) (Experiments a
exps Experiments a
-> Getting [Experiment a] (Experiments a) [Experiment a]
-> [Experiment a]
forall s a. s -> Getting a s a -> a
^. Getting [Experiment a] (Experiments a) [Experiment a]
forall a. Lens' (Experiments a) [Experiment a]
experiments)

smoothAndWriteFileExp :: Experiments a -> Smoothing -> MeasureName -> Experiment a -> DB IO ()
smoothAndWriteFileExp :: Experiments a
-> Smoothing
-> MeasureName
-> Experiment a
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
smoothAndWriteFileExp Experiments a
exps Smoothing
smoothing MeasureName
measureName Experiment a
exp = do
  $(Int
String
LogLevel
String -> MeasureName
String -> String -> String -> CharPos -> CharPos -> Loc
MeasureName -> MeasureName
Loc
-> MeasureName
-> LogLevel
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
(MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> (MeasureName -> MeasureName)
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a. a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> MeasureName -> LogLevel -> msg -> m ()
pack :: String -> MeasureName
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> MeasureName -> LogLevel -> msg -> m ()
. :: forall b c a. (b -> c) -> (a -> b) -> a -> c
id :: forall a. a -> a
logDebug) (MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a b. (a -> b) -> a -> b
$ MeasureName
"Processing CSV for experiment number " MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Int -> MeasureName
forall a. Show a => a -> MeasureName
tshow (Experiment a
exp Experiment a -> Getting Int (Experiment a) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (Experiment a) Int
forall a. Lens' (Experiment a) Int
experimentNumber)
  (ExperimentResult a
 -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> [ExperimentResult a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_
    (\ExperimentResult a
expRes -> do
       $(Int
String
LogLevel
String -> MeasureName
String -> String -> String -> CharPos -> CharPos -> Loc
MeasureName -> MeasureName
Loc
-> MeasureName
-> LogLevel
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
(MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> (MeasureName -> MeasureName)
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a. a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> MeasureName -> LogLevel -> msg -> m ()
pack :: String -> MeasureName
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> MeasureName -> LogLevel -> msg -> m ()
. :: forall b c a. (b -> c) -> (a -> b) -> a -> c
id :: forall a. a -> a
logDebug) (MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a b. (a -> b) -> a -> b
$ MeasureName
"Processing CSV for experiment repetition number " MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Int -> MeasureName
forall a. Show a => a -> MeasureName
tshow (ExperimentResult a
expRes ExperimentResult a -> Getting Int (ExperimentResult a) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (ExperimentResult a) Int
forall a. Lens' (ExperimentResult a) Int
repetitionNumber)
       (ResultData a -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_
         (Experiments a
-> MeasureName
-> Smoothing
-> MeasureName
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a.
Experiments a
-> MeasureName
-> Smoothing
-> MeasureName
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
smoothAndWriteFileResultData Experiments a
exps (Int -> Phase -> Avg -> MeasureName
namePrefix Int
expNr Phase
PreparationPhase (Int -> Maybe Int -> Avg
None (ExperimentResult a
expRes ExperimentResult a -> Getting Int (ExperimentResult a) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (ExperimentResult a) Int
forall a. Lens' (ExperimentResult a) Int
repetitionNumber) Maybe Int
forall a. Maybe a
Nothing)) Smoothing
smoothing MeasureName
measureName ([ResultData a] -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> (ResultData a -> [ResultData a])
-> ResultData a
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResultData a -> [ResultData a]
forall (m :: * -> *) a. Monad m => a -> m a
return)
         (ExperimentResult a
expRes ExperimentResult a
-> Getting
     (Endo [ResultData a]) (ExperimentResult a) (ResultData a)
-> [ResultData a]
forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. (Maybe (ResultData a)
 -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
-> ExperimentResult a
-> Const (Endo [ResultData a]) (ExperimentResult a)
forall a. Lens' (ExperimentResult a) (Maybe (ResultData a))
preparationResults ((Maybe (ResultData a)
  -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
 -> ExperimentResult a
 -> Const (Endo [ResultData a]) (ExperimentResult a))
-> ((ResultData a -> Const (Endo [ResultData a]) (ResultData a))
    -> Maybe (ResultData a)
    -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
-> Getting
     (Endo [ResultData a]) (ExperimentResult a) (ResultData a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ResultData a -> Const (Endo [ResultData a]) (ResultData a))
-> Maybe (ResultData a)
-> Const (Endo [ResultData a]) (Maybe (ResultData a))
forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
traversed)
       -- All experiment evaluations runs
       (ReplicationResult a
 -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> [ReplicationResult a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_
         (\(ReplicationResult a
replRes :: ReplicationResult a) -> do
            (ResultData a -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_
              (\(ResultData a
repRes :: ResultData a) ->
                 Experiments a
-> MeasureName
-> Smoothing
-> MeasureName
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a.
Experiments a
-> MeasureName
-> Smoothing
-> MeasureName
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
smoothAndWriteFileResultData
                   Experiments a
exps
                   (Int -> Phase -> Avg -> MeasureName
namePrefix Int
expNr Phase
WarmUpPhase (Int -> Maybe Int -> Avg
None (ExperimentResult a
expRes ExperimentResult a -> Getting Int (ExperimentResult a) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (ExperimentResult a) Int
forall a. Lens' (ExperimentResult a) Int
repetitionNumber) (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ ReplicationResult a
replRes ReplicationResult a -> Getting Int (ReplicationResult a) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (ReplicationResult a) Int
forall a. Lens' (ReplicationResult a) Int
replicationNumber)))
                   Smoothing
smoothing
                   MeasureName
measureName
                   [ResultData a
repRes])
              (ReplicationResult a
replRes ReplicationResult a
-> Getting
     (Endo [ResultData a]) (ReplicationResult a) (ResultData a)
-> [ResultData a]
forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. (Maybe (ResultData a)
 -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
-> ReplicationResult a
-> Const (Endo [ResultData a]) (ReplicationResult a)
forall a. Lens' (ReplicationResult a) (Maybe (ResultData a))
warmUpResults ((Maybe (ResultData a)
  -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
 -> ReplicationResult a
 -> Const (Endo [ResultData a]) (ReplicationResult a))
-> ((ResultData a -> Const (Endo [ResultData a]) (ResultData a))
    -> Maybe (ResultData a)
    -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
-> Getting
     (Endo [ResultData a]) (ReplicationResult a) (ResultData a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ResultData a -> Const (Endo [ResultData a]) (ResultData a))
-> Maybe (ResultData a)
-> Const (Endo [ResultData a]) (Maybe (ResultData a))
forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
traversed)
            (ResultData a -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_
              (\(ResultData a
repRes :: ResultData a) ->
                 Experiments a
-> MeasureName
-> Smoothing
-> MeasureName
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a.
Experiments a
-> MeasureName
-> Smoothing
-> MeasureName
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
smoothAndWriteFileResultData
                   Experiments a
exps
                   (Int -> Phase -> Avg -> MeasureName
namePrefix Int
expNr Phase
EvaluationPhase (Int -> Maybe Int -> Avg
None (ExperimentResult a
expRes ExperimentResult a -> Getting Int (ExperimentResult a) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (ExperimentResult a) Int
forall a. Lens' (ExperimentResult a) Int
repetitionNumber) (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ ReplicationResult a
replRes ReplicationResult a -> Getting Int (ReplicationResult a) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (ReplicationResult a) Int
forall a. Lens' (ReplicationResult a) Int
replicationNumber)))
                   Smoothing
smoothing
                   MeasureName
measureName
                   [ResultData a
repRes])
              (ReplicationResult a
replRes ReplicationResult a
-> Getting
     (Endo [ResultData a]) (ReplicationResult a) (ResultData a)
-> [ResultData a]
forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. (Maybe (ResultData a)
 -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
-> ReplicationResult a
-> Const (Endo [ResultData a]) (ReplicationResult a)
forall a. Lens' (ReplicationResult a) (Maybe (ResultData a))
evalResults ((Maybe (ResultData a)
  -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
 -> ReplicationResult a
 -> Const (Endo [ResultData a]) (ReplicationResult a))
-> ((ResultData a -> Const (Endo [ResultData a]) (ResultData a))
    -> Maybe (ResultData a)
    -> Const (Endo [ResultData a]) (Maybe (ResultData a)))
-> Getting
     (Endo [ResultData a]) (ReplicationResult a) (ResultData a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ResultData a -> Const (Endo [ResultData a]) (ResultData a))
-> Maybe (ResultData a)
-> Const (Endo [ResultData a]) (Maybe (ResultData a))
forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
traversed))
         (ExperimentResult a
expRes ExperimentResult a
-> Getting
     [ReplicationResult a] (ExperimentResult a) [ReplicationResult a]
-> [ReplicationResult a]
forall s a. s -> Getting a s a -> a
^. Getting
  [ReplicationResult a] (ExperimentResult a) [ReplicationResult a]
forall a. Lens' (ExperimentResult a) [ReplicationResult a]
evaluationResults))
    (Experiment a
exp Experiment a
-> Getting
     (Endo [ExperimentResult a]) (Experiment a) (ExperimentResult a)
-> [ExperimentResult a]
forall s a. s -> Getting (Endo [a]) s a -> [a]
^.. ([ExperimentResult a]
 -> Const (Endo [ExperimentResult a]) [ExperimentResult a])
-> Experiment a -> Const (Endo [ExperimentResult a]) (Experiment a)
forall a. Lens' (Experiment a) [ExperimentResult a]
experimentResults (([ExperimentResult a]
  -> Const (Endo [ExperimentResult a]) [ExperimentResult a])
 -> Experiment a
 -> Const (Endo [ExperimentResult a]) (Experiment a))
-> ((ExperimentResult a
     -> Const (Endo [ExperimentResult a]) (ExperimentResult a))
    -> [ExperimentResult a]
    -> Const (Endo [ExperimentResult a]) [ExperimentResult a])
-> Getting
     (Endo [ExperimentResult a]) (Experiment a) (ExperimentResult a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ExperimentResult a
 -> Const (Endo [ExperimentResult a]) (ExperimentResult a))
-> [ExperimentResult a]
-> Const (Endo [ExperimentResult a]) [ExperimentResult a]
forall (f :: * -> *) a b.
Traversable f =>
IndexedTraversal Int (f a) (f b) a b
traversed)
  -- avg over experiments
  -- when (length (exp ^. experimentResults) > 1) $ do
  --   $(logDebug) $ "Processing aggregated CSV for experiment  number " <> tshow (exp ^. experimentNumber)
  --   mapM_
  --     (\expRes -> do
  --         error "Averages over experiment results are currently not supported. Please feel free to submit a merge request."
  --         print expRes
  --         ) (exp ^. experimentResults)
  where
    expNr :: Int
expNr = Experiment a
exp Experiment a -> Getting Int (Experiment a) Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int (Experiment a) Int
forall a. Lens' (Experiment a) Int
experimentNumber


smoothC :: (Monad m) => Smoothing -> C.ConduitT (Int, Double) (Int, Double) m ()
smoothC :: Smoothing -> ConduitT (Int, Double) (Int, Double) m ()
smoothC Smoothing
NoSmoothing = ((Int, Double) -> Bool)
-> ConduitT (Int, Double) (Int, Double) m ()
forall (m :: * -> *) a. Monad m => (a -> Bool) -> ConduitT a a m ()
C.filterC (Bool -> (Int, Double) -> Bool
forall a b. a -> b -> a
const Bool
True)
smoothC (SmoothingMovAvg Int
nr) = (Double, [(Int, Double)])
-> ConduitT (Int, Double) (Int, Double) m ()
process (Double
0, [])
  where
    process :: (Double, [(Int, Double)])
-> ConduitT (Int, Double) (Int, Double) m ()
process inp :: (Double, [(Int, Double)])
inp@(Double
sm, [(Int, Double)]
xs) = do
      Maybe (Int, Double)
mx <- ConduitT (Int, Double) (Int, Double) m (Maybe (Int, Double))
forall (m :: * -> *) i. Monad m => Consumer i m (Maybe i)
await
      Bool
-> ConduitT (Int, Double) (Int, Double) m ()
-> ConduitT (Int, Double) (Int, Double) m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([(Int, Double)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Int, Double)]
xs) (ConduitT (Int, Double) (Int, Double) m ()
 -> ConduitT (Int, Double) (Int, Double) m ())
-> ConduitT (Int, Double) (Int, Double) m ()
-> ConduitT (Int, Double) (Int, Double) m ()
forall a b. (a -> b) -> a -> b
$ (Int, Double) -> ConduitT (Int, Double) (Int, Double) m ()
forall (m :: * -> *) o i. Monad m => o -> ConduitT i o m ()
yield ((Double, [(Int, Double)]) -> (Int, Double)
movAvg (Double, [(Int, Double)])
inp)
      case Maybe (Int, Double)
mx of
        Maybe (Int, Double)
Nothing -> () -> ConduitT (Int, Double) (Int, Double) m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just ((Int, Double)
x :: (Int, Double)) -> do
          let m :: Double
m
                | [(Int, Double)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Int, Double)]
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
nr = Double
0
                | Bool
otherwise = (Int, Double) -> Double
forall a b. (a, b) -> b
snd ((Int, Double) -> Double) -> (Int, Double) -> Double
forall a b. (a -> b) -> a -> b
$ [(Int, Double)] -> (Int, Double)
forall a. [a] -> a
last [(Int, Double)]
xs
              v :: Double
v = (Int, Double) -> Double
forall a b. (a, b) -> b
snd (Int, Double)
x
          (Double, [(Int, Double)])
-> ConduitT (Int, Double) (Int, Double) m ()
process (Double
sm Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
m, Int -> [(Int, Double)] -> [(Int, Double)]
forall a. Int -> [a] -> [a]
take Int
nr ((Int, Double)
x (Int, Double) -> [(Int, Double)] -> [(Int, Double)]
forall a. a -> [a] -> [a]
: [(Int, Double)]
xs))

-- movAvg :: [Measure] -> Measure
-- movAvg [] = error "empty list when makeing movAvg"
-- movAvg xs@(x:_) = set (measureResults.traversed.resultYValue) (Prelude.sum (concatMap (^.. (measureResults.traversed.resultYValue)) xs) / fromIntegral (length xs)) x

movAvg :: (Double, [(Int, Double)]) -> (Int, Double)
movAvg :: (Double, [(Int, Double)]) -> (Int, Double)
movAvg (Double
_,[])   = String -> (Int, Double)
forall a. HasCallStack => String -> a
error String
"No input for movAvg. Programming error!"
movAvg (Double
sm, [(Int, Double)]
xs) = ((Int, Double) -> Int
forall a b. (a, b) -> a
fst (Int, Double)
x, Double
sm Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([(Int, Double)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Int, Double)]
xs))
  where x :: (Int, Double)
x = [(Int, Double)] -> (Int, Double)
forall a. [a] -> a
last [(Int, Double)]
xs

data Keys = ResultDataPrepKeys ![Key PrepResultData]
          | ResultDataWarmUpKeys ![Key WarmUpResultData]
          | ResultDataRepKeys ![Key RepResultData]

concatKeys :: Keys -> Keys -> Keys
concatKeys :: Keys -> Keys -> Keys
concatKeys (ResultDataPrepKeys [Key PrepResultData]
xs) (ResultDataPrepKeys [Key PrepResultData]
ys) = [Key PrepResultData] -> Keys
ResultDataPrepKeys ([Key PrepResultData]
xs [Key PrepResultData]
-> [Key PrepResultData] -> [Key PrepResultData]
forall a. [a] -> [a] -> [a]
++ [Key PrepResultData]
ys)
concatKeys (ResultDataWarmUpKeys [Key WarmUpResultData]
xs) (ResultDataWarmUpKeys [Key WarmUpResultData]
ys)= [Key WarmUpResultData] -> Keys
ResultDataWarmUpKeys ([Key WarmUpResultData]
xs [Key WarmUpResultData]
-> [Key WarmUpResultData] -> [Key WarmUpResultData]
forall a. [a] -> [a] -> [a]
++ [Key WarmUpResultData]
ys)
concatKeys (ResultDataRepKeys [Key RepResultData]
xs) (ResultDataRepKeys [Key RepResultData]
ys) = [Key RepResultData] -> Keys
ResultDataRepKeys ([Key RepResultData]
xs [Key RepResultData] -> [Key RepResultData] -> [Key RepResultData]
forall a. [a] -> [a] -> [a]
++ [Key RepResultData]
ys)
concatKeys Keys
_ Keys
_ = String -> Keys
forall a. HasCallStack => String -> a
error String
"cannot concat different key types in Csv.hs"

smoothAndWriteFileResultData :: Experiments a -> T.Text -> Smoothing -> MeasureName -> [ResultData a] -> DB IO ()
smoothAndWriteFileResultData :: Experiments a
-> MeasureName
-> Smoothing
-> MeasureName
-> [ResultData a]
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
smoothAndWriteFileResultData Experiments a
_ MeasureName
_ Smoothing
_ MeasureName
_ [] = () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
smoothAndWriteFileResultData Experiments a
exps MeasureName
prefix Smoothing
smoothing MeasureName
measureName [ResultData a]
resData = do
  $(Int
String
LogLevel
String -> MeasureName
String -> String -> String -> CharPos -> CharPos -> Loc
MeasureName -> MeasureName
Loc
-> MeasureName
-> LogLevel
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
(MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> (MeasureName -> MeasureName)
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a. a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> MeasureName -> LogLevel -> msg -> m ()
pack :: String -> MeasureName
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> MeasureName -> LogLevel -> msg -> m ()
. :: forall b c a. (b -> c) -> (a -> b) -> a -> c
id :: forall a. a -> a
logInfo) (MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a b. (a -> b) -> a -> b
$ MeasureName
"Processing measure " MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
measureName MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
". Saving data to: " MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> String -> MeasureName
T.pack String
folder
  IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a b. (a -> b) -> a -> b
$ Bool -> String -> IO ()
createDirectoryIfMissing Bool
True String
folder
  IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a b. (a -> b) -> a -> b
$ String -> String -> IO ()
writeFile String
filePath String
header IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> String -> IO ()
writeFile String
filePathPlotSh String
plotSh
  Handle
fileH <- IO Handle -> ReaderT SqlBackend (LoggingT (ResourceT IO)) Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Handle -> ReaderT SqlBackend (LoggingT (ResourceT IO)) Handle)
-> IO Handle -> ReaderT SqlBackend (LoggingT (ResourceT IO)) Handle
forall a b. (a -> b) -> a -> b
$ String -> IOMode -> IO Handle
openFile String
filePath IOMode
AppendMode
  UTCTime
start <- IO UTCTime -> ReaderT SqlBackend (LoggingT (ResourceT IO)) UTCTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
getCurrentTime
  let vals :: ConduitT
  ()
  (Value Int, Value (Maybe Double))
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
vals =
        -- fmap (map (fromMaybe 0 . E.unValue)) $
        -- E.select $ -- E.selectSource $
        SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
-> ConduitT
     ()
     (Value Int, Value (Maybe Double))
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall a r backend (m :: * -> *).
(SqlSelect a r, BackendCompatible SqlBackend backend,
 IsPersistBackend backend, PersistQueryRead backend,
 PersistStoreRead backend, PersistUniqueRead backend,
 MonadResource m) =>
SqlQuery a -> ConduitT () r (ReaderT backend m) ()
E.selectSource (SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
 -> ConduitT
      ()
      (Value Int, Value (Maybe Double))
      (ReaderT SqlBackend (LoggingT (ResourceT IO)))
      ())
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
-> ConduitT
     ()
     (Value Int, Value (Maybe Double))
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall a b. (a -> b) -> a -> b
$
        case (Keys -> Keys -> Keys) -> [Keys] -> Keys
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 Keys -> Keys -> Keys
concatKeys [Keys]
keys of
          ResultDataPrepKeys [Key PrepResultData]
xs ->
            (InnerJoin
   (SqlExpr (Entity PrepMeasure)) (SqlExpr (Entity PrepResultStep))
 -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall a b. From a => (a -> SqlQuery b) -> SqlQuery b
E.from ((InnerJoin
    (SqlExpr (Entity PrepMeasure)) (SqlExpr (Entity PrepResultStep))
  -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
 -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> (InnerJoin
      (SqlExpr (Entity PrepMeasure)) (SqlExpr (Entity PrepResultStep))
    -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall a b. (a -> b) -> a -> b
$ \(SqlExpr (Entity PrepMeasure)
measure `E.InnerJoin` SqlExpr (Entity PrepResultStep)
result) -> do
              SqlExpr (Value Bool) -> SqlQuery ()
E.on (SqlExpr (Entity PrepMeasure)
measure SqlExpr (Entity PrepMeasure)
-> EntityField PrepMeasure (Key PrepMeasure)
-> SqlExpr (Value (Key PrepMeasure))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepMeasure (Key PrepMeasure)
forall typ. (typ ~ Key PrepMeasure) => EntityField PrepMeasure typ
PrepMeasureId SqlExpr (Value (Key PrepMeasure))
-> SqlExpr (Value (Key PrepMeasure)) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. SqlExpr (Entity PrepResultStep)
result SqlExpr (Entity PrepResultStep)
-> EntityField PrepResultStep (Key PrepMeasure)
-> SqlExpr (Value (Key PrepMeasure))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepResultStep (Key PrepMeasure)
forall typ.
(typ ~ Key PrepMeasure) =>
EntityField PrepResultStep typ
PrepResultStepMeasure)
              case [Key PrepResultData]
xs of
                [Key PrepResultData
x] -> SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity PrepMeasure)
measure SqlExpr (Entity PrepMeasure)
-> EntityField PrepMeasure (Key PrepResultData)
-> SqlExpr (Value (Key PrepResultData))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepMeasure (Key PrepResultData)
forall typ.
(typ ~ Key PrepResultData) =>
EntityField PrepMeasure typ
PrepMeasurePrepResultData SqlExpr (Value (Key PrepResultData))
-> SqlExpr (Value (Key PrepResultData)) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. Key PrepResultData -> SqlExpr (Value (Key PrepResultData))
forall typ. PersistField typ => typ -> SqlExpr (Value typ)
E.val Key PrepResultData
x)
                [Key PrepResultData]
_ -> SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity PrepMeasure)
measure SqlExpr (Entity PrepMeasure)
-> EntityField PrepMeasure (Key PrepResultData)
-> SqlExpr (Value (Key PrepResultData))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepMeasure (Key PrepResultData)
forall typ.
(typ ~ Key PrepResultData) =>
EntityField PrepMeasure typ
PrepMeasurePrepResultData SqlExpr (Value (Key PrepResultData))
-> SqlExpr (ValueList (Key PrepResultData)) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ)
-> SqlExpr (ValueList typ) -> SqlExpr (Value Bool)
`E.in_` [Key PrepResultData] -> SqlExpr (ValueList (Key PrepResultData))
forall typ. PersistField typ => [typ] -> SqlExpr (ValueList typ)
E.valList [Key PrepResultData]
xs)
              SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity PrepResultStep)
result SqlExpr (Entity PrepResultStep)
-> EntityField PrepResultStep MeasureName
-> SqlExpr (Value MeasureName)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepResultStep MeasureName
forall typ. (typ ~ MeasureName) => EntityField PrepResultStep typ
PrepResultStepName SqlExpr (Value MeasureName)
-> SqlExpr (Value MeasureName) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. MeasureName -> SqlExpr (Value MeasureName)
forall typ. PersistField typ => typ -> SqlExpr (Value typ)
E.val MeasureName
measureName)
              [SqlExpr OrderBy] -> SqlQuery ()
E.orderBy [SqlExpr (Value Int) -> SqlExpr OrderBy
forall a. PersistField a => SqlExpr (Value a) -> SqlExpr OrderBy
E.asc (SqlExpr (Entity PrepMeasure)
measure SqlExpr (Entity PrepMeasure)
-> EntityField PrepMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepMeasure Int
forall typ. (typ ~ Int) => EntityField PrepMeasure typ
PrepMeasurePeriod)]
              SqlExpr (Value Int) -> SqlQuery ()
forall a. ToSomeValues a => a -> SqlQuery ()
E.groupBy (SqlExpr (Entity PrepMeasure)
measure SqlExpr (Entity PrepMeasure)
-> EntityField PrepMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepMeasure Int
forall typ. (typ ~ Int) => EntityField PrepMeasure typ
PrepMeasurePeriod)
              (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall (m :: * -> *) a. Monad m => a -> m a
return (SqlExpr (Entity PrepMeasure)
measure SqlExpr (Entity PrepMeasure)
-> EntityField PrepMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepMeasure Int
forall typ. (typ ~ Int) => EntityField PrepMeasure typ
PrepMeasurePeriod, SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double))
forall a b.
(PersistField a, PersistField b) =>
SqlExpr (Value a) -> SqlExpr (Value (Maybe b))
E.avg_ (SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double)))
-> SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double))
forall a b. (a -> b) -> a -> b
$ SqlExpr (Entity PrepResultStep)
result SqlExpr (Entity PrepResultStep)
-> EntityField PrepResultStep Double -> SqlExpr (Value Double)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField PrepResultStep Double
forall typ. (typ ~ Double) => EntityField PrepResultStep typ
PrepResultStepYValue)
          ResultDataWarmUpKeys [Key WarmUpResultData]
xs ->
            (InnerJoin
   (SqlExpr (Entity WarmUpMeasure))
   (SqlExpr (Entity WarmUpResultStep))
 -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall a b. From a => (a -> SqlQuery b) -> SqlQuery b
E.from ((InnerJoin
    (SqlExpr (Entity WarmUpMeasure))
    (SqlExpr (Entity WarmUpResultStep))
  -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
 -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> (InnerJoin
      (SqlExpr (Entity WarmUpMeasure))
      (SqlExpr (Entity WarmUpResultStep))
    -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall a b. (a -> b) -> a -> b
$ \(SqlExpr (Entity WarmUpMeasure)
measure `E.InnerJoin` SqlExpr (Entity WarmUpResultStep)
result) -> do
              SqlExpr (Value Bool) -> SqlQuery ()
E.on (SqlExpr (Entity WarmUpMeasure)
measure SqlExpr (Entity WarmUpMeasure)
-> EntityField WarmUpMeasure (Key WarmUpMeasure)
-> SqlExpr (Value (Key WarmUpMeasure))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpMeasure (Key WarmUpMeasure)
forall typ.
(typ ~ Key WarmUpMeasure) =>
EntityField WarmUpMeasure typ
WarmUpMeasureId SqlExpr (Value (Key WarmUpMeasure))
-> SqlExpr (Value (Key WarmUpMeasure)) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. SqlExpr (Entity WarmUpResultStep)
result SqlExpr (Entity WarmUpResultStep)
-> EntityField WarmUpResultStep (Key WarmUpMeasure)
-> SqlExpr (Value (Key WarmUpMeasure))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpResultStep (Key WarmUpMeasure)
forall typ.
(typ ~ Key WarmUpMeasure) =>
EntityField WarmUpResultStep typ
WarmUpResultStepMeasure)
              case [Key WarmUpResultData]
xs of
                [Key WarmUpResultData
x] -> SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity WarmUpMeasure)
measure SqlExpr (Entity WarmUpMeasure)
-> EntityField WarmUpMeasure (Key WarmUpResultData)
-> SqlExpr (Value (Key WarmUpResultData))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpMeasure (Key WarmUpResultData)
forall typ.
(typ ~ Key WarmUpResultData) =>
EntityField WarmUpMeasure typ
WarmUpMeasureRepResult SqlExpr (Value (Key WarmUpResultData))
-> SqlExpr (Value (Key WarmUpResultData)) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. Key WarmUpResultData -> SqlExpr (Value (Key WarmUpResultData))
forall typ. PersistField typ => typ -> SqlExpr (Value typ)
E.val Key WarmUpResultData
x)
                [Key WarmUpResultData]
_ -> SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity WarmUpMeasure)
measure SqlExpr (Entity WarmUpMeasure)
-> EntityField WarmUpMeasure (Key WarmUpResultData)
-> SqlExpr (Value (Key WarmUpResultData))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpMeasure (Key WarmUpResultData)
forall typ.
(typ ~ Key WarmUpResultData) =>
EntityField WarmUpMeasure typ
WarmUpMeasureRepResult SqlExpr (Value (Key WarmUpResultData))
-> SqlExpr (ValueList (Key WarmUpResultData))
-> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ)
-> SqlExpr (ValueList typ) -> SqlExpr (Value Bool)
`E.in_` [Key WarmUpResultData]
-> SqlExpr (ValueList (Key WarmUpResultData))
forall typ. PersistField typ => [typ] -> SqlExpr (ValueList typ)
E.valList [Key WarmUpResultData]
xs)
              SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity WarmUpResultStep)
result SqlExpr (Entity WarmUpResultStep)
-> EntityField WarmUpResultStep MeasureName
-> SqlExpr (Value MeasureName)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpResultStep MeasureName
forall typ. (typ ~ MeasureName) => EntityField WarmUpResultStep typ
WarmUpResultStepName SqlExpr (Value MeasureName)
-> SqlExpr (Value MeasureName) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. MeasureName -> SqlExpr (Value MeasureName)
forall typ. PersistField typ => typ -> SqlExpr (Value typ)
E.val MeasureName
measureName)
              [SqlExpr OrderBy] -> SqlQuery ()
E.orderBy [SqlExpr (Value Int) -> SqlExpr OrderBy
forall a. PersistField a => SqlExpr (Value a) -> SqlExpr OrderBy
E.asc (SqlExpr (Entity WarmUpMeasure)
measure SqlExpr (Entity WarmUpMeasure)
-> EntityField WarmUpMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpMeasure Int
forall typ. (typ ~ Int) => EntityField WarmUpMeasure typ
WarmUpMeasurePeriod)]
              SqlExpr (Value Int) -> SqlQuery ()
forall a. ToSomeValues a => a -> SqlQuery ()
E.groupBy (SqlExpr (Entity WarmUpMeasure)
measure SqlExpr (Entity WarmUpMeasure)
-> EntityField WarmUpMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpMeasure Int
forall typ. (typ ~ Int) => EntityField WarmUpMeasure typ
WarmUpMeasurePeriod)
              (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall (m :: * -> *) a. Monad m => a -> m a
return (SqlExpr (Entity WarmUpMeasure)
measure SqlExpr (Entity WarmUpMeasure)
-> EntityField WarmUpMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpMeasure Int
forall typ. (typ ~ Int) => EntityField WarmUpMeasure typ
WarmUpMeasurePeriod, SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double))
forall a b.
(PersistField a, PersistField b) =>
SqlExpr (Value a) -> SqlExpr (Value (Maybe b))
E.avg_ (SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double)))
-> SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double))
forall a b. (a -> b) -> a -> b
$ SqlExpr (Entity WarmUpResultStep)
result SqlExpr (Entity WarmUpResultStep)
-> EntityField WarmUpResultStep Double -> SqlExpr (Value Double)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField WarmUpResultStep Double
forall typ. (typ ~ Double) => EntityField WarmUpResultStep typ
WarmUpResultStepYValue)
          ResultDataRepKeys [Key RepResultData]
xs ->
            (InnerJoin
   (SqlExpr (Entity RepMeasure)) (SqlExpr (Entity RepResultStep))
 -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall a b. From a => (a -> SqlQuery b) -> SqlQuery b
E.from ((InnerJoin
    (SqlExpr (Entity RepMeasure)) (SqlExpr (Entity RepResultStep))
  -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
 -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> (InnerJoin
      (SqlExpr (Entity RepMeasure)) (SqlExpr (Entity RepResultStep))
    -> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double))))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall a b. (a -> b) -> a -> b
$ \(SqlExpr (Entity RepMeasure)
measure `E.InnerJoin` SqlExpr (Entity RepResultStep)
result) -> do
              SqlExpr (Value Bool) -> SqlQuery ()
E.on (SqlExpr (Entity RepMeasure)
measure SqlExpr (Entity RepMeasure)
-> EntityField RepMeasure (Key RepMeasure)
-> SqlExpr (Value (Key RepMeasure))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepMeasure (Key RepMeasure)
forall typ. (typ ~ Key RepMeasure) => EntityField RepMeasure typ
RepMeasureId SqlExpr (Value (Key RepMeasure))
-> SqlExpr (Value (Key RepMeasure)) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. SqlExpr (Entity RepResultStep)
result SqlExpr (Entity RepResultStep)
-> EntityField RepResultStep (Key RepMeasure)
-> SqlExpr (Value (Key RepMeasure))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepResultStep (Key RepMeasure)
forall typ. (typ ~ Key RepMeasure) => EntityField RepResultStep typ
RepResultStepMeasure)
              case [Key RepResultData]
xs of
                [Key RepResultData
x] -> SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity RepMeasure)
measure SqlExpr (Entity RepMeasure)
-> EntityField RepMeasure (Key RepResultData)
-> SqlExpr (Value (Key RepResultData))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepMeasure (Key RepResultData)
forall typ. (typ ~ Key RepResultData) => EntityField RepMeasure typ
RepMeasureRepResult SqlExpr (Value (Key RepResultData))
-> SqlExpr (Value (Key RepResultData)) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. Key RepResultData -> SqlExpr (Value (Key RepResultData))
forall typ. PersistField typ => typ -> SqlExpr (Value typ)
E.val Key RepResultData
x)
                [Key RepResultData]
_   -> SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity RepMeasure)
measure SqlExpr (Entity RepMeasure)
-> EntityField RepMeasure (Key RepResultData)
-> SqlExpr (Value (Key RepResultData))
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepMeasure (Key RepResultData)
forall typ. (typ ~ Key RepResultData) => EntityField RepMeasure typ
RepMeasureRepResult SqlExpr (Value (Key RepResultData))
-> SqlExpr (ValueList (Key RepResultData)) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ)
-> SqlExpr (ValueList typ) -> SqlExpr (Value Bool)
`E.in_` [Key RepResultData] -> SqlExpr (ValueList (Key RepResultData))
forall typ. PersistField typ => [typ] -> SqlExpr (ValueList typ)
E.valList [Key RepResultData]
xs)
              SqlExpr (Value Bool) -> SqlQuery ()
E.where_ (SqlExpr (Entity RepResultStep)
result SqlExpr (Entity RepResultStep)
-> EntityField RepResultStep MeasureName
-> SqlExpr (Value MeasureName)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepResultStep MeasureName
forall typ. (typ ~ MeasureName) => EntityField RepResultStep typ
RepResultStepName SqlExpr (Value MeasureName)
-> SqlExpr (Value MeasureName) -> SqlExpr (Value Bool)
forall typ.
PersistField typ =>
SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
E.==. MeasureName -> SqlExpr (Value MeasureName)
forall typ. PersistField typ => typ -> SqlExpr (Value typ)
E.val MeasureName
measureName)
              [SqlExpr OrderBy] -> SqlQuery ()
E.orderBy [SqlExpr (Value Int) -> SqlExpr OrderBy
forall a. PersistField a => SqlExpr (Value a) -> SqlExpr OrderBy
E.asc (SqlExpr (Entity RepMeasure)
measure SqlExpr (Entity RepMeasure)
-> EntityField RepMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepMeasure Int
forall typ. (typ ~ Int) => EntityField RepMeasure typ
RepMeasurePeriod)]
              SqlExpr (Value Int) -> SqlQuery ()
forall a. ToSomeValues a => a -> SqlQuery ()
E.groupBy (SqlExpr (Entity RepMeasure)
measure SqlExpr (Entity RepMeasure)
-> EntityField RepMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepMeasure Int
forall typ. (typ ~ Int) => EntityField RepMeasure typ
RepMeasurePeriod)
              (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
-> SqlQuery (SqlExpr (Value Int), SqlExpr (Value (Maybe Double)))
forall (m :: * -> *) a. Monad m => a -> m a
return (SqlExpr (Entity RepMeasure)
measure SqlExpr (Entity RepMeasure)
-> EntityField RepMeasure Int -> SqlExpr (Value Int)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepMeasure Int
forall typ. (typ ~ Int) => EntityField RepMeasure typ
RepMeasurePeriod, SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double))
forall a b.
(PersistField a, PersistField b) =>
SqlExpr (Value a) -> SqlExpr (Value (Maybe b))
E.avg_ (SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double)))
-> SqlExpr (Value Double) -> SqlExpr (Value (Maybe Double))
forall a b. (a -> b) -> a -> b
$ SqlExpr (Entity RepResultStep)
result SqlExpr (Entity RepResultStep)
-> EntityField RepResultStep Double -> SqlExpr (Value Double)
forall typ val.
(PersistEntity val, PersistField typ) =>
SqlExpr (Entity val) -> EntityField val typ -> SqlExpr (Value typ)
E.^. EntityField RepResultStep Double
forall typ. (typ ~ Double) => EntityField RepResultStep typ
RepResultStepYValue)
      keys :: [Keys]
keys = (ResultData a -> Keys) -> [ResultData a] -> [Keys]
forall a b. (a -> b) -> [a] -> [b]
map (ResultDataKey -> Keys
toKeys (ResultDataKey -> Keys)
-> (ResultData a -> ResultDataKey) -> ResultData a -> Keys
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting ResultDataKey (ResultData a) ResultDataKey
-> ResultData a -> ResultDataKey
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting ResultDataKey (ResultData a) ResultDataKey
forall a. Lens' (ResultData a) ResultDataKey
resultDataKey) [ResultData a]
resData
      toKeys :: ResultDataKey -> Keys
toKeys (ResultDataRep Key RepResultData
key)    = [Key RepResultData] -> Keys
ResultDataRepKeys [Key RepResultData
key]
      toKeys (ResultDataWarmUp Key WarmUpResultData
key) = [Key WarmUpResultData] -> Keys
ResultDataWarmUpKeys [Key WarmUpResultData
key]
      toKeys (ResultDataPrep Key PrepResultData
key)   = [Key PrepResultData] -> Keys
ResultDataPrepKeys [Key PrepResultData
key]


  -- let toMeasure (E.Value p, E.Value v) = Measure p [StepResult measureName Nothing v]
  ConduitT () Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (m :: * -> *) r. Monad m => ConduitT () Void m r -> m r
C.runConduit (ConduitT () Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
 -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> ConduitT
     () Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a b. (a -> b) -> a -> b
$
    ConduitT
  ()
  (Value Int, Value (Maybe Double))
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
vals ConduitT
  ()
  (Value Int, Value (Maybe Double))
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
-> ConduitM
     (Value Int, Value (Maybe Double))
     Void
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
-> ConduitT
     () Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
C..| ((Value Int, Value (Maybe Double)) -> (Int, Double))
-> ConduitT
     (Value Int, Value (Maybe Double))
     (Int, Double)
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall (m :: * -> *) a b. Monad m => (a -> b) -> ConduitT a b m ()
C.mapC (Value Int, Value (Maybe Double)) -> (Int, Double)
fromValueC ConduitT
  (Value Int, Value (Maybe Double))
  (Int, Double)
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
-> ConduitM
     (Int, Double)
     Void
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
-> ConduitM
     (Value Int, Value (Maybe Double))
     Void
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
C..| Smoothing
-> ConduitT
     (Int, Double)
     (Int, Double)
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall (m :: * -> *).
Monad m =>
Smoothing -> ConduitT (Int, Double) (Int, Double) m ()
smoothC Smoothing
smoothing ConduitT
  (Int, Double)
  (Int, Double)
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
-> ConduitM
     (Int, Double)
     Void
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
-> ConduitM
     (Int, Double)
     Void
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
C..| ((Int, Double) -> String)
-> ConduitT
     (Int, Double)
     String
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall (m :: * -> *) a b. Monad m => (a -> b) -> ConduitT a b m ()
C.mapC (Int, Double) -> String
forall a a. (Show a, Show a) => (a, a) -> String
toFileCts ConduitT
  (Int, Double)
  String
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
-> ConduitM
     String Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
-> ConduitM
     (Int, Double)
     Void
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
C..| (String -> Bool)
-> ConduitT
     String String (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
forall (m :: * -> *) a. Monad m => (a -> Bool) -> ConduitT a a m ()
C.filterC (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) ConduitT
  String String (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
-> ConduitM
     String Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
-> ConduitM
     String Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
C..| (String -> MeasureName)
-> ConduitT
     String
     MeasureName
     (ReaderT SqlBackend (LoggingT (ResourceT IO)))
     ()
forall (m :: * -> *) a b. Monad m => (a -> b) -> ConduitT a b m ()
C.mapC (String -> MeasureName
T.pack (String -> MeasureName)
-> (String -> String) -> String -> MeasureName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n")) ConduitT
  String
  MeasureName
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
-> ConduitM
     MeasureName Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
-> ConduitM
     String Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
C..| ConduitT
  MeasureName
  ByteString
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
forall (m :: * -> *) text binary.
(Monad m, Utf8 text binary) =>
ConduitT text binary m ()
C.encodeUtf8C ConduitT
  MeasureName
  ByteString
  (ReaderT SqlBackend (LoggingT (ResourceT IO)))
  ()
-> ConduitM
     ByteString Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
-> ConduitM
     MeasureName Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
forall (m :: * -> *) a b c r.
Monad m =>
ConduitM a b m () -> ConduitM b c m r -> ConduitM a c m r
C..| Handle
-> ConduitM
     ByteString Void (ReaderT SqlBackend (LoggingT (ResourceT IO))) ()
forall (m :: * -> *) o.
MonadIO m =>
Handle -> ConduitT ByteString o m ()
sinkHandle Handle
fileH

  IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> IO () -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a b. (a -> b) -> a -> b
$ Handle -> IO ()
hFlush Handle
fileH IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> IO ()
hClose Handle
fileH
  UTCTime
end <- IO UTCTime -> ReaderT SqlBackend (LoggingT (ResourceT IO)) UTCTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
getCurrentTime
  $(Int
String
LogLevel
String -> MeasureName
String -> String -> String -> CharPos -> CharPos -> Loc
MeasureName -> MeasureName
Loc
-> MeasureName
-> LogLevel
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
(MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> (MeasureName -> MeasureName)
-> MeasureName
-> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a. a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> MeasureName -> LogLevel -> msg -> m ()
pack :: String -> MeasureName
monadLoggerLog :: forall (m :: * -> *) msg.
(MonadLogger m, ToLogStr msg) =>
Loc -> MeasureName -> LogLevel -> msg -> m ()
. :: forall b c a. (b -> c) -> (a -> b) -> a -> c
id :: forall a. a -> a
logInfo) (MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ())
-> MeasureName -> ReaderT SqlBackend (LoggingT (ResourceT IO)) ()
forall a b. (a -> b) -> a -> b
$ MeasureName
"Done. Computation Time: " MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> NominalDiffTime -> MeasureName
forall a. Show a => a -> MeasureName
tshow (UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime UTCTime
end UTCTime
start)
  where
    filePath :: String
filePath = String
folder String -> String -> String
</> MeasureName -> String
T.unpack (MeasureName
prefix MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
"_" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
measureName MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
".csv")
    filePathPlotSh :: String
filePathPlotSh = String
folder String -> String -> String
</> String
"plot.sh"
    folder :: String
folder = Experiments a -> String
forall a. Experiments a -> String
expsPath Experiments a
exps String -> String -> String
</> String
"csv"
    header :: String
header = String
"Period\t" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> MeasureName -> String
T.unpack (MeasureName
prefix MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
"_" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
measureName) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\n"
    toFileCts :: (a, a) -> String
toFileCts (a
p, a
res) = a -> String
forall a. Show a => a -> String
show a
p String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\t" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
res
    fromValueC :: (E.Value Int, E.Value (Maybe Double)) -> (Int, Double)
    fromValueC :: (Value Int, Value (Maybe Double)) -> (Int, Double)
fromValueC (Value Int
vPeriod, Value (Maybe Double)
vMVal) = (Value Int -> Int
forall a. Value a -> a
E.unValue Value Int
vPeriod, Double -> Maybe Double -> Double
forall a. a -> Maybe a -> a
fromMaybe Double
0 (Maybe Double -> Double) -> Maybe Double -> Double
forall a b. (a -> b) -> a -> b
$ Value (Maybe Double) -> Maybe Double
forall a. Value a -> a
E.unValue Value (Maybe Double)
vMVal)


-- smoothAndWriteFileResultData :: Experiments a -> T.Text -> Smoothing -> MeasureName -> ResultData a -> DB IO ()
-- smoothAndWriteFileResultData exps prefix smoothing measureName resData = do
--   $(logInfo) $ "Processing measure " <> measureName <> ". Saving data to: " <> T.pack folder
--   liftIO $ createDirectoryIfMissing True folder
--   liftIO $ writeFile filePath header >> writeFile filePathPlotSh plotSh
--   fileH <- liftIO $ openFile filePath AppendMode
--   start <- liftIO getCurrentTime
--   let src =
--         E.selectSource $
--         case resData ^. resultDataKey of
--           ResultDataPrep key ->
--             E.from $ \(measure `E.InnerJoin` result) -> do
--               E.on (measure E.^. PrepMeasureId E.==. result E.^. PrepResultStepMeasure)
--               E.where_ (measure E.^. PrepMeasurePrepResultData E.==. E.val key)
--               E.where_ (result E.^. PrepResultStepName E.==. E.val measureName)
--               E.orderBy [E.asc (measure E.^. PrepMeasurePeriod)]
--               return (measure E.^. PrepMeasurePeriod, result E.^. PrepResultStepYValue)
--           ResultDataWarmUp key -> undefined
--           ResultDataRep key -> undefined
--   let toMeasure (E.Value p, E.Value v) = Measure p [StepResult measureName Nothing v]
--   C.runConduit $
--     src C..| C.mapC toMeasure C..| smoothC smoothing C..| C.mapC toFileCts C..| C.filterC (not . null) C..| C.mapC (T.pack . (++ "\n")) C..| C.encodeUtf8C C..| sinkHandle fileH
--   liftIO $ hFlush fileH >> hClose fileH
--   end <- liftIO getCurrentTime
--   $(logInfo) $ "Done. Computation Time: " <> tshow (diffUTCTime end start)
--   where
--     filePath = folder </> T.unpack (prefix <> "_" <> measureName <> ".csv")
--     filePathPlotSh = folder </> "plot.sh"
--     folder = expsPath exps </> "csv"
--     header = "Period\t" <> T.unpack (prefix <> "_" <> measureName) <> "\n"
--     toFileCts (Measure p []) = []
--     toFileCts (Measure p [res]) = show p <> "\t" <> show (res ^. resultYValue)
--     toFileCts (Measure p _) = error $ "The measure " <> T.unpack measureName <> " has more than one results in period " <> show p


namePrefix :: Int -> Phase -> Avg -> T.Text
namePrefix :: Int -> Phase -> Avg -> MeasureName
namePrefix Int
expNr Phase
ph Avg
av = MeasureName
"exp" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Int -> MeasureName
forall a. Show a => a -> MeasureName
tshow Int
expNr MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
"_" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Phase -> MeasureName
phaseName Phase
ph MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
"_" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Avg -> MeasureName
avgName Avg
av MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
"_"

phaseName :: Phase -> T.Text
phaseName :: Phase -> MeasureName
phaseName Phase
PreparationPhase = MeasureName
"prep"
phaseName Phase
WarmUpPhase      = MeasureName
"warmUp"
phaseName Phase
EvaluationPhase  = MeasureName
"eval"

type ReplNr = Int
type RepetNr = Int
data Avg = None !RepetNr !(Maybe ReplNr) | Repl !RepetNr | Repet !ReplNr | RepetRepl

avgName :: Avg -> T.Text
avgName :: Avg -> MeasureName
avgName (None Int
repet (Just Int
repl)) = MeasureName
"repet" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Int -> MeasureName
forall a. Show a => a -> MeasureName
tshow Int
repet MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
"_repl" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Int -> MeasureName
forall a. Show a => a -> MeasureName
tshow Int
repl
avgName (None Int
repet Maybe Int
Nothing)     = MeasureName
"repet" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Int -> MeasureName
forall a. Show a => a -> MeasureName
tshow Int
repet
avgName (Repl Int
repet)             = MeasureName
"repet" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Int -> MeasureName
forall a. Show a => a -> MeasureName
tshow Int
repet MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> MeasureName
"_replAvg"
avgName (Repet Int
repl)             = MeasureName
"repetAvg_repl" MeasureName -> MeasureName -> MeasureName
forall a. Semigroup a => a -> a -> a
<> Int -> MeasureName
forall a. Show a => a -> MeasureName
tshow Int
repl
avgName Avg
RepetRepl                = MeasureName
"repetAvg_replAvg"

plotSh :: String
plotSh :: String
plotSh =
  [String] -> String
unlines
    [ String
"FILES=\"\"                                               "
    , String
"                                                         "
    , String
"for arg in $@; do                                        "
    , String
"     FILES=\"$FILES `find . -type f -name \"*$arg*\"`\"  "
    , String
"done                                                     "
    , String
"                                                         "
    , String
"echo $FILES                                              "
    , String
"                                                         "
    , String
"ARRAY=($FILES)                                           "
    , String
"for col in {2,3,4}; do                                   "
    , String
"    CMD=\"set key autotitle columnhead; plot \"          "
    , String
"    for f in $FILES; do                                  "
    , String
"        echo $f                                          "
    , String
"        CMD=\"$CMD '$f' using 0:$col with lines \"       "
    , String
"        if [ \"$f\" != \"${ARRAY[-1]}\" ]; then          "
    , String
"            CMD=\"$CMD, \"                               "
    , String
"        fi                                               "
    , String
"    done                                                 "
    , String
"    CMD=\"$CMD; pause mouse close; \"                    "
    , String
"    echo $CMD                                            "
    , String
"    gnuplot -e \"$CMD\" &                                "
    , String
"done                                                     "
    ]