-
Notifications
You must be signed in to change notification settings - Fork 22
/
batch-by-interval.js
55 lines (50 loc) · 1.28 KB
/
batch-by-interval.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
'use strict';
const express = require('express');
const app = express();
const Influx = require('..');
const _ = require('lodash');
const client = new Influx('http://localhost:8086/mydb');
const onHeaders = require('on-headers');
client.createDatabase().catch(err => {
console.error('create database fail err:', err);
});
// batch post to influxdb every 10s
setInterval(() => {
if (client.writeQueueLength) {
client.syncWrite()
.then(() => console.info('sync write queue success'))
.catch(console.error);
}
}, 10 * 1000);
app.use((req, res, next) => {
const start = Date.now();
onHeaders(res, () => {
const statusCode = res.statusCode;
const use = Date.now() - start;
const tags = {
status: _.sortedIndex([99, 199, 299, 399, 499, 599], statusCode),
spdy: _.sortedIndex([100, 300, 1000, 3000], use)
};
const fields = {
use: use,
code: statusCode
};
// add to the queue
// all batch point has the same time
client.write('http')
.field(fields)
.tag(tags)
.queue();
});
next();
});
app.get('/', (req, res) => {
setTimeout(() => {
res.json({
name: 'Tree Xie'
});
}, 1000);
});
const server = app.listen(() => {
console.info(`listen on http://127.0.0.1:${server.address().port}/`);
});