forked from node-influx/node-influx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
261 lines (226 loc) · 6.92 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var request = require('request');
var url = require('url');
var _ = require('underscore');
var InfluxDB = function(host, port, username, password, database, logFunction) {
this.options = {
host : host || 'localhost',
port : port || 8086,
username : username || 'root',
password : password || 'root',
database : database,
depreciatedLogging : ((process.env.NODE_ENV === undefined || 'development') || logFunction)
? logFunction || console.log : false
};
return this;
};
InfluxDB.prototype._parseCallback = function(callback) {
return function(err, res, body) {
if(err) {
return callback(err);
}
if(res.statusCode < 200 || res.statusCode >= 300) {
return callback(new Error(body));
}
return callback(null, body);
};
};
InfluxDB.prototype.url = function(database, query) {
return url.format({
protocol: 'http:',
hostname: this.options.host,
port: this.options.port,
pathname: database,
query: _.extend({
u: this.options.username,
p: this.options.password
}, query || {})
});
};
InfluxDB.prototype.createDatabase = function(databaseName, callback) {
request.post({
url: this.url('db'),
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
name: databaseName
}, null)
}, this._parseCallback(callback));
};
InfluxDB.prototype.deleteDatabase = function(databaseName, callback) {
request({
method: 'DELETE',
url:this.url('db/' + databaseName)
}, this._parseCallback(callback));
};
InfluxDB.prototype.getDatabaseNames = function(callback) {
request({
url: this.url('db'),
json: true
}, this._parseCallback(function(err, dbs) {
if(err) {
return callback(err, dbs);
}
return callback(err, _.map(dbs, function(db) { return db.name; }));
}));
};
InfluxDB.prototype.getSeriesNames = function(databaseName,callback) {
// if database defined on connection level use it unless overwritten
if ( this.options.database && typeof databaseName == "function" ) {
callback = databaseName;
databaseName = this.options.database;
}
request({
url: this.url('db/' + databaseName + '/series', {q: 'list series'}),
json: true
}, this._parseCallback(function(err, series) {
if(err) {
return callback(err, series);
}
return callback(err, _.map(series, function(series) { return series.name; }));
}));
};
InfluxDB.prototype.createUser = function(databaseName, username, password, callback) {
request.post({
url: this.url('db/' + databaseName + '/users'),
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
name: username,
password: password
}, null)
}, this._parseCallback(callback));
};
InfluxDB.prototype.updateUser = function (databaseName, userName, options, callback)
{
request.post({
url: this.url('db/' + databaseName + '/users/' + userName),
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(options, null)
}, this._parseCallback(callback));
};
InfluxDB.prototype.writeSeries = function(series, options, callback) {
if(typeof options === 'function') {
callback = options;
options = {};
}
var query = options.query || {};
var data = [];
_.each(series, function(dataPoints, seriesName) {
var datum = { points: [], name: seriesName, columns: [] };
// Collect column names first
var columns = {};
_.each(dataPoints, function(values) {
_.each(values, function(_, k) {
columns[k] = true;
});
});
datum.columns = _.keys(columns);
// Add point values with null where needed
_.each(dataPoints, function(values) {
var point = [];
_.each(datum.columns, function(k) {
var v = typeof values[k] === 'undefined' ? null : values[k];
if(k === 'time' && v instanceof Date) {
v = v.valueOf();
query.time_precision = 'm';
}
point.push(v);
});
datum.points.push(point);
});
data.push(datum);
});
request.post({
uri: this.seriesUrl(this.options.database),
headers: {
'content-type': 'application/json'
},
pool : 'undefined' != typeof options.pool ? options.pool : {},
body: JSON.stringify(data)
}, this._parseCallback(callback));
};
InfluxDB.prototype.writePoint = function(seriesName, values, options, callback) {
var data = {};
data[seriesName] = [values];
this.writeSeries(data, options, callback);
};
InfluxDB.prototype.writePoints = function(seriesName, points, options, callback) {
var data = {};
data[seriesName] = points;
this.writeSeries(data, options, callback);
};
InfluxDB.prototype.query = function(query, callback) {
request({
url: this.url('db/' + this.options.database + '/series', { q: query }),
json: true
}, this._parseCallback(callback));
};
InfluxDB.prototype.dropSeries = function(databaseName, seriesName, callback) {
if ('function' === typeof seriesName)
{
callback=seriesName;
seriesName = databaseName;
databaseName = this.options.database;
}
request({
url: this.url('db/' + databaseName + '/series/' + seriesName),
method : 'DELETE',
json: true
}, this._parseCallback(callback));
};
InfluxDB.prototype.getContinuousQueries = function(databaseName,callback)
{
if ('function' === typeof databaseName)
{
callback=databaseName;
databaseName = this.options.database;
}
request({
url: this.url('db/' + databaseName + '/continuous_queries'),
json: true
}, this._parseCallback(callback));
};
InfluxDB.prototype.dropContinuousQuery = function(databaseName, queryID, callback) {
if ('function' === typeof queryID)
{
callback=queryID;
queryID = databaseName;
databaseName = this.options.database;
}
request({
url: this.url('db/' + databaseName + '/continuous_queries/' + queryID ),
method : 'DELETE',
json: true
}, this._parseCallback(callback));
};
// legacy function
InfluxDB.prototype.readPoints = function(query, callback) {
if (this.options.depreciatedLogging) this.options.depreciatedLogging('influx.readPoints() has been depreciated, please use influx.query()');
this.query(query,callback);
};
InfluxDB.prototype.seriesUrl = function(databaseName) {
if ( !databaseName ) databaseName = this.options.database;
return this.url('db/' + databaseName + '/series');
};
var createClient = function() {
var args = arguments;
var client = function () { return InfluxDB.apply(this, args); };
client.prototype = InfluxDB.prototype;
return new client();
};
var parseResult = function(res) {
return _.map(res.points, function(point) {
var objectPoint = {};
_.each(res.columns, function(name, n) {
objectPoint[name] = point[n];
});
return objectPoint;
});
};
module.exports = createClient;
module.exports.parseResult = parseResult;
module.exports.InfluxDB = InfluxDB;