Skip to content

Commit ec158fb

Browse files
committed
Further work
1 parent 46b37ac commit ec158fb

File tree

21 files changed

+901
-736
lines changed

21 files changed

+901
-736
lines changed

timeseries-io/app/Main.hs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,54 @@
33

44
module Main where
55

6+
import Cardano.Timeseries.Import.PlainCBOR
67
import Cardano.Timeseries.Query (interp)
78
import Cardano.Timeseries.Query.Parser (expr)
9+
import Cardano.Timeseries.Query.Value (Error, Value)
810
import Cardano.Timeseries.Store
911
import Cardano.Timeseries.Store.Flat (Flat, Point (instant, name))
1012
import Cardano.Timeseries.Store.Parser (points)
1113

14+
import Control.Monad (forever)
15+
import Control.Monad.Except (runExceptT)
16+
import Control.Monad.State.Strict (evalState)
1217
import Data.Attoparsec (skipMany)
13-
import Data.Attoparsec.Text (decimal, parseOnly, space)
18+
import Data.Attoparsec.Text (decimal, endOfInput, parseOnly, space)
19+
import Data.Foldable (for_, traverse_)
1420
import Data.Text (pack)
1521
import GHC.List (foldl')
1622
import System.Exit (die)
23+
import System.IO (hFlush, stdout)
24+
25+
snapshotsFile :: String
26+
snapshotsFile = "6nodes_4hours_1mininterval.cbor"
27+
28+
printStore :: Flat Double -> IO ()
29+
printStore = traverse_ print
30+
31+
printQueryResult :: Either Error Value -> IO ()
32+
printQueryResult (Left err) = putStrLn ("Error: " <> err)
33+
printQueryResult (Right ok) = print ok
34+
35+
interactive :: Flat Double -> IO ()
36+
interactive store = forever $ do
37+
putStr "> "
38+
hFlush stdout
39+
queryString <- getLine
40+
case parseOnly (expr <* skipMany space <* endOfInput) (pack queryString) of
41+
Left err -> putStrLn err
42+
Right query -> do
43+
putStrLn ("Expr: " <> show query)
44+
printQueryResult (evalState (runExceptT $ interp store mempty query 0) 0)
1745

1846
main :: IO ()
1947
main = do
48+
content <- readFileSnapshots snapshotsFile
49+
let store = snapshotsToFlatStore content
50+
interactive store
51+
52+
main1 :: IO ()
53+
main1 = do
2054
putStrLn "This is a prototype!"
2155
string <- readFile "dataset.txt"
2256
let result = parseOnly (points decimal <* skipMany space) (pack string)
@@ -26,12 +60,12 @@ main = do
2660
print (dataset :: [Point Integer])
2761
let store = foldl' (\s p -> insert s (name p) (instant p)) (new :: Flat Double) (fmap (fmap fromIntegral) dataset)
2862
string <- readFile "expression.txt"
29-
let result = parseOnly (expr <* skipMany space) (pack string)
63+
let result = parseOnly (expr <* skipMany space <* endOfInput) (pack string)
3064
case result of
3165
Left err -> die err
3266
Right expr -> do
3367
print expr
34-
68+
3569
-- no instance Show Value
3670
-- print $ interp store expr 15
3771

timeseries-io/dataset.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

timeseries-io/elab.txt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// WIP: In case we want some ambiguity in the surface language
2+
3+
fst t
4+
snd t
5+
(t, t)
6+
t == t
7+
t < t
8+
t <= t
9+
t > t
10+
t >= t
11+
t + t
12+
t - t
13+
t * t
14+
t / t
15+
not a
16+
a && b
17+
a || b
18+
\x -> t
19+
minutes n
20+
hours n
21+
epoch
22+
now
23+
rewind t d
24+
fast_forward t d
25+
toScalar t
26+
s[t; t]
27+
s[t; t; t]
28+
v{ls}
29+
max t
30+
avg t
31+
filter t t
32+
t ⊗ t
33+
t t
34+
abs t : Scalar
35+
increase t
36+
rate t
37+
avg_over_time t
38+
sum_over_time t
39+
quantile_over_time t t
40+
unless t t
41+
quantile-by ls t t
42+
43+
Γ |- t ~> t' ∈ (A, B)
44+
------------------------
45+
Γ |- fst t ~> fst t' ∈ A
46+
47+
48+
Γ |- t ~> t' ∈ (A, B)
49+
-------------------
50+
Γ |- snd t ~> snd t' ∈ B
51+
52+
53+
Γ |- a ~> a' ∈ A
54+
Γ |- b ~> b' ∈ B
55+
--------------------------------
56+
Γ |- (a, b) ~> (a', b') ∈ (A, B)
57+
58+
59+
Γ |- a₀ ~> a₀' ∈ A
60+
Γ |- a₁ ~> a₁' ∈ B
61+
Γ |- A = B = Scalar
62+
------------------------------------
63+
Γ |- a₀ == a₁ ~> eq_scalar a₀' a₁' ∈ Scalar
64+
65+
66+
Γ |- a₀ ~> a₀' ∈ A
67+
Γ |- a₁ ~> a₁' ∈ B
68+
Γ |- A = B = Bool
69+
---------------------------------------
70+
Γ |- a₀ == a₁ ~> eq_bool a₀' a₁' ∈ Bool
71+
72+
73+
Γ |- a₀ ~> a₀' ∈ Scalar
74+
Γ |- a₁ ~> a₁' ∈ Scalar
75+
-----------------------------------------
76+
Γ |- a₀ < a₁ ~> lt_scalar a₀' a₁' ∈ Scalar
77+
78+
79+
Γ |- a₀ ~> a₀' ∈ InstantVector Scalar
80+
Γ |- a₁ ~> a₁' ∈ Scalar
81+
-----------------------------------------------------------------------
82+
Γ |- a₀ < a₁ ~> lt_instant_vector_scalar a₀' a₁' ∈ InstantVector Scalar
83+
84+
85+
Γ |- a₀ ~> a₀' ∈ Scalar
86+
Γ |- a₁ ~> a₁' ∈ Scalar
87+
----------------------------------------
88+
Γ |- a₀ + a₁ ~> add_scalar a₀ a₁ ∈ Scalar
89+
90+
91+
Γ |- a₀ ~> a₀' ∈ InstantVector Scalar
92+
Γ |- a₁ ~> a₁' ∈ InstantVector Scalar
93+
----------------------------------------------------------------------
94+
Γ |- a₀ + a₁ ~> add_instant_vector_scalar a₀ a₁ ∈ InstantVector Scalar
95+
96+
97+
Γ |- v ∋ Timestamp -> InstantVector Scalar
98+
Γ |- a ∋ Timestamp
99+
Γ |- b ∋ Timestamp
100+
------------------------------------------
101+
Γ |- v[a, b] ∈ RangeVector Scalar
102+
103+
Γ |- v ∋ Timestamp -> InstantVector Scalar
104+
Γ |- a ∋ Timestamp
105+
Γ |- b ∋ Timestamp
106+
Γ |- d ∋ Duration
107+
------------------------------------------
108+
Γ |- v[a, b, d] ∈ RangeVector Scalar

timeseries-io/expression.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Cardano.Timeseries.Domain.Identifier(Identifier(..)) where
2+
3+
-- | Identifiers come in two sorts: Userspace and Machine-generated.
4+
-- | The first kind comes from user-typed expressions.
5+
-- | The second kind is used for automatic code-generation for hygienic scoping (i.e. to avoid unintentional variable capture)
6+
data Identifier = User !String | Machine !Int deriving (Show, Ord, Eq)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import Cardano.Timeseries.Domain.Types (SeriesIdentifier, Timestamp)
44

55
import Data.Vector
66

7+
-- | One datapoint in a series.
78
data Instant a = Instant {
8-
labels :: SeriesIdentifier,
9-
timestamp :: Timestamp,
10-
value :: a
9+
labels :: !SeriesIdentifier,
10+
timestamp :: !Timestamp,
11+
value :: !a
1112
} deriving (Show, Eq, Functor, Foldable, Traversable)
1213

1314
-- | Do the instant vectors share a series?
1415
share :: Instant a -> Instant b -> Bool
1516
share a b = labels a == labels b
1617

18+
-- | Datapoints from different series. The vector must not contain datapoints sharing a series.
1719
type InstantVector a = [Instant a]
1820

1921
mostRecent :: Instant a -> Instant a -> Instant a

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import Cardano.Timeseries.Domain.Types (Timestamp)
44

55
import Prelude hiding (length)
66

7+
-- | A time interval. Assumption: `start` ≤ `end`
78
data Interval = Interval {
8-
start :: Timestamp,
9-
end :: Timestamp
9+
start :: !Timestamp,
10+
end :: !Timestamp
1011
} deriving (Show, Eq)
1112

1213
length :: Interval -> Double

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import qualified Data.Set as Set
1313
import Data.Vector (Vector)
1414
import qualified Data.Vector as Vector
1515

16+
-- | A collection of datapoints sharing a series.
1617
data Timeseries a = Timeseries {
17-
labels :: SeriesIdentifier,
18-
dat :: [(Timestamp, a)]
18+
labels :: !SeriesIdentifier,
19+
dat :: ![(Timestamp, a)]
1920
} deriving (Show, Functor, Foldable, Traversable)
2021

2122
oldest :: Timeseries a -> Maybe (Instant a)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Data.Word (Word64)
99
type MetricIdentifier = String
1010

1111
type Label = String
12+
13+
-- | Key-value pair of a label and its value.
1214
type Labelled a = (Label, a)
1315

1416
-- | Series is identified by a set of labels. Hence the name.

timeseries-io/src/Cardano/Timeseries/Import/PlainCBOR.hs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
{-# LANGUAGE DeriveAnyClass #-}
22
{-# LANGUAGE DeriveGeneric #-}
3-
{-# LANGUAGE LambdaCase #-}
43
{-# LANGUAGE TypeSynonymInstances #-}
54

65
{-# OPTIONS_GHC -Wno-type-defaults -Wno-orphans #-}
76

87
module Cardano.Timeseries.Import.PlainCBOR where
98

9+
import Cardano.Timeseries.Domain.Instant (Instant (..))
10+
import Cardano.Timeseries.Store.Flat (Flat, Point (..))
11+
1012
import Codec.Serialise
1113
import Control.Applicative
14+
import Control.Monad (join)
1215
import Data.Map.Strict as Map (Map, size)
16+
import qualified Data.Map.Strict as Map
17+
import qualified Data.Set as Set
1318
import Data.Text (Text, unpack)
1419
import Data.Time.Clock.POSIX (POSIXTime)
1520
import GHC.Generics (Generic)
@@ -29,7 +34,7 @@ data Snapshot = Snapshot
2934
deriving (Generic, Serialise)
3035

3136
instance Show Snapshot where
32-
show (Snapshot l t s) = "Snapshot{" ++ unpack l ++ "} @ " ++ show t ++ ", entries: " ++ show (Map.size s)
37+
show (Snapshot l t s) = "Snapshot{" ++ unpack l ++ "} @ " ++ show t ++ ", entries: " ++ show s
3338

3439

3540
instance Serialise POSIXTime where
@@ -40,6 +45,20 @@ instance Serialise POSIXTime where
4045
readFileSnapshots :: FilePath -> IO [Snapshot]
4146
readFileSnapshots = readFileDeserialise
4247

48+
numericValueToDouble :: NumericValue -> Double
49+
numericValueToDouble (NVInt x) = fromIntegral x
50+
numericValueToDouble (NVDouble x) = x
51+
52+
scrapeDatapointToPoint :: Text -> POSIXTime -> Text -> NumericValue -> Point Double
53+
scrapeDatapointToPoint node t metric v =
54+
Point (unpack metric) (Instant (Set.fromList [("node", unpack node)]) (floor (t * 1000)) (numericValueToDouble v))
55+
56+
snapshotToFlatStore :: Snapshot -> Flat Double
57+
snapshotToFlatStore (Snapshot l t s) = Map.foldlWithKey' (\acc k v -> scrapeDatapointToPoint l t k v : acc) [] s
58+
59+
snapshotsToFlatStore :: [Snapshot] -> Flat Double
60+
snapshotsToFlatStore = (>>= snapshotToFlatStore)
61+
4362
-- can be used with Data.List.sortBy
4463
snapshotOrd :: Snapshot -> Snapshot -> Ordering
4564
snapshotOrd a b =

0 commit comments

Comments
 (0)