1 {-# OPTIONS_GHC -fno-warn-orphans #-}
2 {-# LANGUAGE TypeOperators #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 module Gargantext.API.EKG where
6 import Data.HashMap.Strict as HM
8 import Data.Text.IO as T
9 import Data.Time.Clock.POSIX (getPOSIXTime)
16 import qualified System.Metrics.Json as J
18 -- Mimics https://github.com/tibbe/ekg/blob/master/System/Remote/Snap.hs#L98
22 ( Get '[JSON] J.Sample :<|>
23 CaptureAll "segments" Text :> Get '[JSON] J.Value
28 ekgServer :: FilePath -> Store -> Server EkgAPI
29 ekgServer assetsDir store = (getAll :<|> getOne) :<|> serveDirectoryFileServer assetsDir
31 where getAll = J.Sample <$> liftIO (sampleAll store)
33 let metric = T.intercalate "." segments
34 metrics <- liftIO (sampleAll store)
35 maybe (liftIO (T.putStrLn "not found boohoo") >> throwError err404) (return . J.Value) (HM.lookup metric metrics)
37 newEkgStore :: HasEndpoint api => Proxy api -> IO (Store, Middleware)
41 registerCounter "ekg.server_timestamp_ms" getTimeMs s -- used by UI
42 mid <- monitorEndpoints api s
45 where getTimeMs = (round . (* 1000)) `fmap` getPOSIXTime
47 instance HasEndpoint api => HasEndpoint (Auth xs a :> api) where
48 getEndpoint _ = getEndpoint (Proxy :: Proxy api)
49 enumerateEndpoints _ = enumerateEndpoints (Proxy :: Proxy api)