Skip to content

Commit

Permalink
Add API type for http monitoring local-cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Apr 29, 2024
1 parent 591f363 commit eee9199
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeOperators #-}

module Cardano.Wallet.Launch.Cluster.Monitoring.Http.API
( API
, ApiT (..)
)
where

import Prelude

import Cardano.Wallet.Launch.Cluster.Monitoring.Phase
( History (..)
)
import Control.Monitoring.Tracing
( MonitorState (..)
)
import Data.Aeson
( FromJSON (..)
, ToJSON
, Value (..)
, object
, toJSON
, withObject
, (.:)
, (.=)
)

import Servant
( Post
, PostNoContent
)
import Servant.API
( Get
, JSON
, (:<|>)
, (:>)
)

-- | The API for the monitoring server
type API =
"ready" :> Get '[JSON] Bool
:<|> "step" :> PostNoContent
:<|> "switch" :> Post '[JSON] (ApiT MonitorState)
:<|> "observe" :> Get '[JSON] (ApiT (History, MonitorState))

-- | A newtype wrapper to avoid orphan instances
newtype ApiT a = ApiT {unApiT :: a}
deriving newtype (Eq, Show)

instance ToJSON (ApiT MonitorState) where
toJSON = \case
ApiT Wait -> String "waiting"
ApiT Step -> String "stepping"
ApiT Run -> String "running"

instance FromJSON (ApiT MonitorState) where
parseJSON = \case
String "waiting" -> pure $ ApiT Wait
String "stepping" -> pure $ ApiT Step
String "running" -> pure $ ApiT Run
_ -> fail "Invalid state"

instance ToJSON (ApiT (History, MonitorState)) where
toJSON (ApiT (History{history}, state)) =
object
[ "phases" .= history
, "state" .= ApiT state
]

instance FromJSON (ApiT (History, MonitorState)) where
parseJSON = withObject "ApiT (History, MonitorState)" $ \o -> do
history <- o .: "phases"
ApiT state <- o .: "state"
pure $ ApiT (History{history}, state)
2 changes: 1 addition & 1 deletion lib/local-cluster/lib/Control/Monitoring/Tracing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ data MonitorState
-- ^ Wating for a step before going into `Wait`
| Run
-- ^ Tracing freely
deriving stock Show
deriving stock (Show, Eq)

-- | Tracing state along with its observation
data Tracing state a b = Tracing
Expand Down
10 changes: 8 additions & 2 deletions lib/local-cluster/local-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ library
Cardano.Wallet.Launch.Cluster.KeyRegistration
Cardano.Wallet.Launch.Cluster.Logging
Cardano.Wallet.Launch.Cluster.MonetaryPolicyScript
Cardano.Wallet.Launch.Cluster.Monitoring.Http.API
Cardano.Wallet.Launch.Cluster.Monitoring.Phase
Cardano.Wallet.Launch.Cluster.Node.GenNodeConfig
Cardano.Wallet.Launch.Cluster.Node.GenTopology
Expand Down Expand Up @@ -120,8 +121,10 @@ library
, profunctors
, resourcet
, retry
, servant
, servant-client
, stm
, servant-server
, tagged
, temporary
, temporary-extra
Expand Down Expand Up @@ -158,18 +161,21 @@ test-suite test
, base
, cardano-wallet-primitive
, cardano-wallet-test-utils
, containers
, foldl
, hspec
, local-cluster
, pathtype
, QuickCheck
, temporary
, time
, unliftio
, with-utf8

build-tool-depends: hspec-discover:hspec-discover
other-modules:
Spec
SpecHook
Cardano.Wallet.Launch.Cluster.Monitoring.Http.APISpec
Control.Monitoring.MonitorSpec
Control.Monitoring.TracingSpec
Spec
SpecHook

0 comments on commit eee9199

Please sign in to comment.