Skip to content

Commit 1297902

Browse files
committed
cardano-timeseries-io, cardano-tracer: fix bugs and style issues from code review
- Elab.hs: fix copy-paste error in binary arithmetic op elab (rhs hole was unified against lhsTy instead of rhsTy); rename evalBinaryArithmethicOpElabProblem to evalBinaryArithmeticOpElabProblem (typo) - Elab.hs: elaborate `metrics` as List Text (was Text); add Str elab case - Elab/Typing.hs, Resolve.hs, Unify.hs: add List Ty to support metrics type - Interp.hs: guard avg/min/max against empty instant vector; fix rate to error on single-point timeseries instead of dividing by zero - Interp/Value.hs: use showFFloat in Show instance for Scalar to avoid scientific notation in JSON output - TimeseriesServer.hs: fix minimumRetentionMillis units (seconds → ms); remove unused RecordWildCards pragma; align sleep delay with Monitoring.hs - Acceptors/Utils.hs: align new imports with surrounding import block
1 parent ff522a5 commit 1297902

9 files changed

Lines changed: 48 additions & 29 deletions

File tree

bench/cardano-timeseries-io/src/Cardano/Timeseries/Elab.hs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ prettyBinaryArithmeticOpElabProblem (BinaryArithmeticOpElabProblem gam loc _ lhs
125125
<> "\n @ "
126126
<> asText loc
127127

128-
evalBinaryArithmethicOpElabProblem :: Defs -> BinaryArithmeticOpElabProblem -> BinaryArithmeticOpElabProblem
129-
evalBinaryArithmethicOpElabProblem defs (BinaryArithmeticOpElabProblem gam loc lhs lhsTy op rhs rhsTy hole holeTy) =
128+
evalBinaryArithmeticOpElabProblem :: Defs -> BinaryArithmeticOpElabProblem -> BinaryArithmeticOpElabProblem
129+
evalBinaryArithmeticOpElabProblem defs (BinaryArithmeticOpElabProblem gam loc lhs lhsTy op rhs rhsTy hole holeTy) =
130130
BinaryArithmeticOpElabProblem
131131
(resolveContext defs gam)
132132
loc
@@ -178,7 +178,7 @@ instance AsText ElabProblem where
178178
evalElabProblem :: Defs -> ElabProblem -> ElabProblem
179179
evalElabProblem defs (General p) = General (evalGeneralElabProblem defs p)
180180
evalElabProblem defs (BinaryRelation p) = BinaryRelation (evalBinaryRelationElabProblem defs p)
181-
evalElabProblem defs (BinaryArithmeticOp p) = BinaryArithmeticOp (evalBinaryArithmethicOpElabProblem defs p)
181+
evalElabProblem defs (BinaryArithmeticOp p) = BinaryArithmeticOp (evalBinaryArithmeticOpElabProblem defs p)
182182
evalElabProblem defs (ToScalar p) = ToScalar (evalToScalarElabProblem defs p)
183183

184184

@@ -490,8 +490,8 @@ solveNoncanonicalBinaryArithmeticOpElabProblem gam loc lhs lhsTy BinaryArithmeti
490490
, [BinaryArithmeticOp $
491491
BinaryArithmeticOpElabProblem gam loc lhs Timestamp BinaryArithmeticOp.Sub rhs Duration hole Timestamp]
492492
)
493-
solveNoncanonicalBinaryArithmeticOpElabProblem gam loc lhs lhsTy op rhs _ hole Scalar = do
494-
pure $ Just ([UnificationProblem loc lhsTy Scalar, UnificationProblem loc lhsTy Scalar],
493+
solveNoncanonicalBinaryArithmeticOpElabProblem gam loc lhs lhsTy op rhs rhsTy hole Scalar = do
494+
pure $ Just ([UnificationProblem loc lhsTy Scalar, UnificationProblem loc rhsTy Scalar],
495495
[BinaryArithmeticOp $ BinaryArithmeticOpElabProblem gam loc lhs Scalar op rhs Scalar hole Scalar])
496496
solveNoncanonicalBinaryArithmeticOpElabProblem gam loc lhs Scalar op rhs Scalar hole holeTy = do
497497
pure $ Just ([UnificationProblem loc holeTy Scalar],
@@ -576,7 +576,7 @@ solveGeneralElabProblem gam (mbBinaryRelation -> Just (l, a, r, b)) x typ = do
576576
typ
577577
pure ([], [e1, e2, e3])
578578
solveGeneralElabProblem _ (Surface.Metrics l) x typ = do
579-
let u = UnificationProblem l typ Types.Text
579+
let u = UnificationProblem l typ (Types.List Types.Text)
580580
modify (updateDefs $ instantiateExpr x Semantic.Metrics)
581581
pure ([u], [])
582582
solveGeneralElabProblem _ (Surface.Number l f) x typ = do
@@ -921,6 +921,10 @@ solveGeneralElabProblem gam (Surface.ToScalar l t) h hty = do
921921
,
922922
ToScalar $ ToScalarElabProblem gam l (Semantic.Hole th) (Hole tTy) h
923923
])
924+
solveGeneralElabProblem _ (Surface.Str l s) x typ = do
925+
let u = UnificationProblem l typ Types.Text
926+
modify (updateDefs $ instantiateExpr x (Semantic.Str (Text.unpack s)))
927+
pure ([u], [])
924928
solveGeneralElabProblem _ s _ _ = throwError $
925929
"Do not know how to elaborate: " <> showT s
926930

bench/cardano-timeseries-io/src/Cardano/Timeseries/Elab/Resolve.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ resolveTy _ Timestamp = Timestamp
2424
resolveTy _ Duration = Duration
2525
resolveTy _ Bool = Bool
2626
resolveTy _ Text = Text
27+
resolveTy defs (List typ) = List (resolveTy defs typ)
2728

2829
-- | Computes the head-normal form of `Binding` w.r.t. hole resolution
2930
-- (i.e. unfolds holes recursively up to the head expression in type of the binding).

bench/cardano-timeseries-io/src/Cardano/Timeseries/Elab/Typing.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ data Ty = InstantVector Ty
3131
| RangeVector Ty
3232
| Scalar
3333
| Text
34+
| List Ty
3435
| Bool
3536
| Pair Ty Ty
3637
| Unit
@@ -60,6 +61,8 @@ prettyTy _ Bool = "Bool"
6061
prettyTy _ Timestamp = "Timestamp"
6162
prettyTy _ Duration = "Duration"
6263
prettyTy _ Text = "Text"
64+
prettyTy prec (List typ) = conditionalParens (prec == Tight) $
65+
"List " <> prettyTy Tight typ
6366
prettyTy _ (Hole idx) = "?" <> showT idx
6467

6568
-- | A context entry of a typing context.

bench/cardano-timeseries-io/src/Cardano/Timeseries/Elab/Unify.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ occursNu _ Bool = pure False
8080
occursNu _ Timestamp = pure False
8181
occursNu _ Duration = pure False
8282
occursNu _ Text = pure False
83+
occursNu x (List ty) = occursNu x ty
8384
occursNu x (Hole x') = pure (x == x')
8485

8586
unify :: Loc -> Ty -> Ty -> UnifyM [UnificationProblem]

bench/cardano-timeseries-io/src/Cardano/Timeseries/Interp.hs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Data.Maybe (fromMaybe)
4444
import Data.Set (Set, isSubsetOf, member)
4545
import qualified Data.Set as Set
4646
import Data.Text (Text)
47+
import qualified Data.Text as Text
4748
import Data.Word (Word64)
4849

4950
import Statistics.Function (minMax)
@@ -98,14 +99,17 @@ interpMap f = traverse (traverse f)
9899

99100
interpRate :: TimeseriesVector Double -> InterpM (InstantVector Double)
100101
interpRate v = do
101-
min <- liftEither $ maybeToEither (InterpError "Can't compute rate") (eachOldest v)
102-
max <- liftEither $ maybeToEither (InterpError "Can't compute rate") (eachNewest v)
103-
pure $ zipWith compute min max where
102+
mins <- liftEither $ maybeToEither (InterpError "Can't compute rate") (eachOldest v)
103+
maxs <- liftEither $ maybeToEither (InterpError "Can't compute rate") (eachNewest v)
104+
traverse (uncurry compute) (zip mins maxs) where
104105

105-
compute :: Instant Double -> Instant Double -> Instant Double
106-
compute min max =
107-
let x = (max.value - min.value) / fromIntegral (max.timestamp - min.timestamp) in
108-
Instant min.labels max.timestamp x
106+
compute :: Instant Double -> Instant Double -> InterpM (Instant Double)
107+
compute minI maxI
108+
| minI.timestamp == maxI.timestamp =
109+
throwInterpError "rate: single-point timeseries has undefined rate (zero time span)"
110+
| otherwise =
111+
let x = (maxI.value - minI.value) / fromIntegral (maxI.timestamp - minI.timestamp)
112+
in pure $ Instant minI.labels maxI.timestamp x
109113

110114
interpIncrease :: TimeseriesVector Double -> InterpM (InstantVector Double)
111115
interpIncrease v = liftEither $ do
@@ -299,13 +303,19 @@ interp cfg store env (Increase r_) now = do
299303
pure (Value.InstantVector (fmap (fmap Value.Scalar) r'))
300304
interp cfg store env (Avg expr) now = do
301305
v <- interp cfg store env expr now >>= expectInstantVectorScalar
302-
pure $ Value.Scalar $ mean (Instant.toVector v)
306+
if null v
307+
then throwInterpError "avg: empty instant vector"
308+
else pure $ Value.Scalar $ mean (Instant.toVector v)
303309
interp cfg store env (Max expr) now = do
304310
v <- interp cfg store env expr now >>= expectInstantVectorScalar
305-
pure $ Value.Scalar $ snd $ minMax (Instant.toVector v)
311+
if null v
312+
then throwInterpError "max: empty instant vector"
313+
else pure $ Value.Scalar $ snd $ minMax (Instant.toVector v)
306314
interp cfg store env (Min expr) now = do
307315
v <- interp cfg store env expr now >>= expectInstantVectorScalar
308-
pure $ Value.Scalar $ fst $ minMax (Instant.toVector v)
316+
if null v
317+
then throwInterpError "min: empty instant vector"
318+
else pure $ Value.Scalar $ fst $ minMax (Instant.toVector v)
309319
interp cfg store env (AvgOverTime expr) now = do
310320
v <- interp cfg store env expr now >>= expectRangeVectorScalar
311321
pure $ Value.InstantVector (fmap Value.Scalar <$> avgOverTime now v)
@@ -393,4 +403,5 @@ interp cfg store env (Expr.AddDuration a_ b_) now = do
393403
pure (Value.Duration (a + b))
394404
interp cfg store env (mbBinaryRelationInstantVector -> Just (v, rel, k)) now =
395405
interpFilterBinaryRelation cfg store env v rel k now
406+
interp _ _ _ (Expr.Str s) _ = pure (Value.Text (Text.pack s))
396407
interp _ _ _ expr _ = throwInterpError $ "Can't interpret expression: " <> showT expr

bench/cardano-timeseries-io/src/Cardano/Timeseries/Interp/Value.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Control.DeepSeq (NFData)
1212
import Data.Text (unpack, Text)
1313
import Data.Word (Word64)
1414
import GHC.Generics (Generic)
15+
import Numeric (showFFloat)
1516

1617
type FunctionValue = Value -> InterpM Value
1718

@@ -47,7 +48,7 @@ data Value where
4748
instance NFData Value
4849

4950
instance Show Value where
50-
show (Scalar x) = show x
51+
show (Scalar x) = showFFloat Nothing x ""
5152
show (RangeVector x) = unpack (asText x)
5253
show (InstantVector x) = unpack (asText x)
5354
show (Pair x y) = "(" <> show x <> ", " <> show y <> ")"

bench/cardano-timeseries-io/src/Cardano/Timeseries/JSON.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import qualified Data.Map.Strict as Map
1313
import qualified Data.Set as Set
1414
import qualified Data.Vector as V
1515

16-
import Cardano.Timeseries.AsText (showT)
17-
1816
import Data.Text (Text)
17+
import qualified Data.Text as Text
1918

2019
import Cardano.Timeseries.Domain.Types (SeriesIdentifier)
2120
import Cardano.Timeseries.Domain.Instant (Instant (..))
@@ -31,13 +30,13 @@ labelsJson si = A.toJSON (Map.fromList (Set.toList si))
3130
instance (A.ToJSON a, Show a) => A.ToJSON (Instant a) where
3231
toJSON (Instant ls ts val) = A.object
3332
[ "metric" A..= labelsJson ls
34-
, "value" A..= A.Array (V.fromList [A.toJSON (fromIntegral ts / 1000.0 :: Double), A.String (showT val)])
33+
, "value" A..= A.Array (V.fromList [A.toJSON (fromIntegral ts / 1000.0 :: Double), A.String (Text.pack (show val))])
3534
]
3635

3736
instance (A.ToJSON a, Show a) => A.ToJSON (Timeseries a) where
3837
toJSON (Timeseries ls ps) = A.object
3938
[ "metric" A..= labelsJson ls
40-
, "values" A..= map (\(t, v) -> A.Array (V.fromList [A.toJSON (fromIntegral t / 1000.0 :: Double), A.String (showT v)])) ps
39+
, "values" A..= map (\(t, v) -> A.Array (V.fromList [A.toJSON (fromIntegral t / 1000.0 :: Double), A.String (Text.pack (show v))])) ps
4140
]
4241

4342
instance A.ToJSON Value where

cardano-tracer/src/Cardano/Tracer/Acceptors/Utils.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ import System.Metrics.ReqResp
4343
import System.Metrics.Store.Acceptor (MetricsLocalStore, emptyMetricsLocalStore,
4444
storeMetrics)
4545

46-
import Trace.Forward.Utils.DataPoint (DataPointRequestor, initDataPointRequestor)
47-
import qualified Data.Text.Read as Text
48-
import Data.Char (isAsciiLower, isAsciiUpper, isDigit)
49-
import qualified Data.Text as T
50-
import Control.Arrow (first)
46+
import Control.Arrow (first)
47+
import Data.Char (isAsciiLower, isAsciiUpper, isDigit)
48+
import qualified Data.Text as T
49+
import qualified Data.Text.Read as Text
50+
import Trace.Forward.Utils.DataPoint (DataPointRequestor, initDataPointRequestor)
5151

5252
prepareDataPointRequestor
5353
:: Show addr

cardano-tracer/src/Cardano/Tracer/Handlers/Metrics/TimeseriesServer.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{-# LANGUAGE LambdaCase #-}
33
{-# LANGUAGE OverloadedRecordDot #-}
44
{-# LANGUAGE OverloadedStrings #-}
5-
{-# LANGUAGE RecordWildCards #-}
65
{-# LANGUAGE ViewPatterns #-}
76

87
module Cardano.Tracer.Handlers.Metrics.TimeseriesServer(runTimeseriesServer) where
@@ -177,7 +176,7 @@ runTimeseriesServer :: TracerEnv -> Endpoint -> TimeseriesHandle -> IO ()
177176
runTimeseriesServer tracerEnv endpoint handle = do
178177

179178
-- Pause to prevent collision between "Listening"-notifications from servers.
180-
sleep 0.1
179+
sleep 0.2
181180

182181
traceWith tracerEnv.teTracer TracerStartedTimeseries
183182
{ ttTimeseriesEndpoint = endpoint
@@ -192,7 +191,7 @@ runTimeseriesServer tracerEnv endpoint handle = do
192191
tlsSettingsChain certificateFile (fromMaybe [] certificateChain) certificateKeyFile
193192

194193
inputSanCfg = InputSanitationConfig {
195-
minimumRetentionMillis = truncate (fromMaybe 1000 tracerEnv.teConfig.ekgRequestFreq)
194+
minimumRetentionMillis = round (fromMaybe 1.0 tracerEnv.teConfig.ekgRequestFreq * 1000)
196195
, minimumPruningPeriodMillis = 1
197196
}
198197

0 commit comments

Comments
 (0)