Skip to content

Commit

Permalink
node name in emails
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Shevchenko committed Jun 21, 2022
1 parent af2ef0f commit 29cb498
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
@@ -1,3 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Cardano.Tracer.Handlers.RTView.Notifications.Send
Expand All @@ -6,28 +7,46 @@ module Cardano.Tracer.Handlers.RTView.Notifications.Send

import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TBQueue (flushTBQueue)
import Control.Monad (unless, void)
import Control.Monad (forM, unless, void)
import Data.List (nub)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Format (defaultTimeLocale, formatTime)

import Cardano.Node.Startup (NodeInfo (..))

import Cardano.Tracer.Handlers.RTView.Notifications.Email
import Cardano.Tracer.Handlers.RTView.Notifications.Settings
import Cardano.Tracer.Handlers.RTView.Notifications.Types
import Cardano.Tracer.Handlers.RTView.Update.Utils
import Cardano.Tracer.Types
import Cardano.Tracer.Utils

makeAndSendNotification :: EventsQueue -> IO ()
makeAndSendNotification eventsQueue = do
makeAndSendNotification
:: DataPointRequestors
-> EventsQueue
-> IO ()
makeAndSendNotification dpRequestors eventsQueue = do
emailSettings <- readSavedEmailSettings
unless (incompleteEmailSettings emailSettings) $
sendNotification emailSettings =<< atomically (flushTBQueue eventsQueue)
unless (incompleteEmailSettings emailSettings) $ do
newEvents <- atomically $ nub <$> flushTBQueue eventsQueue
let nodeIds = nub [nodeId | Event nodeId _ _ _ <- newEvents]
nodeNames <-
forM nodeIds $ \nodeId@(NodeId anId) ->
askDataPoint dpRequestors nodeId "NodeInfo" >>= \case
Nothing -> return anId
Just ni -> return $ niName ni
let nodeIdsWithNames = zip nodeIds nodeNames
sendNotification emailSettings newEvents nodeIdsWithNames

sendNotification
:: EmailSettings
-> [Event]
-> [(NodeId, Text)]
-> IO ()
sendNotification _ [] = return ()
sendNotification emailSettings newEvents =
sendNotification _ [] _ = return ()
sendNotification emailSettings newEvents nodeIdsWithNames =
void $ createAndSendEmail emailSettings body
where
body = preface <> events
Expand All @@ -40,12 +59,13 @@ sendNotification emailSettings newEvents =
]

events = T.intercalate nl
[ "[" <> formatTS ts <> "] [" <> showT nodeId <> "] [" <> showT sev <> "] [" <> showT msg <> "]"
| Event nodeId ts sev msg <- uniqueEvents
[ "[" <> formatTS ts <> "] [" <> getNodeName nodeId <> "] [" <> showT sev <> "] [" <> showT msg <> "]"
| Event nodeId ts sev msg <- newEvents
]

onlyOne = length uniqueEvents == 1

uniqueEvents = nub newEvents
onlyOne = length newEvents == 1

formatTS = T.pack . formatTime defaultTimeLocale "%F %T %Z"

getNodeName nodeId@(NodeId anId) =
fromMaybe anId $ lookup nodeId nodeIdsWithNames
Expand Up @@ -16,20 +16,21 @@ import qualified Data.Map.Strict as M
import Cardano.Tracer.Handlers.RTView.Notifications.Send
import Cardano.Tracer.Handlers.RTView.Notifications.Timer
import Cardano.Tracer.Handlers.RTView.Notifications.Types
import Cardano.Tracer.Types

initEventsQueues :: IO EventsQueues
initEventsQueues = do
initEventsQueues :: DataPointRequestors -> IO EventsQueues
initEventsQueues dpRequestors = do
warnQ <- initEventsQueue
errsQ <- initEventsQueue
critQ <- initEventsQueue
alrtQ <- initEventsQueue
emrgQ <- initEventsQueue

warnT <- mkTimer (makeAndSendNotification warnQ) initCallPeriod
errsT <- mkTimer (makeAndSendNotification errsQ) initCallPeriod
critT <- mkTimer (makeAndSendNotification critQ) initCallPeriod
alrtT <- mkTimer (makeAndSendNotification alrtQ) initCallPeriod
emrgT <- mkTimer (makeAndSendNotification emrgQ) initCallPeriod
warnT <- mkTimer (makeAndSendNotification dpRequestors warnQ) initCallPeriod
errsT <- mkTimer (makeAndSendNotification dpRequestors errsQ) initCallPeriod
critT <- mkTimer (makeAndSendNotification dpRequestors critQ) initCallPeriod
alrtT <- mkTimer (makeAndSendNotification dpRequestors alrtQ) initCallPeriod
emrgT <- mkTimer (makeAndSendNotification dpRequestors emrgQ) initCallPeriod

newTVarIO $ M.fromList
[ (EventWarnings, (warnQ, warnT))
Expand Down
2 changes: 1 addition & 1 deletion cardano-tracer/src/Cardano/Tracer/Handlers/RTView/Run.hs
Expand Up @@ -67,7 +67,7 @@ runRTView TracerConfig{logging, network, hasRTView}
txHistory <- initTransactionsHistory
eraSettings <- initErasSettings
errors <- initErrors
eventsQueues <- initEventsQueues
eventsQueues <- initEventsQueues dpRequestors

void . sequenceConcurrently $
[ UI.startGUI (config host port certFile keyFile) $
Expand Down

0 comments on commit 29cb498

Please sign in to comment.