-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlineproto.js
145 lines (123 loc) · 3.62 KB
/
lineproto.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
/*
* Take the input value and output it as a string.
* If the input value is numeric, look at the given numeric field type.
* If it is 'int' output it as a line protocol int (i.e. 1234i)
* Otherwise, output it as a float.
*/
function formatValue(v, numericType) {
if (typeof v === 'number') {
if (numericType == "int") {
return `${Math.round(v)}i`;
} else if (numericType == "float") {
return String(v);
} else {
return String(v);
}
} else if (typeof v === 'boolean') {
return v ? 'TRUE' : 'FALSE';
} else {
return JSON.stringify(v);
}
}
function formatDate(date) {
return (date instanceof Date ? date.getTime() : date) * 1000000;
}
const INT_REGEX = /^\d+i$/;
const TRUE_REGEX = /^(t|true)$/i;
const FALSE_REGEX = /^(f|false)$/i;
const STRING_REGEX = /^"(.*)"$/;
function parseValue(value) {
if (value == null) {
return undefined;
} else if (INT_REGEX.test(value)) {
return parseInt(value.slice(0, -1));
} else if (TRUE_REGEX.test(value)) {
return true;
} else if (FALSE_REGEX.test(value)) {
return false;
} else if (STRING_REGEX.test(value)) {
return value.slice(1, -1);
} else if (!isNaN(value)) {
return parseFloat(value);
} else {
return undefined;
}
}
function joinObject(obj, withFormatting, config) {
if (!obj) return '';
return Object.keys(obj)
.map(key => {
let override = config.typeMappings.find(i => i.fieldName == key);
let numType = override?.fieldType || config.defaultTypeMapping;
return `${key}=${withFormatting ? formatValue(obj[key], numType) : obj[key]}`;
})
.join(',');
}
function parse(point, config) {
const result = {};
const [tags_, fields_, timestamp] = point.split(' ');
const tags = (tags_ || '').split(',');
const fields = (fields_ || '').split(',');
result.measurement = tags.shift();
result.tags = tags.reduce((out, tag) => {
if (!tag) return out;
var [key, value] = tag.split('=');
out[key] = value;
return out;
}, {});
result.fields = fields.reduce((out, field) => {
if (!field) return out;
var [key, value] = field.split('=');
out[key] = parseValue(value);
return out;
}, {});
if (timestamp) {
result.timestamp = parseInt(timestamp) / 1000000;
} else if (config.addTimestamp) {
result.timestamp = Date.now();
}
return result;
}
function format(pointJson, config) {
const { measurement, tags, fields, timestamp } = pointJson;
var str = measurement;
const tagsStr = joinObject(tags, false, config);
if (tagsStr) {
str += ',' + tagsStr;
}
str += ' ' + joinObject(fields, true, config);
if (timestamp) {
str += ' ' + formatDate(timestamp);
} else if (config.addTimestamp) {
str += ' ' + formatDate(new Date());
}
return str;
}
function transform(item, config) {
if (item == null) {
return item;
} else if (typeof item === 'string') {
return parse(item, config);
} else if (typeof item === 'object' && 'measurement' in item) {
return format(item, config);
} else {
return item;
}
}
function transformArray(itemOrArray, config) {
if (itemOrArray && Array.isArray(itemOrArray)) {
return itemOrArray.map(item => transform(item, config));
} else {
return transform(itemOrArray, config);
}
}
export {
formatValue,
formatDate,
parseValue,
joinObject,
parse,
format,
transform,
transformArray,
};