Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
viv3kshukla-juspay committed Nov 12, 2023
1 parent 97c25f7 commit 6004757
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions stat-collector-src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Control.Monad.IO.Class (MonadIO(..))
import Data.Function ((&))
import Data.List (foldl', findIndex, sortBy)
import Data.Map (Map)
import Data.Maybe (fromJust)
import Data.Word (Word8)
import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
import Foreign.Storable (Storable, peek)
Expand Down Expand Up @@ -148,15 +149,15 @@ parseInputToEventStream inp =
-- Processing stats
--------------------------------------------------------------------------------

eventKeyOrderFold :: MonadIO m => Fold m Event [EventId]
eventKeyOrderFold =
Fold.lmap getEventId (Fold.foldl' insertKey [])
keyCounterFold :: MonadIO m => Fold m Event [(EventId, Int)]
keyCounterFold =
Fold.lmap getEventId (Fold.foldl' updateCount [])
where
insertKey [] k = k : []
insertKey xxs@(x:xs) k =
if k == x
then xxs
else x : insertKey xs k
updateCount [] k = (k, 1) : []
updateCount (x@(v, c):xs) k =
if k == v
then (v, c + 1) : xs
else x : updateCount xs k

statCollector :: MonadIO m => Int -> Fold m Double StatResult
statCollector winSize =
Expand All @@ -170,9 +171,9 @@ eventCollector winSize =

scanStats
:: MonadIO m
=> Int -> Stream m Event -> Stream m ([EventId], Map EventId StatResult)
=> Int -> Stream m Event -> Stream m ([(EventId, Int)], Map EventId StatResult)
scanStats winSize =
Stream.postscan (Fold.tee eventKeyOrderFold (eventCollector winSize))
Stream.postscan (Fold.tee keyCounterFold (eventCollector winSize))

--------------------------------------------------------------------------------
-- Printing stats
Expand All @@ -196,37 +197,39 @@ printTable rows = do
separatorRow = map (\n -> replicate n '-') maxLengths
fillRow r = zipWith (\n x -> fill n x) maxLengths r

statsToTable :: [EventId] -> Map EventId StatResult -> [[String]]
statsToTable order mp =
["Tag", "Counter", "Mean", "Min", "Max"]
statsToTable :: Counter -> [(EventId, Int)] -> Map EventId StatResult -> [[String]]
statsToTable counter keyTracker mp =
["Tag", "Mean", "Min", "Max", "Count"]
: map
(\((t, c), (me, rg)) ->
(\(evId@(t, _), (me, rg)) ->
[ t
, show c
, showFFloat (Just 2) me ""
, showMaybe (fmap fst rg)
, showMaybe (fmap snd rg)
, show $ fromJust $ lookup evId keyTracker
])
(sortBy sorterFunc (Map.toList mp))
(sortBy sorterFunc
$ filter ((== counter) . snd . fst) (Map.toList mp))

where

showMaybe Nothing = "-"
showMaybe (Just x) = showFFloat (Just 2) x ""

order = map fst keyTracker
sorterFunc (a, _) (b, _) =
case (findIndex (== a) order, findIndex (== b) order) of
(Just i, Just j) -> compare (-i) (-j)
_ -> error "Key not found"

printSlidingStats ::
Int -> Stream IO ([EventId], Map EventId StatResult) -> IO ()
printSlidingStats rowsOnPage strm =
Counter -> Int -> Stream IO ([(EventId, Int)], Map EventId StatResult) -> IO ()
printSlidingStats counter rowsOnPage strm =
Stream.fold
(Fold.drainMapM
(\(order, mp) -> do
ANSI.setCursorPosition 0 0
printTable (take rowsOnPage (statsToTable order mp))
printTable (take rowsOnPage (statsToTable counter order mp))
hFlush stdout
))
strm
Expand All @@ -238,11 +241,13 @@ printSlidingStats rowsOnPage strm =
main :: IO ()
main = do
let rowsOnPage = 50
windowSize = 50
-- XXX This should be different for different windows.
windowSize = 100
counter = CpuTime
ANSI.hideCursor
ANSI.clearScreen
Stream.unfold Handle.chunkReader stdin
& parseInputToEventStream
& scanStats windowSize
& Stream.sampleIntervalEnd 2
& printSlidingStats rowsOnPage
& printSlidingStats counter rowsOnPage

0 comments on commit 6004757

Please sign in to comment.