forked from node-influx/node-influx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
550 lines (473 loc) Β· 15.8 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
var InfluxRequest = require('./lib/InfluxRequest.js')
var url = require('url')
var _ = require('lodash')
var defaultOptions = {
hosts: [],
disabled_hosts: [],
username: 'root',
password: 'root',
port: 8086,
protocol: 'http',
depreciatedLogging: (process.env.NODE_ENV === undefined || 'development') ? console.log : false,
failoverTimeout: 60000,
requestTimeout: null,
maxRetries: 2,
timePrecision: 'ms'
}
/**
* Backslash-escape commas, equal signs and spaces per
* https://docs.influxdata.com/influxdb/v1.0/write_protocols/line_protocol_tutorial/#special-characters-and-keywords
* @param {string} s
* @returns {string}
*/
function escape (s) {
return s.replace(/[,= ]/g, '\\$&')
}
function parseOptionsUrl (url_) {
var parsed = url.parse(url_)
var options = {
host: parsed.hostname,
port: parsed.port,
protocol: parsed.protocol
}
if (parsed.auth) {
var authSplit = parsed.auth.split(':')
if (authSplit.length !== 2) throw new Error('Invalid authentication: ' + parsed.auth)
options.username = authSplit[0]
options.password = authSplit[1]
}
if (parsed.pathname.length > 1) options.database = parsed.pathname.slice(1)
return options
}
/**
* Figure out if the user passed an options object or not.
* This only exists because we're not using ES6 default parameters.
* @param options
* @param callback
* @returns {{callback: (*|_.noop), options: (*|{})}}
*/
function resolveOptCallback (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
return {
callback: callback || _.noop,
options: options || {}
}
}
var InfluxDB = function (options) {
if (typeof options === 'string') options = parseOptionsUrl(options)
this.options = _.extend(_.clone(defaultOptions), options)
this.request = new InfluxRequest({
failoverTimeout: this.options.failoverTimeout,
maxRetries: this.options.maxRetries,
requestTimeout: this.options.requestTimeout
})
if ((!_.isArray(this.options.hosts) || this.options.hosts.length === 0) && typeof this.options.host === 'string') {
this.request.addHost(this.options.host, this.options.port, this.options.protocol)
}
if (_.isArray(this.options.hosts) && this.options.hosts.length > 0) {
var self = this
_.each(this.options.hosts, function (host) {
if (typeof host === 'string') host = parseOptionsUrl(host)
var port = host.port || self.options.port
var protocol = host.protocol || self.options.protocol
self.request.addHost(host.host, port, protocol)
})
}
return this
}
InfluxDB.prototype._parseResults = function (response, callback) {
var results = []
_.each(response, function (result) {
var tmp = []
if (result.series) {
_.each(result.series, function (series) {
var rows = _.map(series.values, function (values) {
return _.extend(_.zipObject(series.columns, values), series.tags)
})
tmp = _.chain(tmp).concat(rows).value()
})
}
results.push(tmp)
})
return callback(null, results)
}
InfluxDB.prototype._parseCallback = function (callback) {
return function (err, res, body) {
if (typeof callback === 'undefined') return
if (err) {
return callback(err)
}
if (res.statusCode < 200 || res.statusCode >= 300) {
var errorMessage
if (!body) {
errorMessage = 'No body received with status code ' + res.statusCode + ' from Influx.'
} else if (body.error) {
errorMessage = body.error
} else if (typeof body === 'object') {
errorMessage = JSON.stringify(body)
} else {
errorMessage = body
}
return callback(new Error(errorMessage))
}
// Look for errors in the response body
if (_.isObject(body) && body.results && _.isArray(body.results)) {
for (var i = 0; i <= body.results.length; ++i) {
if (body.results[i] && body.results[i].error && body.results[i].error !== '') {
return callback(new Error(body.results[i].error))
}
}
}
if (body === undefined) {
return callback(new Error('body is undefined'))
}
return callback(null, body.results)
}
}
InfluxDB.prototype.setRequestTimeout = function (value) {
return this.request.setRequestTimeout(value)
}
InfluxDB.prototype.setFailoverTimeout = function (value) {
return this.request.setFailoverTimeout(value)
}
// possible options:
// {db: databaseName, rp: retentionPolicy, precision: timePrecision}
InfluxDB.prototype.url = function (endpoint, options, query) {
// prepare the query object
var queryObj = _.extend({
u: this.options.username,
p: this.options.password
}, options || {}, query || {})
// add the global configuration if they are set and not provided by the options
if (this.options.timePrecision && !queryObj.precision) {
queryObj.precision = this.options.timePrecision
}
if (this.options.database && !queryObj.db) {
queryObj.db = this.options.database
}
if (this.options.retentionPolicy && !queryObj.rp) {
queryObj.rp = this.options.retentionPolicy
}
return url.format({
pathname: endpoint,
query: queryObj
})
}
// Prepares and sends the actual request
InfluxDB.prototype.queryDB = function (query, options, callback) {
var args = resolveOptCallback(options, callback)
this.request.get({
url: this.url('query', args.options, {q: query}),
json: true
}, this._parseCallback(args.callback))
}
/**
* Send a POST request. Used for commands that have side effects, e.g.
* 'CREATE DATABASE' or 'DROP USER'. GET is now deprecated for these.
* @param query
* @param options
* @param callback
*/
InfluxDB.prototype.updateDB = function (query, options, callback) {
var args = resolveOptCallback(options, callback)
this.request.post({
url: this.url('query', args.options, {q: query}),
json: true
}, this._parseCallback(args.callback))
}
// creates a new database
InfluxDB.prototype.createDatabase = function (databaseName, callback) {
this.updateDB('create database "' + databaseName + '"', callback)
}
InfluxDB.prototype.dropDatabase = function (databaseName, callback) {
this.updateDB('drop database "' + databaseName + '"', callback)
}
InfluxDB.prototype.getDatabaseNames = function (callback) {
this.queryDB('show databases', function (err, results) {
if (err) {
return callback(err, results)
}
var names = _.get(results, '[0].series[0].values')
if (!_.isArray(names)) {
return callback(new Error('bad response from server', results))
}
callback(null, names.map(function (dbarray) {
if (_.isArray(dbarray)) {
return dbarray[0]
}
}))
})
}
InfluxDB.prototype.getMeasurements = function (callback) {
this.queryDB('show measurements', callback)
}
InfluxDB.prototype.getSeriesNames = function (measurementName, callback) {
var query = 'show series'
// if no measurement name is given
if (typeof measurementName === 'function') {
callback = measurementName
} else {
query = query + ' from "' + measurementName + '"'
}
this.queryDB(query, function (err, results) {
if (err) {
return callback(err, results)
}
// Influx version 0.11 changed the SHOW SERIES format. Support both versions.
if (_.get(results[0].series[0], 'columns')) {
results = _(results[0].series) // v0.11
.map('values')
.flattenDepth(2)
.map(function (value) { return value.split(',', 1)[0] })
.uniq()
.value()
} else {
results = _.map(results[0].series, 'name') // < v0.11
}
return callback(err, results)
})
}
InfluxDB.prototype.getSeries = function (measurementName, callback) {
var query = 'show series'
// if no measurement name is given
if (typeof measurementName === 'function') {
callback = measurementName
} else {
query = query + ' from "' + measurementName + '"'
}
this.queryDB(query, function (err, results) {
if (err) {
return callback(err, results)
}
return callback(err, results[0].series)
})
}
InfluxDB.prototype.dropMeasurement = function (measurementName, callback) {
this.updateDB('drop measurement "' + measurementName + '"', callback)
}
InfluxDB.prototype.dropSeries = function (seriesId, callback) {
this.updateDB('drop series ' + seriesId, callback)
}
InfluxDB.prototype.getUsers = function (callback) {
var self = this
// TODO strip unused arguments from quoery. Now we have:
// query?u=...&p=...&q=show%20users&precision=ms&db=...&rp=...
this.queryDB('show users', function (err, results) {
if (err) {
return callback(err, results)
}
return self._parseResults(results, function (err, results) {
return callback(err, results[0])
})
// return callback(err, results[0].series[0].values)
})
}
InfluxDB.prototype.createUser = function (username, password, isAdmin, callback) {
if (typeof isAdmin === 'function') {
callback = isAdmin
isAdmin = false
}
var query = 'create user "' + username + '" with password \'' + password + "'"
if (isAdmin) {
query += ' with all privileges'
}
this.updateDB(query, callback)
}
InfluxDB.prototype.setPassword = function (username, password, callback) {
this.updateDB('set password for "' + username + '" = \'' + password + "'", callback)
}
InfluxDB.prototype.grantPrivilege = function (privilege, databaseName, userName, callback) {
this.updateDB('grant ' + privilege + ' on "' + databaseName + '" to "' + userName + '"', callback)
}
InfluxDB.prototype.revokePrivilege = function (privilege, databaseName, userName, callback) {
this.updateDB('revoke ' + privilege + ' on "' + databaseName + '" from "' + userName + '"', callback)
}
InfluxDB.prototype.grantAdminPrivileges = function (userName, callback) {
this.updateDB('grant all privileges to "' + userName + '"', callback)
}
InfluxDB.prototype.revokeAdminPrivileges = function (userName, callback) {
this.updateDB('revoke all privileges from "' + userName + '"', callback)
}
InfluxDB.prototype.dropUser = function (username, callback) {
this.updateDB('drop user "' + username + '"', callback)
}
InfluxDB.prototype._createKeyValueString = function (object) {
var output = []
var clone = _.clone(object)
delete clone.time
_.forOwn(clone, function (value, key) {
if (typeof value === 'string') {
output.push(escape(key) + '="' + value.replace(/"/g, '\\"') + '"') // For string field values use a backslash character to escape double quotes
} else {
output.push(escape(key) + '=' + value)
}
})
return output.join(',')
}
/**
* Return a sorted string of comma-separated key=value tags, with escaped values
* @param object
* @returns {string}
* @private
*/
InfluxDB.prototype._createKeyTagString = function (object) {
var output = []
_.forOwn(object, function (value, key) {
if (typeof value === 'string') {
output.push(escape(key) + '=' + escape(value))
} else {
output.push(escape(key) + '=' + value)
}
})
// "For best performance you should sort tags by key before sending them to the database."
return output.sort().join(',')
}
InfluxDB.prototype._prepareValues = function (series) {
var self = this
var output = []
_.forEach(series, function (fields, seriesName) {
_.each(fields, function (points) {
var line = seriesName.replace(/ /g, '\\ ').replace(/,/g, '\\,')
if (points[1] && _.isObject(points[1]) && _.keys(points[1]).length > 0) {
line += ',' + self._createKeyTagString(points[1])
}
if (_.isObject(points[0])) {
var timestamp = null
if (points[0].time) {
timestamp = points[0].time
}
line += ' ' + self._createKeyValueString(points[0])
if (timestamp) {
if (timestamp instanceof Date) {
line += ' ' + timestamp.getTime()
} else {
line += ' ' + timestamp
}
}
} else {
if (typeof points[0] === 'string') {
line += ' value="' + points[0] + '"'
} else {
line += ' value=' + points[0]
}
}
output.push(line)
}, this)
}, this)
return output.join('\n')
}
InfluxDB.prototype.writeSeries = function (series, options, callback) {
var args = resolveOptCallback(options, callback)
if (!args.options.database) {
args.options.database = this.options.database
}
if (!args.options.precision) {
args.options.precision = this.options.timePrecision
}
this.request.post({
url: this.url('write', args.options),
pool: typeof args.options.pool !== 'undefined' ? args.options.pool : {},
body: this._prepareValues(series)
}, this._parseCallback(args.callback))
}
InfluxDB.prototype.writePoint = function (seriesName, fields, tags, options, callback) {
this.writePoints(seriesName, [[fields, tags]], options, callback)
}
InfluxDB.prototype.writePoints = function (seriesName, points, options, callback) {
var data = {}
data[seriesName] = points
this.writeSeries(data, options, callback)
}
InfluxDB.prototype.query = function (databaseName, query, callback) {
if (typeof query === 'function') {
callback = query
query = databaseName
databaseName = this.options.database
}
var self = this
this.queryDB(query, {db: databaseName}, function (err, results) {
if (err) {
return callback(err, results)
}
return self._parseResults(results, function (err, results) {
return callback(err, results)
})
})
}
InfluxDB.prototype.queryRaw = function (databaseName, query, callback) {
if (typeof query === 'function') {
callback = query
query = databaseName
databaseName = this.options.database
}
this.queryDB(query, {db: databaseName}, callback)
}
InfluxDB.prototype.createContinuousQuery = function (queryName, queryString, databaseName, callback) {
if (typeof databaseName === 'function') {
callback = databaseName
databaseName = this.options.database
}
var query = 'CREATE CONTINUOUS QUERY ' + queryName + ' ON "' + databaseName + '" BEGIN ' +
queryString +
' END'
this.updateDB(query, callback)
}
InfluxDB.prototype.getContinuousQueries = function (callback) {
var self = this
this.queryDB('SHOW CONTINUOUS QUERIES', function (err, result) {
if (err) return callback(err)
self._parseResults(result, callback)
})
}
InfluxDB.prototype.dropContinuousQuery = function (queryName, databaseName, callback) {
if (typeof databaseName === 'function') {
callback = databaseName
databaseName = this.options.database
}
this.updateDB('DROP CONTINUOUS QUERY "' + queryName + '" ON "' + databaseName + '"', callback)
}
InfluxDB.prototype.createRetentionPolicy = function (rpName, databaseName, duration, replication, isDefault, callback) {
var query = 'create retention policy "' + rpName +
'" on "' + databaseName +
'" duration ' + duration +
' replication ' + replication
if (isDefault) {
query += ' default'
}
this.updateDB(query, callback)
}
InfluxDB.prototype.getRetentionPolicies = function (databaseName, callback) {
this.queryDB('show retention policies on "' + databaseName + '"', callback)
}
InfluxDB.prototype.alterRetentionPolicy = function (rpName, databaseName, duration, replication, isDefault, callback) {
var query = 'alter retention policy "' + rpName +
'" on "' + databaseName + '"'
if (duration) {
query += ' duration ' + duration
}
if (replication) {
query += ' replication ' + replication
}
if (isDefault) {
query += ' default'
}
this.updateDB(query, callback)
}
InfluxDB.prototype.getHostsAvailable = function () {
return this.request.getHostsAvailable()
}
InfluxDB.prototype.getHostsDisabled = function () {
return this.request.getHostsDisabled()
}
var createClient = function () {
var args = arguments
var Client = function () { return InfluxDB.apply(this, args) }
Client.prototype = InfluxDB.prototype
return new Client()
}
module.exports = createClient
module.exports.InfluxDB = InfluxDB
module.exports.defaultOptions = defaultOptions