forked from stellar/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (65 loc) · 2.13 KB
/
app.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
var express = require('express');
var stellarSdk = require('stellar-sdk');
var moment = require('moment');
var redis = require("redis"),
redisClient = redis.createClient(process.env.REDIS_URL);
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static('dist'));
app.get('/ledgers/public', function(req, res) {
var day = moment();
var response = [];
var multi = redisClient.multi();
var days = 30;
for (var i = 0; i < days; i++) {
response.push({date: day.format("DD")});
multi.hmget('ledger_public'+day.format("YYYY-MM-DD"), 'transaction_count', 'operation_count');
day = day.subtract(1, 'days')
}
multi.exec(function (err, redisRes) {
if (err) {
res.sendStatus(500);
console.error(err);
res.error("Error");
return;
}
for (var i = 0; i < days; i++) {
response[i].transaction_count = parseInt(redisRes[i][0]);
response[i].operation_count = parseInt(redisRes[i][1]);
}
res.send(response);
});
});
// Stream ledgers - get last paging_token
redisClient.get('paging_token', function(err, pagingToken) {
if (err) {
console.error(err);
return
}
if (!pagingToken) {
pagingToken = '37188317929799680'; // Ledger 8658580 2017-01-18T16:25:54Z
}
var horizon = new stellarSdk.Server('https://horizon.stellar.org');
horizon.ledgers().cursor(pagingToken).stream({
onmessage: function(ledger) {
var date = moment(ledger.closed_at).format("YYYY-MM-DD");
var key = "ledger_public"+date;
var multi = redisClient.multi();
multi.hincrby(key, "transaction_count", ledger.transaction_count);
multi.hincrby(key, "operation_count", ledger.operation_count);
// Expire the key after 32 days
multi.expire(key, 60*60*24*32);
multi.set("paging_token", ledger.paging_token);
multi.exec(function (err, res) {
if (err) {
console.error(err);
return;
}
console.log("Added Ledger:"+ledger.sequence+" "+ledger.closed_at+" "+res);
});
}
});
});
app.listen(app.get('port'), function() {
console.log('Listening on port', app.get('port'));
});