Adds time function in Timer

This commit is contained in:
Abhinav Sarkar 2017-11-15 18:27:05 +05:30
parent a08d3e6867
commit f5036f0d58
2 changed files with 15 additions and 1 deletions

View File

@ -11,7 +11,8 @@
"purescript-console": "^3.0.0",
"purescript-aff": "^3.1.0",
"purescript-maybe": "^3.0.0",
"purescript-foreign-generic": "^5.0.0"
"purescript-foreign-generic": "^5.0.0",
"purescript-now": "^3.0.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"

View File

@ -3,6 +3,7 @@ module System.Metrics.Timer
, Summary
, new
, update
, time
, fifteenMinuteRate
, fiveMinuteRate
, oneMinuteRate
@ -20,8 +21,12 @@ module System.Metrics.Timer
import Prelude
import Control.Monad.Aff (Aff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Class (liftEff)
import Control.Monad.Eff.Now (NOW, now)
import Control.Monad.Eff.Ref (REF)
import Data.DateTime.Instant (unInstant)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
import Data.Maybe (Maybe)
@ -44,6 +49,14 @@ update (Timer { meter, histogram }) d = do
Histogram.update histogram ms
Meter.mark meter
time :: forall a eff. Timer -> Aff (ref :: REF, now :: NOW | eff) a -> Aff (ref :: REF, now :: NOW | eff) a
time timer f = do
start <- liftEff now
r <- f
end <- liftEff now
liftEff $ update timer (unInstant end - unInstant start)
pure r
fifteenMinuteRate :: forall eff. Timer -> Eff (ref :: REF | eff) Number
fifteenMinuteRate (Timer { meter }) = Meter.fifteenMinuteRate meter