2
2
{-# LANGUAGE OverloadedStrings #-}
3
3
4
4
module Cardano.Tracer.Handlers.RTView.State.TraceObjects
5
- ( Namespace
5
+ ( LogsLiveViewCounters
6
+ , Namespace
6
7
, SavedTraceObjects
7
8
, TraceObjectInfo
9
+ , getLogsLiveViewCounter
10
+ , getTraceObjects
11
+ , incLogsLiveViewCounter
12
+ , initLogsLiveViewCounters
8
13
, initSavedTraceObjects
9
14
, saveTraceObjects
10
15
) where
11
16
12
17
import Control.Concurrent.STM (atomically )
13
- import Control.Concurrent.STM.TVar (TVar , modifyTVar' , newTVarIO )
14
- import Control.Monad (unless )
18
+ import Control.Concurrent.STM.TQueue
19
+ import Control.Concurrent.STM.TVar (TVar , modifyTVar' , newTVarIO , readTVar , readTVarIO )
20
+ import Control.Monad (forM_ , unless )
15
21
import Data.Map.Strict (Map )
16
22
import qualified Data.Map.Strict as M
17
23
import Data.Maybe (mapMaybe )
@@ -25,24 +31,32 @@ import Cardano.Tracer.Types (NodeId)
25
31
type Namespace = Text
26
32
type TraceObjectInfo = (Text , SeverityS , UTCTime )
27
33
28
- -- | We have to store 'TraceObject's received from the node,
29
- -- to be able to update corresponding elements (on the web page)
30
- -- using the values extracted from these 'TraceObject's.
31
- type SavedForNode = Map Namespace TraceObjectInfo
34
+ type SavedForNode = TQueue (Namespace , TraceObjectInfo )
32
35
type SavedTraceObjects = TVar (Map NodeId SavedForNode )
33
36
34
37
initSavedTraceObjects :: IO SavedTraceObjects
35
38
initSavedTraceObjects = newTVarIO M. empty
36
39
37
- saveTraceObjects :: SavedTraceObjects -> NodeId -> [TraceObject ] -> IO ()
40
+ saveTraceObjects
41
+ :: SavedTraceObjects
42
+ -> NodeId
43
+ -> [TraceObject ]
44
+ -> IO ()
38
45
saveTraceObjects savedTraceObjects nodeId traceObjects =
39
- unless (null itemsToSave) $
40
- atomically $ modifyTVar' savedTraceObjects $ \ savedTO ->
41
- case M. lookup nodeId savedTO of
42
- Nothing ->
43
- M. insert nodeId (M. fromList itemsToSave) savedTO
44
- Just savedTOForThisNode ->
45
- M. adjust (const $! savedTOForThisNode `updateSavedBy` itemsToSave) nodeId savedTO
46
+ unless (null itemsToSave) $ atomically $ do
47
+ savedTO' <- readTVar savedTraceObjects
48
+ case M. lookup nodeId savedTO' of
49
+ Nothing -> do
50
+ -- There is no queue for this node yet, so create it, fill it and save it.
51
+ newQ <- newTQueue
52
+ pushItemsToQueue newQ
53
+ modifyTVar' savedTraceObjects $ \ savedTO ->
54
+ case M. lookup nodeId savedTO of
55
+ Nothing -> M. insert nodeId newQ savedTO
56
+ Just _ -> savedTO
57
+ Just qForThisNode ->
58
+ -- There is a queue for this node already, so fill it.
59
+ pushItemsToQueue qForThisNode
46
60
where
47
61
itemsToSave = mapMaybe getTOValue traceObjects
48
62
@@ -56,8 +70,34 @@ saveTraceObjects savedTraceObjects nodeId traceObjects =
56
70
57
71
mkName = intercalate " ."
58
72
59
- -- Update saved 'TraceObject's by new ones: existing value will be replaced.
60
- updateSavedBy = go
61
- where
62
- go saved [] = saved
63
- go saved ((ns, toI): others) = M. insert ns toI saved `go` others
73
+ pushItemsToQueue = forM_ itemsToSave . writeTQueue
74
+
75
+ getTraceObjects
76
+ :: SavedTraceObjects
77
+ -> NodeId
78
+ -> IO [(Namespace , TraceObjectInfo )]
79
+ getTraceObjects savedTraceObjects nodeId = atomically $ do
80
+ qForThisNode <- M. lookup nodeId <$> readTVar savedTraceObjects
81
+ maybe (return [] ) flushTQueue qForThisNode
82
+
83
+ -- | Counters for displayed logs item in "live view window".
84
+ type LogsLiveViewCounters = TVar (Map NodeId Int )
85
+
86
+ initLogsLiveViewCounters :: IO LogsLiveViewCounters
87
+ initLogsLiveViewCounters = newTVarIO M. empty
88
+
89
+ incLogsLiveViewCounter
90
+ :: LogsLiveViewCounters
91
+ -> NodeId
92
+ -> IO ()
93
+ incLogsLiveViewCounter llvCounters nodeId = atomically $
94
+ modifyTVar' llvCounters $ \ currentCounters ->
95
+ case M. lookup nodeId currentCounters of
96
+ Nothing -> M. insert nodeId 1 currentCounters
97
+ Just counterForNode -> M. adjust (const $! counterForNode + 1 ) nodeId currentCounters
98
+
99
+ getLogsLiveViewCounter
100
+ :: LogsLiveViewCounters
101
+ -> NodeId
102
+ -> IO (Maybe Int )
103
+ getLogsLiveViewCounter llvCounters nodeId = M. lookup nodeId <$> readTVarIO llvCounters
0 commit comments