-- |
-- Module     : Simulation.Aivika.Experiment.Base.TimingStatsWriter
-- Copyright  : Copyright (c) 2012-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- The module defines 'TimingStatsWriter' that knows how to write
-- the timing statistics in HTML.
--

module Simulation.Aivika.Experiment.Base.TimingStatsWriter 
       (TimingStatsWriter(..),
        defaultTimingStatsWriter) where

import Simulation.Aivika.Experiment.Types
import Simulation.Aivika.Experiment.Base.HtmlWriter

import Simulation.Aivika.Statistics

-- | Defines a writer that knows how to represent the 'TimingStats'
-- as the HTML table.
data TimingStatsWriter a =
  TimingStatsWriter { forall a. TimingStatsWriter a -> Int
timingStatsWidth         :: Int,
                      -- ^ The width of the HTML table.
                      forall a. TimingStatsWriter a -> String
timingStatsTimeText      :: String,
                      -- ^ Translated text \"time\".
                      forall a. TimingStatsWriter a -> String
timingStatsMeanText      :: String,
                      -- ^ Translated text \"mean\".
                      forall a. TimingStatsWriter a -> String
timingStatsDeviationText :: String,
                      -- ^ Translated text \"deviation\".
                      forall a. TimingStatsWriter a -> String
timingStatsMinText       :: String,
                      -- ^ Translated text \"minimum\".
                      forall a. TimingStatsWriter a -> String
timingStatsMaxText       :: String,
                      -- ^ Translated text \"maximum\".
                      forall a. TimingStatsWriter a -> ShowS
timingStatsFormatter     :: ShowS,
                      -- ^ The formatter of numbers.
                      forall a.
TimingStatsWriter a
-> TimingStatsWriter a -> String -> TimingStats a -> HtmlWriter ()
timingStatsWrite         :: TimingStatsWriter a -> String ->
                                                  TimingStats a -> HtmlWriter ()
                      -- ^ This function reprensents the named statistics
                      -- as the HTML table.
                      }

-- | The default writer.
defaultTimingStatsWriter :: (Show a, TimingData a) => TimingStatsWriter a
defaultTimingStatsWriter :: forall a. (Show a, TimingData a) => TimingStatsWriter a
defaultTimingStatsWriter =
  TimingStatsWriter { 
    timingStatsWidth :: Int
timingStatsWidth = Int
400,
    timingStatsMeanText :: String
timingStatsMeanText = String
"mean",
    timingStatsTimeText :: String
timingStatsTimeText = String
"time",
    timingStatsDeviationText :: String
timingStatsDeviationText = String
"deviation",
    timingStatsMinText :: String
timingStatsMinText = String
"minimum",
    timingStatsMaxText :: String
timingStatsMaxText = String
"maximum",
    timingStatsFormatter :: ShowS
timingStatsFormatter = forall a. a -> a
id,
    timingStatsWrite :: TimingStatsWriter a -> String -> TimingStats a -> HtmlWriter ()
timingStatsWrite = \TimingStatsWriter a
writer String
name TimingStats a
stats ->
      do let format :: ShowS
format String
x = forall a. TimingStatsWriter a -> ShowS
timingStatsFormatter TimingStatsWriter a
writer String
x
         String -> HtmlWriter ()
writeHtml String
"<p>"
         String -> HtmlWriter ()
writeHtml String
"<table frame='border' cellspacing='4' width='"
         String -> HtmlWriter ()
writeHtml forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingStatsWriter a -> Int
timingStatsWidth TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"'>"
         String -> HtmlWriter ()
writeHtml String
"<tr>"
         String -> HtmlWriter ()
writeHtml String
"<td colspan='3'>"
         String -> HtmlWriter ()
writeHtml String
"<p align='center'>"
         String -> HtmlWriter ()
writeHtmlText String
name
         String -> HtmlWriter ()
writeHtml String
"</h4>"
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"</tr>"
         String -> HtmlWriter ()
writeHtml String
"<tr>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ forall a. TimingStatsWriter a -> String
timingStatsMeanText TimingStatsWriter a
writer 
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td colspan='2'>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ ShowS
format forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingData a => TimingStats a -> Double
timingStatsMean TimingStats a
stats
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"</tr>"
         String -> HtmlWriter ()
writeHtml String
"<tr>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ forall a. TimingStatsWriter a -> String
timingStatsDeviationText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td colspan='2'>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ ShowS
format forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingData a => TimingStats a -> Double
timingStatsDeviation TimingStats a
stats
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"</tr>"
         String -> HtmlWriter ()
writeHtml String
"<tr>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ forall a. TimingStatsWriter a -> String
timingStatsMinText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ ShowS
format forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingStats a -> a
timingStatsMin TimingStats a
stats
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText String
"("
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ forall a. TimingStatsWriter a -> String
timingStatsTimeText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtmlText String
" = "
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ ShowS
format forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingStats a -> Double
timingStatsMinTime TimingStats a
stats
         String -> HtmlWriter ()
writeHtmlText String
")"
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"</tr>"
         String -> HtmlWriter ()
writeHtml String
"<tr>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ forall a. TimingStatsWriter a -> String
timingStatsMaxText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ ShowS
format forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingStats a -> a
timingStatsMax TimingStats a
stats
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText String
"("
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ forall a. TimingStatsWriter a -> String
timingStatsTimeText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtmlText String
" = "
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ ShowS
format forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingStats a -> Double
timingStatsMaxTime TimingStats a
stats
         String -> HtmlWriter ()
writeHtmlText String
")"
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"</tr>"
         String -> HtmlWriter ()
writeHtml String
"<tr>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText forall a b. (a -> b) -> a -> b
$ forall a. TimingStatsWriter a -> String
timingStatsTimeText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td colspan='2'>"
         String -> HtmlWriter ()
writeHtml String
"["
         String -> HtmlWriter ()
writeHtml forall a b. (a -> b) -> a -> b
$ ShowS
format forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingStats a -> Double
timingStatsStartTime TimingStats a
stats
         String -> HtmlWriter ()
writeHtml String
"; "
         String -> HtmlWriter ()
writeHtml forall a b. (a -> b) -> a -> b
$ ShowS
format forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show forall a b. (a -> b) -> a -> b
$ forall a. TimingStats a -> Double
timingStatsLastTime TimingStats a
stats
         String -> HtmlWriter ()
writeHtml String
"]"
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"</tr>"
         String -> HtmlWriter ()
writeHtml String
"</table>" 
         String -> HtmlWriter ()
writeHtml String
"</p>"
    }