diff options
author | Joris | 2020-01-20 19:47:23 +0100 |
---|---|---|
committer | Joris | 2020-01-20 22:11:19 +0100 |
commit | 47c2a4d6b68c54eed5f7b45671b1ccaf8c0db200 (patch) | |
tree | f5c1c4281bb26810bdd0fea3d6582d3eafa227cf /server/src/Statistics.hs | |
parent | d20d7ceec2a14f79ebb06555a71d424aeaa90e54 (diff) |
Show payment stats
Diffstat (limited to 'server/src/Statistics.hs')
-rw-r--r-- | server/src/Statistics.hs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/server/src/Statistics.hs b/server/src/Statistics.hs new file mode 100644 index 0000000..371fba2 --- /dev/null +++ b/server/src/Statistics.hs @@ -0,0 +1,34 @@ +module Statistics + ( compute + ) where + +import qualified Data.List as L +import qualified Data.Map as M +import qualified Data.Time.Calendar as Calendar + +import Common.Model (Payment (..), PaymentStats) + +compute :: [Payment] -> PaymentStats +compute payments = + + M.toList $ foldl + (\m p -> M.alter (alter p) (startOfMonth $ _payment_date p) m) + M.empty + payments + + where + + initMonthStats = + M.fromList + . map (\category -> (category, 0)) + . L.nub + $ map _payment_category payments + + alter p Nothing = Just (addPayment p initMonthStats) + alter p (Just monthStats) = Just (addPayment p monthStats) + + addPayment p monthStats = M.adjust ((+) (_payment_cost p)) (_payment_category p) monthStats + + startOfMonth day = + let (y, m, _) = Calendar.toGregorian day + in Calendar.fromGregorian y m 1 |