Skip to content

Commit 46b37ac

Browse files
mgmeierRussoul
authored andcommitted
add simple CBOR importer
1 parent 0e047d6 commit 46b37ac

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{-# LANGUAGE DeriveAnyClass #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE LambdaCase #-}
4+
{-# LANGUAGE TypeSynonymInstances #-}
5+
6+
{-# OPTIONS_GHC -Wno-type-defaults -Wno-orphans #-}
7+
8+
module Cardano.Timeseries.Import.PlainCBOR where
9+
10+
import Codec.Serialise
11+
import Control.Applicative
12+
import Data.Map.Strict as Map (Map, size)
13+
import Data.Text (Text, unpack)
14+
import Data.Time.Clock.POSIX (POSIXTime)
15+
import GHC.Generics (Generic)
16+
17+
18+
data NumericValue =
19+
NVInt Int
20+
| NVDouble Double
21+
deriving (Generic, Show, Serialise)
22+
23+
24+
data Snapshot = Snapshot
25+
{ singletonLabel :: Text
26+
, timeStamp :: POSIXTime
27+
, scrape :: Map Text NumericValue
28+
}
29+
deriving (Generic, Serialise)
30+
31+
instance Show Snapshot where
32+
show (Snapshot l t s) = "Snapshot{" ++ unpack l ++ "} @ " ++ show t ++ ", entries: " ++ show (Map.size s)
33+
34+
35+
instance Serialise POSIXTime where
36+
encode = encode . toInteger . floor
37+
decode = fromInteger <$> decode
38+
39+
40+
readFileSnapshots :: FilePath -> IO [Snapshot]
41+
readFileSnapshots = readFileDeserialise
42+
43+
-- can be used with Data.List.sortBy
44+
snapshotOrd :: Snapshot -> Snapshot -> Ordering
45+
snapshotOrd a b =
46+
singletonLabel a `compare` singletonLabel b
47+
<> timeStamp a `compare` timeStamp b

timeseries-io/timeseries-io.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ executable timeseries-io
6969
Cardano.Timeseries.Domain.Interval,
7070
Cardano.Timeseries.Domain.Timeseries,
7171
Cardano.Timeseries.Domain.Types,
72+
Cardano.Timeseries.Import.PlainCBOR,
7273
Cardano.Timeseries.Query,
7374
Cardano.Timeseries.Query.Expr,
7475
Cardano.Timeseries.Query.Parser,
@@ -92,7 +93,9 @@ executable timeseries-io
9293
attoparsec,
9394
vector,
9495
text,
96+
serialise,
9597
statistics,
98+
time,
9699
containers
97100

98101
-- Directories containing source files.

0 commit comments

Comments
 (0)