packages feed

live-stats (empty) → 0.1.0.0

raw patch · 3 files changed

+138/−0 lines, 3 filesdep +basedep +reludedep +time

Dependencies added: base, relude, time

Files

+ LICENSE view
@@ -0,0 +1,11 @@+Copyright (c) 2014, Soostone Inc++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ live-stats.cabal view
@@ -0,0 +1,34 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.38.2.+--+-- see: https://github.com/sol/hpack++name:                live-stats+version:             0.1.0.0+synopsis:            A reusable run-time stats module.+license:             BSD3+license-file:        LICENSE+author:              Ozgun Ataman+maintainer:          alexander.shestakov@soostone.com+copyright:           2014, Soostone Inc+category:            Control+build-type:          Simple++library+  exposed-modules:+      LiveStats+  other-modules:+      Paths_live_stats+  build-depends:+      base >=4.19 && <=5+    , relude+    , time+  hs-source-dirs:+      src+  default-extensions:+      OverloadedStrings+      NoFieldSelectors+      NoImplicitPrelude+  ghc-options: -Wall+  default-language: GHC2024
+ src/LiveStats.hs view
@@ -0,0 +1,93 @@+module LiveStats+  ( StatState (..)+  , Stats+  , newStats+  , addCounter+  , readStats+  , incStats+  , obtainReport+  , reportStats+  , reportStatsForever+  ) where++import Relude+import Relude.Extra++import Data.Time.Clock.POSIX+import Control.Concurrent+import Numeric++newtype Stats = Stats (IORef (Map Text StatState))++data StatState = StatState+  { statStart  :: !Double+  -- ^ Origin start of run+  , statChkPnt :: !Double+  -- ^ Last time checkpoint+  , statGen    :: !Int+  -- ^ Hits since last checkpoint+  , statTotal  :: !Int+  -- ^ Total hits since original start+  }++newStats :: (MonadIO m) => m Stats+newStats = Stats <$> newIORef mempty++-- | Each named counter needs to be added (initialized) before it can be used.+addCounter :: (MonadIO m) => Stats -> Text -> m ()+addCounter (Stats s) nm = do+  now <- getTime+  atomicModifyIORef' s $ \m -> (insert nm (StatState now now 0 0) m, ())++readStats :: (MonadIO m) => Stats -> m (Map Text StatState)+readStats (Stats s) = readIORef s++-- | Increment given counter by n.+incStats :: (MonadIO m) => Stats -> Text -> Int -> m ()+incStats (Stats stats) nm n = atomicModifyIORef' stats $ \m -> (alter f nm m, ())+  where+    f = \case+      Nothing -> error "Counter should be added before usage"+      Just old@StatState {statGen, statTotal} ->+        let !x  = Just $! old { statGen = statGen + n, statTotal = statTotal + n }+        in x++-- | Produce stats messages - one for each registered counter.+obtainReport :: (MonadIO m) => Stats -> m [Text]+obtainReport (Stats stats) = do+  now <- liftIO getTime+  olds <- atomicModifyIORef' stats $ \m -> do+    let+      olds = toPairs m+      reset (nm, old) = (nm, old { statGen = 0, statChkPnt = now })+      news = fromList $ reset <$> olds+    (news, olds)+  pure $ go now <$> olds+  where+    go now (nm, StatState {statGen, statTotal, statStart, statChkPnt}) = do+      let+        chk = fromIntegral statGen / (now - statChkPnt)+        tot = fromIntegral statTotal / (now - statStart)+      toText $ "[" <> toString nm <> "]" <>+        " Processed " <> showInt statGen+        " items at " <> showFFloat (Just 2) chk+        " items/sec for a total of " <> showInt statTotal+        " items at " <> showFFloat (Just 2) tot " items/sec."++-- | Obtain report and call function for each line.+reportStats+  :: (MonadIO m)+  => (Text -> m ())+  -- ^ How to render each line+  -> Stats+  -> m ()+reportStats f stats = obtainReport stats >>= mapM_ f++-- | Report stats every n seconds. Blocks forever.+reportStatsForever :: (MonadIO m) => (Text -> m ()) -> Stats -> Int -> m ()+reportStatsForever f stats n = forever $ do+  reportStats f stats+  liftIO $ threadDelay $ n*1000*1000++getTime :: (MonadIO m) => m Double+getTime = realToFrac <$> liftIO getPOSIXTime