-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledgersClosed.js
237 lines (194 loc) · 6.1 KB
/
ledgersClosed.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
232
233
234
235
236
237
var winston = require('winston'),
moment = require('moment'),
_ = require('lodash'),
tools = require('../utils');
/*
* ledgersClosed:
*
*
* request:
*
* {
* 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 ledgers for pagination
* format : 'json', 'csv' // optional
* }
*
*
* response (default):
*
* [
* ["time", "count"],
* ["2014-03-10T00:00:00+00:00",7654],
* ["2014-03-11T00:00:00+00:00",18323],
* ...
* ...
* ...
* ]
*
*
* response (json):
*
* {
"startTime": "1970-01-01T00:00:00+00:00",
"endTime": "2014-04-05T17:05:31+00:00",
"timeIncrement": "day",
"total": 292022,
"results": [
{
"time": "2014-03-10T00:00:00+00:00",
"count": 7654
},
{
"time": "2014-03-11T00:00:00+00:00",
"count": 18323
},
...
...
...
]
}
*
*
* response (reduce = false):
*
* [
* ["time", "ledgerIndex"],
* ["2014-04-01T18:39:30+00:00",5842321],
* ["2014-04-01T18:39:30+00:00",5842322],
* ...
* ...
* ...
* ]
*
*
*
*
*
*
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "Apr 1, 2014 10:00 am",
"endTime" : "Apr 10, 2014 10:00 am",
"reduce" : false
}' http://localhost:5993/api/ledgersClosed
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "Apr 1, 2014 10:00 am",
"endTime" : "Apr 10, 2014 10:00 am",
"timeIncrement" : "day"
}' http://localhost:5993/api/ledgersClosed
curl -H "Content-Type: application/json" -X POST -d '{
"startTime" : "Apr 1, 2014 10:00 am",
"endTime" : "Apr 1, 2014 11:00 am",
"reduce" : false,
"limit" : 10,
"descending" : true,
"offset" : 10
}' http://localhost:5993/api/ledgersClosed
curl -H "Content-Type: application/json" -X POST -d '{
"timeIncrement" : "day"
}' http://localhost:5993/api/ledgersClosed
*
*/
function ledgersClosed(params, callback) {
var viewOpts = {},
limit = params.limit ? parseInt(params.limit, 10) : 0,
offset = params.offset ? parseInt(params.offset, 10) : 0,
maxLimit = 500,
intervalCount;
if (!limit || limit>maxLimit) limit = maxLimit;
//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");
// set startkey and endkey for couchdb query
viewOpts.startkey = range.start.toArray().slice(0,6);
viewOpts.endkey = range.end.toArray().slice(0,6);
if (params.descending) viewOpts.descending = true;
//parse time increment and time multiple
var results = tools.parseTimeIncrement(params.timeIncrement);
//set reduce option only if its false
if (results.group_level) viewOpts.group_level = results.group_level + 1;
else if (params.reduce === false) viewOpts.reduce = false;
if (viewOpts.reduce===false) {
if (limit && !isNaN(limit)) viewOpts.limit = limit;
if (offset && !isNaN(offset)) viewOpts.skip = offset;
}
if (results.group !== false) {
intervalCount = tools.countIntervals(range.start, range.end, results.name);
if (intervalCount>maxLimit) {
return callback("Please specify a smaller time range or larger interval");
}
}
viewOpts.stale = "ok"; //dont wait for updates
db.view('ledgersClosed', 'v1', viewOpts, function(error, couchRes){
if (error) return callback ('CouchDB - ' + error);
handleResponse(couchRes.rows);
});
function handleResponse (rows) {
if (params.format === 'json') {
var apiRes = {};
apiRes.startTime = range.start.format();
apiRes.endTime = range.end.format();
apiRes.timeIncrement = params.timeIncrement;
apiRes.total = 0;
if (viewOpts.reduce === false) {
apiRes.total = rows.length;
apiRes.results = [];
rows.forEach(function(row){
apiRes.results.push({
time : moment.utc(row.key).format(),
ledgerIndex : parseInt(row.id, 10)
});
});
} else if (params.timeIncrement) {
apiRes.results = [];
rows.forEach(function(row){
apiRes.total += row.value;
apiRes.results.push({
time : moment.utc(row.key).format(),
count : row.value
});
});
} else {
apiRes.total = rows[0] ? rows[0].value : 0;
}
return callback(null, apiRes);
} else {
var data = [], keys = {}, nKeys = 0;
if (viewOpts.reduce === false) {
data.push(["time","ledgerIndex"]);
rows.forEach(function(row){
data.push([
moment.utc(row.key).format(),
parseInt(row.id, 10)
]);
});
} else if (params.timeIncrement) {
data.push(["time","count"]);
for (var j=0; j<rows.length; j++) {
data.push([
moment.utc(rows[j].key || range.start).format(),
rows[j].value
]);
}
} else return callback(null, rows[0] ? rows[0].value.toString() : 0);
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 = ledgersClosed;