From c41bbb85aaa145e33fe9c73e50e689790e88c207 Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Fri, 15 Dec 2017 20:25:37 +0100 Subject: [PATCH] add granularity control --- credger.cr | 1 + ledger.cr | 3 +- public/ts/components/dashboard.ts | 47 +++++++++++++++++-------------- public/ts/services/API.ts | 3 +- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/credger.cr b/credger.cr index e666bd2..48f805d 100644 --- a/credger.cr +++ b/credger.cr @@ -46,6 +46,7 @@ get "/api/ledger/graph_values" do |env| env.response.content_type = "application/json" ledger.graph_values( env.params.query["period"], + "--#{env.params.query["granularity"]}", env.params.query["categories"].split(" ") ).to_json end diff --git a/ledger.cr b/ledger.cr index 1ccea00..f88df1f 100644 --- a/ledger.cr +++ b/ledger.cr @@ -59,13 +59,14 @@ class Ledger end def graph_values( period : String = "", + granularity : String = "", categories : Array(String) = ["Expenses"] ) : Hash( String, Array( NamedTuple( date: String, amount: String, currency: String ) ) ) period = period == "" ? "" : "-p '#{period}'" result = {} of String => Array(NamedTuple(date: String, amount: String, currency: String)) categories.map do |category| result[category] = CSV - .parse( run( "-MAn --exchange '#{CURRENCY}' #{period}", "csv --no-revalued", category ) ) + .parse( run( "-MAn --exchange '#{CURRENCY}' #{period} #{granularity}", "csv --no-revalued", category ) ) .map do |row| { date: row[ 0 ], amount: row[ 5 ], diff --git a/public/ts/components/dashboard.ts b/public/ts/components/dashboard.ts index b390c9c..1be580a 100644 --- a/public/ts/components/dashboard.ts +++ b/public/ts/components/dashboard.ts @@ -3,6 +3,9 @@ app.component('dashboard', controller: ['$filter', 'API', function($filter, API) { let ctrl = this; + ctrl.granularity = "monthly"; + + let is_monthly = () => { return ctrl.granularity == "monthly"; }; ctrl.compute_selected_accounts = () => { ctrl.graphed_accounts = _.chain(ctrl.main_accounts_depths) @@ -25,7 +28,7 @@ app.component('dashboard', }; ctrl.retrieve_graph_values = (categories) => { - API.graph_values("", categories.join(" ")) + API.graph_values("", ctrl.granularity, categories.join(" ")) .then((response) => { ctrl.periods = []; @@ -75,12 +78,12 @@ app.component('dashboard', tooltip: { contentGenerator: function(e) { let format_line = (serie) => { - return ` - - - ${serie.key} - ${serie.value} - + return ` + + +${serie.key} +${serie.value} + `; }; @@ -90,19 +93,19 @@ app.component('dashboard', return series.filter((s) => { return s.value != 0; }); }; -return ` + return `

${e.value}

- - ${prepare_series(e.series).map((s) => { return format_line(s); }).join("")} - - - - - - - - + +${prepare_series(e.series).map((s) => { return format_line(s); }).join("")} + + + + + + + +
Total${e.series.reduce((memo, serie) => { return memo + serie.value; }, 0)}
Total${e.series.reduce((memo, serie) => { return memo + serie.value; }, 0)}
`; } @@ -119,7 +122,7 @@ return ` values: _.chain(response.data[key]) .map((value) => { let date = new Date(value.date); - let period = date.getFullYear() + '-' + (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1); + let period = is_monthly() ? date.getFullYear() + '-' + (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1) : date.getFullYear(); ctrl.periods.push(period); return { @@ -164,10 +167,12 @@ return ` } ], - template: ` + template: `
+ +
  • @@ -184,7 +189,7 @@ return `

- +

diff --git a/public/ts/services/API.ts b/public/ts/services/API.ts index bee22ff..5448d80 100644 --- a/public/ts/services/API.ts +++ b/public/ts/services/API.ts @@ -22,10 +22,11 @@ app.service('API', }); }; - API.graph_values = function(period, categories) { + API.graph_values = function(period, granularity, categories) { return $http.get('/api/ledger/graph_values', { params: { period: period, + granularity: granularity, categories: categories } });