-- | This module provides a service for network connectivity.
module Navi.Services.Network.NetInterfaces
  ( toEvent,
  )
where

import Navi.Data.NaviNote (NaviNote (..))
import Navi.Data.PollInterval (PollInterval (..))
import Navi.Event.Toml qualified as EventToml
import Navi.Event.Types (AnyEvent (..), Event (..))
import Navi.Prelude
import Navi.Services.Network.NetInterfaces.Toml (NetInterfacesToml)
import Navi.Services.Types (ServiceType (..))
import Pythia.Services.NetInterface
  ( Device (..),
    NetInterface (..),
    NetInterfaceState (..),
  )

-- | Transforms toml configuration data into an 'AnyEvent'.
toEvent ::
  (MonadIORef m) =>
  NetInterfacesToml ->
  m AnyEvent
toEvent :: forall (m :: OpticKind -> OpticKind).
MonadIORef m =>
NetInterfacesToml -> m AnyEvent
toEvent NetInterfacesToml
toml = do
  RepeatEvent NetInterface
repeatEvent <- forall (m :: OpticKind -> OpticKind) (a :: OpticKind).
MonadIORef m =>
Maybe RepeatEventToml -> m (RepeatEvent a)
EventToml.mRepeatEventTomlToVal forall (a :: OpticKind) b. (a -> b) -> a -> b
$ NetInterfacesToml
toml forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (a :: OpticKind). IsLabel "repeatEvent" a => a
#repeatEvent
  ErrorNote
errorNote <- forall (m :: OpticKind -> OpticKind).
MonadIORef m =>
Maybe ErrorNoteToml -> m ErrorNote
EventToml.mErrorNoteTomlToVal forall (a :: OpticKind) b. (a -> b) -> a -> b
$ NetInterfacesToml
toml forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (a :: OpticKind). IsLabel "errorNote" a => a
#errorNote
  pure $
    forall (result :: OpticKind).
(Eq result, Show result) =>
Event result -> AnyEvent
MkAnyEvent forall (a :: OpticKind) b. (a -> b) -> a -> b
$
      MkEvent
        { $sel:name:MkEvent :: Text
name = Text
"network-interface",
          $sel:serviceType:MkEvent :: ServiceType NetInterface
serviceType = ServiceType NetInterface
cmd,
          $sel:pollInterval:MkEvent :: PollInterval
pollInterval = PollInterval
pi,
          $sel:raiseAlert:MkEvent :: NetInterface -> Maybe NaviNote
raiseAlert = NetInterfacesToml -> NetInterface -> Maybe NaviNote
toNote NetInterfacesToml
toml,
          $sel:repeatEvent:MkEvent :: RepeatEvent NetInterface
repeatEvent = RepeatEvent NetInterface
repeatEvent,
          $sel:errorNote:MkEvent :: ErrorNote
errorNote = ErrorNote
errorNote
        }
  where
    device :: Device
device = Text -> Device
MkDevice forall (a :: OpticKind) b. (a -> b) -> a -> b
$ NetInterfacesToml
toml forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (a :: OpticKind). IsLabel "deviceName" a => a
#deviceName
    pi :: PollInterval
pi = forall (a :: OpticKind). a -> Maybe a -> a
fromMaybe (Natural -> PollInterval
MkPollInterval Natural
30) (NetInterfacesToml
toml forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (a :: OpticKind). IsLabel "pollInterval" a => a
#pollInterval)
    cmd :: ServiceType NetInterface
cmd =
      Device -> NetInterfaceApp -> ServiceType NetInterface
NetworkInterface Device
device (NetInterfacesToml
toml forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (a :: OpticKind). IsLabel "app" a => a
#app)
{-# INLINEABLE toEvent #-}

toNote :: NetInterfacesToml -> NetInterface -> Maybe NaviNote
toNote :: NetInterfacesToml -> NetInterface -> Maybe NaviNote
toNote NetInterfacesToml
noteToml NetInterface
conn =
  forall (a :: OpticKind). a -> Maybe a
Just forall (a :: OpticKind) b. (a -> b) -> a -> b
$
    MkNaviNote
      { $sel:summary:MkNaviNote :: Text
summary = Text
"Network Connectivity",
        $sel:body:MkNaviNote :: Maybe Text
body = forall (a :: OpticKind). a -> Maybe a
Just Text
body,
        $sel:urgency:MkNaviNote :: Maybe UrgencyLevel
urgency = forall (a :: OpticKind). Maybe a
Nothing,
        $sel:timeout:MkNaviNote :: Maybe Timeout
timeout = NetInterfacesToml
noteToml forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (a :: OpticKind). IsLabel "mTimeout" a => a
#mTimeout
      }
  where
    deviceTxt :: Text
deviceTxt = NetInterface
conn forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. (forall (a :: OpticKind). IsLabel "device" a => a
#device forall (k :: OpticKind) (l :: OpticKind) (m :: OpticKind)
       (is :: IxList) (js :: IxList) (ks :: IxList) (s :: OpticKind)
       (t :: OpticKind) (u :: OpticKind) (v :: OpticKind) (a :: OpticKind)
       (b :: OpticKind).
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% forall (a :: OpticKind). IsLabel "unDevice" a => a
#unDevice)
    nameTxt :: Text
nameTxt = forall (a :: OpticKind). a -> Maybe a -> a
fromMaybe Text
"Unknown" forall (a :: OpticKind) b. (a -> b) -> a -> b
$ NetInterface
conn forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (a :: OpticKind). IsLabel "name" a => a
#name
    body :: Text
body = Text
"Device " forall (a :: OpticKind). Semigroup a => a -> a -> a
<> Text
deviceTxt forall (a :: OpticKind). Semigroup a => a -> a -> a
<> Text
stateTxt
    stateTxt :: Text
stateTxt = case NetInterface
conn forall (k :: OpticKind) (s :: OpticKind) (is :: IxList)
       (a :: OpticKind).
Is k A_Getter =>
s -> Optic' k is s a -> a
^. forall (a :: OpticKind). IsLabel "state" a => a
#state of
      NetInterfaceState
NetStateUp -> Text
" is connected to: " forall (a :: OpticKind). Semigroup a => a -> a -> a
<> Text
nameTxt
      NetInterfaceState
NetStateDown -> Text
" is disconnected from: " forall (a :: OpticKind). Semigroup a => a -> a -> a
<> Text
nameTxt
      NetStateUnknown Text
txt -> Text
" is in an unknown state: " forall (a :: OpticKind). Semigroup a => a -> a -> a
<> Text
txt
{-# INLINEABLE toNote #-}