Skip to content

Commit 7abd485

Browse files
committed
Update
1 parent f2120e7 commit 7abd485

File tree

8 files changed

+461
-178
lines changed

8 files changed

+461
-178
lines changed

timeseries-io/src/Cardano/Timeseries/Domain/Instant.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ data Instant a = Instant {
88
labels :: SeriesIdentifier,
99
timestamp :: Timestamp,
1010
value :: a
11-
} deriving (Show, Eq, Functor)
11+
} deriving (Show, Eq, Functor, Foldable, Traversable)
1212

1313
-- | Do the instant vectors share a series?
14-
share :: Instant a -> Instant a -> Bool
14+
share :: Instant a -> Instant b -> Bool
1515
share a b = labels a == labels b
1616

1717
type InstantVector a = [Instant a]

timeseries-io/src/Cardano/Timeseries/Domain/Timeseries.hs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
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))
57
import qualified Cardano.Timeseries.Domain.Instant as Instant
68
import Cardano.Timeseries.Domain.Types
79

8-
import Data.List (find)
10+
import Data.List (find, maximumBy, minimumBy)
911
import Data.Set
1012
import qualified Data.Set as Set
1113
import Data.Vector (Vector)
@@ -14,12 +16,29 @@ import qualified Data.Vector as Vector
1416
data 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.
2135
type 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
4867
transpose 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

Comments
 (0)