{-# LANGUAGE AllowAmbiguousTypes #-}
module Data.Registry.Internal.Statistics where
import Data.Registry.Internal.Types
import Protolude
import Type.Reflection
data Statistics = Statistics {
operations :: Operations
, values :: Values
} deriving (Show)
instance Semigroup Statistics where
Statistics ops1 vs1 <> Statistics ops2 vs2 =
Statistics (ops1 <> ops2) (vs1 <> vs2)
instance Monoid Statistics where
mempty = Statistics mempty mempty
mappend = (<>)
type Operations = [AppliedFunction]
type Paths = [[Value]]
data AppliedFunction = AppliedFunction {
_outputValue :: Value
, _inputValues ::[Value]
} deriving (Show)
initStatistics :: Values -> Statistics
initStatistics vs = mempty { values = vs }
usedSpecializations :: Statistics -> [Specialization]
usedSpecializations stats =
case values stats of
Values [] -> []
Values (v : vs) ->
case usedSpecialization v of
Just s -> s : usedSpecializations stats { values = Values vs }
Nothing -> usedSpecializations stats { values = Values vs }
allValuesPaths :: Statistics -> Paths
allValuesPaths stats = do
v <- unValues $ values stats
valuePaths v
valuePaths :: Value -> Paths
valuePaths v@(CreatedValue _ _ _ _ (Dependencies ds)) = do
d <- ds
(v :) <$> valuePaths d
valuePaths _ = []
findMostRecentValue :: forall a . (Typeable a) => Statistics -> Maybe Value
findMostRecentValue stats = find (\v -> valueDynTypeRep v == someTypeRep (Proxy :: Proxy a)) $ unValues (values stats)