module BuildBox.Data.Comparison
(
Comparison (..)
, makeComparison)
where
import BuildBox.Pretty
import Text.Printf
import qualified Data.Text as T
data Comparison a
= Comparison
{ comparisonBaseline :: a
, comparisonRecent :: a
, comparisonSwing :: Double }
| ComparisonNew
{ comparisonNew :: a }
deriving (Read, Show)
instance Pretty a => Pretty (Comparison a) where
ppr (Comparison _ recent ratio)
| abs ratio < 0.01
= string $ printf "%s (----)"
(T.unpack $ ppr recent)
| otherwise
= string $ printf "%s (%+4.0f)"
(T.unpack $ ppr recent)
(ratio * 100)
ppr (ComparisonNew new)
= (padL 10 $ ppr new)
makeComparison :: Real a => a -> a -> Comparison a
makeComparison base recent
= Comparison base recent swing
where dBase = fromRational $ toRational base
dRecent = fromRational $ toRational recent
swing = ((dRecent - dBase) / dBase)