From c35d73100a1b71deaf0a643bcb844af2fd6750c7 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 09:43:20 +0100 Subject: [PATCH 1/9] Fix spelling mistake in package.json, solve #8 --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index ee14658..121a35a 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,13 @@ "name": "kairosdb", "version": "0.0.3", "author": "Itay Weinberger ", - "description": "NodeJS library for communication with KariosDB", + "description": "NodeJS library for communication with KairosDB", "engine": "node >= 0.10.x", "repository": { "type": "git", - "url": "https://github.com/itayw/kariosdb.git" + "url": "https://github.com/itayw/kairosdb.git" }, - "bugs": "https://github.com/itayw/kariosdb/issues", + "bugs": "https://github.com/itayw/kairosdb/issues", "contributors": [ { "name": "Itay Weinberger", @@ -37,7 +37,7 @@ }, "main": "lib/kairosdb.js", "keywords": [ - "kariosdb", + "kairosdb", "cassandra" ], "license": "MIT" From fb8a5a9767307061679e66375856a3224ff32c59 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 10:12:24 +0100 Subject: [PATCH 2/9] Fix switched host, port --- lib/kairosdb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kairosdb.js b/lib/kairosdb.js index b77959d..2ff009a 100644 --- a/lib/kairosdb.js +++ b/lib/kairosdb.js @@ -136,7 +136,7 @@ exports.init = function (host_arg, port_arg, options) { host = host_arg || default_host, kdb; - kdb = new KairosDB(port, host, options); + kdb = new KairosDB(host, port, options); kdb.port = port; kdb.host = host; From 601e1506febeac48a5b129be080cf7e3cac7f6e6 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 10:16:46 +0100 Subject: [PATCH 3/9] Add statusCode property to error objects --- lib/kairosdb.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/kairosdb.js b/lib/kairosdb.js index 2ff009a..ae83118 100644 --- a/lib/kairosdb.js +++ b/lib/kairosdb.js @@ -90,13 +90,21 @@ KairosDB.prototype.execute = function (endpoint, options, payload, callback) { } res.setEncoding('utf8'); - if (res.statusCode === 404) - return callback(new Error('404: page not found')); + if (res.statusCode === 404) { + var e = new Error('404: page not found'); + e.statusCode = res.statusCode; + return callback(e); + } else if (res.statusCode === 400) { - return callback(new Error('400: server error')); + var e = new Error('400: server error'); + e.statusCode = res.statusCode; + return callback(e); + } + else if (res.statusCode === 500) { + var e = new Error('500: server error'); + e.statusCode = res.statusCode; + return callback(e); } - else if (res.statusCode === 500) - return callback(new Error('500: server error')); var buffer = ''; res.on('data', function (chunk) { From 7d43c22e16dab4080474f5b3b011c9fab8ba8c73 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 10:29:45 +0100 Subject: [PATCH 4/9] Update version to 0.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 121a35a..25232f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kairosdb", - "version": "0.0.3", + "version": "0.0.4", "author": "Itay Weinberger ", "description": "NodeJS library for communication with KairosDB", "engine": "node >= 0.10.x", From 9a8318ad0c9f7a7e9b855fe279108fba7b3d5f1d Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 13:15:03 +0100 Subject: [PATCH 5/9] Add endpoints for metricnames and tags --- lib/kairosdb.js | 6 +++++- test/unit/metricnames.spec.js | 16 +++++++++++++++ test/unit/tagnames.spec.js | 16 +++++++++++++++ test/unit/tags.spec.js | 38 +++++++++++++++++++++++++++++++++++ test/unit/tagvalues.spec.js | 16 +++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/unit/metricnames.spec.js create mode 100644 test/unit/tagnames.spec.js create mode 100644 test/unit/tags.spec.js create mode 100644 test/unit/tagvalues.spec.js diff --git a/lib/kairosdb.js b/lib/kairosdb.js index ae83118..5b0e218 100644 --- a/lib/kairosdb.js +++ b/lib/kairosdb.js @@ -17,7 +17,11 @@ function KairosDB(host, port, options) { this.endpoints = [ ['version', 'GET'], ['query', 'POST', 'datapoints'], - 'datapoints' + 'datapoints', + ['metricnames', 'GET'], + ['tagnames', 'GET'], + ['tagvalues', 'GET'], + ['tags', 'POST', 'datapoints/query'] ]; this.buildStub(); diff --git a/test/unit/metricnames.spec.js b/test/unit/metricnames.spec.js new file mode 100644 index 0000000..fb430ba --- /dev/null +++ b/test/unit/metricnames.spec.js @@ -0,0 +1,16 @@ +var kdb = require('../../lib/kairosdb'); + +describe("metricnames", function () { + it("should get all existing metricnames", function (done) { + var client = kdb.init(global.options.host, global.options.port); + + client.metricnames(function (err, metrics) { + if (err) + return done(err); + expect(metrics.results).to.be.ok; + metrics = metrics.results; + expect(metrics).to.not.be.empty; + done(); + }); + }); +}); \ No newline at end of file diff --git a/test/unit/tagnames.spec.js b/test/unit/tagnames.spec.js new file mode 100644 index 0000000..e3bdccd --- /dev/null +++ b/test/unit/tagnames.spec.js @@ -0,0 +1,16 @@ +var kdb = require('../../lib/kairosdb'); + +describe("tagnames", function () { + it("should get all existing tagnames", function (done) { + var client = kdb.init(global.options.host, global.options.port); + + client.tagnames(function (err, tags) { + if (err) + return done(err); + expect(tags.results).to.be.ok; + tags = tags.results; + expect(tags).to.not.be.empty; + done(); + }); + }); +}); \ No newline at end of file diff --git a/test/unit/tags.spec.js b/test/unit/tags.spec.js new file mode 100644 index 0000000..bf32d4f --- /dev/null +++ b/test/unit/tags.spec.js @@ -0,0 +1,38 @@ +var kdb = require('../../lib/kairosdb'); + +describe("tags", function () { + it("should query tags of a given metric", function (done) { + var client = kdb.init(global.options.host, global.options.port); + + var data = { + "start_relative": { + "value": "1", + "unit": "years" + }, + "metrics": [ + { + "name": "metric_for_or", + "tags": { + "host": ["server2"] + } + } + ] + }; + + client.tags(data, function (err, metrics) { + if (err) + return done(err); + + expect(metrics.queries).to.be.ok; + metrics = metrics.queries; + expect(metrics).to.not.be.empty; + + var metric = metrics[0].results[0]; + expect(metric.tags).to.be.ok; + expect(metric.tags).to.have.property("host"); + expect('server2').to.be.oneOf(metric.tags.host); + + done(); + }); + }); +}); \ No newline at end of file diff --git a/test/unit/tagvalues.spec.js b/test/unit/tagvalues.spec.js new file mode 100644 index 0000000..e3b7a42 --- /dev/null +++ b/test/unit/tagvalues.spec.js @@ -0,0 +1,16 @@ +var kdb = require('../../lib/kairosdb'); + +describe("tagvalues", function () { + it("should get all existing tagvalues", function (done) { + var client = kdb.init(global.options.host, global.options.port); + + client.tagvalues(function (err, tags) { + if (err) + return done(err); + expect(tags.results).to.be.ok; + tags = tags.results; + expect(tags).to.not.be.empty; + done(); + }); + }); +}); \ No newline at end of file From 04cff1b6d364c4435ec57456f8d3e71a117ee258 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 13:16:04 +0100 Subject: [PATCH 6/9] Update version to 0.0.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 25232f3..416f3cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kairosdb", - "version": "0.0.4", + "version": "0.0.5", "author": "Itay Weinberger ", "description": "NodeJS library for communication with KairosDB", "engine": "node >= 0.10.x", From 16769a9e12e3da93d7f604eef1b03556617f3529 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 14:35:07 +0100 Subject: [PATCH 7/9] Use request client instead of http --- lib/kairosdb.js | 48 +++++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/lib/kairosdb.js b/lib/kairosdb.js index 5b0e218..e0b9242 100644 --- a/lib/kairosdb.js +++ b/lib/kairosdb.js @@ -1,7 +1,6 @@ var util = require('util'), events = require('events'), - http = require('http'), request = require('request'), connection_id = 0, @@ -74,9 +73,7 @@ KairosDB.prototype.execute = function (endpoint, options, payload, callback) { method = options.method; var _options = { - host: this.host, - port: this.port, - path: '/api/' + apiversion + '/' + endpoint, + uri: 'http://' + this.host + ':' + this.port + '/api/' + apiversion + '/' + endpoint, method: method }; if (options.method === 'POST' && payload) { @@ -87,12 +84,18 @@ KairosDB.prototype.execute = function (endpoint, options, payload, callback) { } try { - var req = http.request(_options, function (res) { + if (options.method === 'POST' && payload) + _options.body = JSON.stringify(payload); + + request(_options, function (error, res, body) { + if (error) + return callback(error); + if (self.options.debug) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); + console.log('BODY: ' + body); } - res.setEncoding('utf8'); if (res.statusCode === 404) { var e = new Error('404: page not found'); @@ -109,33 +112,16 @@ KairosDB.prototype.execute = function (endpoint, options, payload, callback) { e.statusCode = res.statusCode; return callback(e); } + else if (res.statusCode === 200) { + return callback(null, JSON.parse(body)); + } + else if (res.statusCode === 204) { + return callback(null, ""); + } + else + return callback(null, body); - var buffer = ''; - res.on('data', function (chunk) { - if (self.options.debug) - console.log('BODY: ' + chunk); - buffer += chunk; - }); - - res.on('end', function () { - try { - buffer = JSON.parse(buffer); - } - catch (ex) { - - } - return callback(null, buffer); - }); - }); - - req.on('error', function (err) { - return callback(err); }); - - if (options.method === 'POST' && payload) - req.write(JSON.stringify(payload)); - - req.end(); } catch (ex) { return callback(ex); From 93848ffe922177c1c7c83543864961711d82af09 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 15:04:01 +0100 Subject: [PATCH 8/9] Refactor response error handling Change error messages to those from Wikipedia --- lib/kairosdb.js | 62 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/lib/kairosdb.js b/lib/kairosdb.js index e0b9242..e51ff89 100644 --- a/lib/kairosdb.js +++ b/lib/kairosdb.js @@ -88,39 +88,59 @@ KairosDB.prototype.execute = function (endpoint, options, payload, callback) { _options.body = JSON.stringify(payload); request(_options, function (error, res, body) { - if (error) + if (error) { + if (self.options.debug) + console.log(error); return callback(error); - + } + if (self.options.debug) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); console.log('BODY: ' + body); } - if (res.statusCode === 404) { - var e = new Error('404: page not found'); - e.statusCode = res.statusCode; - return callback(e); + var e = null; + var resultObj = null; + + switch (res.statusCode) { + case 200: + resultObj = JSON.parse(body); + break; + case 204: + resultObj = ""; + break; + case 400: + e = new Error('400: Bad Request'); + break; + case 401: + e = new Error('401: Unauthorized'); + break; + case 403: + e = new Error('403: Forbidden'); + break; + case 404: + e = new Error('404: Not Found'); + break; + case 500: + e = new Error('500: Internal Server Error'); + break; + case 503: + e = new Error('503: Service Unavailable'); + break; + default: + resultObj = body; + break; } - else if (res.statusCode === 400) { - var e = new Error('400: server error'); - e.statusCode = res.statusCode; - return callback(e); - } - else if (res.statusCode === 500) { - var e = new Error('500: server error'); + + if (e) { e.statusCode = res.statusCode; + e.body = body; return callback(e); } - else if (res.statusCode === 200) { - return callback(null, JSON.parse(body)); - } - else if (res.statusCode === 204) { - return callback(null, ""); + else { + return callback(null, resultObj); } - else - return callback(null, body); - }); } catch (ex) { From 5509a54b46bcc886b5c6c946785d8c43092022e0 Mon Sep 17 00:00:00 2001 From: David Wettstein Date: Thu, 29 Dec 2016 15:15:19 +0100 Subject: [PATCH 9/9] Update version to 0.0.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 416f3cf..1ea0f02 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kairosdb", - "version": "0.0.5", + "version": "0.0.6", "author": "Itay Weinberger ", "description": "NodeJS library for communication with KairosDB", "engine": "node >= 0.10.x",