module Villefort.Stats (genStats, makeTable )where import Villefort.Database import Control.Monad.IO.Class import Paths_Villefort import System.Random -- | Query to get average of subjects --getAvg :: IO (Maybe [String]) getAvg :: IO [[String]] getAvg = makeQuery "select avg(time), Subject from todo group by Subject order by avg(time) desc" -- | Query to get sum of subjects --getSum :: IO (Maybe [String]) getSum :: IO [[String]] getSum = makeQuery "select sum(time), Subject from todo group by Subject order by sum(time) desc" -- | Helper function to generate row of table makeRow :: [String] -> String makeRow x = " " ++ (x !! 0) ++ " " ++ (x!! 1) ++ " " -- | Generate Table makeTable ::[String] -> [[String]] -> String makeTable tableData stats = " " ++ "" ++ ( makeRow tableData) ++ "" ++ (mconcat (map makeRow stats)) ++ "
" -- | Generate stats {- genStats :: IO String genStats = getSubjects >>= \subjects -> mapM makeGithub subjects >>= \gits -> getAvg >>= \avg -> getSum >>= \statsSum -> getDataFileName "templates/header" >>= \x -> liftIO (readFile x) >>= \header -> return (header ++ table ++ (makeTable ["Subject","time"] avg) ++ "

Sum

" ++ (makeTable ["Subject","time"] statsSum) ++ "" ++ (mconcat gits)) -} genStats :: IO String genStats = do subjects <- getSubjects gits <- mapM makeGithub subjects avg <- getAvg statsSum <- getSum x <- getDataFileName "templates/header" header <- liftIO (readFile x) return (header ++ table ++ (makeTable ["Subject","time"] avg) ++ "

Sum

" ++ (makeTable ["Subject","time"] statsSum) ++ "" ++ (mconcat gits)) -- | Library to include for github table table :: String table = "

avg

" -- | creates the github like graph from database makeGithub :: String -> IO String makeGithub subject = do z <- makeQuery ("select substr(Due,1,10) from todo where subject = '" ++ subject ++ "' and state = 0") color <- getColor let header = "

Calendar "++ subject ++ "

" return (header ++ (Prelude.concat q )++ bot) colors :: [String] colors = ["'#F44336'" ,"'#E91E63'" ,"'#9C27B0'" ,"'#673AB7'" ,"'#3F51B5'" ,"'#2196F3'" ,"'#03A9F4'" ,"'#00BCD4'" ,"'#009688'" ,"'#4CAF50'" ,"'#8BC34A'" ,"'#CDDC39'" ,"'#FFEB3B'" ,"'#FFC107'" ,"'#FF9800'" ,"'#FF5722'" ,"'#795548'" ,"'#9E9E9E'" ,"'#607D8B'" ] getColor :: IO String getColor = do number <- randomRIO (0, (length $ colors)-1) :: IO Int return (colors !! number)