@@ -24,6 +24,7 @@ import Data.Text (Text)
2424import Data.Time.Calendar.Month (pattern MonthDay )
2525import Data.Time.Calendar.OrdinalDate (mondayStartWeek )
2626import Data.Time.Clock (UTCTime (.. ))
27+ import Data.Time.Clock.System (systemEpochDay )
2728import GHC.IORef (atomicSwapIORef )
2829import Simplex.Messaging.Encoding.String
2930import Simplex.Messaging.Protocol (EntityId (.. ))
@@ -179,7 +180,7 @@ newServerStats ts = do
179180 msgSentLarge <- newIORef 0
180181 msgSentBlock <- newIORef 0
181182 msgRecv <- newIORef 0
182- msgRecvAckTimes <- newIORef $ TimeBuckets 0 0 IM. empty
183+ msgRecvAckTimes <- newIORef $ TimeBuckets 0 0 IM. empty ts
183184 msgRecvGet <- newIORef 0
184185 msgGet <- newIORef 0
185186 msgGetNoMsg <- newIORef 0
@@ -472,7 +473,6 @@ instance StrEncoding ServerStatsData where
472473 " msgSentLarge=" <> strEncode (_msgSentLarge d),
473474 " msgSentBlock=" <> strEncode (_msgSentBlock d),
474475 " msgRecv=" <> strEncode (_msgRecv d),
475- " msgRecvAckTimes=" <> strEncode (_msgRecvAckTimes d),
476476 " msgRecvGet=" <> strEncode (_msgRecvGet d),
477477 " msgGet=" <> strEncode (_msgGet d),
478478 " msgGetNoMsg=" <> strEncode (_msgGetNoMsg d),
@@ -536,7 +536,6 @@ instance StrEncoding ServerStatsData where
536536 _msgSentLarge <- opt " msgSentLarge="
537537 _msgSentBlock <- opt " msgSentBlock="
538538 _msgRecv <- " msgRecv=" *> strP <* A. endOfLine
539- _msgRecvAckTimes <- " msgRecvAckTimes=" *> strP <* A. endOfLine <|> pure (TimeBuckets 0 0 IM. empty)
540539 _msgRecvGet <- opt " msgRecvGet="
541540 _msgGet <- opt " msgGet="
542541 _msgGetNoMsg <- opt " msgGetNoMsg="
@@ -604,7 +603,7 @@ instance StrEncoding ServerStatsData where
604603 _msgSentLarge,
605604 _msgSentBlock,
606605 _msgRecv,
607- _msgRecvAckTimes,
606+ _msgRecvAckTimes = emptyTimeBuckets ,
608607 _msgRecvGet,
609608 _msgGet,
610609 _msgGetNoMsg,
@@ -961,16 +960,20 @@ instance StrEncoding ServiceStatsData where
961960data TimeBuckets = TimeBuckets
962961 { sumTime :: Int64 ,
963962 maxTime :: Int64 ,
964- timeBuckets :: IM. IntMap Int
963+ timeBuckets :: IM. IntMap Int ,
964+ createdAt :: UTCTime
965965 }
966966 deriving (Show )
967967
968+ emptyTimeBuckets :: TimeBuckets
969+ emptyTimeBuckets = TimeBuckets 0 0 IM. empty (UTCTime systemEpochDay 0 )
970+
968971updateTimeBuckets :: RoundedSystemTime -> RoundedSystemTime -> TimeBuckets -> TimeBuckets
969972updateTimeBuckets
970973 (RoundedSystemTime deliveryTime)
971974 (RoundedSystemTime currTime)
972- TimeBuckets {sumTime, maxTime, timeBuckets} =
973- TimeBuckets
975+ times @ TimeBuckets {sumTime, maxTime, timeBuckets} =
976+ times
974977 { sumTime = sumTime + t,
975978 maxTime = max maxTime t,
976979 timeBuckets = IM. alter (Just . maybe 1 (+ 1 )) seconds timeBuckets
@@ -984,11 +987,3 @@ updateTimeBuckets
984987 | t <= 180 = t `toBucket` 30
985988 | otherwise = t `toBucket` 60
986989 toBucket n m = - fromIntegral (((- n) `div` m) * m) -- round up
987-
988- instance StrEncoding TimeBuckets where
989- strEncode TimeBuckets {sumTime, maxTime, timeBuckets} =
990- strEncode (sumTime, maxTime) <> " " <> strEncodeList (IM. toList timeBuckets)
991- strP = do
992- (sumTime, maxTime) <- strP_
993- tbs <- strListP
994- pure TimeBuckets {sumTime, maxTime, timeBuckets = IM. fromList tbs}
0 commit comments