11{-# OPTIONS_GHC -Wno-name-shadowing #-}
2- module Cardano.Timeseries.Domain.Timeseries (Timeseries (.. ), TimeseriesVector , transpose , toVector ) where
2+ {-# LANGUAGE RecordWildCards #-}
3+ module Cardano.Timeseries.Domain.Timeseries (Timeseries (.. ), TimeseriesVector ,
4+ transpose , toVector , oldest , newest , eachOldest , eachNewest ) where
35
4- import Cardano.Timeseries.Domain.Instant (InstantVector )
6+ import Cardano.Timeseries.Domain.Instant (InstantVector , Instant ( Instant ) )
57import qualified Cardano.Timeseries.Domain.Instant as Instant
68import Cardano.Timeseries.Domain.Types
79
8- import Data.List (find )
10+ import Data.List (find , maximumBy , minimumBy )
911import Data.Set
1012import qualified Data.Set as Set
1113import Data.Vector (Vector )
@@ -14,12 +16,29 @@ import qualified Data.Vector as Vector
1416data Timeseries a = Timeseries {
1517 labels :: SeriesIdentifier ,
1618 dat :: [(Timestamp , a )]
17- }
19+ } deriving (Show , Functor , Foldable , Traversable )
20+
21+ oldest :: Timeseries a -> Maybe (Instant a )
22+ oldest Timeseries {.. } | Prelude. null dat = Nothing
23+ oldest Timeseries {.. } =
24+ let (t, x) = minimumBy (\ (x, _) (y, _) -> compare x y) dat in
25+ Just (Instant labels t x)
26+
27+ newest :: Timeseries a -> Maybe (Instant a )
28+ newest Timeseries {.. } | Prelude. null dat = Nothing
29+ newest Timeseries {.. } =
30+ let (t, x) = maximumBy (\ (x, _) (y, _) -> compare x y) dat in
31+ Just (Instant labels t x)
1832
1933-- | Every two elements in the list must have distinct series identifiers (set of labels),
2034-- | i.e. the series in the list must be distinct.
2135type TimeseriesVector a = [Timeseries a ]
2236
37+ eachOldest :: TimeseriesVector a -> Maybe [Instant a ]
38+ eachOldest = traverse oldest
39+
40+ eachNewest :: TimeseriesVector a -> Maybe [Instant a ]
41+ eachNewest = traverse newest
2342
2443-- | Given a list of range vectors, forms up a timeseries vector.
2544-- | This operation is, in some sense, transposition:
@@ -44,27 +63,27 @@ type TimeseriesVector a = [Timeseries a]
4463-- |
4564-- | ----------------------------------------> t
4665-- t₀ t₁ t₂ t₃
47- transpose :: [InstantVector Double ] -> TimeseriesVector Double
66+ transpose :: [InstantVector a ] -> TimeseriesVector a
4867transpose instants =
4968 Set. foldl' (\ vector ls -> form ls instants : vector) [] (setOfLabels instants) where
5069
5170 -- | Given a set of labels (identifying a series) form up a series from a list of instant vectors.
52- form :: SeriesIdentifier -> [InstantVector Double ] -> Timeseries Double
71+ form :: SeriesIdentifier -> [InstantVector a ] -> Timeseries a
5372 form ls insts = Timeseries ls (form ls insts) where
5473 -- | Extract the data pertaining to the series (identified by the given `SeriesIdentifier`) from the list of
5574 -- | ranges vectors.
56- form :: SeriesIdentifier -> [InstantVector Double ] -> [(Timestamp , Double )]
75+ form :: SeriesIdentifier -> [InstantVector a ] -> [(Timestamp , a )]
5776 form _ [] = []
5877 form ls (inst : insts) =
5978 case find (\ i -> Instant. labels i == ls) inst of
6079 Just i -> (Instant. timestamp i, Instant. value i) : form ls insts
6180 Nothing -> form ls insts
6281
63- setOfLabels' :: InstantVector Double -> Set SeriesIdentifier
82+ setOfLabels' :: InstantVector a -> Set SeriesIdentifier
6483 setOfLabels' [] = Set. empty
6584 setOfLabels' (i : is) = Set. insert (Instant. labels i) (setOfLabels' is)
6685
67- setOfLabels :: [InstantVector Double ] -> Set SeriesIdentifier
86+ setOfLabels :: [InstantVector a ] -> Set SeriesIdentifier
6887 setOfLabels [] = Set. empty
6988 setOfLabels (v : vs) = setOfLabels' v `Set.union` setOfLabels vs
7089
0 commit comments