-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregation.js
199 lines (180 loc) · 6.67 KB
/
aggregation.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
const db = require('ocore/db.js');
const storage = require('ocore/storage.js');
const rates = require('ocore/network.js').exchangeRates;
const wallet = require('ocore/wallet.js');
const CronJob = require('cron').CronJob;
const { assetsMetadata } = require('./assets');
const { getFromKV, storeIntoKV, kv_key } = require('./kv');
let aggregationInProgress = {};
async function start() {
console.log('Starting aggregation');
await updateAssetMetadata();
await aggregatePeriod(60);
setInterval(() => {aggregatePeriod(60)}, 1000 * 60);
await aggregatePeriod(60 * 24);
setInterval(() => {aggregatePeriod(60 * 24)}, 1000 * 60 * 10 + 30 * 1000); // offset by 30 seconds to not interfere with hourly queries
await snapshotBalances();
(new CronJob('1 * * * * *', snapshotBalances)).start();
}
// timeframe is in minutes
async function aggregatePeriod(timeframe) {
if (aggregationInProgress[timeframe]) return;
aggregationInProgress[timeframe] = true;
let lastResponseId = await getFromKV(`${kv_key}${timeframe}`) || 0;
console.log(`aggregatePeriod ${timeframe} last reponse id ${lastResponseId}`);
// we use two distinct queries as they have different join conditions
const inputRows = await db.query(`SELECT
MAX(aa_response_id) AS last_response_id,
units.timestamp / 60 / ? AS period,
aa_address,
total_inputs.asset,
SUM(total_inputs.amount) AS amount_in,
0 AS amount_out,
COUNT(1) AS triggers_count,
SUM(bounced) AS bounced_count,
COUNT(DISTINCT trigger_address) AS num_users
FROM aa_responses
JOIN units ON aa_responses.trigger_unit=units.unit
JOIN outputs AS total_inputs ON total_inputs.unit=aa_responses.trigger_unit AND total_inputs.address=aa_address
WHERE aa_response_id > ?
GROUP BY units.timestamp / 60 / ?, aa_address, total_inputs.asset
ORDER BY period ASC`, [timeframe, lastResponseId, timeframe]);
const outputRows = await db.query(`SELECT
MAX(aa_response_id) AS last_response_id,
units.timestamp / 60 / ? AS period,
aa_address,
total_outputs.asset,
0 AS amount_in,
SUM(total_outputs.amount) AS amount_out,
COUNT(1) AS triggers_count,
SUM(bounced) AS bounced_count,
COUNT(DISTINCT trigger_address) AS num_users
FROM aa_responses
JOIN units ON aa_responses.trigger_unit=units.unit
JOIN outputs AS total_outputs ON total_outputs.unit=aa_responses.response_unit AND total_outputs.address!=aa_address
WHERE aa_response_id > ?
GROUP BY units.timestamp / 60 / ?, aa_address, total_outputs.asset
ORDER BY period ASC`, [timeframe, lastResponseId, timeframe]);
// now merge two results
let rowsMap = {};
const _key = row => '' + row.period + row.aa_address + row.asset;
for (let row of inputRows) {
rowsMap[_key(row)] = row;
}
for (let row of outputRows) {
let iRow = rowsMap[_key(row)];
if (iRow == null) {
rowsMap[_key(row)] = row;
continue;
}
iRow.last_response_id = Math.max(iRow.last_response_id, row.last_response_id);
iRow.amount_out = row.amount_out;
}
let rows = Object.values(rowsMap).sort((a,b) => (a.period > b.period) ? 1 : ((b.period > a.period) ? -1 : 0));
// request missing asset infos
let assets = [...new Set(rows.map(r => r.asset))].filter(a => !assetsMetadata.hasOwnProperty(a) && a != null);
await updateAssetMetadata(assets);
// aggregate
let lastPeriod = 0;
let periodRows = [];
for (const row of rows) {
if (lastPeriod > 0 && row.period > lastPeriod) { // close this timeframe
const conn = await db.takeConnectionFromPool();
await conn.query(`BEGIN`);
for (const row of periodRows) {
const usd_amount_in = getUSDAmount(row.asset, row.amount_in);
const usd_amount_out = getUSDAmount(row.asset, row.amount_out);
const tableName = `aa_stats_${timeframe === 60 ? 'hourly' : 'daily'}`;
const periodColumnName = timeframe === 60 ? 'hour' : 'day';
await conn.query(`
INSERT INTO ${tableName} (
${periodColumnName},
period_start_date,
address,
asset,
amount_in,
amount_out,
usd_amount_in,
usd_amount_out,
triggers_count,
bounced_count,
num_users
) VALUES (?, ${db.getFromUnixTime('?')}, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
lastPeriod,
lastPeriod * 3600 * (timeframe === 60 ? 1 : 24),
row.aa_address,
row.asset,
row.amount_in,
row.amount_out,
usd_amount_in,
usd_amount_out,
row.triggers_count,
row.bounced_count,
row.num_users
]);
}
await conn.query(`COMMIT`);
conn.release();
periodRows = [];
await storeIntoKV(`${kv_key}${timeframe}`, lastResponseId);
console.log(`Aggregated ${timeframe} minutes: ${lastPeriod}, lastResponseId: ${lastResponseId}`);
}
lastPeriod = row.period;
lastResponseId = Math.max(lastResponseId, row.last_response_id);
periodRows.push(row);
}
aggregationInProgress[timeframe] = false;
}
async function snapshotBalances() {
const lastHour = (await db.query(`SELECT MAX(hour) AS hour FROM aa_balances_hourly`))[0].hour || 0;
const currentHour = Math.floor(Date.now() / 1000 / 60 / 60);
if (currentHour <= lastHour)
return;
console.log('starting snapshotting of balances', currentHour, lastHour);
const conn = await db.takeConnectionFromPool();
await conn.query(`BEGIN`);
if (Object.keys(assetsMetadata).length === 0)
throw Error(`no asset metadata yet`);
const rows = await conn.query(`SELECT address, asset, balance FROM aa_balances`);
for (const row of rows) {
row.asset = row.asset === "base" ? null : row.asset;
if (row.asset) {
const { cap, definer_address } = await storage.readAssetInfo(conn, row.asset);
if (!cap && definer_address === row.address) // skip balances in assets this same AA has defined
continue;
}
row.usd_balance = getUSDAmount(row.asset, row.balance);
await conn.query(`INSERT INTO aa_balances_hourly (hour, date, address, asset, balance, usd_balance) VALUES (?, ${db.getFromUnixTime('?')}, ?, ?, ?, ?)`,
[currentHour, currentHour * 3600, row.address, row.asset, row.balance, row.usd_balance]);
}
await conn.query(`COMMIT`);
conn.release();
console.log(`Snapshot of aa_balances done, hour: ${currentHour}`);
}
async function updateAssetMetadata(assets) {
const aMs = await new Promise((resolve, reject) => {
let aMs;
wallet.readAssetMetadata(assets, (ams) => {
aMs = ams;
}, () => {
resolve(aMs);
});
});
Object.assign(assetsMetadata, aMs);
}
function getUSDAmount(asset, amount) {
let rate;
if (asset) {
if (rates[`${asset}_USD`]) {
rate = rates[`${asset}_USD`];
if (assetsMetadata[asset] && assetsMetadata[asset].decimals > 0)
rate /= Math.pow(10, assetsMetadata[asset].decimals);
}
} else if (rates['RECH_USD']) {
rate = rates['RECH_USD'] / 1e9;
}
if (rate)
return +(amount * rate).toFixed(2);
return null;
}
exports.start = start;