{-# LANGUAGE OverloadedStrings #-}
module Instana.SDK.Internal.Metrics.Compression
( compressSample
) where
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import qualified Data.List as List
import Data.Text (Text)
import Instana.SDK.Internal.Metrics.Deltas (deltaKeyList)
import Instana.SDK.Internal.Metrics.Sample (InstanaMetricValue (IntegralValue),
InstanaSample)
dummyMapWithKeysEligibleForDeltaComputation :: HashMap Text InstanaMetricValue
dummyMapWithKeysEligibleForDeltaComputation =
List.foldl
(\dummyMap key -> HashMap.insert key dummyValue dummyMap)
HashMap.empty
deltaKeyList
where
dummyValue = IntegralValue 0
compressSample :: InstanaSample -> InstanaSample -> InstanaSample
compressSample previous next =
let
deltaSourcesRemoved =
HashMap.difference
next
dummyMapWithKeysEligibleForDeltaComputation
in
HashMap.differenceWith dropUnchanged deltaSourcesRemoved previous
dropUnchanged ::
InstanaMetricValue
-> InstanaMetricValue
-> Maybe InstanaMetricValue
dropUnchanged nextValue previousValue =
if nextValue == previousValue then
Nothing
else
Just nextValue