Implement basic scheduling

This commit is contained in:
Tissevert 2019-04-10 11:44:46 +02:00
parent b4fcc7857c
commit 0c875b78fe
2 changed files with 17 additions and 4 deletions

View file

@ -1,12 +1,15 @@
{-# LANGUAGE NamedFieldPuns #-}
module Main where
import CLI (Invocation(..), invoked)
import CSV (Row, parse)
import Data.Time (UTCTime(..), getCurrentTime)
import Data.List (minimum)
import Data.Map (elems, mergeWithKey)
import Data.Time (UTCTime(..), getCurrentTime, addDays)
import Event (Event)
import Medicine (Medicine, Pharmacy, pharmacy)
import System.Exit (exitFailure, exitSuccess)
import Timeline (State, currentState)
import Timeline (State(..), currentState)
readCSV :: Row a => FilePath -> IO [a]
readCSV filePath = do
@ -22,10 +25,20 @@ getCurrentState =
<*> (pharmacy <$> readCSV "medicine.csv")
<*> readCSV "timeline.csv"
schedule :: State -> String
schedule (State {day, stock, consumptionRate}) = unlines [
"Days left: " ++ show deltaDays
, "Provision on: " ++ show (deltaDays `addDays` day )
]
where
daysLeftByMedicine =
mergeWithKey (\k a b -> Just $ a / b) id id stock consumptionRate
deltaDays = truncate . minimum $ elems daysLeftByMedicine
main :: IO ()
main = do
invocation <- invoked
case invocation of
Inventory -> show <$> getCurrentState >>= putStrLn
Schedule -> putStrLn "schedule"
Schedule -> schedule <$> getCurrentState >>= putStrLn
Version version -> putStrLn version >> exitSuccess

View file

@ -1,6 +1,6 @@
{-# LANGUAGE NamedFieldPuns #-}
module Timeline (
State
State(..)
, currentState
) where