module System.Taffybar.Information.StreamInfo
( getParsedInfo
, getLoad
, getAccLoad
, getTransfer
) where
import Control.Concurrent ( threadDelay )
import Data.IORef
import Data.Maybe ( fromMaybe )
getParsedInfo :: FilePath -> (String -> [(String, [a])]) -> String -> IO [a]
getParsedInfo :: forall a.
FilePath -> (FilePath -> [(FilePath, [a])]) -> FilePath -> IO [a]
getParsedInfo FilePath
path FilePath -> [(FilePath, [a])]
parser FilePath
selector = do
FilePath
file <- FilePath -> IO FilePath
readFile FilePath
path
FilePath -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length FilePath
file Int -> IO () -> IO ()
forall a b. a -> b -> b
`seq` () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
[a] -> IO [a]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> Maybe [a] -> [a]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [a] -> [a]) -> Maybe [a] -> [a]
forall a b. (a -> b) -> a -> b
$ FilePath -> [(FilePath, [a])] -> Maybe [a]
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup FilePath
selector ([(FilePath, [a])] -> Maybe [a]) -> [(FilePath, [a])] -> Maybe [a]
forall a b. (a -> b) -> a -> b
$ FilePath -> [(FilePath, [a])]
parser FilePath
file)
truncVal :: (RealFloat a) => a -> a
truncVal :: forall a. RealFloat a => a -> a
truncVal a
v
| a -> Bool
forall a. RealFloat a => a -> Bool
isNaN a
v Bool -> Bool -> Bool
|| a
v a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0.0 = a
0.0
| Bool
otherwise = a
v
toRatioList :: (Integral a, RealFloat b) => [a] -> [b]
toRatioList :: forall a b. (Integral a, RealFloat b) => [a] -> [b]
toRatioList [a]
deltas = (b -> b) -> [b] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map b -> b
forall a. RealFloat a => a -> a
truncVal [b]
ratios
where total :: b
total = a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$ [a] -> a
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [a]
deltas
ratios :: [b]
ratios = (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map ((b -> b -> b
forall a. Fractional a => a -> a -> a
/b
total) (b -> b) -> (a -> b) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral) [a]
deltas
probe :: (Num a, RealFrac b) => IO [a] -> b -> IO [a]
probe :: forall a b. (Num a, RealFrac b) => IO [a] -> b -> IO [a]
probe IO [a]
action b
delay = do
[a]
a <- IO [a]
action
Int -> IO ()
threadDelay (Int -> IO ()) -> Int -> IO ()
forall a b. (a -> b) -> a -> b
$ b -> Int
forall b. Integral b => b -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (b
delay b -> b -> b
forall a. Num a => a -> a -> a
* b
1e6)
[a]
b <- IO [a]
action
[a] -> IO [a]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> IO [a]) -> [a] -> IO [a]
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> [a] -> [a] -> [a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (-) [a]
b [a]
a
accProbe :: (Num a) => IO [a] -> IORef [a] -> IO [a]
accProbe :: forall a. Num a => IO [a] -> IORef [a] -> IO [a]
accProbe IO [a]
action IORef [a]
sample = do
[a]
a <- IORef [a] -> IO [a]
forall a. IORef a -> IO a
readIORef IORef [a]
sample
[a]
b <- IO [a]
action
IORef [a] -> [a] -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef [a]
sample [a]
b
[a] -> IO [a]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> IO [a]) -> [a] -> IO [a]
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> [a] -> [a] -> [a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (-) [a]
b [a]
a
getTransfer :: (Integral a, RealFloat b) => b -> IO [a] -> IO [b]
getTransfer :: forall a b. (Integral a, RealFloat b) => b -> IO [a] -> IO [b]
getTransfer b
interval IO [a]
action = do
[a]
deltas <- IO [a] -> b -> IO [a]
forall a b. (Num a, RealFrac b) => IO [a] -> b -> IO [a]
probe IO [a]
action b
interval
[b] -> IO [b]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([b] -> IO [b]) -> [b] -> IO [b]
forall a b. (a -> b) -> a -> b
$ (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map (b -> b
forall a. RealFloat a => a -> a
truncVal (b -> b) -> (a -> b) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> b -> b
forall a. Fractional a => a -> a -> a
/b
interval) (b -> b) -> (a -> b) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral) [a]
deltas
getLoad :: (Integral a, RealFloat b) => b -> IO [a] -> IO [b]
getLoad :: forall a b. (Integral a, RealFloat b) => b -> IO [a] -> IO [b]
getLoad b
interval IO [a]
action = [a] -> [b]
forall a b. (Integral a, RealFloat b) => [a] -> [b]
toRatioList ([a] -> [b]) -> IO [a] -> IO [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [a] -> b -> IO [a]
forall a b. (Num a, RealFrac b) => IO [a] -> b -> IO [a]
probe IO [a]
action b
interval
getAccLoad :: (Integral a, RealFloat b) => IORef [a] -> IO [a] -> IO [b]
getAccLoad :: forall a b.
(Integral a, RealFloat b) =>
IORef [a] -> IO [a] -> IO [b]
getAccLoad IORef [a]
sample IO [a]
action = [a] -> [b]
forall a b. (Integral a, RealFloat b) => [a] -> [b]
toRatioList ([a] -> [b]) -> IO [a] -> IO [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO [a] -> IORef [a] -> IO [a]
forall a. Num a => IO [a] -> IORef [a] -> IO [a]
accProbe IO [a]
action IORef [a]
sample