mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-10-12 02:22:45 +00:00
agent: getAgentSubsTotal api (#1238)
* agent: getAgentSubsTotal api * move * export * count sessions * simplify * simplify 2 --------- Co-authored-by: Evgeny Poberezkin <[email protected]>
This commit is contained in:
co-authored by
Evgeny Poberezkin
parent
051bf38bc7
commit
c605156302
@@ -106,6 +106,7 @@ module Simplex.Messaging.Agent
|
||||
rcConnectHost,
|
||||
rcConnectCtrl,
|
||||
rcDiscoverCtrl,
|
||||
getAgentSubsTotal,
|
||||
getAgentServersSummary,
|
||||
resetAgentServersStats,
|
||||
foregroundAgent,
|
||||
|
||||
@@ -93,6 +93,7 @@ module Simplex.Messaging.Agent.Client
|
||||
AgentServersSummary (..),
|
||||
ServerSessions (..),
|
||||
SMPServerSubs (..),
|
||||
getAgentSubsTotal,
|
||||
getAgentServersSummary,
|
||||
getAgentSubscriptions,
|
||||
slowNetworkConfig,
|
||||
@@ -2017,6 +2018,23 @@ data ServerSessions = ServerSessions
|
||||
}
|
||||
deriving (Show)
|
||||
|
||||
getAgentSubsTotal :: AgentClient -> [UserId] -> IO (SMPServerSubs, Bool)
|
||||
getAgentSubsTotal c userIds = do
|
||||
ssActive <- getSubsCount activeSubs
|
||||
ssPending <- getSubsCount pendingSubs
|
||||
sess <- hasSession . M.toList =<< readTVarIO (smpClients c)
|
||||
pure (SMPServerSubs {ssActive, ssPending}, sess)
|
||||
where
|
||||
getSubsCount subs = M.foldrWithKey' addSub 0 <$> readTVarIO (getRcvQueues $ subs c)
|
||||
addSub (userId, _, _) _ cnt = if userId `elem` userIds then cnt + 1 else cnt
|
||||
hasSession :: [(SMPTransportSession, SMPClientVar)] -> IO Bool
|
||||
hasSession = \case
|
||||
[] -> pure False
|
||||
(s : ss) -> ifM (isConnected s) (pure True) (hasSession ss)
|
||||
isConnected ((userId, _, _), SessionVar {sessionVar})
|
||||
| userId `elem` userIds = atomically $ maybe False isRight <$> tryReadTMVar sessionVar
|
||||
| otherwise = pure False
|
||||
|
||||
getAgentServersSummary :: AgentClient -> IO AgentServersSummary
|
||||
getAgentServersSummary c@AgentClient {smpServersStats, xftpServersStats, ntfServersStats, srvStatsStartedAt, agentEnv} = do
|
||||
sss <- mapM getAgentSMPServerStats =<< readTVarIO smpServersStats
|
||||
|
||||
Reference in New Issue
Block a user