From f5c2e0f19a69ae28a98232e13a68b08ede3a010e Mon Sep 17 00:00:00 2001 From: Alexander Bondarenko <486682+dpwiz@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:00:13 +0300 Subject: [PATCH] remove Maybe --- src/Simplex/Messaging/Server.hs | 3 ++- src/Simplex/Messaging/Server/Env/STM.hs | 2 +- src/Simplex/Messaging/Server/Stats.hs | 14 +++++++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Simplex/Messaging/Server.hs b/src/Simplex/Messaging/Server.hs index 84cfd14da..7ec133b91 100644 --- a/src/Simplex/Messaging/Server.hs +++ b/src/Simplex/Messaging/Server.hs @@ -52,6 +52,7 @@ import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import qualified Data.ByteString.Lazy.Char8 as LB import Data.Either (fromRight, partitionEithers) +import Data.Foldable (toList) import Data.Functor (($>)) import Data.Int (Int64) import Data.IntMap.Strict (IntMap) @@ -369,7 +370,7 @@ smpServer started cfg@ServerConfig {transports, transportConfig = tCfg} = do rates <- readTVarIO rates' forM_ (listToMaybe rates) $ \cs -> do ts <- getCurrentTime - let values = concatMap (concatMap $ pure . maybe "0" bshow) cs + let values = concatMap (map bshow . toList) cs withFile statsFilePath AppendMode $ \h -> liftIO $ do hSetBuffering h LineBuffering B.hPut h $ B.intercalate "," (strEncode ts : values) <> "\n" diff --git a/src/Simplex/Messaging/Server/Env/STM.hs b/src/Simplex/Messaging/Server/Env/STM.hs index aba2d6bef..6c7179161 100644 --- a/src/Simplex/Messaging/Server/Env/STM.hs +++ b/src/Simplex/Messaging/Server/Env/STM.hs @@ -137,7 +137,7 @@ data Env = Env clientStats :: TVar (IntMap ClientStats), -- transitive session stats statsClients :: TVar (IntMap ClientStatsId), -- reverse index from sockets sendSignedClients :: TMap RecipientId (TVar ClientStatsId), -- reverse index from queues to their senders - serverRates :: TVar [ClientStatsC (Distribution (Maybe Int))], -- current (head) + historical distributions extracted from clientStats for logging and assessing ClientStatsData deviations + serverRates :: TVar [ClientStatsC (Distribution Int)], -- current (head) + historical distributions extracted from clientStats for logging and assessing ClientStatsData deviations sockets :: SocketState, clientSeq :: TVar ClientId, clients :: TVar (IntMap Client), diff --git a/src/Simplex/Messaging/Server/Stats.hs b/src/Simplex/Messaging/Server/Stats.hs index 336e39408..08c2be841 100644 --- a/src/Simplex/Messaging/Server/Stats.hs +++ b/src/Simplex/Messaging/Server/Stats.hs @@ -129,7 +129,7 @@ getServerStatsData s = do _qDeletedSecured <- readTVar $ qDeletedSecured s _qSub <- readTVar $ qSub s _qSubAuth <- readTVar $ qSubAuth s - _qSubDuplicate <- readTVar $ qSubDuplicate s + _qSubDuplicate <- readTVar $ qSubDuplicate s _qSubProhibited <- readTVar $ qSubProhibited s _msgSent <- readTVar $ msgSent s _msgSentAuth <- readTVar $ msgSentAuth s @@ -159,7 +159,7 @@ setServerStats s d = do writeTVar (qDeletedNew s) $! _qDeletedNew d writeTVar (qDeletedSecured s) $! _qDeletedSecured d writeTVar (qSub s) $! _qSub d - writeTVar (qSubAuth s) $! _qSubAuth d + writeTVar (qSubAuth s) $! _qSubAuth d writeTVar (qSubDuplicate s) $! _qSubDuplicate d writeTVar (qSubProhibited s) $! _qSubProhibited d writeTVar (msgSent s) $! _msgSent d @@ -417,21 +417,21 @@ histogram :: Foldable t => t Int -> Histogram histogram = Histogram . IM.fromListWith (+) . map (,1) . toList {-# INLINE histogram #-} -distribution :: Histogram -> Distribution (Maybe Int) +distribution :: Histogram -> Distribution Int distribution h = Distribution - { minimal = fst <$> listToMaybe cdf', + { minimal = maybe 0 fst $ listToMaybe cdf', bottom50p = bot 0.5, -- std median top50p = top 0.5, top20p = top 0.2, top10p = top 0.1, top5p = top 0.05, top1p = top 0.01, - maximal = fst <$> listToMaybe rcdf' + maximal = maybe 0 fst $ listToMaybe rcdf' } where - bot p = fst <$> find (\(_, p') -> p' >= p) cdf' - top p = fst <$> find (\(_, p') -> p' <= 1 - p) rcdf' + bot p = maybe 0 fst $ find (\(_, p') -> p' >= p) cdf' + top p = maybe 0 fst $ find (\(_, p') -> p' <= 1 - p) rcdf' cdf' = cdf h rcdf' = reverse cdf' -- allow find to work from the smaller end