index.js
3.55 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
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
const http = require('http');
var _ = require('lodash');
/**
*
* const options =
* {
* host:'xxx.xxx.xxx',
* db:'xxxxx',
* measurement:'api-duration'
* duration:2000 //ms per 200 send message,
* records:100 //when message over 100 send them
* }
*/
class Sender {
constructor(options) {
this.bulks = [];
this.batchMessages = [];
this.options = options;
this.options.duration = this.options.duration || 2000;
this.options.records = this.options.records || 100;
let addBulkTime = new Date().getTime();
setInterval(() => {
const now = new Date().getTime();
if ((addBulkTime + this.options.duration < now ||
this.batchMessages.length > this.options.records) &&
this.batchMessages.length > 0) {
this.bulks.push(this.batchMessages);
this.batchMessages = [];
addBulkTime = new Date().getTime();
}
}, 100);
setInterval(() => {
if (this.bulks.length > 0) {
this._send(this.bulks.shift());
}
}, 100);
}
// add message to batchMessage
addMessage(message) {
message.time = message.time || new Date().getTime() * 1000000;
this.batchMessages.push(message);
}
_makeline(obj, withQuote = false) {
let arr = []
for (let key in obj) {
const value = this._escape(obj[key], withQuote);
arr.push(`${key}=${value}`);
}
return arr.join(',');
}
//use tcp send message
_send(bulk) {
if (!this.options.db) {
throw new Error('db options must be set when inital Sender!');
return;
}
if (!this.options.measurement) {
throw new Error('measurement options must be set when inital Sender!');
return;
}
let bulkMessage = [];
for (let i = 0; i < bulk.length; i++) {
const tagStr = this._makeline(bulk[i].tags);
const fieldStr = this._makeline(bulk[i].fields, true);
const messageStr = `${this.options.measurement},${tagStr} ${fieldStr} ${bulk[i].time}`;
bulkMessage.push(messageStr);
}
const data = bulkMessage.join('\n');
const options = {
hostname: this.options.host,
port: this.options.port || 80,
path: `/write?db=${this.options.db}`,
method: 'POST',
headers: {
'Content-Length': Buffer.byteLength(data)
}
};
const req = http.request(options, (res) => {
console.log(`status code: ${res.statusCode}`);
});
req.on('error', (e) => {
return Promise.reject(e);
});
req.write(data);
req.end();
}
//trans to line string
_escape(value, withQuote) {
if (_.isString(value)) {
if (withQuote) {
value = '"' + value + '"';
} else {
value = _.replace(value, /,/g, '\\,');
value = _.replace(value, /=/g, '\\=');
value = _.replace(value, /\s/g, '\\ ');
}
} else if (_.isInteger(value)) {
if (withQuote) {
value = value + 'i';
}
} else if (_.isObject(value)) {
value = '"' + _.replace(JSON.stringify(value), /"/g, '\\"') + '"';
} else if (_.isNull(value) || _.isNil(value)) {
value = '""';
}
return value;
};
}
module.exports = Sender;