@@ -43,14 +43,13 @@ import qualified Data.ByteString.Builder as BB
4343import Data.ByteString.Char8 (ByteString )
4444import qualified Data.ByteString.Lazy as LB
4545import Data.Bitraversable (bimapM )
46- import Data.Either (fromRight , lefts , rights )
46+ import Data.Either (fromRight , lefts )
4747import Data.Functor (($>) )
4848import Data.Int (Int64 )
4949import Data.List (foldl' , intersperse , partition )
5050import Data.List.NonEmpty (NonEmpty )
51- import qualified Data.List.NonEmpty as L
5251import qualified Data.Map.Strict as M
53- import Data.Maybe (catMaybes , fromMaybe , mapMaybe )
52+ import Data.Maybe (catMaybes , fromMaybe )
5453import qualified Data.Set as S
5554import Data.Text (Text )
5655import Data.Text.Encoding (decodeLatin1 , encodeUtf8 )
@@ -65,7 +64,7 @@ import Database.PostgreSQL.Simple.ToField (Action (..), ToField (..))
6564import Database.PostgreSQL.Simple.Errors (ConstraintViolation (.. ), constraintViolation )
6665import Database.PostgreSQL.Simple.SqlQQ (sql )
6766import GHC.IO (catchAny )
68- import Simplex.Messaging.Agent.Client (withLockMap , withLocksMap )
67+ import Simplex.Messaging.Agent.Client (withLockMap )
6968import Simplex.Messaging.Agent.Lock (Lock )
7069import Simplex.Messaging.Agent.Store.AgentStore ()
7170import Simplex.Messaging.Agent.Store.Postgres (createDBStore , closeDBStore )
@@ -84,7 +83,7 @@ import Simplex.Messaging.Server.StoreLog
8483import Simplex.Messaging.TMap (TMap )
8584import qualified Simplex.Messaging.TMap as TM
8685import Simplex.Messaging.Transport (SMPServiceRole (.. ))
87- import Simplex.Messaging.Util (eitherToMaybe , firstRow , ifM , maybeFirstRow , tshow , (<$$>) , ($>>=) )
86+ import Simplex.Messaging.Util (eitherToMaybe , firstRow , ifM , maybeFirstRow , tshow , (<$$>) )
8887import System.Exit (exitFailure )
8988import System.IO (IOMode (.. ), hFlush , stdout )
9089import UnliftIO.STM
@@ -485,11 +484,15 @@ instance StoreQueueClass q => QueueStoreClass q (PostgresQueueStore q) where
485484 let (sNtfs, restNtfs) = partition (\ (nId, _) -> S. member nId snIds) ntfs'
486485 in ((serviceId, sNtfs) : ssNtfs, restNtfs)
487486
488- getNtfServiceQueueCount :: PostgresQueueStore q -> ServiceId -> IO (Either ErrorType Int64 )
489- getNtfServiceQueueCount st serviceId =
490- E. uninterruptibleMask_ $ runExceptT $ withDB' " getNtfServiceQueueCount " st $ \ db ->
487+ getServiceQueueCount :: ( PartyI p , ServiceParty p ) => PostgresQueueStore q -> SParty p -> ServiceId -> IO (Either ErrorType Int64 )
488+ getServiceQueueCount st party serviceId =
489+ E. uninterruptibleMask_ $ runExceptT $ withDB' " getServiceQueueCount " st $ \ db ->
491490 fmap (fromMaybe 0 ) $ maybeFirstRow fromOnly $
492- DB. query db " SELECT count(1) FROM msg_queues WHERE ntf_service_id = ? AND deleted_at IS NULL" (Only serviceId)
491+ DB. query db query (Only serviceId)
492+ where
493+ query = case party of
494+ SRecipientService -> " SELECT count(1) FROM msg_queues WHERE rcv_service_id = ? AND deleted_at IS NULL"
495+ SNotifierService -> " SELECT count(1) FROM msg_queues WHERE ntf_service_id = ? AND deleted_at IS NULL"
493496
494497batchInsertServices :: [STMService ] -> PostgresQueueStore q -> IO Int64
495498batchInsertServices services' toStore =
0 commit comments