{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE OverloadedStrings #-}
module Experimenter.Eval.Util where

import           Control.DeepSeq
import           Control.Lens             hiding (Cons, Over, over)
import           Control.Monad.IO.Class
import qualified Data.Text                as T
import           Data.Time.Clock          (diffUTCTime, getCurrentTime)
import           System.FilePath.Posix

import           Experimenter.Result.Type


rootPath :: FilePath
rootPath :: FilePath
rootPath = FilePath
"results"

mainFile :: Experiments a -> FilePath
mainFile :: forall a. Experiments a -> FilePath
mainFile Experiments a
exps = FilePath
"main_" forall a. Semigroup a => a -> a -> a
<> forall a. Experiments a -> FilePath
getTime Experiments a
exps forall a. Semigroup a => a -> a -> a
<> FilePath
".tex"

getTime :: Experiments a -> String
getTime :: forall a. Experiments a -> FilePath
getTime Experiments a
exps = forall b a. b -> (a -> b) -> Maybe a -> b
maybe FilePath
"unfinished_experiment" (Text -> FilePath
T.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text -> Text
T.replace Text
" " Text
"_" forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> FilePath
show) (Experiments a
exps forall s a. s -> Getting a s a -> a
^. forall a. Lens' (Experiments a) (Maybe UTCTime)
experimentsEndTime)

scalarFile :: Experiments a -> FilePath
scalarFile :: forall a. Experiments a -> FilePath
scalarFile Experiments a
exps = FilePath
"scalar_" forall a. Semigroup a => a -> a -> a
<> forall a. Experiments a -> FilePath
getTime Experiments a
exps forall a. Semigroup a => a -> a -> a
<> FilePath
".tex"

repetitionFile :: Experiments a -> FilePath
repetitionFile :: forall a. Experiments a -> FilePath
repetitionFile Experiments a
exps = FilePath
"repetition_" forall a. Semigroup a => a -> a -> a
<> forall a. Experiments a -> FilePath
getTime Experiments a
exps forall a. Semigroup a => a -> a -> a
<> FilePath
".tex"

replicationFile :: Experiments a -> FilePath
replicationFile :: forall a. Experiments a -> FilePath
replicationFile Experiments a
exps = FilePath
"replication_" forall a. Semigroup a => a -> a -> a
<> forall a. Experiments a -> FilePath
getTime Experiments a
exps forall a. Semigroup a => a -> a -> a
<> FilePath
".tex"

periodicFile :: Experiments a -> FilePath
periodicFile :: forall a. Experiments a -> FilePath
periodicFile Experiments a
exps = FilePath
"periodic_" forall a. Semigroup a => a -> a -> a
<> forall a. Experiments a -> FilePath
getTime Experiments a
exps forall a. Semigroup a => a -> a -> a
<> FilePath
".tex"

mainFilePdf :: Experiments a -> FilePath
mainFilePdf :: forall a. Experiments a -> FilePath
mainFilePdf Experiments a
exps = Text -> FilePath
T.unpack ((Char -> Bool) -> Text -> Text
T.dropWhileEnd (forall a. Eq a => a -> a -> Bool
/= Char
'.') (FilePath -> Text
T.pack forall a b. (a -> b) -> a -> b
$ forall a. Experiments a -> FilePath
mainFile Experiments a
exps)) forall a. Semigroup a => a -> a -> a
<> FilePath
"pdf"

getExpsName :: Experiments a -> String
getExpsName :: forall a. Experiments a -> FilePath
getExpsName Experiments a
exps  = Text -> FilePath
T.unpack forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text -> Text
T.replace Text
"/" Text
"_" forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text -> Text
T.replace Text
" " Text
"_" forall a b. (a -> b) -> a -> b
$ Experiments a
exps forall s a. s -> Getting a s a -> a
^. forall a. Lens' (Experiments a) Text
experimentsName

expsPath :: Experiments a -> FilePath
expsPath :: forall a. Experiments a -> FilePath
expsPath Experiments a
exps = FilePath
rootPath FilePath -> FilePath -> FilePath
</> forall a. Experiments a -> FilePath
getExpsName Experiments a
exps

mkTime :: (MonadIO m, NFData t) => String -> m t -> m t
mkTime :: forall (m :: * -> *) t.
(MonadIO m, NFData t) =>
FilePath -> m t -> m t
mkTime FilePath
name m t
a = do
  UTCTime
start <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
getCurrentTime
  !t
val <- forall a. NFData a => a -> a
force forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m t
a
  UTCTime
end <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
getCurrentTime
  let name' :: FilePath
name' | forall (t :: * -> *) a. Foldable t => t a -> Bool
null FilePath
name = FilePath
name
            | Bool
otherwise = FilePath
name forall a. [a] -> [a] -> [a]
++ [Char
' ']
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO ()
putStrLn (FilePath
name' forall a. Semigroup a => a -> a -> a
<> FilePath
"Computation Time: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> FilePath
show (UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime UTCTime
end UTCTime
start))
  forall (m :: * -> *) a. Monad m => a -> m a
return t
val