Skip to content

Instantly share code, notes, and snippets.

@nakal
Created November 3, 2016 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakal/2d1bc381e0cf0e4f22e80bbe6baf067b to your computer and use it in GitHub Desktop.
Save nakal/2d1bc381e0cf0e4f22e80bbe6baf067b to your computer and use it in GitHub Desktop.
Calculates a diagram of average usage per day from timestamp/value pairs
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