Skip to content

Commit

Permalink
Add local-cluster swagger file
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Apr 30, 2024
1 parent 8f6efd7 commit e90b84b
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ test-output

## Membench uncompressed data
lib/wallet-benchmarks/data/membench-snapshot

## Local cluster swagger golden actual
lib/local-cluster/data/swagger.json.actual
152 changes: 152 additions & 0 deletions lib/local-cluster/data/swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"definitions": {
"History": {
"properties": {
"history": {
"additionalProperties": {
"$ref": "#/definitions/Phase"
},
"type": "object"
}
},
"required": [
"history"
],
"type": "object"
},
"MonitorState": {
"enum": [
"Wait",
"Step",
"Run"
],
"type": "string"
},
"Phase": {
"maxProperties": 1,
"minProperties": 1,
"properties": {
"Cluster": {
"$ref": "#/definitions/RelayNode"
},
"Funding": {
"example": [],
"items": {},
"maxItems": 0,
"type": "array"
},
"Genesis": {
"example": [],
"items": {},
"maxItems": 0,
"type": "array"
},
"Metadata": {
"example": [],
"items": {},
"maxItems": 0,
"type": "array"
},
"Pool0": {
"example": [],
"items": {},
"maxItems": 0,
"type": "array"
},
"Pools": {
"example": [],
"items": {},
"maxItems": 0,
"type": "array"
},
"Relay": {
"example": [],
"items": {},
"maxItems": 0,
"type": "array"
},
"RetrievingFunds": {
"example": [],
"items": {},
"maxItems": 0,
"type": "array"
}
},
"type": "object"
},
"RelayNode": {
"type": "string"
}
},
"info": {
"title": "",
"version": ""
},
"paths": {
"/control/observe": {
"get": {
"produces": [
"application/json;charset=utf-8"
],
"responses": {
"200": {
"description": "",
"schema": {
"items": [
{
"$ref": "#/definitions/History"
},
{
"$ref": "#/definitions/MonitorState"
}
],
"maxItems": 2,
"minItems": 2,
"type": "array"
}
}
}
}
},
"/control/step": {
"post": {
"responses": {
"204": {
"description": ""
}
}
}
},
"/control/switch": {
"post": {
"produces": [
"application/json;charset=utf-8"
],
"responses": {
"200": {
"description": "",
"schema": {
"$ref": "#/definitions/MonitorState"
}
}
}
}
},
"/ready": {
"get": {
"produces": [
"application/json;charset=utf-8"
],
"responses": {
"200": {
"description": "",
"schema": {
"type": "boolean"
}
}
}
}
}
},
"swagger": "2.0"
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Wallet.Launch.Cluster.Monitoring.Http.API
( API
Expand All @@ -16,6 +19,8 @@ import Prelude

import Cardano.Wallet.Launch.Cluster.Monitoring.Phase
( History (..)
, Phase
, RelayNode
)
import Control.Monitoring.Tracing
( MonitorState (..)
Expand All @@ -30,7 +35,21 @@ import Data.Aeson
, (.:)
, (.=)
)

import Data.Swagger
( ToSchema (..)
, defaultSchemaOptions
, genericDeclareNamedSchemaUnrestricted
)
import Data.Swagger.Internal.Schema
( GToSchema
)
import Data.Swagger.Internal.TypeShape
( GenericHasSimpleShape
, GenericShape
)
import GHC.Generics
( Generic (..)
)
import Servant
( Post
, PostNoContent
Expand All @@ -51,8 +70,18 @@ type API =

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

instance
( GenericHasSimpleShape
(ApiT a)
"genericDeclareNamedSchemaUnrestricted"
(GenericShape (Rep a))
, ToSchema a
, Generic a
, GToSchema (Rep a)
)
=> ToSchema (ApiT a)
instance ToJSON (ApiT MonitorState) where
toJSON = \case
ApiT Wait -> String "waiting"
Expand All @@ -69,7 +98,7 @@ instance FromJSON (ApiT MonitorState) where
instance ToJSON (ApiT (History, MonitorState)) where
toJSON (ApiT (History{history}, state)) =
object
[ "phases" .= history
[ "phases" .= history
, "state" .= ApiT state
]

Expand All @@ -78,3 +107,12 @@ instance FromJSON (ApiT (History, MonitorState)) where
history <- o .: "phases"
ApiT state <- o .: "state"
pure $ ApiT (History{history}, state)

instance ToSchema MonitorState

instance ToSchema History

instance ToSchema Phase where
declareNamedSchema = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions

instance ToSchema RelayNode
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ where
import Prelude

import Cardano.Launcher.Node
( CardanoNodeConn
, cardanoNodeConn
( cardanoNodeConn
, nodeSocketFile
)
import Data.Aeson
Expand All @@ -35,17 +34,17 @@ import GHC.Generics
)

-- | A relay node as a reference to it's socket file or pipe
newtype RelayNode = RelayNode CardanoNodeConn
newtype RelayNode = RelayNode FilePath
deriving stock (Eq, Show, Generic)

instance ToJSON RelayNode where
toJSON (RelayNode f) = toJSON $ nodeSocketFile f
toJSON (RelayNode f) = toJSON f

instance FromJSON RelayNode where
parseJSON x = do
f <- parseJSON x
case cardanoNodeConn f of
Right conn -> pure $ RelayNode conn
Right conn -> pure $ RelayNode $ nodeSocketFile conn
Left e -> fail e

-- | The different phases the cluster can be in. We use the convention to report
Expand All @@ -66,4 +65,4 @@ data Phase
newtype History = History
{ history :: Map UTCTime Phase
}
deriving stock (Eq, Show)
deriving stock (Eq, Show, Generic)
6 changes: 5 additions & 1 deletion lib/local-cluster/lib/Control/Monitoring/Tracing.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
Expand Down Expand Up @@ -33,6 +34,9 @@ import Data.Profunctor
( Profunctor (..)
, dimap
)
import GHC.Generics
( Generic
)

-- | The state of the tracing at the type level and single type value level
data MonitorState
Expand All @@ -42,7 +46,7 @@ data MonitorState
-- ^ Wating for a step before going into `Wait`
| Run
-- ^ Tracing freely
deriving stock (Show, Eq)
deriving stock (Show, Eq, Generic)

-- | Tracing state along with its observation
data Tracing state a b = Tracing
Expand Down
10 changes: 10 additions & 0 deletions lib/local-cluster/local-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ library
, servant-client
, stm
, servant-server
, servant-swagger
, swagger2
, tagged
, temporary
, temporary-extra
Expand Down Expand Up @@ -163,17 +165,21 @@ test-suite test

build-depends:
, aeson
, aeson-pretty
, base
, bytestring
, cardano-wallet-primitive
, cardano-wallet-test-utils
, containers
, contra-tracer
, foldl
, hspec
, hspec-golden
, local-cluster
, mtl
, pathtype
, QuickCheck
, servant-swagger
, temporary
, time
, unliftio
Expand All @@ -182,8 +188,12 @@ test-suite test
build-tool-depends: hspec-discover:hspec-discover
other-modules:
Cardano.Wallet.Launch.Cluster.Monitoring.Http.APISpec
Cardano.Wallet.Launch.Cluster.Monitoring.Http.SwaggerSpec
Cardano.Wallet.Launch.Cluster.Monitoring.MonitorSpec
Control.Monitoring.MonitorSpec
Control.Monitoring.TracingSpec
Paths_local_cluster
Spec
SpecHook

data-files: data/swagger.json

0 comments on commit e90b84b

Please sign in to comment.