-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_calculateDailyMetrics.js
369 lines (335 loc) · 10.1 KB
/
function_calculateDailyMetrics.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// TO DO:
// - make sure first and last block are correct via RPC calls
function validateInput(params) {
// Check required parameters
if (!params.user_data?.date || !params.user_data?.chain) {
return {
error: "Missing required parameters: date and chain must be provided",
};
}
// Validate date format
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
if (!dateRegex.test(params.user_data.date)) {
return {
error: "Invalid date format. Expected YYYY-MM-DD",
date: params.user_data.date,
};
}
// Validate chain
if (
typeof params.user_data.chain !== "string" ||
!params.user_data.chain.trim()
) {
return {
error: "Invalid chain parameter. Expected a non-empty string",
chain: params.user_data.chain,
};
}
// Return validated config
return {
success: true,
config: {
simulateOnly: params.user_data.simulateOnly ?? false,
cleanupEnabled: params.user_data.cleanup ?? true,
date: params.user_data.date,
chain: params.user_data.chain,
},
};
}
async function main(params) {
// Validate input
const validation = validateInput(params);
if (validation.error) {
return validation;
}
const config = validation.config;
// Setup keys
const prefix = `MA_${config.chain.toUpperCase()}_`;
const keys = {
dailyMetrics: (date) => `${prefix}daily-metrics_${date}`,
dailyBlocks: (date) => `${prefix}daily-blocks_${date}`,
dailyAddresses: (date) => `${prefix}daily-active-addresses_${date}`,
blockMetrics: (blockNum) => `${prefix}block-metrics_${blockNum}`,
};
// Check if daily metrics already exist
try {
const existingMetrics = await qnLib.qnGetSet(
keys.dailyMetrics(config.date)
);
if (existingMetrics) {
try {
const parsedMetrics = JSON.parse(existingMetrics);
return {
error: `Daily metrics already exist`,
existingMetrics: parsedMetrics,
chain: config.chain,
date: config.date,
};
} catch (parseError) {
return {
error: `Invalid JSON format in existing metrics: ${parseError.message}`,
chain: config.chain,
date: config.date,
rawMetrics: existingMetrics,
};
}
}
} catch (error) {
return {
error: `Failed to check for existing daily metrics: ${error.message}`,
chain: config.chain,
date: config.date,
};
}
// Fetch and validate block numbers
let blockNumbers;
try {
blockNumbers = await qnLib.qnGetList(keys.dailyBlocks(config.date));
if (!Array.isArray(blockNumbers) || blockNumbers.length === 0) {
return {
error: `No blocks found`,
chain: config.chain,
date: config.date,
};
}
blockNumbers = blockNumbers.map(Number);
} catch (error) {
return {
error: `Failed to fetch block numbers: ${error.message}`,
chain: config.chain,
date: config.date,
};
}
// Calculate metrics (no side effects)
let dailyMetrics;
try {
dailyMetrics = await calculateDailyMetrics(config.date, blockNumbers, keys);
// Check for missing blocks
if (dailyMetrics.missingBlocks) {
return {
error: "Cannot proceed - missing block metrics",
chain: config.chain,
date: config.date,
totalMissingBlocks: dailyMetrics.totalMissing,
missingBlocksSample: dailyMetrics.sampleMissing,
message: `Missing ${dailyMetrics.totalMissing} blocks`,
};
}
if (!dailyMetrics?.metrics || !dailyMetrics?.metadata) {
return {
error: "Failed to calculate daily metrics",
chain: config.chain,
date: config.date,
};
}
} catch (error) {
return {
error: `Metrics calculation failed: ${error.message}`,
chain: config.chain,
date: config.date,
};
}
// Handle storage and cleanup
if (config.simulateOnly) {
return {
status: "success",
dailyMetricsWritten: false,
cleanupPerformed: false,
date: config.date,
chain: config.chain,
message: "Simulation complete",
data: dailyMetrics,
};
}
try {
// Store metrics
await qnLib.qnAddSet(
keys.dailyMetrics(config.date),
JSON.stringify(dailyMetrics)
);
// Perform cleanup if enabled
if (config.cleanupEnabled) {
await performCleanup(config.date, blockNumbers, keys);
return {
status: "success",
date: config.date,
chain: config.chain,
dailyMetricsWritten: true,
cleanupPerformed: true,
message: "Metrics stored and cleanup completed",
data: dailyMetrics,
};
}
return {
status: "success",
date: config.date,
chain: config.chain,
dailyMetricsWritten: true,
cleanupPerformed: false,
message: "Metrics stored successfully",
data: dailyMetrics,
};
} catch (error) {
return {
error: `Storage/cleanup failed: ${error.message}`,
date: config.date,
chain: config.chain,
data: dailyMetrics,
};
}
}
async function calculateDailyMetrics(date, blockNumbers, keys) {
let numTransactions = 0;
let totalFees = 0;
let numContractDeployments = 0;
let firstBlock = null;
let lastBlock = null;
let firstBlockTimestamp = null;
let lastBlockTimestamp = null;
let numProcessedBlocks = 0;
let failedBlocks = [];
let sequenceErrors = [];
// Sort blocks first
blockNumbers.sort((a, b) => a - b);
// Check sequence before processing
for (let i = 1; i < blockNumbers.length; i++) {
const expected = blockNumbers[i - 1] + 1;
const actual = blockNumbers[i];
if (actual !== expected) {
sequenceErrors.push({
gap: {
start: blockNumbers[i - 1],
end: blockNumbers[i],
missing: actual - expected,
},
});
}
}
const blockMetricsPromises = blockNumbers.map(async (blockNumber) => {
try {
const blockMetricsStr = await qnLib.qnGetSet(
keys.blockMetrics(blockNumber)
);
if (!blockMetricsStr) {
failedBlocks.push({
block: blockNumber,
reason: "Missing block metrics",
});
return null;
}
const blockMetrics = JSON.parse(blockMetricsStr);
return { blockNumber, blockMetrics };
} catch (e) {
failedBlocks.push({ block: blockNumber, reason: e.message });
return null;
}
});
const blockMetricsResults = await Promise.all(blockMetricsPromises);
// Early check for missing blocks
const missingBlocks = blockMetricsResults
.map((result, index) => (!result ? blockNumbers[index] : null))
.filter((block) => block !== null);
if (missingBlocks.length > 0) {
return {
missingBlocks: true,
totalMissing: missingBlocks.length,
sampleMissing: missingBlocks.slice(0, 10),
allMissingBlocks: missingBlocks,
};
}
blockMetricsResults.forEach(({ blockNumber, blockMetrics }) => {
if (!blockMetrics) return;
if (!firstBlock || blockNumber < firstBlock) {
firstBlock = blockNumber;
firstBlockTimestamp = blockMetrics.timestamp;
}
if (!lastBlock || blockNumber > lastBlock) {
lastBlock = blockNumber;
lastBlockTimestamp = blockMetrics.timestamp;
}
numTransactions += blockMetrics.numTransactions;
totalFees += blockMetrics.totalFees;
numContractDeployments += blockMetrics.numContractDeployments;
numProcessedBlocks++;
});
let numActiveAddresses = 0;
try {
const activeAddresses = await qnLib.qnGetList(keys.dailyAddresses(date));
numActiveAddresses = activeAddresses ? activeAddresses.length : 0;
} catch (e) {
console.error(
`Failed to get active addresses for date ${date}: ${e.message}`
);
}
const avgTxFee = numTransactions > 0 ? totalFees / numTransactions : 0;
const avgBlockFees =
numProcessedBlocks > 0 ? totalFees / numProcessedBlocks : 0;
const avgBlockTime =
firstBlockTimestamp && lastBlockTimestamp && lastBlock > firstBlock
? (lastBlockTimestamp - firstBlockTimestamp) / (lastBlock - firstBlock)
: 0;
// Get contract deployment coverage from first valid block metric
const coverageType =
blockMetricsResults.find((r) => r?.blockMetrics)?.blockMetrics
.contractDeploymentCoverage || "unknown";
// Calculate median processing time
const sortedMetrics = blockMetricsResults
.filter((result) => result?.blockMetrics?.lastUpdated)
.sort((a, b) => a.blockMetrics.timestamp - b.blockMetrics.timestamp);
const processingTimes = [];
for (let i = 1; i < sortedMetrics.length; i++) {
const currentUpdated = new Date(
sortedMetrics[i].blockMetrics.lastUpdated
).getTime();
const previousUpdated = new Date(
sortedMetrics[i - 1].blockMetrics.lastUpdated
).getTime();
const diff = currentUpdated - previousUpdated;
if (diff > 0) processingTimes.push(diff);
}
const medianProcessingTime =
processingTimes.length > 0
? processingTimes.sort((a, b) => a - b)[
Math.floor(processingTimes.length / 2)
]
: 0;
return {
metrics: {
numTransactions,
avgTxFee,
totalFees,
avgBlockFees,
numContractDeployments,
contractDeploymentCoverage: coverageType,
numActiveAddresses,
avgBlockTime,
},
metadata: {
date,
firstBlock,
lastBlock,
firstBlockTimestamp,
lastBlockTimestamp,
numBlocks: blockNumbers.length,
numProcessedBlocks,
numFailedBlocks: failedBlocks.length,
failedBlocks,
isSequential: sequenceErrors.length === 0,
sequenceErrors,
isComplete: failedBlocks.length === 0 && sequenceErrors.length === 0,
lastUpdated: new Date().toISOString(),
cleanupPerformed: false, // Will be updated after cleanup
medianBlockProcessingTime: medianProcessingTime,
},
};
}
async function performCleanup(date, blockNumbers, keys) {
// Cleanup temporary lists
await qnLib.qnDeleteList(keys.dailyBlocks(date));
await qnLib.qnDeleteList(keys.dailyAddresses(date));
// Clean up block metric sets
const blockMetricsToDelete = blockNumbers.map((num) =>
keys.blockMetrics(num)
);
await qnLib.qnBulkSets({ delete_sets: blockMetricsToDelete });
}