|
| 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 |
0 commit comments