-- |
-- 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 = ShowS
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 = SamplingStatsWriter a -> ShowS
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 (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
$ SamplingStatsWriter a -> Int
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ SamplingStatsWriter a -> String
forall a. SamplingStatsWriter a -> String
samplingStatsMeanText SamplingStatsWriter 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
$ Double -> String
forall a. Show a => a -> String
show (Double -> String) -> Double -> String
forall a b. (a -> b) -> a -> b
$ SamplingStats a -> Double
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ SamplingStatsWriter a -> String
forall a. SamplingStatsWriter a -> String
samplingStatsDeviationText SamplingStatsWriter 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
$ Double -> String
forall a. Show a => a -> String
show (Double -> String) -> Double -> String
forall a b. (a -> b) -> a -> b
$ SamplingStats a -> Double
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ SamplingStatsWriter a -> String
forall a. SamplingStatsWriter a -> String
samplingStatsMinText SamplingStatsWriter 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
$ SamplingStats a -> a
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ SamplingStatsWriter a -> String
forall a. SamplingStatsWriter a -> String
samplingStatsMaxText SamplingStatsWriter 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
$ SamplingStats a -> a
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 (String -> HtmlWriter ()) -> String -> HtmlWriter ()
forall a b. (a -> b) -> a -> b
$ SamplingStatsWriter a -> String
forall a. SamplingStatsWriter a -> String
samplingStatsCountText SamplingStatsWriter a
writer
         String -> HtmlWriter ()
writeHtml String
"</td>"
         String -> HtmlWriter ()
writeHtml String
"<td>"
         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
$ Int -> String
forall a. Show a => a -> String
show (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ SamplingStats a -> Int
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>"
    }