Created
November 3, 2016 16:57
-
-
Save nakal/2d1bc381e0cf0e4f22e80bbe6baf067b to your computer and use it in GitHub Desktop.
Calculates a diagram of average usage per day from timestamp/value pairs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.ByteString.Char8 as C8 | |
import Data.UnixTime | |
csvTableToTuple :: String -> (Int, Int) | |
csvTableToTuple line = | |
let date = takeWhile (/= ';') line | |
val = tail $ dropWhile (/= ';') line | |
in (fromEnum (utSeconds (parseUnixTime (C8.pack "%Y-%m-%d") (C8.pack date))), read val) | |
secondsInDay :: Int | |
secondsInDay = 60 * 60 * 24 | |
valDiff :: (Int, Int) -> (Int, Int) -> (Int, Int) | |
valDiff (a,b) (c,d) = | |
let dd = (a-c) `div` secondsInDay | |
in | |
(dd,(b-d) `div` dd) | |
outputM :: Int -> Int -> Int -> IO() | |
outputM _ 0 _ = return() | |
outputM act repeat diff = do | |
putStrLn $ (show ts) ++ ", " ++ (show diff) | |
outputM ts (repeat - 1) diff | |
where ts = act + secondsInDay | |
perDay :: (Int, Int) -> [ (Int, Int) ] -> IO() | |
perDay _ [] = return () | |
perDay last remain = do | |
let next = head remain | |
(days, diffv) = valDiff next last | |
outputM (fst last) days diffv | |
perDay next (tail remain) | |
main = do | |
l <- fmap lines getContents | |
let dat = map csvTableToTuple l | |
perDay (head dat) (tail dat) | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment