{-# LANGUAGE TemplateHaskell #-}

-- | Effect for handling metrics
module Calamity.Metrics.Eff (
  Counter,
  Gauge,
  Histogram,
  HistogramSample (..),
  MetricEff (..),
  registerCounter,
  registerGauge,
  registerHistogram,
  addCounter,
  modifyGauge,
  observeHistogram,
) where

import Calamity.Internal.Utils (DefaultingMap)
import Calamity.Metrics.Internal
import Data.Default.Class
import Data.Map
import Data.Text
import GHC.Generics
import Polysemy
import TextShow
import Calamity.Internal.OverriddenVia

data HistogramSample' = HistogramSample'
  { HistogramSample' -> DefaultingMap Double Double
buckets :: DefaultingMap Double Double
  , HistogramSample' -> Double
sum :: Double
  , HistogramSample' -> Int
count :: Int
  }
  deriving ((forall x. HistogramSample' -> Rep HistogramSample' x)
-> (forall x. Rep HistogramSample' x -> HistogramSample')
-> Generic HistogramSample'
forall x. Rep HistogramSample' x -> HistogramSample'
forall x. HistogramSample' -> Rep HistogramSample' x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep HistogramSample' x -> HistogramSample'
$cfrom :: forall x. HistogramSample' -> Rep HistogramSample' x
Generic)
  deriving (HistogramSample'
HistogramSample' -> Default HistogramSample'
forall a. a -> Default a
def :: HistogramSample'
$cdef :: HistogramSample'
Default)

data HistogramSample = HistogramSample
  { HistogramSample -> Map Double Double
buckets :: Map Double Double
  , HistogramSample -> Double
sum :: Double
  , HistogramSample -> Int
count :: Int
  }
  deriving (HistogramSample -> HistogramSample -> Bool
(HistogramSample -> HistogramSample -> Bool)
-> (HistogramSample -> HistogramSample -> Bool)
-> Eq HistogramSample
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HistogramSample -> HistogramSample -> Bool
$c/= :: HistogramSample -> HistogramSample -> Bool
== :: HistogramSample -> HistogramSample -> Bool
$c== :: HistogramSample -> HistogramSample -> Bool
Eq, Int -> HistogramSample -> ShowS
[HistogramSample] -> ShowS
HistogramSample -> String
(Int -> HistogramSample -> ShowS)
-> (HistogramSample -> String)
-> ([HistogramSample] -> ShowS)
-> Show HistogramSample
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HistogramSample] -> ShowS
$cshowList :: [HistogramSample] -> ShowS
show :: HistogramSample -> String
$cshow :: HistogramSample -> String
showsPrec :: Int -> HistogramSample -> ShowS
$cshowsPrec :: Int -> HistogramSample -> ShowS
Show, (forall x. HistogramSample -> Rep HistogramSample x)
-> (forall x. Rep HistogramSample x -> HistogramSample)
-> Generic HistogramSample
forall x. Rep HistogramSample x -> HistogramSample
forall x. HistogramSample -> Rep HistogramSample x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep HistogramSample x -> HistogramSample
$cfrom :: forall x. HistogramSample -> Rep HistogramSample x
Generic)
  deriving (HistogramSample
HistogramSample -> Default HistogramSample
forall a. a -> Default a
def :: HistogramSample
$cdef :: HistogramSample
Default) via OverriddenVia HistogramSample HistogramSample
  deriving
    (Int -> HistogramSample -> Builder
Int -> HistogramSample -> Text
Int -> HistogramSample -> Text
[HistogramSample] -> Builder
[HistogramSample] -> Text
[HistogramSample] -> Text
HistogramSample -> Builder
HistogramSample -> Text
HistogramSample -> Text
(Int -> HistogramSample -> Builder)
-> (HistogramSample -> Builder)
-> ([HistogramSample] -> Builder)
-> (Int -> HistogramSample -> Text)
-> (HistogramSample -> Text)
-> ([HistogramSample] -> Text)
-> (Int -> HistogramSample -> Text)
-> (HistogramSample -> Text)
-> ([HistogramSample] -> Text)
-> TextShow HistogramSample
forall a.
(Int -> a -> Builder)
-> (a -> Builder)
-> ([a] -> Builder)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> (Int -> a -> Text)
-> (a -> Text)
-> ([a] -> Text)
-> TextShow a
showtlList :: [HistogramSample] -> Text
$cshowtlList :: [HistogramSample] -> Text
showtl :: HistogramSample -> Text
$cshowtl :: HistogramSample -> Text
showtlPrec :: Int -> HistogramSample -> Text
$cshowtlPrec :: Int -> HistogramSample -> Text
showtList :: [HistogramSample] -> Text
$cshowtList :: [HistogramSample] -> Text
showt :: HistogramSample -> Text
$cshowt :: HistogramSample -> Text
showtPrec :: Int -> HistogramSample -> Text
$cshowtPrec :: Int -> HistogramSample -> Text
showbList :: [HistogramSample] -> Builder
$cshowbList :: [HistogramSample] -> Builder
showb :: HistogramSample -> Builder
$cshowb :: HistogramSample -> Builder
showbPrec :: Int -> HistogramSample -> Builder
$cshowbPrec :: Int -> HistogramSample -> Builder
TextShow)
    via FromStringShow HistogramSample

data MetricEff m a where
  -- | Register a 'Counter'
  RegisterCounter ::
    -- | Name
    Text ->
    -- | Labels
    [(Text, Text)] ->
    MetricEff m Counter
  -- | Register a 'Gauge'
  RegisterGauge ::
    -- | Name
    Text ->
    -- | Labels
    [(Text, Text)] ->
    MetricEff m Gauge
  -- | Register a 'Histogram'
  RegisterHistogram ::
    -- | Name
    Text ->
    -- | Labels
    [(Text, Text)] ->
    -- | Upper bounds
    [Double] ->
    MetricEff m Histogram
  AddCounter :: Int -> Counter -> MetricEff m Int
  ModifyGauge :: (Double -> Double) -> Gauge -> MetricEff m Double
  ObserveHistogram :: Double -> Histogram -> MetricEff m HistogramSample

makeSem ''MetricEff