-- |
-- 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 { TimingStatsWriter a -> Int
timingStatsWidth         :: Int,
                      -- ^ The width of the HTML table.
                      TimingStatsWriter a -> String
timingStatsTimeText      :: String,
                      -- ^ Translated text \"time\".
                      TimingStatsWriter a -> String
timingStatsMeanText      :: String,
                      -- ^ Translated text \"mean\".
                      TimingStatsWriter a -> String
timingStatsDeviationText :: String,
                      -- ^ Translated text \"deviation\".
                      TimingStatsWriter a -> String
timingStatsMinText       :: String,
                      -- ^ Translated text \"minimum\".
                      TimingStatsWriter a -> String
timingStatsMaxText       :: String,
                      -- ^ Translated text \"maximum\".
                      TimingStatsWriter a -> ShowS
timingStatsFormatter     :: ShowS,
                      -- ^ The formatter of numbers.
                      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 :: TimingStatsWriter a
defaultTimingStatsWriter =
  TimingStatsWriter :: forall a.
Int
-> String
-> String
-> String
-> String
-> String
-> ShowS
-> (TimingStatsWriter a
    -> String -> TimingStats a -> HtmlWriter ())
-> TimingStatsWriter a
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 = ShowS
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 = TimingStatsWriter a -> ShowS
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ TimingStatsWriter a -> Int
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ TimingStatsWriter a -> String
forall a. TimingStatsWriter a -> String
timingStatsMeanText TimingStatsWriter a
writer 
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td colspan='2'>"
         String -> HtmlWriter ()
writeHtmlText (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ ShowS
format ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Double -> String
forall a. Show a => a -> String
show (Double -> String) -> Double -> String
forall a b. (a -> b) -> a -> b
$ TimingStats a -> Double
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ TimingStatsWriter a -> String
forall a. TimingStatsWriter a -> String
timingStatsDeviationText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td colspan='2'>"
         String -> HtmlWriter ()
writeHtmlText (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ ShowS
format ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Double -> String
forall a. Show a => a -> String
show (Double -> String) -> Double -> String
forall a b. (a -> b) -> a -> b
$ TimingStats a -> Double
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ TimingStatsWriter a -> String
forall a. TimingStatsWriter a -> String
timingStatsMinText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ ShowS
format ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ a -> String
forall a. Show a => a -> String
show (a -> String) -> a -> String
forall a b. (a -> b) -> a -> b
$ TimingStats a -> a
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ TimingStatsWriter a -> String
forall a. TimingStatsWriter a -> String
timingStatsTimeText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtmlText String
" = "
         String -> HtmlWriter ()
writeHtmlText (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ ShowS
format ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Double -> String
forall a. Show a => a -> String
show (Double -> String) -> Double -> String
forall a b. (a -> b) -> a -> b
$ TimingStats a -> Double
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ TimingStatsWriter a -> String
forall a. TimingStatsWriter a -> String
timingStatsMaxText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         String -> HtmlWriter ()
writeHtmlText (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ ShowS
format ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ a -> String
forall a. Show a => a -> String
show (a -> String) -> a -> String
forall a b. (a -> b) -> a -> b
$ TimingStats a -> a
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ TimingStatsWriter a -> String
forall a. TimingStatsWriter a -> String
timingStatsTimeText TimingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtmlText String
" = "
         String -> HtmlWriter ()
writeHtmlText (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ ShowS
format ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Double -> String
forall a. Show a => a -> String
show (Double -> String) -> Double -> String
forall a b. (a -> b) -> a -> b
$ TimingStats a -> Double
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ TimingStatsWriter a -> String
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ ShowS
format ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Double -> String
forall a. Show a => a -> String
show (Double -> String) -> Double -> String
forall a b. (a -> b) -> a -> b
$ TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsStartTime TimingStats a
stats
         String -> HtmlWriter ()
writeHtml String
"; "
         String -> HtmlWriter ()
writeHtml (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ ShowS
format ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ Double -> String
forall a. Show a => a -> String
show (Double -> String) -> Double -> String
forall a b. (a -> b) -> a -> b
$ TimingStats a -> Double
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>"
    }