module System.Information.StreamInfo
( getParsedInfo
, getLoad
, getTransfer) where
import Control.Concurrent (threadDelay)
import Data.Maybe (fromJust)
getParsedInfo :: FilePath -> (String -> [(String, [Integer])]) -> String -> IO [Integer]
getParsedInfo path parser selector = do
file <- readFile path
(length file) `seq` return ()
return (fromJust $ lookup selector $ parser file)
truncVal :: Double -> Double
truncVal v
| isNaN v || v < 0.0 = 0.0
| otherwise = v
probe :: IO [Integer] -> Double -> IO [Integer]
probe action delay = do
a <- action
threadDelay $ round (delay * 1e6)
b <- action
return $ zipWith () b a
getTransfer :: Double -> IO [Integer] -> IO [Double]
getTransfer interval action = do
deltas <- probe action interval
return $ map (truncVal . (/interval) . fromIntegral) deltas
getLoad :: Double -> IO [Integer] -> IO [Double]
getLoad interval action = do
deltas <- probe action interval
let total = fromIntegral $ foldr (+) 0 deltas
ratios = map ((/total) . fromIntegral) deltas
return $ map truncVal ratios