-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccountsCreated.js
220 lines (177 loc) · 6.06 KB
/
accountsCreated.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
'use strict';
var moment = require('moment');
var _ = require('lodash');
var tools = require('../utils');
/**
* accountsCreated returns the number of accounts created per time increment
*
* max returned results : 500;
*
* expects params to have:
* {
* startTime: (any momentjs-readable date), // optional, defaults to 30 days before endTime
* endTime: (any momentjs-readable date), // optional, defaults to now
* timeIncrement : (any of the following: "all", "year", "month", "day", "hour", "minute", "second") // optional, defaults to "day"
* descending: true/false, // optional, defaults to true
* reduce: true/false // optional, defaults to false, ignored if timeIncrement is set. false returns individual transactions
* limit: limit the number of responses, ignored if time increment is set or reduce is true
* offset: offset by n transactions for pagination
* format: 'json', 'csv'
* }
*
curl -H "Content-Type: application/json" -X POST -d '{
"reduce" : false,
"format" : "json",
"limit" : 2
}' http://localhost:5993/api/accountsCreated
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "Dec 30, 2012 10:00 am",
"endTime" : "Jan 30, 2014 10:00 am",
"timeIncrement": "day"
}' http://localhost:5993/api/accountsCreated
curl -o accounts.csv -H "Content-Type: application/json" -X POST -d '{
"reduce" : false,
"format" : "csv"
}' http://localhost:5993/api/accountsCreated
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "Mar 9, 2014 10:00 am",
"endTime" : "Mar 10, 2014 10:00 am",
"reduce" : false,
"format" : "json",
"limit" :20
}' http://localhost:5993/api/accountsCreated
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "Mar 9, 2014 10:00 am",
"endTime" : "Mar 10, 2014 10:00 am",
"descending" : true,
"reduce" : false,
"limit" : 20,
"offset" : 20,
"format" : "csv"
}' http://localhost:5993/api/accountsCreated
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "Mar 9, 2014 10:00 am",
"endTime" : "Mar 10, 2014 10:00 am",
"format" : "json"
}' http://localhost:5993/api/accountsCreated
*/
function accountsCreated(params, callback) {
var viewOpts = {};
var limit = params.limit ? parseInt(params.limit, 10) : 0;
var maxLimit = 500;
var intervals = ['hour', 'day', 'week'];
if (!limit || limit > maxLimit) limit = maxLimit;
if (params.offset) {
callback('offset is no longer supported. use marker instead');
return;
}
//Parse start and end times
var range = tools.parseTimeRange(params.startTime, params.endTime, params.descending);
if (range.error) return callback(range.error);
if (!range.end) range.end = moment.utc();
if (!range.start) range.start = moment.utc(range.end).subtract(30, "days");
if (params.timeIncrement === 'all') {
params.reduce = true;
params.timeIncrement = null;
} else if (params.timeIncrement) {
params.reduce = false; //using a time increment will reduce already
params.timeIncrement = params.timeIncrement.toLowerCase();
if (intervals.indexOf(params.timeIncrement) === -1) {
callback('invalid time increment - use: ' + intervals.join(', '));
return;
}
}
var options = {
descending: (/false/i).test(params.descending) ? false : true,
reduce: (/true/i).test(params.reduce) ? true : false,
limit: limit || 500,
start: range.start,
end: range.end,
interval: params.timeIncrement === 'all' ? undefined : params.timeIncrement
};
hbase.getAccounts(options, function(err, resp) {
handleResponse(resp || {});
});
/**
* handleResponse - format the data according to the requirements
* of the request and return it to the caller.
*
*/
function handleResponse (resp) {
var rows = resp.rows || [];
if (params.format === 'json') {
var response = {
startTime : range.start.format(),
endTime : range.end.format(),
timeIncrement : params.timeIncrement,
marker : resp.marker,
total : 0
}
// aggregated rows
if (params.timeIncrement) {
response.results = [];
rows.forEach(function(row){
response.total += row.count;
response.results.push({
time : row.date,
count : row.count
});
});
// individual rows
} else if (!params.reduce) {
response.total = rows ? rows.length : 0;
response.results = [];
rows.forEach(function(row){
response.results.push({
time : row.executed_time,
account : row.account,
txHash : row.tx_hash,
ledgerIndex : row.ledger_index
});
});
// count only
} else {
response.total = rows && rows[0] ? rows[0] : 0;
}
callback(null, response);
} else {
var data = [];
// aggregated rows
if (params.timeIncrement) {
data.push(['time', 'count']);
rows.forEach(function(row) {
data.push([
row.date || range.start.format(),
row.count || row
]);
});
// individual rows
} else if (!params.reduce) {
data.push(['time', 'account', 'txHash', 'ledgerIndex']);
rows.forEach(function(row) {
data.push([
row.executed_time,
row.account,
row.tx_hash,
row.ledger_index
]);
});
// count only
} else {
callback(null, rows && rows[0] ? rows[0] : 0);
return;
}
if (params.format === 'csv') {
var csvStr = _.map(data, function(row){
return row.join(', ');
}).join('\n');
// provide output as CSV
return callback(null, csvStr);
} else {
//no format or incorrect format specified
return callback(null, data);
}
}
}
}
module.exports = accountsCreated;