-
Notifications
You must be signed in to change notification settings - Fork 3
/
influxdb.js
142 lines (125 loc) · 4.19 KB
/
influxdb.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
class Influxdb {
constructor(host, sendPointsOnClose) {
this.sendPointsOnClose = sendPointsOnClose;
this.host = host;
this.points = [];
this.beaconSent = false;
if (this.sendPointsOnClose) this.registerUnloadEvent();
return this;
}
registerUnloadEvent() {
var _self = this;
//need both events to work in chrome http://stackoverflow.com/a/20322988/1368683
window.addEventListener('unload', function (e) {
_self.sendBeacon(_self);
}, false);
window.onbeforeunload = function (e) {
_self.sendBeacon(_self);
};
}
sendBeacon(_self) {
//need this polyfill https://github.com/miguelmota/Navigator.sendBeacon
if (_self.beaconSent) return;
if (_self.points && _self.points.length == 0) return;
_self.beaconSent = true;
if (!navigator && !navigator.sendBeacon) return;
var data = _self.implodePoints();
navigator.sendBeacon(_self.host, data);
}
point(key, fields, tags, time) {
return this._addPoint(new Influxdb_Point(key, fields, tags, time));
}
_addPoint(point) {
if (point.isValid()) {
this.points.push(point);
}
return this;
}
implodePoints() {
if (this.points.length == 0) return '';
var index, data = '';
for (index = 0; index < this.points.length; ++index) {
if (!this.points[index].isValid()) {
this.points.slice(index, 1);
continue;
}
data = data + this.points[index].getLine();
}
return data;
}
send() {
if (this.points.length == 0) return false;
var data = this.implodePoints();
if (data) {
var request = new XMLHttpRequest();
request.open('POST', this.host, true);
request.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8');
request.send(data);
}
this.points = [];
}
}
class Influxdb_Point {
constructor(key, fields, tags, time) {
this.key = key;
if (typeof fields !== 'undefined' && !this.isEmpty(fields)) this.fields = fields;
if (typeof tags !== 'undefined' && !this.isEmpty(fields)) this.tags = tags;
if (typeof time !== 'undefined') this.time = time;
}
isValid() {
if (!this.key || !this.fields) return false;
return true;
}
getLine() {
if (!this.key && !this.fields) return;
this.line = this.key;
if (this.tags) {
this.line = this.line + ',' + this.objToString(this.tags)
}
if (this.fields) {
this.line = this.line + ' ' + this.objToString(this.fields);
}
if (this.time) {
this.line = this.line + ' ' + this.time;
}
this.line = this.line + '\n';
return this.line;
}
sortObjectByKey(obj) {
if (!Array.prototype.forEach) return obj;
if (!Array.prototype.sort) return obj;
if (!Object.keys) return obj;
var ordered = {};
Object.keys(obj).sort().forEach(function (key) {
ordered[key] = obj[key];
});
return ordered;
}
objToString(obj) {
var i = 0;
var str = '';
var key, value;
//Tags should be sorted by key before being sent for best performance https://docs.influxdata.com/influxdb/v0.13/write_protocols/line/#key
obj = this.sortObjectByKey(obj);
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
//todo: right now not supporting space in tags where it should be support with back slash
key = key.replace(/\s+/g, '');
value = obj[key].toString();
value = value.replace(/\s+/g, '');
i++;
if (i > 1) str = str + ',';
str += key + '=' + value;
}
}
return str;
}
isEmpty(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop))
return false;
}
if (!window.JSON) return true;
return true && JSON.stringify(obj) === JSON.stringify({});
}
}