aboutsummaryrefslogtreecommitdiff
path: root/client/src/View/Statistics/Statistics.hs
blob: d931b2b8e7325d9750050ac0e5069b2a3606a6f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module View.Statistics.Statistics
  ( view
  , In(..)
  ) where

import           Control.Monad         (void)
import           Data.Map              (Map)
import qualified Data.Map              as M
import qualified Data.Text             as T
import           Data.Time.Calendar    (Day)
import qualified Data.Time.Calendar    as Calendar
import           Loadable              (Loadable)
import qualified Loadable
import           Reflex.Dom            (Dynamic, MonadWidget)
import qualified Reflex.Dom            as R
import qualified Util.Ajax             as AjaxUtil
import qualified View.Statistics.Chart as Chart

import           Common.Model          (Category (..), Currency, Income,
                                        MonthStats (..), Stats, User (..))
import qualified Common.Msg            as Msg
import qualified Common.View.Format    as Format

data In = In
  { _in_currency    :: Currency
  }

view :: forall t m. MonadWidget t m => In -> m ()
view input = do

  users <- AjaxUtil.getNow "api/users"
  categories <- AjaxUtil.getNow "api/allCategories"
  statistics <- AjaxUtil.getNow "api/statistics"

  let loadable = (\u c s -> (,,) <$> u <*> c <*> s) <$> users <*> categories <*> statistics

  R.divClass "withMargin" $
    R.divClass "titleButton" $
      R.el "h1" $
        R.text $ Msg.get Msg.Statistics_Title

  void . R.dyn . R.ffor loadable . Loadable.viewHideValueWhileLoading $
    stats (_in_currency input)

stats :: forall t m. MonadWidget t m => Currency -> ([User], [Category], Stats) -> m ()
stats currency (users, categories, stats) =
  Chart.view $ Chart.In
    { Chart._in_title = Msg.get (Msg.Statistics_ByMonthsAndMean averagePayment averageIncome)
    , Chart._in_labels = map (Format.monthAndYear . _monthStats_start) stats
    , Chart._in_datasets = totalIncomeDataset : totalPaymentDataset : (map categoryDataset categories)
    }

  where
    averageIncome =
      Format.price currency $ sum  totalIncomes `div` length stats

    totalIncomeDataset =
      Chart.Dataset
        { Chart._dataset_label = Msg.get Msg.Statistics_TotalIncomes
        , Chart._dataset_data  = totalIncomes
        , Chart._dataset_color = "#222222"
        }

    totalIncomes =
      map (sum . map snd . M.toList . _monthStats_incomeByUser) stats

    averagePayment =
      Format.price currency $ sum totalPayments `div` length stats

    totalPaymentDataset =
      Chart.Dataset
        { Chart._dataset_label = Msg.get Msg.Statistics_TotalPayments
        , Chart._dataset_data  = totalPayments
        , Chart._dataset_color = "#555555"
        }

    totalPayments =
      map (sum . map snd . M.toList . _monthStats_paymentsByCategory) stats

    categoryDataset category =
      Chart.Dataset
        { Chart._dataset_label = _category_name category
        , Chart._dataset_data  = map (M.findWithDefault 0 (_category_id category) . _monthStats_paymentsByCategory) stats
        , Chart._dataset_color = _category_color category
        }