-- |
-- Module     : Simulation.Aivika.Experiment.Base.SamplingStatsWriter
-- 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 'SamplingStatsWriter' that knows how to write
-- the sampling statistics in HTML.
--

module Simulation.Aivika.Experiment.Base.SamplingStatsWriter 
       (SamplingStatsWriter(..),
        defaultSamplingStatsWriter) 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 'SamplingStats'
-- as the HTML table.
data SamplingStatsWriter a =
  SamplingStatsWriter { forall a. SamplingStatsWriter a -> Int
samplingStatsWidth         :: Int,
                        -- ^ The width of the HTML table.
                        forall a. SamplingStatsWriter a -> String
samplingStatsMeanText      :: String,
                        -- ^ Translated text \"mean\".
                        forall a. SamplingStatsWriter a -> String
samplingStatsDeviationText :: String,
                        -- ^ Translated text \"deviation\".
                        forall a. SamplingStatsWriter a -> String
samplingStatsMinText       :: String,
                        -- ^ Translated text \"minimum\".
                        forall a. SamplingStatsWriter a -> String
samplingStatsMaxText       :: String,
                        -- ^ Translated text \"maximum\".
                        forall a. SamplingStatsWriter a -> String
samplingStatsCountText       :: String,
                        -- ^ Translated text \"count\".
                        forall a. SamplingStatsWriter a -> ShowS
samplingStatsFormatter     :: ShowS,
                        -- ^ The formatter of numbers.
                        forall a.
SamplingStatsWriter a
-> SamplingStatsWriter a
-> String
-> SamplingStats a
-> HtmlWriter ()
samplingStatsWrite         :: SamplingStatsWriter a -> String ->
                                                      SamplingStats a -> HtmlWriter ()
                        -- ^ This function reprensents the named statistics
                        -- as the HTML table.
                      }

-- | The default writer.
defaultSamplingStatsWriter :: Show a => SamplingStatsWriter a
defaultSamplingStatsWriter :: forall a. Show a => SamplingStatsWriter a
defaultSamplingStatsWriter =
  SamplingStatsWriter { 
    samplingStatsWidth :: Int
samplingStatsWidth = Int
400,
    samplingStatsMeanText :: String
samplingStatsMeanText = String
"mean",
    samplingStatsDeviationText :: String
samplingStatsDeviationText = String
"deviation",
    samplingStatsMinText :: String
samplingStatsMinText = String
"minimum",
    samplingStatsMaxText :: String
samplingStatsMaxText = String
"maximum",
    samplingStatsCountText :: String
samplingStatsCountText = String
"count",
    samplingStatsFormatter :: ShowS
samplingStatsFormatter = forall a. a -> a
id,
    samplingStatsWrite :: SamplingStatsWriter a -> String -> SamplingStats a -> HtmlWriter ()
samplingStatsWrite = \SamplingStatsWriter a
writer String
name SamplingStats a
stats ->
      do let format :: ShowS
format String
x = forall a. SamplingStatsWriter a -> ShowS
samplingStatsFormatter SamplingStatsWriter 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. SamplingStatsWriter a -> Int
samplingStatsWidth SamplingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"'>"
         String -> HtmlWriter ()
writeHtml String
"<tr>"
         String -> HtmlWriter ()
writeHtml String
"<td colspan='2'>"
         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. SamplingStatsWriter a -> String
samplingStatsMeanText SamplingStatsWriter 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. SamplingStats a -> Double
samplingStatsMean SamplingStats 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. SamplingStatsWriter a -> String
samplingStatsDeviationText SamplingStatsWriter 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. SamplingStats a -> Double
samplingStatsDeviation SamplingStats 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. SamplingStatsWriter a -> String
samplingStatsMinText SamplingStatsWriter 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. SamplingStats a -> a
samplingStatsMin SamplingStats 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. SamplingStatsWriter a -> String
samplingStatsMaxText SamplingStatsWriter 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. SamplingStats a -> a
samplingStatsMax SamplingStats 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. SamplingStatsWriter a -> String
samplingStatsCountText SamplingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         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. SamplingStats a -> Int
samplingStatsCount SamplingStats a
stats
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"</tr>"
         String -> HtmlWriter ()
writeHtml String
"</table>" 
         String -> HtmlWriter ()
writeHtml String
"</p>"
    }