{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports    #-}

--------------------------------------------------------------------------------

-- Orphan instances module for Cardano tracer.
{-# OPTIONS_GHC -Wno-orphans #-}
-- Extracted from "cardano-node" `Cardano.Node.Tracing.Tracers.P2P`.
-- Branch "master" (2026-02-11, 85869e9dd21d9dac7c4381418346e97259c3303b).

--------------------------------------------------------------------------------

module Ouroboros.Network.Tracing.PeerSelection.PeerStateActions () where

--------------------------------------------------------------------------------

---------
-- base -
---------
import Control.Exception (displayException)
---------------------
-- Package: "aeson" -
---------------------
import "aeson" Data.Aeson (Value (String), toJSON, (.=))
-----------------------
-- Package: "network" -
-----------------------
import "network" Network.Socket (SockAddr)
---------------------------------
-- Package: "ouroboros-network" -
---------------------------------
import "ouroboros-network" Ouroboros.Network.PeerSelection.PeerStateActions
           (PeerSelectionActionsTrace (..))
-- Needed for `instance ToJSON ConnectionId`.
import "ouroboros-network" Ouroboros.Network.OrphanInstances ()
--------------------
-- Package: "text" -
--------------------
import "text" Data.Text (pack)
--------------------------------
-- Package: "trace-dispatcher" -
--------------------------------
import "trace-dispatcher" Cardano.Logging

--------------------------------------------------------------------------------
-- PeerSelectionActions Tracer.
--------------------------------------------------------------------------------

-- TODO: Write PeerStatusChangeType ToJSON at ouroboros-network
-- For that an export is needed at ouroboros-network
instance Show lAddr => LogFormatting (PeerSelectionActionsTrace SockAddr lAddr) where
  forMachine :: DetailLevel -> PeerSelectionActionsTrace SockAddr lAddr -> Object
forMachine DetailLevel
_dtal (PeerStatusChanged PeerStatusChangeType SockAddr
ps) =
    [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"PeerStatusChanged"
             , Key
"peerStatusChangeType" Key -> String -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= PeerStatusChangeType SockAddr -> String
forall a. Show a => a -> String
show PeerStatusChangeType SockAddr
ps
             ]
  forMachine DetailLevel
_dtal (PeerStatusChangeFailure PeerStatusChangeType SockAddr
ps FailureType lAddr
f) =
    [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"PeerStatusChangeFailure"
             , Key
"peerStatusChangeType" Key -> String -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= PeerStatusChangeType SockAddr -> String
forall a. Show a => a -> String
show PeerStatusChangeType SockAddr
ps
             , Key
"reason" Key -> String -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= FailureType lAddr -> String
forall a. Show a => a -> String
show FailureType lAddr
f
             ]
  forMachine DetailLevel
_dtal (PeerMonitoringError ConnectionId SockAddr
connId SomeException
s) =
    [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"PeerMonitoringError"
             , Key
"connectionId" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ConnectionId SockAddr -> Value
forall a. ToJSON a => a -> Value
toJSON ConnectionId SockAddr
connId
             , Key
"reason" Key -> String -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= SomeException -> String
forall a. Show a => a -> String
show SomeException
s
             ]
  forMachine DetailLevel
_dtal (PeerMonitoringResult ConnectionId SockAddr
connId Maybe (WithSomeProtocolTemperature FirstToFinishResult)
wf) =
    [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"PeerMonitoringResult"
             , Key
"connectionId" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ConnectionId SockAddr -> Value
forall a. ToJSON a => a -> Value
toJSON ConnectionId SockAddr
connId
             , Key
"withProtocolTemp" Key -> String -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Maybe (WithSomeProtocolTemperature FirstToFinishResult) -> String
forall a. Show a => a -> String
show Maybe (WithSomeProtocolTemperature FirstToFinishResult)
wf
             ]
  forMachine DetailLevel
_dtal (AcquireConnectionError SomeException
exception) =
    [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"AcquireConnectionError"
            , Key
"error" Key -> String -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= SomeException -> String
forall e. Exception e => e -> String
displayException SomeException
exception
            ]
  forMachine DetailLevel
_dtal (PeerHotDuration ConnectionId SockAddr
connId DiffTime
dt) =
    [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"PeerHotDuration"
            , Key
"connectionId" Key -> Value -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ConnectionId SockAddr -> Value
forall a. ToJSON a => a -> Value
toJSON ConnectionId SockAddr
connId
            , Key
"time" Key -> String -> Object
forall v. ToJSON v => Key -> v -> Object
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= DiffTime -> String
forall a. Show a => a -> String
show DiffTime
dt]
  forHuman :: PeerSelectionActionsTrace SockAddr lAddr -> Text
forHuman = String -> Text
pack (String -> Text)
-> (PeerSelectionActionsTrace SockAddr lAddr -> String)
-> PeerSelectionActionsTrace SockAddr lAddr
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PeerSelectionActionsTrace SockAddr lAddr -> String
forall a. Show a => a -> String
show

instance MetaTrace (PeerSelectionActionsTrace SockAddr lAddr) where
    namespaceFor :: PeerSelectionActionsTrace SockAddr lAddr
-> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
namespaceFor PeerStatusChanged {} = [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"StatusChanged"]
    namespaceFor PeerStatusChangeFailure {} = [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"StatusChangeFailure"]
    namespaceFor PeerMonitoringError {} = [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"MonitoringError"]
    namespaceFor PeerMonitoringResult {} = [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"MonitoringResult"]
    namespaceFor AcquireConnectionError {} = [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"ConnectionError"]
    namespaceFor PeerHotDuration {} = [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"PeerHotDuration"]

    severityFor :: Namespace (PeerSelectionActionsTrace SockAddr lAddr)
-> Maybe (PeerSelectionActionsTrace SockAddr lAddr)
-> Maybe SeverityS
severityFor (Namespace [Text]
_ [Text
"StatusChanged"]) Maybe (PeerSelectionActionsTrace SockAddr lAddr)
_       = SeverityS -> Maybe SeverityS
forall a. a -> Maybe a
Just SeverityS
Info
    severityFor (Namespace [Text]
_ [Text
"StatusChangeFailure"]) Maybe (PeerSelectionActionsTrace SockAddr lAddr)
_ = SeverityS -> Maybe SeverityS
forall a. a -> Maybe a
Just SeverityS
Error
    severityFor (Namespace [Text]
_ [Text
"MonitoringError"]) Maybe (PeerSelectionActionsTrace SockAddr lAddr)
_     = SeverityS -> Maybe SeverityS
forall a. a -> Maybe a
Just SeverityS
Error
    severityFor (Namespace [Text]
_ [Text
"MonitoringResult"]) Maybe (PeerSelectionActionsTrace SockAddr lAddr)
_    = SeverityS -> Maybe SeverityS
forall a. a -> Maybe a
Just SeverityS
Debug
    severityFor (Namespace [Text]
_ [Text
"ConnectionError"]) Maybe (PeerSelectionActionsTrace SockAddr lAddr)
_     = SeverityS -> Maybe SeverityS
forall a. a -> Maybe a
Just SeverityS
Error
    severityFor (Namespace [Text]
_ [Text
"PeerHotDuration"]) Maybe (PeerSelectionActionsTrace SockAddr lAddr)
_     = SeverityS -> Maybe SeverityS
forall a. a -> Maybe a
Just SeverityS
Info
    severityFor Namespace (PeerSelectionActionsTrace SockAddr lAddr)
_ Maybe (PeerSelectionActionsTrace SockAddr lAddr)
_                                     = Maybe SeverityS
forall a. Maybe a
Nothing

    documentFor :: Namespace (PeerSelectionActionsTrace SockAddr lAddr) -> Maybe Text
documentFor (Namespace [Text]
_ [Text
"StatusChanged"]) = Text -> Maybe Text
forall a. a -> Maybe a
Just
      Text
""
    documentFor (Namespace [Text]
_ [Text
"StatusChangeFailure"]) = Text -> Maybe Text
forall a. a -> Maybe a
Just
      Text
""
    documentFor (Namespace [Text]
_ [Text
"MonitoringError"]) = Text -> Maybe Text
forall a. a -> Maybe a
Just
      Text
""
    documentFor (Namespace [Text]
_ [Text
"MonitoringResult"]) = Text -> Maybe Text
forall a. a -> Maybe a
Just
      Text
""
    documentFor (Namespace [Text]
_ [Text
"ConnectionError"]) = Text -> Maybe Text
forall a. a -> Maybe a
Just
      Text
""
    documentFor (Namespace [Text]
_ [Text
"PeerHotDuration"]) = Text -> Maybe Text
forall a. a -> Maybe a
Just
      Text
"Reports how long the outbound connection was in hot state"
    documentFor Namespace (PeerSelectionActionsTrace SockAddr lAddr)
_ = Maybe Text
forall a. Maybe a
Nothing

    allNamespaces :: [Namespace (PeerSelectionActionsTrace SockAddr lAddr)]
allNamespaces = [
        [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"StatusChanged"]
      , [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"StatusChangeFailure"]
      , [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"MonitoringError"]
      , [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"MonitoringResult"]
      , [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"ConnectionError"]
      , [Text]
-> [Text] -> Namespace (PeerSelectionActionsTrace SockAddr lAddr)
forall a. [Text] -> [Text] -> Namespace a
Namespace [] [Text
"PeerHotDuration"]
      ]