forked from rotki/rotki
/
balances_table.js
231 lines (205 loc) · 7.9 KB
/
balances_table.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var settings = require("./settings.js")();
require("./utils.js")();
let TOTAL_BALANCES_TABLE = null;
let SAVED_BALANCES = {};
function create_full_data() {
let inter_data = {};
for (var location in SAVED_BALANCES) {
if (SAVED_BALANCES.hasOwnProperty(location)) {
// for every location's balances
let balances = SAVED_BALANCES[location];
for (let asset in balances) {
if(balances.hasOwnProperty(asset)) {
let old_amount = 0;
let old_value = 0;
if (inter_data.hasOwnProperty(asset)) {
old_amount = inter_data[asset]['amount'];
old_value = inter_data[asset]['usd_value'];
}
let amount = old_amount + balances[asset]['amount'];
let value = old_value + balances[asset]['usd_value'];
inter_data[asset] = {'amount': amount, 'usd_value': value};
}
}
}
}
// now let's get the total usd
let total_usd = 0;
for (let asset in inter_data) {
if (inter_data.hasOwnProperty(asset)) {
total_usd += inter_data[asset]['usd_value'];
}
}
let full_data = [];
// we should have all balances by now
for (let asset in inter_data) {
if (inter_data.hasOwnProperty(asset)) {
let amount = inter_data[asset]['amount'];
amount = amount.toFixed(settings.floating_precision);
let value = inter_data[asset]['usd_value'];
let percentage = value / total_usd;
percentage = (percentage * 100).toFixed(settings.floating_precision);
value = value.toFixed(settings.floating_precision);
full_data.push({
'asset': asset,
'amount': amount,
'usd_value': value,
'percentage': percentage
});
}
}
return full_data;
}
function total_balances_get() {
return SAVED_BALANCES;
}
function total_table_recreate() {
// only create the total table if we are in the dashboard
if (settings.current_location != 'index') {
return;
}
let full_data = create_full_data();
if (!TOTAL_BALANCES_TABLE) {
init_balances_table(full_data);
} else {
if (!$('#table_balances_total_body').length) {
// if table has already been initialized but no longer existing on the page
TOTAL_BALANCES_TABLE.destroy(false);
init_balances_table(full_data);
} else {
// update the already existing table
TOTAL_BALANCES_TABLE.clear();
TOTAL_BALANCES_TABLE.rows.add(full_data);
balance_table_init_callback.call(TOTAL_BALANCES_TABLE);
$(TOTAL_BALANCES_TABLE.column(2).header()).text(settings.main_currency.ticker_symbol + ' value');
TOTAL_BALANCES_TABLE.draw();
}
}
update_last_balance_save();
}
function total_table_add_balances(location, query_result) {
let data = {};
for (var asset in query_result) {
if(query_result.hasOwnProperty(asset)) {
if (asset == 'location' || asset == 'net_usd') {
continue;
}
let amount = parseFloat(query_result[asset]['amount']);
let value = parseFloat(query_result[asset]['usd_value']);
data[asset] = {'amount': amount, 'usd_value': value};
}
}
SAVED_BALANCES[location] = data;
total_table_recreate();
}
function balance_table_init_callback(settings, json) {
var api;
if (!settings) { // called manually by us via .call() through a DT instance
api = this;
} else { // called from inside initComplete
api = this.api();
}
if (api.data().count()) {
api.column(2).every(function(){
var column = this;
var sum = column
.data()
.reduce(function (a, b) {
a = parseFloat(a);
if(isNaN(a)){ a = 0; }
b = parseFloat(b);
if(isNaN(b)){ b = 0; }
return a + b;
});
sum = format_currency_value(sum);
$(column.footer()).html('Total Sum: ' + sum);
});
}
}
function add_balances_table_html() {
var str = '<div class="row"><div class="col-lg-12"><h1 class=page-header">All Balances</h1></div></div>';
str += '<div class="list-group"><div class="list-group-item"><i class="fa fa-save fa-fw"></i> Last Time Balances were saved:<span id="last_balance_save_field" class="pull-right small">Never</span></div></div>';
str += '<div class="row"><table id="table_balances_total"><thead><tr><th>Asset</th><th>Amount</th><th>USD Value</th><th>% of net value</th></tr/></thead><tfoot><tr><th></th><th></th><th></th><th></th></tr></tfoot><tbody id="table_balances_total_body"></tbody></table></div>';
$(str).appendTo($('#dashboard-contents'));
}
function update_last_balance_save() {
let str_value = 'Never';
if (settings.last_balance_save != 0) {
str_value = timestamp_to_date(settings.last_balance_save);
}
$('#last_balance_save_field').html(str_value);
}
function init_balances_table(data) {
add_balances_table_html();
TOTAL_BALANCES_TABLE = $('#table_balances_total').DataTable({
"data": data,
"columns": [
{
"title": 'asset',
"data": 'asset',
"render": function (data, type, row) {
return format_asset_title_for_ui(row['asset']);
}
},
{"data": "amount"},
{
"title": settings.main_currency.ticker_symbol + ' value',
"data": 'usd_value',
"render": function (data, type, row) {
return format_currency_value(data, row['asset'], row['amount']);
}
},
{"data": "percentage"}
],
'initComplete': balance_table_init_callback,
"order": [[3, 'desc']]
});
// also save the dashboard page
settings.page_index = $('#page-wrapper').html();
}
function reload_balance_table_if_existing() {
if (TOTAL_BALANCES_TABLE) {
TOTAL_BALANCES_TABLE.rows().invalidate();
balance_table_init_callback.call(TOTAL_BALANCES_TABLE);
$(TOTAL_BALANCES_TABLE.column(2).header()).text(settings.main_currency.ticker_symbol + ' value');
TOTAL_BALANCES_TABLE.draw();
}
}
function get_total_asssets_value(asset_dict) {
var value = 0;
for (var asset in asset_dict) {
if (asset_dict.hasOwnProperty(asset)) {
value += parseFloat(asset_dict[asset]['usd_value']);
}
}
return value;
}
function* iterate_saved_balances() {
let saved_balances = total_balances_get();
for (var location in saved_balances) {
if (saved_balances.hasOwnProperty(location)) {
let total = get_total_asssets_value(saved_balances[location]);
if (settings.EXCHANGES.indexOf(location) >= 0) {
yield [location, total, null];
} else {
let icon;
if (location == 'blockchain') {
icon = 'fa-hdd-o';
} else if (location == 'banks') {
icon = 'fa-university';
} else {
throw 'Invalid location at dashboard box from saved balance creation';
}
yield [location, total, icon];
}
}
}
}
module.exports = function() {
this.total_balances_get = total_balances_get;
this.total_table_recreate = total_table_recreate;
this.total_table_add_balances = total_table_add_balances;
this.reload_balance_table_if_existing = reload_balance_table_if_existing;
this.get_total_asssets_value = get_total_asssets_value;
this.iterate_saved_balances = iterate_saved_balances;
};