{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-ambiguous-fields #-}

module Simplex.Chat.Stats where

import qualified Data.Aeson.TH as J
import Data.List (partition)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe, isJust)
import Data.Time.Clock (UTCTime)
import Simplex.Chat.Types
import Simplex.Messaging.Agent.Client
import Simplex.Messaging.Agent.Protocol (UserId)
import Simplex.Messaging.Agent.Stats
import Simplex.Messaging.Parsers (defaultJSON)
import Simplex.Messaging.Protocol

data PresentedServersSummary = PresentedServersSummary
  { PresentedServersSummary -> UTCTime
statsStartedAt :: UTCTime,
    PresentedServersSummary -> SMPServersSummary
allUsersSMP :: SMPServersSummary,
    PresentedServersSummary -> XFTPServersSummary
allUsersXFTP :: XFTPServersSummary,
    PresentedServersSummary -> NtfServersSummary
allUsersNtf :: NtfServersSummary,
    PresentedServersSummary -> SMPServersSummary
currentUserSMP :: SMPServersSummary,
    PresentedServersSummary -> XFTPServersSummary
currentUserXFTP :: XFTPServersSummary,
    PresentedServersSummary -> NtfServersSummary
currentUserNtf :: NtfServersSummary
  }
  deriving (Int -> PresentedServersSummary -> ShowS
[PresentedServersSummary] -> ShowS
PresentedServersSummary -> String
(Int -> PresentedServersSummary -> ShowS)
-> (PresentedServersSummary -> String)
-> ([PresentedServersSummary] -> ShowS)
-> Show PresentedServersSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PresentedServersSummary -> ShowS
showsPrec :: Int -> PresentedServersSummary -> ShowS
$cshow :: PresentedServersSummary -> String
show :: PresentedServersSummary -> String
$cshowList :: [PresentedServersSummary] -> ShowS
showList :: [PresentedServersSummary] -> ShowS
Show)

-- Presentation of servers will be split into separate categories,
-- so users can differentiate currently used (connected) servers,
-- previously connected servers that were in use in previous sessions,
-- and servers that are only proxied (not connected directly).
data SMPServersSummary = SMPServersSummary
  { -- SMP totals are calculated from all accounted SMP server summaries
    SMPServersSummary -> SMPTotals
smpTotals :: SMPTotals,
    -- currently used SMP servers are those with Just in sessions and/or subs in SMPServerSummary;
    -- all other servers would fall either into previously used or only proxied servers category
    SMPServersSummary -> [SMPServerSummary]
currentlyUsedSMPServers :: [SMPServerSummary],
    -- previously used SMP servers are those with Nothing in sessions and subs,
    -- and have any of sentDirect, sentProxied, recvMsgs, etc. > 0 in server stats (see toPresentedServersSummary);
    -- remaining servers would fall into only proxied servers category
    SMPServersSummary -> [SMPServerSummary]
previouslyUsedSMPServers :: [SMPServerSummary],
    -- only proxied SMP servers are those that aren't (according to current state - sessions and subs)
    -- and weren't (according to stats) connected directly; they would have Nothing in sessions and subs,
    -- and have all of sentDirect, sentProxied, recvMsgs, etc. = 0 in server stats
    SMPServersSummary -> [SMPServerSummary]
onlyProxiedSMPServers :: [SMPServerSummary]
  }
  deriving (Int -> SMPServersSummary -> ShowS
[SMPServersSummary] -> ShowS
SMPServersSummary -> String
(Int -> SMPServersSummary -> ShowS)
-> (SMPServersSummary -> String)
-> ([SMPServersSummary] -> ShowS)
-> Show SMPServersSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SMPServersSummary -> ShowS
showsPrec :: Int -> SMPServersSummary -> ShowS
$cshow :: SMPServersSummary -> String
show :: SMPServersSummary -> String
$cshowList :: [SMPServersSummary] -> ShowS
showList :: [SMPServersSummary] -> ShowS
Show)

data SMPTotals = SMPTotals
  { SMPTotals -> ServerSessions
sessions :: ServerSessions,
    SMPTotals -> SMPServerSubs
subs :: SMPServerSubs,
    SMPTotals -> AgentSMPServerStatsData
stats :: AgentSMPServerStatsData
  }
  deriving (Int -> SMPTotals -> ShowS
[SMPTotals] -> ShowS
SMPTotals -> String
(Int -> SMPTotals -> ShowS)
-> (SMPTotals -> String)
-> ([SMPTotals] -> ShowS)
-> Show SMPTotals
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SMPTotals -> ShowS
showsPrec :: Int -> SMPTotals -> ShowS
$cshow :: SMPTotals -> String
show :: SMPTotals -> String
$cshowList :: [SMPTotals] -> ShowS
showList :: [SMPTotals] -> ShowS
Show)

data SMPServerSummary = SMPServerSummary
  { SMPServerSummary -> SMPServer
smpServer :: SMPServer,
    -- known:
    -- for simplicity always Nothing in totalServersSummary - allows us to load configured servers only for current user,
    -- and also unnecessary unless we want to add navigation to other users servers settings;
    -- always Just in currentUserServers - True if server is in list of user servers, otherwise False;
    -- True - allows to navigate to server settings, False - allows to add server to configured as known (SEKnown)
    SMPServerSummary -> Maybe Bool
known :: Maybe Bool,
    SMPServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions,
    SMPServerSummary -> Maybe SMPServerSubs
subs :: Maybe SMPServerSubs,
    -- stats:
    -- even if sessions and subs are Nothing, stats can be Just - server could be used earlier in session,
    -- or in previous sessions and stats for it were restored; server would fall into a category of
    -- previously used or only proxied servers - see ServersSummary above
    SMPServerSummary -> Maybe AgentSMPServerStatsData
stats :: Maybe AgentSMPServerStatsData
  }
  deriving (Int -> SMPServerSummary -> ShowS
[SMPServerSummary] -> ShowS
SMPServerSummary -> String
(Int -> SMPServerSummary -> ShowS)
-> (SMPServerSummary -> String)
-> ([SMPServerSummary] -> ShowS)
-> Show SMPServerSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SMPServerSummary -> ShowS
showsPrec :: Int -> SMPServerSummary -> ShowS
$cshow :: SMPServerSummary -> String
show :: SMPServerSummary -> String
$cshowList :: [SMPServerSummary] -> ShowS
showList :: [SMPServerSummary] -> ShowS
Show)

data XFTPServersSummary = XFTPServersSummary
  { -- XFTP totals are calculated from all accounted XFTP server summaries
    XFTPServersSummary -> XFTPTotals
xftpTotals :: XFTPTotals,
    -- currently used XFTP servers are those with Just in sessions in XFTPServerSummary,
    -- and/or have upload/download/deletion in progress;
    -- all other servers would fall into previously used servers category
    XFTPServersSummary -> [XFTPServerSummary]
currentlyUsedXFTPServers :: [XFTPServerSummary],
    -- previously used XFTP servers are those with Nothing in sessions and don't have any process in progress
    XFTPServersSummary -> [XFTPServerSummary]
previouslyUsedXFTPServers :: [XFTPServerSummary]
  }
  deriving (Int -> XFTPServersSummary -> ShowS
[XFTPServersSummary] -> ShowS
XFTPServersSummary -> String
(Int -> XFTPServersSummary -> ShowS)
-> (XFTPServersSummary -> String)
-> ([XFTPServersSummary] -> ShowS)
-> Show XFTPServersSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> XFTPServersSummary -> ShowS
showsPrec :: Int -> XFTPServersSummary -> ShowS
$cshow :: XFTPServersSummary -> String
show :: XFTPServersSummary -> String
$cshowList :: [XFTPServersSummary] -> ShowS
showList :: [XFTPServersSummary] -> ShowS
Show)

data XFTPTotals = XFTPTotals
  { XFTPTotals -> ServerSessions
sessions :: ServerSessions,
    XFTPTotals -> AgentXFTPServerStatsData
stats :: AgentXFTPServerStatsData
  }
  deriving (Int -> XFTPTotals -> ShowS
[XFTPTotals] -> ShowS
XFTPTotals -> String
(Int -> XFTPTotals -> ShowS)
-> (XFTPTotals -> String)
-> ([XFTPTotals] -> ShowS)
-> Show XFTPTotals
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> XFTPTotals -> ShowS
showsPrec :: Int -> XFTPTotals -> ShowS
$cshow :: XFTPTotals -> String
show :: XFTPTotals -> String
$cshowList :: [XFTPTotals] -> ShowS
showList :: [XFTPTotals] -> ShowS
Show)

data XFTPServerSummary = XFTPServerSummary
  { XFTPServerSummary -> XFTPServer
xftpServer :: XFTPServer,
    XFTPServerSummary -> Maybe Bool
known :: Maybe Bool, -- same as for SMPServerSummary
    XFTPServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions,
    XFTPServerSummary -> Maybe AgentXFTPServerStatsData
stats :: Maybe AgentXFTPServerStatsData,
    XFTPServerSummary -> Bool
rcvInProgress :: Bool,
    XFTPServerSummary -> Bool
sndInProgress :: Bool,
    XFTPServerSummary -> Bool
delInProgress :: Bool
  }
  deriving (Int -> XFTPServerSummary -> ShowS
[XFTPServerSummary] -> ShowS
XFTPServerSummary -> String
(Int -> XFTPServerSummary -> ShowS)
-> (XFTPServerSummary -> String)
-> ([XFTPServerSummary] -> ShowS)
-> Show XFTPServerSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> XFTPServerSummary -> ShowS
showsPrec :: Int -> XFTPServerSummary -> ShowS
$cshow :: XFTPServerSummary -> String
show :: XFTPServerSummary -> String
$cshowList :: [XFTPServerSummary] -> ShowS
showList :: [XFTPServerSummary] -> ShowS
Show)

data NtfServersSummary = NtfServersSummary
  { NtfServersSummary -> NtfTotals
ntfTotals :: NtfTotals,
    NtfServersSummary -> [NtfServerSummary]
currentlyUsedNtfServers :: [NtfServerSummary],
    NtfServersSummary -> [NtfServerSummary]
previouslyUsedNtfServers :: [NtfServerSummary]
  }
  deriving (Int -> NtfServersSummary -> ShowS
[NtfServersSummary] -> ShowS
NtfServersSummary -> String
(Int -> NtfServersSummary -> ShowS)
-> (NtfServersSummary -> String)
-> ([NtfServersSummary] -> ShowS)
-> Show NtfServersSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NtfServersSummary -> ShowS
showsPrec :: Int -> NtfServersSummary -> ShowS
$cshow :: NtfServersSummary -> String
show :: NtfServersSummary -> String
$cshowList :: [NtfServersSummary] -> ShowS
showList :: [NtfServersSummary] -> ShowS
Show)

data NtfTotals = NtfTotals
  { NtfTotals -> ServerSessions
sessions :: ServerSessions,
    NtfTotals -> AgentNtfServerStatsData
stats :: AgentNtfServerStatsData
  }
  deriving (Int -> NtfTotals -> ShowS
[NtfTotals] -> ShowS
NtfTotals -> String
(Int -> NtfTotals -> ShowS)
-> (NtfTotals -> String)
-> ([NtfTotals] -> ShowS)
-> Show NtfTotals
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NtfTotals -> ShowS
showsPrec :: Int -> NtfTotals -> ShowS
$cshow :: NtfTotals -> String
show :: NtfTotals -> String
$cshowList :: [NtfTotals] -> ShowS
showList :: [NtfTotals] -> ShowS
Show)

data NtfServerSummary = NtfServerSummary
  { NtfServerSummary -> NtfServer
ntfServer :: NtfServer,
    NtfServerSummary -> Maybe Bool
known :: Maybe Bool,
    NtfServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions,
    NtfServerSummary -> Maybe AgentNtfServerStatsData
stats :: Maybe AgentNtfServerStatsData
  }
  deriving (Int -> NtfServerSummary -> ShowS
[NtfServerSummary] -> ShowS
NtfServerSummary -> String
(Int -> NtfServerSummary -> ShowS)
-> (NtfServerSummary -> String)
-> ([NtfServerSummary] -> ShowS)
-> Show NtfServerSummary
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NtfServerSummary -> ShowS
showsPrec :: Int -> NtfServerSummary -> ShowS
$cshow :: NtfServerSummary -> String
show :: NtfServerSummary -> String
$cshowList :: [NtfServerSummary] -> ShowS
showList :: [NtfServerSummary] -> ShowS
Show)

-- Maps AgentServersSummary to PresentedServersSummary:
-- - currentUserServers is for currentUser;
-- - users are passed to exclude hidden users from totalServersSummary;
-- - if currentUser is hidden, it should be accounted in totalServersSummary;
-- - known is set only in user level summaries based on passed userSMPSrvs and userXFTPSrvs
toPresentedServersSummary :: AgentServersSummary -> [User] -> User -> [SMPServer] -> [XFTPServer] -> [NtfServer] -> PresentedServersSummary
toPresentedServersSummary :: AgentServersSummary
-> [User]
-> User
-> [SMPServer]
-> [XFTPServer]
-> [NtfServer]
-> PresentedServersSummary
toPresentedServersSummary AgentServersSummary
agentSummary [User]
users User
currentUser [SMPServer]
userSMPSrvs [XFTPServer]
userXFTPSrvs [NtfServer]
userNtfSrvs = do
  let (Map SMPServer SMPServerSummary
userSMPSrvsSumms, Map SMPServer SMPServerSummary
allSMPSrvsSumms) = (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
accSMPSrvsSummaries
      ([SMPServerSummary]
userSMPCurr, [SMPServerSummary]
userSMPPrev, [SMPServerSummary]
userSMPProx) = Map SMPServer SMPServerSummary
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
smpSummsIntoCategories Map SMPServer SMPServerSummary
userSMPSrvsSumms
      ([SMPServerSummary]
allSMPCurr, [SMPServerSummary]
allSMPPrev, [SMPServerSummary]
allSMPProx) = Map SMPServer SMPServerSummary
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
smpSummsIntoCategories Map SMPServer SMPServerSummary
allSMPSrvsSumms
  let (Map XFTPServer XFTPServerSummary
userXFTPSrvsSumms, Map XFTPServer XFTPServerSummary
allXFTPSrvsSumms) = (Map XFTPServer XFTPServerSummary,
 Map XFTPServer XFTPServerSummary)
accXFTPSrvsSummaries
      ([XFTPServerSummary]
userXFTPCurr, [XFTPServerSummary]
userXFTPPrev) = Map XFTPServer XFTPServerSummary
-> ([XFTPServerSummary], [XFTPServerSummary])
xftpSummsIntoCategories Map XFTPServer XFTPServerSummary
userXFTPSrvsSumms
      ([XFTPServerSummary]
allXFTPCurr, [XFTPServerSummary]
allXFTPPrev) = Map XFTPServer XFTPServerSummary
-> ([XFTPServerSummary], [XFTPServerSummary])
xftpSummsIntoCategories Map XFTPServer XFTPServerSummary
allXFTPSrvsSumms
  let (Map NtfServer NtfServerSummary
userNtfSrvsSumms, Map NtfServer NtfServerSummary
allNtfSrvsSumms) = (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
accNtfSrvsSummaries
      ([NtfServerSummary]
userNtfCurr, [NtfServerSummary]
userNtfPrev) = Map NtfServer NtfServerSummary
-> ([NtfServerSummary], [NtfServerSummary])
ntfSummsIntoCategories Map NtfServer NtfServerSummary
userNtfSrvsSumms
      ([NtfServerSummary]
allNtfCurr, [NtfServerSummary]
allNtfPrev) = Map NtfServer NtfServerSummary
-> ([NtfServerSummary], [NtfServerSummary])
ntfSummsIntoCategories Map NtfServer NtfServerSummary
allNtfSrvsSumms
  PresentedServersSummary
    { UTCTime
statsStartedAt :: UTCTime
statsStartedAt :: UTCTime
statsStartedAt,
      allUsersSMP :: SMPServersSummary
allUsersSMP =
        SMPServersSummary
          { smpTotals :: SMPTotals
smpTotals = Map SMPServer SMPServerSummary -> SMPTotals
accSMPTotals Map SMPServer SMPServerSummary
allSMPSrvsSumms,
            currentlyUsedSMPServers :: [SMPServerSummary]
currentlyUsedSMPServers = [SMPServerSummary]
allSMPCurr,
            previouslyUsedSMPServers :: [SMPServerSummary]
previouslyUsedSMPServers = [SMPServerSummary]
allSMPPrev,
            onlyProxiedSMPServers :: [SMPServerSummary]
onlyProxiedSMPServers = [SMPServerSummary]
allSMPProx
          },
      allUsersXFTP :: XFTPServersSummary
allUsersXFTP =
        XFTPServersSummary
          { xftpTotals :: XFTPTotals
xftpTotals = Map XFTPServer XFTPServerSummary -> XFTPTotals
accXFTPTotals Map XFTPServer XFTPServerSummary
allXFTPSrvsSumms,
            currentlyUsedXFTPServers :: [XFTPServerSummary]
currentlyUsedXFTPServers = [XFTPServerSummary]
allXFTPCurr,
            previouslyUsedXFTPServers :: [XFTPServerSummary]
previouslyUsedXFTPServers = [XFTPServerSummary]
allXFTPPrev
          },
      allUsersNtf :: NtfServersSummary
allUsersNtf =
        NtfServersSummary
          { ntfTotals :: NtfTotals
ntfTotals = Map NtfServer NtfServerSummary -> NtfTotals
accNtfTotals Map NtfServer NtfServerSummary
allNtfSrvsSumms,
            currentlyUsedNtfServers :: [NtfServerSummary]
currentlyUsedNtfServers = [NtfServerSummary]
allNtfCurr,
            previouslyUsedNtfServers :: [NtfServerSummary]
previouslyUsedNtfServers = [NtfServerSummary]
allNtfPrev
          },
      currentUserSMP :: SMPServersSummary
currentUserSMP =
        SMPServersSummary
          { smpTotals :: SMPTotals
smpTotals = Map SMPServer SMPServerSummary -> SMPTotals
accSMPTotals Map SMPServer SMPServerSummary
userSMPSrvsSumms,
            currentlyUsedSMPServers :: [SMPServerSummary]
currentlyUsedSMPServers = [SMPServerSummary]
userSMPCurr,
            previouslyUsedSMPServers :: [SMPServerSummary]
previouslyUsedSMPServers = [SMPServerSummary]
userSMPPrev,
            onlyProxiedSMPServers :: [SMPServerSummary]
onlyProxiedSMPServers = [SMPServerSummary]
userSMPProx
          },
      currentUserXFTP :: XFTPServersSummary
currentUserXFTP =
        XFTPServersSummary
          { xftpTotals :: XFTPTotals
xftpTotals = Map XFTPServer XFTPServerSummary -> XFTPTotals
accXFTPTotals Map XFTPServer XFTPServerSummary
userXFTPSrvsSumms,
            currentlyUsedXFTPServers :: [XFTPServerSummary]
currentlyUsedXFTPServers = [XFTPServerSummary]
userXFTPCurr,
            previouslyUsedXFTPServers :: [XFTPServerSummary]
previouslyUsedXFTPServers = [XFTPServerSummary]
userXFTPPrev
          },
      currentUserNtf :: NtfServersSummary
currentUserNtf =
        NtfServersSummary
          { ntfTotals :: NtfTotals
ntfTotals = Map NtfServer NtfServerSummary -> NtfTotals
accNtfTotals Map NtfServer NtfServerSummary
userNtfSrvsSumms,
            currentlyUsedNtfServers :: [NtfServerSummary]
currentlyUsedNtfServers = [NtfServerSummary]
userNtfCurr,
            previouslyUsedNtfServers :: [NtfServerSummary]
previouslyUsedNtfServers = [NtfServerSummary]
userNtfPrev
          }
    }
  where
    AgentServersSummary {UTCTime
statsStartedAt :: UTCTime
statsStartedAt :: AgentServersSummary -> UTCTime
statsStartedAt, Map (UserId, SMPServer) ServerSessions
smpServersSessions :: Map (UserId, SMPServer) ServerSessions
smpServersSessions :: AgentServersSummary -> Map (UserId, SMPServer) ServerSessions
smpServersSessions, Map (UserId, SMPServer) SMPServerSubs
smpServersSubs :: Map (UserId, SMPServer) SMPServerSubs
smpServersSubs :: AgentServersSummary -> Map (UserId, SMPServer) SMPServerSubs
smpServersSubs, Map (UserId, SMPServer) AgentSMPServerStatsData
smpServersStats :: Map (UserId, SMPServer) AgentSMPServerStatsData
smpServersStats :: AgentServersSummary
-> Map (UserId, SMPServer) AgentSMPServerStatsData
smpServersStats, Map (UserId, XFTPServer) ServerSessions
xftpServersSessions :: Map (UserId, XFTPServer) ServerSessions
xftpServersSessions :: AgentServersSummary -> Map (UserId, XFTPServer) ServerSessions
xftpServersSessions, Map (UserId, XFTPServer) AgentXFTPServerStatsData
xftpServersStats :: Map (UserId, XFTPServer) AgentXFTPServerStatsData
xftpServersStats :: AgentServersSummary
-> Map (UserId, XFTPServer) AgentXFTPServerStatsData
xftpServersStats, [XFTPServer]
xftpRcvInProgress :: [XFTPServer]
xftpRcvInProgress :: AgentServersSummary -> [XFTPServer]
xftpRcvInProgress, [XFTPServer]
xftpSndInProgress :: [XFTPServer]
xftpSndInProgress :: AgentServersSummary -> [XFTPServer]
xftpSndInProgress, [XFTPServer]
xftpDelInProgress :: [XFTPServer]
xftpDelInProgress :: AgentServersSummary -> [XFTPServer]
xftpDelInProgress, Map (UserId, NtfServer) ServerSessions
ntfServersSessions :: Map (UserId, NtfServer) ServerSessions
ntfServersSessions :: AgentServersSummary -> Map (UserId, NtfServer) ServerSessions
ntfServersSessions, Map (UserId, NtfServer) AgentNtfServerStatsData
ntfServersStats :: Map (UserId, NtfServer) AgentNtfServerStatsData
ntfServersStats :: AgentServersSummary
-> Map (UserId, NtfServer) AgentNtfServerStatsData
ntfServersStats} = AgentServersSummary
agentSummary
    countUserInAll :: UserId -> Bool
countUserInAll UserId
auId = AgentUserId -> User -> [User] -> Bool
countUserInAllStats (UserId -> AgentUserId
AgentUserId UserId
auId) User
currentUser [User]
users
    accSMPTotals :: Map SMPServer SMPServerSummary -> SMPTotals
    accSMPTotals :: Map SMPServer SMPServerSummary -> SMPTotals
accSMPTotals = (SMPServerSummary -> SMPTotals -> SMPTotals)
-> SMPTotals -> Map SMPServer SMPServerSummary -> SMPTotals
forall a b k. (a -> b -> b) -> b -> Map k a -> b
M.foldr' SMPServerSummary -> SMPTotals -> SMPTotals
addTotals SMPTotals
initialTotals
      where
        initialTotals :: SMPTotals
initialTotals = SMPTotals {sessions :: ServerSessions
sessions = Int -> Int -> Int -> ServerSessions
ServerSessions Int
0 Int
0 Int
0, subs :: SMPServerSubs
subs = Int -> Int -> SMPServerSubs
SMPServerSubs Int
0 Int
0, stats :: AgentSMPServerStatsData
stats = AgentSMPServerStatsData
newAgentSMPServerStatsData}
        addTotals :: SMPServerSummary -> SMPTotals -> SMPTotals
addTotals SMPServerSummary {Maybe ServerSessions
sessions :: SMPServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions, Maybe SMPServerSubs
subs :: SMPServerSummary -> Maybe SMPServerSubs
subs :: Maybe SMPServerSubs
subs, Maybe AgentSMPServerStatsData
stats :: SMPServerSummary -> Maybe AgentSMPServerStatsData
stats :: Maybe AgentSMPServerStatsData
stats} SMPTotals {sessions :: SMPTotals -> ServerSessions
sessions = ServerSessions
accSess, subs :: SMPTotals -> SMPServerSubs
subs = SMPServerSubs
accSubs, stats :: SMPTotals -> AgentSMPServerStatsData
stats = AgentSMPServerStatsData
accStats} =
          SMPTotals
            { sessions :: ServerSessions
sessions = ServerSessions
-> (ServerSessions -> ServerSessions)
-> Maybe ServerSessions
-> ServerSessions
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ServerSessions
accSess (ServerSessions
accSess ServerSessions -> ServerSessions -> ServerSessions
`addServerSessions`) Maybe ServerSessions
sessions,
              subs :: SMPServerSubs
subs = SMPServerSubs
-> (SMPServerSubs -> SMPServerSubs)
-> Maybe SMPServerSubs
-> SMPServerSubs
forall b a. b -> (a -> b) -> Maybe a -> b
maybe SMPServerSubs
accSubs (SMPServerSubs
accSubs SMPServerSubs -> SMPServerSubs -> SMPServerSubs
`addSMPSubs`) Maybe SMPServerSubs
subs,
              stats :: AgentSMPServerStatsData
stats = AgentSMPServerStatsData
-> (AgentSMPServerStatsData -> AgentSMPServerStatsData)
-> Maybe AgentSMPServerStatsData
-> AgentSMPServerStatsData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AgentSMPServerStatsData
accStats (AgentSMPServerStatsData
accStats AgentSMPServerStatsData
-> AgentSMPServerStatsData -> AgentSMPServerStatsData
`addSMPStatsData`) Maybe AgentSMPServerStatsData
stats
            }
    accXFTPTotals :: Map XFTPServer XFTPServerSummary -> XFTPTotals
    accXFTPTotals :: Map XFTPServer XFTPServerSummary -> XFTPTotals
accXFTPTotals = (XFTPServerSummary -> XFTPTotals -> XFTPTotals)
-> XFTPTotals -> Map XFTPServer XFTPServerSummary -> XFTPTotals
forall a b k. (a -> b -> b) -> b -> Map k a -> b
M.foldr' XFTPServerSummary -> XFTPTotals -> XFTPTotals
addTotals XFTPTotals
initialTotals
      where
        initialTotals :: XFTPTotals
initialTotals = XFTPTotals {sessions :: ServerSessions
sessions = Int -> Int -> Int -> ServerSessions
ServerSessions Int
0 Int
0 Int
0, stats :: AgentXFTPServerStatsData
stats = AgentXFTPServerStatsData
newAgentXFTPServerStatsData}
        addTotals :: XFTPServerSummary -> XFTPTotals -> XFTPTotals
addTotals XFTPServerSummary {Maybe ServerSessions
sessions :: XFTPServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions, Maybe AgentXFTPServerStatsData
stats :: XFTPServerSummary -> Maybe AgentXFTPServerStatsData
stats :: Maybe AgentXFTPServerStatsData
stats} XFTPTotals {sessions :: XFTPTotals -> ServerSessions
sessions = ServerSessions
accSess, stats :: XFTPTotals -> AgentXFTPServerStatsData
stats = AgentXFTPServerStatsData
accStats} =
          XFTPTotals
            { sessions :: ServerSessions
sessions = ServerSessions
-> (ServerSessions -> ServerSessions)
-> Maybe ServerSessions
-> ServerSessions
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ServerSessions
accSess (ServerSessions
accSess ServerSessions -> ServerSessions -> ServerSessions
`addServerSessions`) Maybe ServerSessions
sessions,
              stats :: AgentXFTPServerStatsData
stats = AgentXFTPServerStatsData
-> (AgentXFTPServerStatsData -> AgentXFTPServerStatsData)
-> Maybe AgentXFTPServerStatsData
-> AgentXFTPServerStatsData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AgentXFTPServerStatsData
accStats (AgentXFTPServerStatsData
accStats AgentXFTPServerStatsData
-> AgentXFTPServerStatsData -> AgentXFTPServerStatsData
`addXFTPStatsData`) Maybe AgentXFTPServerStatsData
stats
            }
    accNtfTotals :: Map NtfServer NtfServerSummary -> NtfTotals
    accNtfTotals :: Map NtfServer NtfServerSummary -> NtfTotals
accNtfTotals = (NtfServerSummary -> NtfTotals -> NtfTotals)
-> NtfTotals -> Map NtfServer NtfServerSummary -> NtfTotals
forall a b k. (a -> b -> b) -> b -> Map k a -> b
M.foldr' NtfServerSummary -> NtfTotals -> NtfTotals
addTotals NtfTotals
initialTotals
      where
        initialTotals :: NtfTotals
initialTotals = NtfTotals {sessions :: ServerSessions
sessions = Int -> Int -> Int -> ServerSessions
ServerSessions Int
0 Int
0 Int
0, stats :: AgentNtfServerStatsData
stats = AgentNtfServerStatsData
newAgentNtfServerStatsData}
        addTotals :: NtfServerSummary -> NtfTotals -> NtfTotals
addTotals NtfServerSummary {Maybe ServerSessions
sessions :: NtfServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions, Maybe AgentNtfServerStatsData
stats :: NtfServerSummary -> Maybe AgentNtfServerStatsData
stats :: Maybe AgentNtfServerStatsData
stats} NtfTotals {sessions :: NtfTotals -> ServerSessions
sessions = ServerSessions
accSess, stats :: NtfTotals -> AgentNtfServerStatsData
stats = AgentNtfServerStatsData
accStats} =
          NtfTotals
            { sessions :: ServerSessions
sessions = ServerSessions
-> (ServerSessions -> ServerSessions)
-> Maybe ServerSessions
-> ServerSessions
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ServerSessions
accSess (ServerSessions
accSess ServerSessions -> ServerSessions -> ServerSessions
`addServerSessions`) Maybe ServerSessions
sessions,
              stats :: AgentNtfServerStatsData
stats = AgentNtfServerStatsData
-> (AgentNtfServerStatsData -> AgentNtfServerStatsData)
-> Maybe AgentNtfServerStatsData
-> AgentNtfServerStatsData
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AgentNtfServerStatsData
accStats (AgentNtfServerStatsData
accStats AgentNtfServerStatsData
-> AgentNtfServerStatsData -> AgentNtfServerStatsData
`addNtfStatsData`) Maybe AgentNtfServerStatsData
stats
            }
    smpSummsIntoCategories :: Map SMPServer SMPServerSummary -> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
    smpSummsIntoCategories :: Map SMPServer SMPServerSummary
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
smpSummsIntoCategories = (SMPServerSummary
 -> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
 -> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary]))
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
-> Map SMPServer SMPServerSummary
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
forall a b k. (a -> b -> b) -> b -> Map k a -> b
M.foldr' SMPServerSummary
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
addSummary ([], [], [])
      where
        addSummary :: SMPServerSummary
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
-> ([SMPServerSummary], [SMPServerSummary], [SMPServerSummary])
addSummary SMPServerSummary
srvSumm ([SMPServerSummary]
curr, [SMPServerSummary]
prev, [SMPServerSummary]
prox)
          | SMPServerSummary -> Bool
isCurrentlyUsed SMPServerSummary
srvSumm = (SMPServerSummary
srvSumm SMPServerSummary -> [SMPServerSummary] -> [SMPServerSummary]
forall a. a -> [a] -> [a]
: [SMPServerSummary]
curr, [SMPServerSummary]
prev, [SMPServerSummary]
prox)
          | SMPServerSummary -> Bool
isPreviouslyUsed SMPServerSummary
srvSumm = ([SMPServerSummary]
curr, SMPServerSummary
srvSumm SMPServerSummary -> [SMPServerSummary] -> [SMPServerSummary]
forall a. a -> [a] -> [a]
: [SMPServerSummary]
prev, [SMPServerSummary]
prox)
          | Bool
otherwise = ([SMPServerSummary]
curr, [SMPServerSummary]
prev, SMPServerSummary
srvSumm SMPServerSummary -> [SMPServerSummary] -> [SMPServerSummary]
forall a. a -> [a] -> [a]
: [SMPServerSummary]
prox)
        isCurrentlyUsed :: SMPServerSummary -> Bool
isCurrentlyUsed SMPServerSummary {Maybe ServerSessions
sessions :: SMPServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions, Maybe SMPServerSubs
subs :: SMPServerSummary -> Maybe SMPServerSubs
subs :: Maybe SMPServerSubs
subs} = Maybe ServerSessions -> Bool
forall a. Maybe a -> Bool
isJust Maybe ServerSessions
sessions Bool -> Bool -> Bool
|| Maybe SMPServerSubs -> Bool
forall a. Maybe a -> Bool
isJust Maybe SMPServerSubs
subs
        isPreviouslyUsed :: SMPServerSummary -> Bool
isPreviouslyUsed SMPServerSummary {Maybe AgentSMPServerStatsData
stats :: SMPServerSummary -> Maybe AgentSMPServerStatsData
stats :: Maybe AgentSMPServerStatsData
stats} = case Maybe AgentSMPServerStatsData
stats of
          Maybe AgentSMPServerStatsData
Nothing -> Bool
False
          -- add connCompleted, connDeleted?
          -- check: should connCompleted be counted for proxy? is it?
          Just AgentSMPServerStatsData {Int
_sentDirect :: Int
_sentDirect :: AgentSMPServerStatsData -> Int
_sentDirect, Int
_sentProxied :: Int
_sentProxied :: AgentSMPServerStatsData -> Int
_sentProxied, Int
_sentDirectAttempts :: Int
_sentDirectAttempts :: AgentSMPServerStatsData -> Int
_sentDirectAttempts, Int
_sentProxiedAttempts :: Int
_sentProxiedAttempts :: AgentSMPServerStatsData -> Int
_sentProxiedAttempts, Int
_recvMsgs :: Int
_recvMsgs :: AgentSMPServerStatsData -> Int
_recvMsgs, Int
_connCreated :: Int
_connCreated :: AgentSMPServerStatsData -> Int
_connCreated, Int
_connSecured :: Int
_connSecured :: AgentSMPServerStatsData -> Int
_connSecured, Int
_connSubscribed :: Int
_connSubscribed :: AgentSMPServerStatsData -> Int
_connSubscribed, Int
_connSubAttempts :: Int
_connSubAttempts :: AgentSMPServerStatsData -> Int
_connSubAttempts} ->
            Int
_sentDirect Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
_sentProxied Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
_sentDirectAttempts Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
_sentProxiedAttempts Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
_recvMsgs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
_connCreated Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
_connSecured Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
_connSubscribed Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
|| Int
_connSubAttempts Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
    xftpSummsIntoCategories :: Map XFTPServer XFTPServerSummary -> ([XFTPServerSummary], [XFTPServerSummary])
    xftpSummsIntoCategories :: Map XFTPServer XFTPServerSummary
-> ([XFTPServerSummary], [XFTPServerSummary])
xftpSummsIntoCategories = (XFTPServerSummary -> Bool)
-> [XFTPServerSummary]
-> ([XFTPServerSummary], [XFTPServerSummary])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition XFTPServerSummary -> Bool
isCurrentlyUsed ([XFTPServerSummary] -> ([XFTPServerSummary], [XFTPServerSummary]))
-> (Map XFTPServer XFTPServerSummary -> [XFTPServerSummary])
-> Map XFTPServer XFTPServerSummary
-> ([XFTPServerSummary], [XFTPServerSummary])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map XFTPServer XFTPServerSummary -> [XFTPServerSummary]
forall k a. Map k a -> [a]
M.elems
      where
        isCurrentlyUsed :: XFTPServerSummary -> Bool
isCurrentlyUsed XFTPServerSummary {Maybe ServerSessions
sessions :: XFTPServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions, Bool
rcvInProgress :: XFTPServerSummary -> Bool
rcvInProgress :: Bool
rcvInProgress, Bool
sndInProgress :: XFTPServerSummary -> Bool
sndInProgress :: Bool
sndInProgress, Bool
delInProgress :: XFTPServerSummary -> Bool
delInProgress :: Bool
delInProgress} =
          Maybe ServerSessions -> Bool
forall a. Maybe a -> Bool
isJust Maybe ServerSessions
sessions Bool -> Bool -> Bool
|| Bool
rcvInProgress Bool -> Bool -> Bool
|| Bool
sndInProgress Bool -> Bool -> Bool
|| Bool
delInProgress
    ntfSummsIntoCategories :: Map NtfServer NtfServerSummary -> ([NtfServerSummary], [NtfServerSummary])
    ntfSummsIntoCategories :: Map NtfServer NtfServerSummary
-> ([NtfServerSummary], [NtfServerSummary])
ntfSummsIntoCategories = (NtfServerSummary -> Bool)
-> [NtfServerSummary] -> ([NtfServerSummary], [NtfServerSummary])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition NtfServerSummary -> Bool
isCurrentlyUsed ([NtfServerSummary] -> ([NtfServerSummary], [NtfServerSummary]))
-> (Map NtfServer NtfServerSummary -> [NtfServerSummary])
-> Map NtfServer NtfServerSummary
-> ([NtfServerSummary], [NtfServerSummary])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map NtfServer NtfServerSummary -> [NtfServerSummary]
forall k a. Map k a -> [a]
M.elems
      where
        isCurrentlyUsed :: NtfServerSummary -> Bool
isCurrentlyUsed NtfServerSummary {Maybe ServerSessions
sessions :: NtfServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions} = Maybe ServerSessions -> Bool
forall a. Maybe a -> Bool
isJust Maybe ServerSessions
sessions
    accSMPSrvsSummaries :: (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
    accSMPSrvsSummaries :: (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
accSMPSrvsSummaries = ((UserId, SMPServer)
 -> AgentSMPServerStatsData
 -> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
 -> (Map SMPServer SMPServerSummary,
     Map SMPServer SMPServerSummary))
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> Map (UserId, SMPServer) AgentSMPServerStatsData
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey' ((AgentSMPServerStatsData -> SMPServerSummary -> SMPServerSummary)
-> (UserId, SMPServer)
-> AgentSMPServerStatsData
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
forall {a}.
(a -> SMPServerSummary -> SMPServerSummary)
-> (UserId, SMPServer)
-> a
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
addServerData AgentSMPServerStatsData -> SMPServerSummary -> SMPServerSummary
addStats) (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
summs2 Map (UserId, SMPServer) AgentSMPServerStatsData
smpServersStats
      where
        summs1 :: (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
summs1 = ((UserId, SMPServer)
 -> ServerSessions
 -> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
 -> (Map SMPServer SMPServerSummary,
     Map SMPServer SMPServerSummary))
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> Map (UserId, SMPServer) ServerSessions
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey' ((ServerSessions -> SMPServerSummary -> SMPServerSummary)
-> (UserId, SMPServer)
-> ServerSessions
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
forall {a}.
(a -> SMPServerSummary -> SMPServerSummary)
-> (UserId, SMPServer)
-> a
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
addServerData ServerSessions -> SMPServerSummary -> SMPServerSummary
addSessions) (Map SMPServer SMPServerSummary
forall k a. Map k a
M.empty, Map SMPServer SMPServerSummary
forall k a. Map k a
M.empty) Map (UserId, SMPServer) ServerSessions
smpServersSessions
        summs2 :: (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
summs2 = ((UserId, SMPServer)
 -> SMPServerSubs
 -> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
 -> (Map SMPServer SMPServerSummary,
     Map SMPServer SMPServerSummary))
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> Map (UserId, SMPServer) SMPServerSubs
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey' ((SMPServerSubs -> SMPServerSummary -> SMPServerSummary)
-> (UserId, SMPServer)
-> SMPServerSubs
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
forall {a}.
(a -> SMPServerSummary -> SMPServerSummary)
-> (UserId, SMPServer)
-> a
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
addServerData SMPServerSubs -> SMPServerSummary -> SMPServerSummary
addSubs) (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
summs1 Map (UserId, SMPServer) SMPServerSubs
smpServersSubs
        addServerData :: (a -> SMPServerSummary -> SMPServerSummary)
-> (UserId, SMPServer)
-> a
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
addServerData = (SMPServer -> SMPServerSummary)
-> (SMPServer -> SMPServerSummary)
-> (a -> SMPServerSummary -> SMPServerSummary)
-> (UserId, SMPServer)
-> a
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
-> (Map SMPServer SMPServerSummary, Map SMPServer SMPServerSummary)
forall (p :: ProtocolType) s a.
(ProtocolServer p -> s)
-> (ProtocolServer p -> s)
-> (a -> s -> s)
-> (UserId, ProtocolServer p)
-> a
-> (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
-> (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
addServerData_ SMPServer -> SMPServerSummary
newSummary SMPServer -> SMPServerSummary
newUserSummary
        newUserSummary :: SMPServer -> SMPServerSummary
newUserSummary SMPServer
srv = (SMPServer -> SMPServerSummary
newSummary SMPServer
srv :: SMPServerSummary) {known = Just $ srv `elem` userSMPSrvs}
        newSummary :: SMPServer -> SMPServerSummary
newSummary SMPServer
srv =
          SMPServerSummary
            { smpServer :: SMPServer
smpServer = SMPServer
srv,
              known :: Maybe Bool
known = Maybe Bool
forall a. Maybe a
Nothing,
              sessions :: Maybe ServerSessions
sessions = Maybe ServerSessions
forall a. Maybe a
Nothing,
              subs :: Maybe SMPServerSubs
subs = Maybe SMPServerSubs
forall a. Maybe a
Nothing,
              stats :: Maybe AgentSMPServerStatsData
stats = Maybe AgentSMPServerStatsData
forall a. Maybe a
Nothing
            }
        addSessions :: ServerSessions -> SMPServerSummary -> SMPServerSummary
        addSessions :: ServerSessions -> SMPServerSummary -> SMPServerSummary
addSessions ServerSessions
s summ :: SMPServerSummary
summ@SMPServerSummary {Maybe ServerSessions
sessions :: SMPServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions} = SMPServerSummary
summ {sessions = Just $ maybe s (s `addServerSessions`) sessions}
        addSubs :: SMPServerSubs -> SMPServerSummary -> SMPServerSummary
        addSubs :: SMPServerSubs -> SMPServerSummary -> SMPServerSummary
addSubs SMPServerSubs
s summ :: SMPServerSummary
summ@SMPServerSummary {Maybe SMPServerSubs
subs :: SMPServerSummary -> Maybe SMPServerSubs
subs :: Maybe SMPServerSubs
subs} = SMPServerSummary
summ {subs = Just $ maybe s (s `addSMPSubs`) subs}
        addStats :: AgentSMPServerStatsData -> SMPServerSummary -> SMPServerSummary
        addStats :: AgentSMPServerStatsData -> SMPServerSummary -> SMPServerSummary
addStats AgentSMPServerStatsData
s summ :: SMPServerSummary
summ@SMPServerSummary {Maybe AgentSMPServerStatsData
stats :: SMPServerSummary -> Maybe AgentSMPServerStatsData
stats :: Maybe AgentSMPServerStatsData
stats} = SMPServerSummary
summ {stats = Just $ maybe s (s `addSMPStatsData`) stats}
    accXFTPSrvsSummaries :: (Map XFTPServer XFTPServerSummary, Map XFTPServer XFTPServerSummary)
    accXFTPSrvsSummaries :: (Map XFTPServer XFTPServerSummary,
 Map XFTPServer XFTPServerSummary)
accXFTPSrvsSummaries = ((UserId, XFTPServer)
 -> AgentXFTPServerStatsData
 -> (Map XFTPServer XFTPServerSummary,
     Map XFTPServer XFTPServerSummary)
 -> (Map XFTPServer XFTPServerSummary,
     Map XFTPServer XFTPServerSummary))
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
-> Map (UserId, XFTPServer) AgentXFTPServerStatsData
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey' ((AgentXFTPServerStatsData
 -> XFTPServerSummary -> XFTPServerSummary)
-> (UserId, XFTPServer)
-> AgentXFTPServerStatsData
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
forall {a}.
(a -> XFTPServerSummary -> XFTPServerSummary)
-> (UserId, XFTPServer)
-> a
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
addServerData AgentXFTPServerStatsData -> XFTPServerSummary -> XFTPServerSummary
addStats) (Map XFTPServer XFTPServerSummary,
 Map XFTPServer XFTPServerSummary)
summs1 Map (UserId, XFTPServer) AgentXFTPServerStatsData
xftpServersStats
      where
        summs1 :: (Map XFTPServer XFTPServerSummary,
 Map XFTPServer XFTPServerSummary)
summs1 = ((UserId, XFTPServer)
 -> ServerSessions
 -> (Map XFTPServer XFTPServerSummary,
     Map XFTPServer XFTPServerSummary)
 -> (Map XFTPServer XFTPServerSummary,
     Map XFTPServer XFTPServerSummary))
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
-> Map (UserId, XFTPServer) ServerSessions
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey' ((ServerSessions -> XFTPServerSummary -> XFTPServerSummary)
-> (UserId, XFTPServer)
-> ServerSessions
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
forall {a}.
(a -> XFTPServerSummary -> XFTPServerSummary)
-> (UserId, XFTPServer)
-> a
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
addServerData ServerSessions -> XFTPServerSummary -> XFTPServerSummary
addSessions) (Map XFTPServer XFTPServerSummary
forall k a. Map k a
M.empty, Map XFTPServer XFTPServerSummary
forall k a. Map k a
M.empty) Map (UserId, XFTPServer) ServerSessions
xftpServersSessions
        addServerData :: (a -> XFTPServerSummary -> XFTPServerSummary)
-> (UserId, XFTPServer)
-> a
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
addServerData = (XFTPServer -> XFTPServerSummary)
-> (XFTPServer -> XFTPServerSummary)
-> (a -> XFTPServerSummary -> XFTPServerSummary)
-> (UserId, XFTPServer)
-> a
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
-> (Map XFTPServer XFTPServerSummary,
    Map XFTPServer XFTPServerSummary)
forall (p :: ProtocolType) s a.
(ProtocolServer p -> s)
-> (ProtocolServer p -> s)
-> (a -> s -> s)
-> (UserId, ProtocolServer p)
-> a
-> (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
-> (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
addServerData_ XFTPServer -> XFTPServerSummary
newSummary XFTPServer -> XFTPServerSummary
newUserSummary
        addSessions :: ServerSessions -> XFTPServerSummary -> XFTPServerSummary
        addSessions :: ServerSessions -> XFTPServerSummary -> XFTPServerSummary
addSessions ServerSessions
s summ :: XFTPServerSummary
summ@XFTPServerSummary {Maybe ServerSessions
sessions :: XFTPServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions} = XFTPServerSummary
summ {sessions = Just $ maybe s (s `addServerSessions`) sessions}
        addStats :: AgentXFTPServerStatsData -> XFTPServerSummary -> XFTPServerSummary
        addStats :: AgentXFTPServerStatsData -> XFTPServerSummary -> XFTPServerSummary
addStats AgentXFTPServerStatsData
s summ :: XFTPServerSummary
summ@XFTPServerSummary {Maybe AgentXFTPServerStatsData
stats :: XFTPServerSummary -> Maybe AgentXFTPServerStatsData
stats :: Maybe AgentXFTPServerStatsData
stats} = XFTPServerSummary
summ {stats = Just $ maybe s (s `addXFTPStatsData`) stats}
        newUserSummary :: XFTPServer -> XFTPServerSummary
newUserSummary XFTPServer
srv = (XFTPServer -> XFTPServerSummary
newSummary XFTPServer
srv :: XFTPServerSummary) {known = Just $ srv `elem` userXFTPSrvs}
        newSummary :: XFTPServer -> XFTPServerSummary
newSummary XFTPServer
srv =
          XFTPServerSummary
            { xftpServer :: XFTPServer
xftpServer = XFTPServer
srv,
              known :: Maybe Bool
known = Maybe Bool
forall a. Maybe a
Nothing,
              sessions :: Maybe ServerSessions
sessions = Maybe ServerSessions
forall a. Maybe a
Nothing,
              stats :: Maybe AgentXFTPServerStatsData
stats = Maybe AgentXFTPServerStatsData
forall a. Maybe a
Nothing,
              rcvInProgress :: Bool
rcvInProgress = XFTPServer
srv XFTPServer -> [XFTPServer] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [XFTPServer]
xftpRcvInProgress,
              sndInProgress :: Bool
sndInProgress = XFTPServer
srv XFTPServer -> [XFTPServer] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [XFTPServer]
xftpSndInProgress,
              delInProgress :: Bool
delInProgress = XFTPServer
srv XFTPServer -> [XFTPServer] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [XFTPServer]
xftpDelInProgress
            }
    accNtfSrvsSummaries :: (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
    accNtfSrvsSummaries :: (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
accNtfSrvsSummaries = ((UserId, NtfServer)
 -> AgentNtfServerStatsData
 -> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
 -> (Map NtfServer NtfServerSummary,
     Map NtfServer NtfServerSummary))
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
-> Map (UserId, NtfServer) AgentNtfServerStatsData
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey' ((AgentNtfServerStatsData -> NtfServerSummary -> NtfServerSummary)
-> (UserId, NtfServer)
-> AgentNtfServerStatsData
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
forall {a}.
(a -> NtfServerSummary -> NtfServerSummary)
-> (UserId, NtfServer)
-> a
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
addServerData AgentNtfServerStatsData -> NtfServerSummary -> NtfServerSummary
addStats) (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
summs1 Map (UserId, NtfServer) AgentNtfServerStatsData
ntfServersStats
      where
        summs1 :: (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
summs1 = ((UserId, NtfServer)
 -> ServerSessions
 -> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
 -> (Map NtfServer NtfServerSummary,
     Map NtfServer NtfServerSummary))
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
-> Map (UserId, NtfServer) ServerSessions
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey' ((ServerSessions -> NtfServerSummary -> NtfServerSummary)
-> (UserId, NtfServer)
-> ServerSessions
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
forall {a}.
(a -> NtfServerSummary -> NtfServerSummary)
-> (UserId, NtfServer)
-> a
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
addServerData ServerSessions -> NtfServerSummary -> NtfServerSummary
addSessions) (Map NtfServer NtfServerSummary
forall k a. Map k a
M.empty, Map NtfServer NtfServerSummary
forall k a. Map k a
M.empty) Map (UserId, NtfServer) ServerSessions
ntfServersSessions
        addServerData :: (a -> NtfServerSummary -> NtfServerSummary)
-> (UserId, NtfServer)
-> a
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
addServerData = (NtfServer -> NtfServerSummary)
-> (NtfServer -> NtfServerSummary)
-> (a -> NtfServerSummary -> NtfServerSummary)
-> (UserId, NtfServer)
-> a
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
-> (Map NtfServer NtfServerSummary, Map NtfServer NtfServerSummary)
forall (p :: ProtocolType) s a.
(ProtocolServer p -> s)
-> (ProtocolServer p -> s)
-> (a -> s -> s)
-> (UserId, ProtocolServer p)
-> a
-> (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
-> (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
addServerData_ NtfServer -> NtfServerSummary
newSummary NtfServer -> NtfServerSummary
newUserSummary
        addSessions :: ServerSessions -> NtfServerSummary -> NtfServerSummary
        addSessions :: ServerSessions -> NtfServerSummary -> NtfServerSummary
addSessions ServerSessions
s summ :: NtfServerSummary
summ@NtfServerSummary {Maybe ServerSessions
sessions :: NtfServerSummary -> Maybe ServerSessions
sessions :: Maybe ServerSessions
sessions} = NtfServerSummary
summ {sessions = Just $ maybe s (s `addServerSessions`) sessions}
        addStats :: AgentNtfServerStatsData -> NtfServerSummary -> NtfServerSummary
        addStats :: AgentNtfServerStatsData -> NtfServerSummary -> NtfServerSummary
addStats AgentNtfServerStatsData
s summ :: NtfServerSummary
summ@NtfServerSummary {Maybe AgentNtfServerStatsData
stats :: NtfServerSummary -> Maybe AgentNtfServerStatsData
stats :: Maybe AgentNtfServerStatsData
stats} = NtfServerSummary
summ {stats = Just $ maybe s (s `addNtfStatsData`) stats}
        newUserSummary :: NtfServer -> NtfServerSummary
newUserSummary NtfServer
srv = (NtfServer -> NtfServerSummary
newSummary NtfServer
srv :: NtfServerSummary) {known = Just $ srv `elem` userNtfSrvs}
        newSummary :: NtfServer -> NtfServerSummary
newSummary NtfServer
srv =
          NtfServerSummary
            { ntfServer :: NtfServer
ntfServer = NtfServer
srv,
              known :: Maybe Bool
known = Maybe Bool
forall a. Maybe a
Nothing,
              sessions :: Maybe ServerSessions
sessions = Maybe ServerSessions
forall a. Maybe a
Nothing,
              stats :: Maybe AgentNtfServerStatsData
stats = Maybe AgentNtfServerStatsData
forall a. Maybe a
Nothing
            }
    addServerData_ ::
      (ProtocolServer p -> s) ->
      (ProtocolServer p -> s) ->
      (a -> s -> s) ->
      (UserId, ProtocolServer p) ->
      a ->
      (Map (ProtocolServer p) s, Map (ProtocolServer p) s) ->
      (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
    addServerData_ :: forall (p :: ProtocolType) s a.
(ProtocolServer p -> s)
-> (ProtocolServer p -> s)
-> (a -> s -> s)
-> (UserId, ProtocolServer p)
-> a
-> (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
-> (Map (ProtocolServer p) s, Map (ProtocolServer p) s)
addServerData_ ProtocolServer p -> s
newSummary ProtocolServer p -> s
newUserSummary a -> s -> s
addData (UserId
userId, ProtocolServer p
srv) a
d (Map (ProtocolServer p) s
userSumms, Map (ProtocolServer p) s
allUsersSumms) = (Map (ProtocolServer p) s
userSumms', Map (ProtocolServer p) s
allUsersSumms')
      where
        userSumms' :: Map (ProtocolServer p) s
userSumms'
          | UserId
userId UserId -> UserId -> Bool
forall a. Eq a => a -> a -> Bool
== User -> UserId
aUserId User
currentUser = s -> Map (ProtocolServer p) s -> Map (ProtocolServer p) s
alterSumms (ProtocolServer p -> s
newUserSummary ProtocolServer p
srv) Map (ProtocolServer p) s
userSumms
          | Bool
otherwise = Map (ProtocolServer p) s
userSumms
        allUsersSumms' :: Map (ProtocolServer p) s
allUsersSumms'
          | UserId -> Bool
countUserInAll UserId
userId = s -> Map (ProtocolServer p) s -> Map (ProtocolServer p) s
alterSumms (ProtocolServer p -> s
newSummary ProtocolServer p
srv) Map (ProtocolServer p) s
allUsersSumms
          | Bool
otherwise = Map (ProtocolServer p) s
allUsersSumms
        alterSumms :: s -> Map (ProtocolServer p) s -> Map (ProtocolServer p) s
alterSumms s
n = (Maybe s -> Maybe s)
-> ProtocolServer p
-> Map (ProtocolServer p) s
-> Map (ProtocolServer p) s
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
M.alter (s -> Maybe s
forall a. a -> Maybe a
Just (s -> Maybe s) -> (Maybe s -> s) -> Maybe s -> Maybe s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> s -> s
addData a
d (s -> s) -> (Maybe s -> s) -> Maybe s -> s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe s -> s
forall a. a -> Maybe a -> a
fromMaybe s
n) ProtocolServer p
srv
    addServerSessions :: ServerSessions -> ServerSessions -> ServerSessions
    addServerSessions :: ServerSessions -> ServerSessions -> ServerSessions
addServerSessions ServerSessions
ss1 ServerSessions
ss2 =
      ServerSessions
        { ssConnected :: Int
ssConnected = ServerSessions -> Int
ssConnected ServerSessions
ss1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ServerSessions -> Int
ssConnected ServerSessions
ss2,
          ssErrors :: Int
ssErrors = ServerSessions -> Int
ssErrors ServerSessions
ss1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ServerSessions -> Int
ssErrors ServerSessions
ss2,
          ssConnecting :: Int
ssConnecting = ServerSessions -> Int
ssConnecting ServerSessions
ss1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ServerSessions -> Int
ssConnecting ServerSessions
ss2
        }

countUserInAllStats :: AgentUserId -> User -> [User] -> Bool
countUserInAllStats :: AgentUserId -> User -> [User] -> Bool
countUserInAllStats (AgentUserId UserId
auId) User
currentUser [User]
users =
  UserId
auId UserId -> UserId -> Bool
forall a. Eq a => a -> a -> Bool
== User -> UserId
aUserId User
currentUser Bool -> Bool -> Bool
|| UserId
auId UserId -> [UserId] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [UserId]
hiddenUserIds
  where
    hiddenUserIds :: [UserId]
hiddenUserIds = (User -> UserId) -> [User] -> [UserId]
forall a b. (a -> b) -> [a] -> [b]
map User -> UserId
aUserId ([User] -> [UserId]) -> [User] -> [UserId]
forall a b. (a -> b) -> a -> b
$ (User -> Bool) -> [User] -> [User]
forall a. (a -> Bool) -> [a] -> [a]
filter (Maybe UserPwdHash -> Bool
forall a. Maybe a -> Bool
isJust (Maybe UserPwdHash -> Bool)
-> (User -> Maybe UserPwdHash) -> User -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. User -> Maybe UserPwdHash
viewPwdHash) [User]
users

addSMPSubs :: SMPServerSubs -> SMPServerSubs -> SMPServerSubs
addSMPSubs :: SMPServerSubs -> SMPServerSubs -> SMPServerSubs
addSMPSubs SMPServerSubs
ss1 SMPServerSubs
ss2 =
  SMPServerSubs
    { ssActive :: Int
ssActive = SMPServerSubs -> Int
ssActive SMPServerSubs
ss1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ SMPServerSubs -> Int
ssActive SMPServerSubs
ss2,
      ssPending :: Int
ssPending = SMPServerSubs -> Int
ssPending SMPServerSubs
ss1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ SMPServerSubs -> Int
ssPending SMPServerSubs
ss2
    }

$(J.deriveJSON defaultJSON ''SMPTotals)

$(J.deriveJSON defaultJSON ''SMPServerSummary)

$(J.deriveJSON defaultJSON ''SMPServersSummary)

$(J.deriveJSON defaultJSON ''XFTPTotals)

$(J.deriveJSON defaultJSON ''XFTPServerSummary)

$(J.deriveJSON defaultJSON ''XFTPServersSummary)

$(J.deriveJSON defaultJSON ''NtfTotals)

$(J.deriveJSON defaultJSON ''NtfServerSummary)

$(J.deriveJSON defaultJSON ''NtfServersSummary)

$(J.deriveJSON defaultJSON ''PresentedServersSummary)