-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchData.js
542 lines (457 loc) · 21.4 KB
/
fetchData.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
const axios = require('axios');
const client = require('./db.js');
// Availability
async function getDownloadSuccess24h(db) {
try {
const db = client.db('sla_metrics');
const query = '(increase(beekeeper_net_avail_download_attempts[24h]) - increase(beekeeper_net_avail_download_errors_count[24h])) / increase(beekeeper_net_avail_download_attempts[24h])'
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let { values } = data.data.result[0];
// Convert timestamps to human-readable ISO date strings and check for numeric values
values = values.reduce((acc, [timestamp, value]) => {
const date = new Date(timestamp * 1000).toISOString();
let numericValue = parseFloat(value) * 100;
if (Number.isFinite(numericValue)) {
acc.push([date, numericValue]);
}
return acc;
}, []);
const downloadSuccess24h = {
values: values,
unit: "%",
metric: "24h Download Success Rate"
};
// Insert the data into the MongoDB collection
const collection = db.collection('download_success_24h');
const insertResult = await collection.insertOne(downloadSuccess24h);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted.');
}
console.log('Data saved to MongoDB: download_success_24h', JSON.stringify(downloadSuccess24h, null, 2));
} else {
console.error(`Invalid response from Prometheus (${downloadSuccess24h.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
} finally {
}
}
async function getUploadSuccess24h(db) {
try {
const db = client.db('sla_metrics');
const query = '(increase(beekeeper_net_avail_upload_attempts[24h]) - increase(beekeeper_net_avail_upload_errors_count[24h])) / increase(beekeeper_net_avail_upload_attempts[24h])'
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let { values } = data.data.result[0];
// Convert timestamps to human-readable ISO date strings and check for numeric values
values = values.reduce((acc, [timestamp, value]) => {
const date = new Date(timestamp * 1000).toISOString();
let numericValue = parseFloat(value) * 100;
if (Number.isFinite(numericValue)) {
acc.push([date, numericValue]);
}
return acc;
}, []);
const uploadSuccess24h = {
values: values,
unit: "%",
metric: "24h Upload Success Rate"
};
// Insert the data into the MongoDB collection
const collection = db.collection('upload_success_24h');
const insertResult = await collection.insertOne(uploadSuccess24h);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted');
}
console.log('Data saved to MongoDB: upload_success_24h', JSON.stringify(uploadSuccess24h, null, 2));
} else {
console.error(`Invalid response from Prometheus (${uploadSuccess24h.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
}
}
async function getDownloadSuccessAllTime(db) {
// For individual chunks
try {
const db = client.db('sla_metrics');
const query = '(sum_over_time(beekeeper_net_avail_download_attempts[1h]) - sum_over_time(beekeeper_net_avail_download_errors_count[1h])) / sum_over_time(beekeeper_net_avail_download_attempts[1h])'
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let { values } = data.data.result[0];
// Convert timestamps to human-readable ISO date strings and check for numeric values
values = values.reduce((acc, [timestamp, value]) => {
const date = new Date(timestamp * 1000).toISOString();
let numericValue = parseFloat(value) * 100;
if (Number.isFinite(numericValue)) {
acc.push([date, numericValue]);
}
return acc;
}, []);
const downloadSuccessAllTime = {
values: values,
unit: "%",
metric: "All Time Chunk Download Success Rate"
};
// Insert the data into the MongoDB collection
const collection = db.collection('download_success_all_time');
const insertResult = await collection.insertOne(downloadSuccessAllTime);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted.');
}
console.log('Data saved to MongoDB: downloadSuccessAllTime', JSON.stringify(downloadSuccessAllTime, null, 2));
} else {
console.error(`Invalid response from Prometheus (${downloadSuccessAllTime.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
} finally {
}
}
async function getUploadSuccessAllTime(db) {
try {
const db = client.db('sla_metrics');
const query = '(sum_over_time(beekeeper_net_avail_upload_attempts[1h]) - sum_over_time(beekeeper_net_avail_upload_errors_count[1h])) / sum_over_time(beekeeper_net_avail_upload_attempts[1h])'
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let { values } = data.data.result[0];
// Convert timestamps to human-readable ISO date strings and check for numeric values
values = values.reduce((acc, [timestamp, value]) => {
const date = new Date(timestamp * 1000).toISOString();
let numericValue = parseFloat(value) * 100;
if (Number.isFinite(numericValue)) {
acc.push([date, numericValue]);
}
return acc;
}, []);
const uploadSuccessAllTime = {
values: values,
unit: "%",
metric: "All Time Upload Success Rate"
};
// Insert the data into the MongoDB collection
const collection = db.collection('upload_success_all_time');
const insertResult = await collection.insertOne(uploadSuccessAllTime);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted');
}
console.log('Data saved to MongoDB: uploadSuccessAllTime', JSON.stringify(uploadSuccessAllTime, null, 2));
} else {
console.error(`Invalid response from Prometheus (${uploadSuccessAllTime.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
} finally {
}
}
// Durability
async function getFileRetrievalRate24h(db) {
try {
const db = client.db('sla_metrics');
const query = '1.0 - (sum(increase(beekeeper_check_data_durability_file_download_errors{job="bee-sla"}[86400s]))/sum(increase(beekeeper_check_data_durability_file_download_attempts{job="bee-sla"}[86400s])))'
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let { values } = data.data.result[0];
// Convert timestamps to human-readable ISO date strings and check for numeric values
values = values.reduce((acc, [timestamp, value]) => {
const date = new Date(timestamp * 1000).toISOString();
let numericValue = parseFloat(value) * 100;
if (Number.isFinite(numericValue)) {
acc.push([date, numericValue]);
}
return acc;
}, []);
const fileRetrievalRate24h = {
values: values,
unit: "%",
metric: "24h File Retrieval Rate"
};
// Insert the data into the MongoDB collection
const collection = db.collection('file_retrieval_rate_24h');
const insertResult = await collection.insertOne(fileRetrievalRate24h);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted');
}
console.log('Data saved to MongoDB: file_retrieval_rate', JSON.stringify(fileRetrievalRate24h, null, 2));
} else {
console.error(`Invalid response from Prometheus (${fileRetrievalRate24h.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
}
}
async function getChunkRetrievalRate24h(db) {
try {
const db = client.db('sla_metrics');
const query = '1.0 - (sum(increase(beekeeper_check_data_durability_chunk_download_errors{job="bee-sla"}[172800s]))/sum(increase(beekeeper_check_data_durability_chunk_download_attempts{job="bee-sla"}[172800s])))'
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let { values } = data.data.result[0];
// Convert timestamps to human-readable ISO date strings and check for numeric values
values = values.reduce((acc, [timestamp, value]) => {
const date = new Date(timestamp * 1000).toISOString();
let numericValue = parseFloat(value) * 100;
if (Number.isFinite(numericValue)) {
acc.push([date, numericValue]);
}
return acc;
}, []);
const chunkRetrievalRate = {
values: values,
unit: "%",
metric: "24h Chunk Retrieval Rate"
};
// Insert the data into the MongoDB collection
const collection = db.collection('chunk_retrieval_rate_24h');
const insertResult = await collection.insertOne(chunkRetrievalRate);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted');
}
console.log('Data saved to MongoDB: file_retrieval_rate', JSON.stringify(chunkRetrievalRate, null, 2));
} else {
console.error(`Invalid response from Prometheus (${chunkRetrievalRate.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
} finally {
}
}
// Latency
async function getChunkRetrievalDuration24h(db) {
try {
const db = client.db('sla_metrics');
const query = 'increase(beekeeper_net_avail_data_download_duration_sum{success="true"}[24h]) / increase(beekeeper_net_avail_data_download_duration_count{success="true"}[24h])'
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let { values } = data.data.result[0];
// Convert timestamps to human-readable ISO date strings and check for numeric values
values = values.reduce((acc, [timestamp, value]) => {
const date = new Date(timestamp * 1000).toISOString();
let numericValue = parseFloat(value) * 1000;
if (Number.isFinite(numericValue)) {
acc.push([date, numericValue]);
}
return acc;
}, []);
const chunkRetrievalDuration24h = {
values: values,
unit: "ms",
metric: "24h Chunk Retrieval Duration"
};
// Insert the data into the MongoDB collection
const collection = db.collection('chunk_retrieval_duration_24h');
const insertResult = await collection.insertOne(chunkRetrievalDuration24h);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted.');
}
console.log('Data saved to MongoDB: chunk_retrieval_duration_24h', JSON.stringify(chunkRetrievalDuration24h, null, 2));
} else {
// console.error(`Invalid response from Prometheus (${chunkRetrievalDuration24h.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
}
}
async function getChunkRetrievalDurationAllTime(db) {
try {
const db = client.db('sla_metrics');
const query = 'beekeeper_net_avail_data_download_duration_sum{success="true"} / beekeeper_net_avail_data_download_duration_count{success="true"}'
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let { values } = data.data.result[0];
// Convert timestamps to human-readable ISO date strings and check for numeric values
values = values.reduce((acc, [timestamp, value]) => {
const date = new Date(timestamp * 1000).toISOString();
let numericValue = parseFloat(value) * 1000;
if (Number.isFinite(numericValue)) {
acc.push([date, numericValue]);
}
return acc;
}, []);
const chunkRetrievalDurationAllTime = {
values: values,
unit: "ms",
metric: "All Time Chunk Retrieval Duration"
};
// Insert the data into the MongoDB collection
const collection = db.collection('chunk_retrieval_duration_all_time');
const insertResult = await collection.insertOne(chunkRetrievalDurationAllTime);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted.');
}
console.log('Data saved to MongoDB: chunk_retrieval_duration_all_time', JSON.stringify(chunkRetrievalDurationAllTime, null, 2));
} else {
console.error(`Invalid response from Prometheus (${chunkRetrievalDurationAllTime.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
}
}
async function getFileDownloadSpeed24h(db) {
try {
const db = client.db('sla_metrics');
const query = 'avg by(job) ( beekeeper_check_longavailability_d_download_size_bytes / (rate(beekeeper_check_longavailability_d_download_duration_seconds_sum{job="bee-sla"}[24h]) / rate(beekeeper_check_longavailability_d_download_duration_seconds_count{job="bee-sla"}[24h])))';
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
console.log(JSON.stringify(data, null, 2))
if (data.status === 'success' && data.data.resultType === 'matrix') {
let aggregatedValues = {};
// Aggregate values by timestamp
data.data.result.forEach(result => {
result.values.forEach(([timestamp, value]) => {
if (!aggregatedValues[timestamp]) {
aggregatedValues[timestamp] = [];
}
aggregatedValues[timestamp].push(parseFloat(value));
});
});
// Calculate averages for each timestamp and convert to Mebibytes
let averageValues = Object.entries(aggregatedValues).map(([timestamp, values]) => {
const average = values.reduce((sum, val) => sum + val, 0) / values.length;
const averageMebibytes = average / 1048576; // Convert bytes to Mebibytes
return [new Date(timestamp * 1000).toISOString(), averageMebibytes];
});
const fileDownloadSpeed24h = {
values: averageValues,
unit: "MiB/s",
metric: "File Download Speed 24h"
};
// Insert the data into the MongoDB collection
const collection = db.collection('file_download_speed_24h');
const insertResult = await collection.insertOne(fileDownloadSpeed24h);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted.');
}
console.log('Data saved to MongoDB: file_download_speed_24h', JSON.stringify(fileDownloadSpeed24h, null, 2));
} else {
console.error(`Invalid response from Prometheus (${fileDownloadSpeed24h.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
}
}
async function getFileDownloadSpeedAllTime(db) {
try {
const db = client.db('sla_metrics');
const query = 'beekeeper_check_longavailability_d_download_size_bytes / (beekeeper_check_longavailability_d_download_duration_seconds_sum{job="bee-sla"} / beekeeper_check_longavailability_d_download_duration_seconds_count{job="bee-sla"})';
const encodedQuery = encodeURIComponent(query);
const start = "2023-12-13T00:00:00Z"
const end = new Date().toISOString().split('.')[0] + 'Z';
const response = await axios.get(`${process.env.PROMETHEUS}query_range?query=${encodedQuery}&start=${start}&end=${end}&step=1d`);
const { data } = response;
if (data.status === 'success' && data.data.resultType === 'matrix') {
let aggregatedValues = {};
// Aggregate values by timestamp
data.data.result.forEach(result => {
result.values.forEach(([timestamp, value]) => {
if (!aggregatedValues[timestamp]) {
aggregatedValues[timestamp] = [];
}
aggregatedValues[timestamp].push(parseFloat(value));
});
});
// Calculate averages for each timestamp and convert to Mebibytes
let averageValues = Object.entries(aggregatedValues).map(([timestamp, values]) => {
const average = values.reduce((sum, val) => sum + val, 0) / values.length;
const averageMebibytes = average / 1048576; // Convert bytes to Mebibytes
return [new Date(timestamp * 1000).toISOString(), averageMebibytes];
});
const fileDownloadSpeedAllTime = {
values: averageValues,
unit: "MiB/s",
metric: "File Download Speed All Time"
};
// Insert the data into the MongoDB collection
const collection = db.collection('file_download_speed_all_time');
const insertResult = await collection.insertOne(fileDownloadSpeedAllTime);
if (insertResult.insertedId) {
// Delete all older entries
await collection.deleteMany({ _id: { $ne: insertResult.insertedId } });
console.log('Old entries deleted.');
}
console.log('Data saved to MongoDB: file_download_speed_all_time', JSON.stringify(fileDownloadSpeedAllTime, null, 2));
} else {
console.error(`Invalid response from Prometheus (${fileDownloadSpeedAllTime.metric}):`, data);
}
} catch (error) {
console.error('Error fetching data from Prometheus:', error.message);
}
}
async function fetchData() {
try {
const db = client.db('sla_metrics');
let metrics = [
getDownloadSuccess24h(db),
getUploadSuccess24h(db),
getDownloadSuccessAllTime(db),
getUploadSuccessAllTime(db),
getFileRetrievalRate24h(db),
getChunkRetrievalRate24h(db),
getChunkRetrievalDuration24h(db),
getChunkRetrievalDurationAllTime(db),
getFileDownloadSpeed24h(db),
getFileDownloadSpeedAllTime(db)
];
// Execute all metrics functions;
await Promise.all(metrics);
console.log('All metrics functions executed successfully.');
} catch (error) {
console.error('Error fetching data:', error.message);
// Additional error handling can be added here if necessary
}
}
module.exports = fetchData;