This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
/
totalValueSent.js
193 lines (161 loc) · 5.39 KB
/
totalValueSent.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
var winston = require('winston');
var moment = require('moment');
var ripple = require('ripple-lib');
var async = require('async');
var utils = require('../library/utils');
var transactionVolume = require('../library/metrics/transactionVolume');
var intervals = [
'day',
'week',
'month'
];
/**
* totalValueSent:
*
* total of amounts sent or exchanged from any wallet, either through a payment
* or an "offerCreate" that exercises another offer, for a curated list of
* currency/issuers and XRP, normalized to a specified currency
*
* request :
*
* {
* startTime : (any momentjs-readable date), // optional, defaults to 1 day before end time
* endTime : (any momentjs-readable date), // optional, defaults to now
* exchange : { // optional, defaults to XRP
* currency : (XRP, USD, BTC, etc.),
* issuer : "rAusZ...." // optional, required if currency != XRP
* }
* }
*
* response :
*
* {
* startTime : "2014-03-13T20:39:26+00:00", //period start
* endTime : "2014-03-14T20:39:26+00:00", //period end
* exchange : {
* currency : "USD", //exchange currency
* issuer : "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B" //exchange issuer
* },
* exchangeRate : 0.014301217579817786, //exchange rate
* total : 726824.6504823748, //total value sent
* count : 6040, //number of transactions
* components : [ //list of component currencies
* {
* currency : "USD",
* issuer : "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
* amount : 27606.296227064257,
* count : 51,
* rate : 1,
* convertedAmount : 27606.296227064257
* },
* .
* .
* .
* .
* ]
* }
*
*
curl -H "Content-Type: application/json" -X POST -d '{
"exchange" : {"currency": "USD", "issuer" : "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"}
}' http://localhost:5993/api/total_value_sent
curl -H "Content-Type: application/json" -X POST -d '{
"exchange" : {"currency": "USD", "issuer" : "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"},
"startTime" : "2015-03-20",
"interval" : "day"
}' http://localhost:5993/api/total_value_sent
*/
function totalValueSent(params, callback) {
var viewOpts = {};
var ex = params.exchange || {currency:'XRP'};
var interval;
var startTime;
var rowkey;
if (typeof ex != 'object') {
return callback('invalid exchange currency');
} else if (!ex.currency) {
return callback('exchange currency is required');
} else if (typeof ex.currency != 'string') {
return callback('invalid exchange currency');
} else if (ex.currency.toUpperCase() != "XRP" && !ex.issuer) {
return callback('exchange issuer is required');
} else if (ex.currency == "XRP" && ex.issuer) {
return callback('XRP cannot have an issuer');
}
interval = (params.interval || '').toLowerCase();
startTime = moment.utc(params.startTime);
rowkey = 'transaction_volume';
if (!params.startTime) {
startTime.subtract(1, 'day');
rowkey += '|live';
} else if (!interval || intervals.indexOf(interval) === -1) {
callback('invalid interval');
return;
} else if (!startTime.isValid()) {
return callback('invalid time: ' + params.startTime);
} else {
startTime.startOf(interval === 'week' ? 'isoWeek' : interval);
rowkey += '|' + interval + '|' + utils.formatTime(startTime);
}
//get the row from hbase
hbase.getRow({
table: 'agg_metrics',
rowkey: rowkey
}, function(err, row) {
var options = {
start : moment.utc(startTime),
interval : interval,
ex : ex
};
if (row) {
row.components = JSON.parse(row.components);
row.exchange = JSON.parse(row.exchange);
row.total = parseFloat(row.total);
row.count = parseFloat(row.count);
row.exchangeRate = parseFloat(row.exchangeRate);
handleResponse (options, row, callback);
} else {
callback(err, row);
}
});
function handleResponse (options, row, callback) {
var params;
if (options.ex.currency === 'XRP') {
callback(null, row);
return;
}
row.exchange = options.ex;
//check for final rate within row first
for(var i=0; i<row.components.length; i++) {
if (row.components[i].currency === options.ex.currency &&
row.components[i].issuer === options.ex.issuer) {
//normalize and return
normalize(row.components[i].rate, row, callback);
return;
}
}
utils.getConversion({
currency : options.ex.currency,
issuer : options.ex.issuer,
start : options.start,
end : moment.utc(options.start).add(1, options.interval),
interval : options.interval
}, function (err, rate) {
if (err) {
return callback(err);
}
normalize(rate, row, callback);
});
function normalize (rate, row, callback) {
row.total = 0;
row.components.forEach(function(c, index) {
c.convertedAmount *= rate;
c.rate = c.rate ? rate / c.rate : 0;
row.total += c.convertedAmount;
});
row.exchangeRate = rate;
callback(null, row);
}
}
}
module.exports = totalValueSent;