@@ -11,6 +11,7 @@ module Simplex.Messaging.Agent.Store.SQLite.DB
1111 Binary (.. ),
1212 Connection (.. ),
1313 SlowQueryStats (.. ),
14+ TrackQueries (.. ),
1415 open ,
1516 close ,
1617 execute ,
@@ -38,7 +39,7 @@ import Database.SQLite.Simple.ToField (ToField (..))
3839import Simplex.Messaging.Parsers (defaultJSON )
3940import Simplex.Messaging.TMap (TMap )
4041import qualified Simplex.Messaging.TMap as TM
41- import Simplex.Messaging.Util (diffToMilliseconds , tshow )
42+ import Simplex.Messaging.Util (diffToMicroseconds , tshow )
4243
4344newtype BoolInt = BI { unBI :: Bool }
4445 deriving newtype (FromField , ToField )
@@ -48,9 +49,13 @@ newtype Binary = Binary {fromBinary :: ByteString}
4849
4950data Connection = Connection
5051 { conn :: SQL. Connection ,
52+ track :: TrackQueries ,
5153 slow :: TMap Query SlowQueryStats
5254 }
5355
56+ data TrackQueries = TQAll | TQSlow Int64 | TQOff
57+ deriving (Eq )
58+
5459data SlowQueryStats = SlowQueryStats
5560 { count :: Int64 ,
5661 timeMax :: Int64 ,
@@ -59,22 +64,29 @@ data SlowQueryStats = SlowQueryStats
5964 }
6065 deriving (Show )
6166
62- timeIt :: TMap Query SlowQueryStats -> Query -> IO a -> IO a
63- timeIt slow sql a = do
64- t <- getCurrentTime
65- r <-
66- a `catch` \ e -> do
67- atomically $ TM. alter (Just . updateQueryErrors e) sql slow
68- throwIO e
69- t' <- getCurrentTime
70- let diff = diffToMilliseconds $ diffUTCTime t' t
71- when (diff > 1 ) $ atomically $ TM. alter (updateQueryStats diff) sql slow
72- pure r
67+ timeIt :: Connection -> Query -> IO a -> IO a
68+ timeIt Connection {slow, track} sql a
69+ | track == TQOff = makeQuery
70+ | otherwise = do
71+ t <- getCurrentTime
72+ r <- makeQuery
73+ t' <- getCurrentTime
74+ let diff = diffToMicroseconds $ diffUTCTime t' t
75+ when (trackQuery diff) $ atomically $ TM. alter (updateQueryStats diff) sql slow
76+ pure r
7377 where
78+ makeQuery =
79+ a `catch` \ e -> do
80+ atomically $ TM. alter (Just . updateQueryErrors e) sql slow
81+ throwIO e
82+ trackQuery diff = case track of
83+ TQOff -> False
84+ TQSlow t -> diff > t
85+ TQAll -> True
7486 updateQueryErrors :: SomeException -> Maybe SlowQueryStats -> SlowQueryStats
7587 updateQueryErrors e Nothing = SlowQueryStats 0 0 0 $ M. singleton (tshow e) 1
76- updateQueryErrors e (Just stats @ SlowQueryStats {errs}) =
77- stats {errs = M. alter (Just . maybe 1 (+ 1 )) (tshow e) errs}
88+ updateQueryErrors e (Just st @ SlowQueryStats {errs}) =
89+ st {errs = M. alter (Just . maybe 1 (+ 1 )) (tshow e) errs}
7890 updateQueryStats :: Int64 -> Maybe SlowQueryStats -> Maybe SlowQueryStats
7991 updateQueryStats diff Nothing = Just $ SlowQueryStats 1 diff diff M. empty
8092 updateQueryStats diff (Just SlowQueryStats {count, timeMax, timeAvg, errs}) =
@@ -86,33 +98,33 @@ timeIt slow sql a = do
8698 errs
8799 }
88100
89- open :: String -> IO Connection
90- open f = do
101+ open :: String -> TrackQueries -> IO Connection
102+ open f track = do
91103 conn <- SQL. open f
92104 slow <- TM. emptyIO
93- pure Connection {conn, slow}
105+ pure Connection {conn, slow, track }
94106
95107close :: Connection -> IO ()
96108close = SQL. close . conn
97109
98110execute :: ToRow q => Connection -> Query -> q -> IO ()
99- execute Connection {conn, slow} sql = timeIt slow sql . SQL. execute conn sql
111+ execute c sql = timeIt c sql . SQL. execute ( conn c) sql
100112{-# INLINE execute #-}
101113
102114execute_ :: Connection -> Query -> IO ()
103- execute_ Connection {conn, slow} sql = timeIt slow sql $ SQL. execute_ conn sql
115+ execute_ c sql = timeIt c sql $ SQL. execute_ ( conn c) sql
104116{-# INLINE execute_ #-}
105117
106118executeMany :: ToRow q => Connection -> Query -> [q ] -> IO ()
107- executeMany Connection {conn, slow} sql = timeIt slow sql . SQL. executeMany conn sql
119+ executeMany c sql = timeIt c sql . SQL. executeMany ( conn c) sql
108120{-# INLINE executeMany #-}
109121
110122query :: (ToRow q , FromRow r ) => Connection -> Query -> q -> IO [r ]
111- query Connection {conn, slow} sql = timeIt slow sql . SQL. query conn sql
123+ query c sql = timeIt c sql . SQL. query ( conn c) sql
112124{-# INLINE query #-}
113125
114126query_ :: FromRow r => Connection -> Query -> IO [r ]
115- query_ Connection {conn, slow} sql = timeIt slow sql $ SQL. query_ conn sql
127+ query_ c sql = timeIt c sql $ SQL. query_ ( conn c) sql
116128{-# INLINE query_ #-}
117129
118130$ (J. deriveJSON defaultJSON ''SlowQueryStats)
0 commit comments