line-parse.js
1.06 KB
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
const qs = require('querystring');
const crypto = global.yoho.crypto;
const logger = global.yoho.logger;
class Message {
constructor(measurement, tags, fields, time) {
this.measurement = measurement;
this.tags = tags;
this.fields = fields;
this.time = time;
}
}
function parseMessage(line) {
let measurement = line.split(',', 1)[0];
let body = line.substring(measurement.length + 1);
let tokens = body.split(' ');
let [tags, fields, time] = [qs.parse(tokens[0], ',', '='), qs.parse(tokens[1], ',', '='), tokens[2]];
if (tags.route) {
try {
let [method, route] = tags.route.split(']');
route = crypto.decrypt(route);
tags.route = `${method}]${route}`;
} catch (e) {
logger.warn('parse message wrong ', tags.route);
}
}
return new Message(measurement, tags, fields, time);
}
function parse(data) {
let lines = (data || '').split('\n');
let messages = lines.map(parseMessage);
return messages;
}
module.exports = {
parse
};