-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·64 lines (58 loc) · 1.55 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
#!/usr/bin/env node
const fs = require('fs');
const nReadlines = require('n-readlines');
const err0regex = /\[([A-Z][A-Z0-9]*-[0-9]+)\]/;
const axios = require("axios");
let token = null;
let buffer = [];
const flush = async function() {
if (buffer.length > 0) {
const lines = buffer;
buffer = [];
await axios.post(token.url + '~/api/bulk-log', { logs: lines }, {
headers: {
'Authorization': 'Bearer ' + token.token_value
}
});
}
return null;
}
const push = async function(log) {
buffer.push(log);
if (buffer.length >= 1000) {
await flush();
}
}
async function main() {
if (process.argv.length < 4) {
console.log('Usage:');
console.log('err0-import token.json log1 [log2 .. logN]');
return;
}
token = JSON.parse(fs.readFileSync(process.argv[2]));
for (const file of process.argv.slice(3)) {
console.log(file);
const reader = new nReadlines(file);
let line;
while (line = reader.next()) {
const str = line.toString('utf-8');
const match = err0regex.exec(str);
if (match != null) {
await push({
error_code: match[1],
ts: (new Date().getTime())
});
}
}
}
await flush();
}
main()
.then(() => {
process.exit(0)
})
.catch(err => {
console.error(err.message || err);
if (err.stack) console.error(err.stack);
process.exit(1);
});