mirror of
https://github.com/gwenhael-le-moine/credger.git
synced 2024-12-26 09:58:36 +01:00
add granularity control
This commit is contained in:
parent
923957aab4
commit
c41bbb85aa
4 changed files with 31 additions and 23 deletions
|
@ -46,6 +46,7 @@ get "/api/ledger/graph_values" do |env|
|
||||||
env.response.content_type = "application/json"
|
env.response.content_type = "application/json"
|
||||||
|
|
||||||
ledger.graph_values( env.params.query["period"],
|
ledger.graph_values( env.params.query["period"],
|
||||||
|
"--#{env.params.query["granularity"]}",
|
||||||
env.params.query["categories"].split(" ") ).to_json
|
env.params.query["categories"].split(" ") ).to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -59,13 +59,14 @@ class Ledger
|
||||||
end
|
end
|
||||||
|
|
||||||
def graph_values( period : String = "",
|
def graph_values( period : String = "",
|
||||||
|
granularity : String = "",
|
||||||
categories : Array(String) = ["Expenses"] ) : Hash( String, Array( NamedTuple( date: String, amount: String, currency: String ) ) )
|
categories : Array(String) = ["Expenses"] ) : Hash( String, Array( NamedTuple( date: String, amount: String, currency: String ) ) )
|
||||||
period = period == "" ? "" : "-p '#{period}'"
|
period = period == "" ? "" : "-p '#{period}'"
|
||||||
|
|
||||||
result = {} of String => Array(NamedTuple(date: String, amount: String, currency: String))
|
result = {} of String => Array(NamedTuple(date: String, amount: String, currency: String))
|
||||||
categories.map do |category|
|
categories.map do |category|
|
||||||
result[category] = CSV
|
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|
|
.map do |row|
|
||||||
{ date: row[ 0 ],
|
{ date: row[ 0 ],
|
||||||
amount: row[ 5 ],
|
amount: row[ 5 ],
|
||||||
|
|
|
@ -3,6 +3,9 @@ app.component('dashboard',
|
||||||
controller: ['$filter', 'API',
|
controller: ['$filter', 'API',
|
||||||
function($filter, API) {
|
function($filter, API) {
|
||||||
let ctrl = this;
|
let ctrl = this;
|
||||||
|
ctrl.granularity = "monthly";
|
||||||
|
|
||||||
|
let is_monthly = () => { return ctrl.granularity == "monthly"; };
|
||||||
|
|
||||||
ctrl.compute_selected_accounts = () => {
|
ctrl.compute_selected_accounts = () => {
|
||||||
ctrl.graphed_accounts = _.chain(ctrl.main_accounts_depths)
|
ctrl.graphed_accounts = _.chain(ctrl.main_accounts_depths)
|
||||||
|
@ -25,7 +28,7 @@ app.component('dashboard',
|
||||||
};
|
};
|
||||||
|
|
||||||
ctrl.retrieve_graph_values = (categories) => {
|
ctrl.retrieve_graph_values = (categories) => {
|
||||||
API.graph_values("", categories.join(" "))
|
API.graph_values("", ctrl.granularity, categories.join(" "))
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
ctrl.periods = [];
|
ctrl.periods = [];
|
||||||
|
|
||||||
|
@ -75,12 +78,12 @@ app.component('dashboard',
|
||||||
tooltip: {
|
tooltip: {
|
||||||
contentGenerator: function(e) {
|
contentGenerator: function(e) {
|
||||||
let format_line = (serie) => {
|
let format_line = (serie) => {
|
||||||
return `
|
return `
|
||||||
<tr>
|
<tr>
|
||||||
<td style="background-color: ${serie.color}"> </td>
|
<td style="background-color: ${serie.color}"> </td>
|
||||||
<td>${serie.key}</td>
|
<td>${serie.key}</td>
|
||||||
<td style="text-align: right; font-weight: bold;">${serie.value}</td>
|
<td style="text-align: right; font-weight: bold;">${serie.value}</td>
|
||||||
</tr>
|
</tr>
|
||||||
`;
|
`;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -90,19 +93,19 @@ app.component('dashboard',
|
||||||
return series.filter((s) => { return s.value != 0; });
|
return series.filter((s) => { return s.value != 0; });
|
||||||
};
|
};
|
||||||
|
|
||||||
return `
|
return `
|
||||||
<h2>${e.value}</h2>
|
<h2>${e.value}</h2>
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
${prepare_series(e.series).map((s) => { return format_line(s); }).join("")}
|
${prepare_series(e.series).map((s) => { return format_line(s); }).join("")}
|
||||||
</tbody>
|
</tbody>
|
||||||
<tfoot>
|
<tfoot>
|
||||||
<tr>
|
<tr>
|
||||||
<td> </td>
|
<td> </td>
|
||||||
<td style="text-align: right; text-decoration: underline; font-weight: bold;">Total</td>
|
<td style="text-align: right; text-decoration: underline; font-weight: bold;">Total</td>
|
||||||
<td style="text-align: right; font-weight: bold;">${e.series.reduce((memo, serie) => { return memo + serie.value; }, 0)}</td>
|
<td style="text-align: right; font-weight: bold;">${e.series.reduce((memo, serie) => { return memo + serie.value; }, 0)}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tfoot>
|
</tfoot>
|
||||||
</table>
|
</table>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
@ -119,7 +122,7 @@ return `
|
||||||
values: _.chain(response.data[key])
|
values: _.chain(response.data[key])
|
||||||
.map((value) => {
|
.map((value) => {
|
||||||
let date = new Date(value.date);
|
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);
|
ctrl.periods.push(period);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -164,10 +167,12 @@ return `
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
||||||
template: `
|
template: `
|
||||||
<div class="dashboard">
|
<div class="dashboard">
|
||||||
<div class="global-graph" style="height: 450px;">
|
<div class="global-graph" style="height: 450px;">
|
||||||
<div class="accounts" style="width: 20%; height: 100%; float: left;">
|
<div class="accounts" style="width: 20%; height: 100%; float: left;">
|
||||||
|
<label><input type="radio" ng:model="$ctrl.granularity" value="monthly" name="monthly" ng:change="$ctrl.compute_selected_accounts()" />monthly</label>
|
||||||
|
<label><input type="radio" ng:model="$ctrl.granularity" value="yearly" name="yearly" ng:change="$ctrl.compute_selected_accounts()" />yearly</label>
|
||||||
<ul>
|
<ul>
|
||||||
<li ng:repeat="account in $ctrl.main_accounts_depths">
|
<li ng:repeat="account in $ctrl.main_accounts_depths">
|
||||||
<label>{{account.name}} depth</label>
|
<label>{{account.name}} depth</label>
|
||||||
|
@ -184,7 +189,7 @@ return `
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h1 style="text-align: center;">
|
<h1 style="text-align: center;">
|
||||||
<select ng:options="p as p | amDateFormat:'MMMM YYYY' for p in $ctrl.periods" ng:model="$ctrl.period"></select>
|
<select ng:options="p as p for p in $ctrl.periods" ng:model="$ctrl.period"></select>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<bucket categories="'Expenses Income Equity Liabilities'" period="$ctrl.period"></bucket>
|
<bucket categories="'Expenses Income Equity Liabilities'" period="$ctrl.period"></bucket>
|
||||||
|
|
|
@ -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', {
|
return $http.get('/api/ledger/graph_values', {
|
||||||
params: {
|
params: {
|
||||||
period: period,
|
period: period,
|
||||||
|
granularity: granularity,
|
||||||
categories: categories
|
categories: categories
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue