{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}

module Cardano.Node.Tracing.Peers
  ( NodePeers (..)
  , traceNodePeers
  ) where

import           Cardano.Prelude
import           Data.Aeson (FromJSON, ToJSON)

import           Cardano.Logging

import           Cardano.Node.Tracing.Tracers.Peer (PeerT, ppPeer)

type PeerInfoPP = Text -- The result of 'ppPeer' function.

-- | This type contains an information about current peers of the node.
--   It will be asked by external applications as a DataPoint.
newtype NodePeers = NodePeers [PeerInfoPP]

deriving instance Generic NodePeers

instance ToJSON NodePeers
instance FromJSON NodePeers

traceNodePeers
  :: Trace IO NodePeers
  -> [PeerT blk]
  -> IO ()
traceNodePeers :: Trace IO NodePeers -> [PeerT blk] -> IO ()
traceNodePeers Trace IO NodePeers
tr [PeerT blk]
ev = Trace IO NodePeers -> NodePeers -> IO ()
forall (m :: * -> *) a. Monad m => Trace m a -> a -> m ()
traceWith Trace IO NodePeers
tr (NodePeers -> IO ()) -> NodePeers -> IO ()
forall a b. (a -> b) -> a -> b
$ [PeerInfoPP] -> NodePeers
NodePeers ((PeerT blk -> PeerInfoPP) -> [PeerT blk] -> [PeerInfoPP]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map PeerT blk -> PeerInfoPP
forall blk. PeerT blk -> PeerInfoPP
ppPeer [PeerT blk]
ev)