diff --git a/lib/kairosdb.js b/lib/kairosdb.js index b77959d..e51ff89 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, @@ -17,7 +16,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(); @@ -70,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) { @@ -83,47 +84,64 @@ 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) { + 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); } - res.setEncoding('utf8'); - if (res.statusCode === 404) - return callback(new Error('404: page not found')); - else if (res.statusCode === 400) { - return callback(new Error('400: server error')); + 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 === 500) - return callback(new Error('500: server error')); - - 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 (e) { + e.statusCode = res.statusCode; + e.body = body; + return callback(e); + } + else { + return callback(null, resultObj); + } }); - - if (options.method === 'POST' && payload) - req.write(JSON.stringify(payload)); - - req.end(); } catch (ex) { return callback(ex); @@ -136,7 +154,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; diff --git a/package.json b/package.json index ee14658..1ea0f02 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "kairosdb", - "version": "0.0.3", + "version": "0.0.6", "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" 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