-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 2.14 KB
/
index.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
const program = require("commander");
const moment = require("moment-timezone");
const request = require("request-promise-native");
const AsciiTable = require('ascii-table');
const { prometheusUrl, defaultFormat } = require("./config.js");
program
.version("0.1.0")
.option("-s --start <start>", "Starting day")
.option("-e --end <end>", "End day")
.option("-t --step <step>", "Step")
.option("-f --format <format>", "Output format [table|json]", /^(table|json)$/i, 'table')
.parse(process.argv);
if (!program.start || !program.end) {
program.outputHelp();
process.exit(1);
}
// const format = program.format ? program.format.toUpperCase() : defaultFormat;
const base = '/api/v1/query_range';
const outputTable = data => {
for (const {metric, values} of data.result) {
const table = new AsciiTable('Functions');
table.fromJSON({
heading: ['','Day/Hour', 'Accum. Calls', 'Calls'],
rows: values.map((v,i,x) => {
return [
i+1, // index
moment(v[0], 'X').format("YYYY-MM-DD HH:MM"), // Day/Hour
v[1], // Accummulated calls
i>0 ? parseInt(v[1])-parseInt(x[i-1][1]) : '' // Calls in this step
] // return ...
}) // rows: ...
}); // fromJSON ...
const sum = parseInt(values[values.length-1][1]) - parseInt(values[0][1]);
console.log(`\n${metric.function_name}/${metric.code} sum=${sum}`)
console.log(table.toString());
} // for ...
} // outputTable ...
request({
uri: `${prometheusUrl}${base}`,
qs: {
query: 'gateway_function_invocation_total',
start: moment(program.start, "YYYY-MM-DD").format(),
end: moment(program.end, "YYYY-MM-DD").format(),
step: program.step || '1h'
}
})
.then(result => {
const { data } = JSON.parse(result);
if (program.format.toLowerCase() == 'table') {
outputTable(data);
}
if (program.format.toLowerCase() == 'json') {
console.log(JSON.stringify(data.result));
}
})
.catch(e => console.log(e))
;