node-tsguru
Advanced tools
Comparing version 1.2.0 to 1.3.0
44
index.js
var MAX_BODY_SIZE = 4000; | ||
var CSV_LINE_END = "\n"; | ||
@@ -20,3 +21,3 @@ var assert = require("assert-plus"); | ||
util.inherits(Client, events.EventEmitter); | ||
Client.prototype._insert = function(timeseriesName, value, cb) { | ||
Client.prototype._insert = function(timeseriesName, col, cb) { | ||
"use strict"; | ||
@@ -27,9 +28,7 @@ request({ | ||
headers: { | ||
"Authorization": "Token " + this.token | ||
}, | ||
"Authorization": "Token " + this.token, | ||
"Content-Type": "text/csv" | ||
}, | ||
timeout: this.timeout, | ||
json: true, | ||
body: { | ||
value: value | ||
} | ||
body: col.join(",") | ||
}, function(err, res) { | ||
@@ -47,13 +46,23 @@ if (err) { | ||
}; | ||
Client.prototype.insert = function(timeseriesName, value, cb) { | ||
Client.prototype.insert = function(timeseriesName) { | ||
"use strict"; | ||
assert.string(timeseriesName, "timeseriesName"); | ||
assert.number(value, "value"); | ||
if (arguments.length < 2) { | ||
assert.fail(arguments.length, 2, "to less arguments", ">="); | ||
} | ||
var cb, col; | ||
if (typeof arguments[arguments.length-1] === "function") { | ||
cb = arguments[arguments.length-1]; | ||
col = Array.prototype.slice.call(arguments, 1, (arguments.length-1)); | ||
} else { | ||
cb = undefined; | ||
col = Array.prototype.slice.call(arguments, 1, arguments.length); | ||
} | ||
assert.optionalFunc(cb, "cb"); | ||
var self = this; | ||
if (cb !== undefined) { | ||
self._insert(timeseriesName, value, cb); | ||
self._insert(timeseriesName, col, cb); | ||
} else { | ||
return new Promise(function(resolve, reject) { | ||
self._insert(timeseriesName, value, function(err, res) { | ||
self._insert(timeseriesName, col, function(err, res) { | ||
if (err) { | ||
@@ -70,16 +79,15 @@ reject(err); | ||
"use strict"; | ||
var values = []; | ||
var lines = []; | ||
var self = this; | ||
return { | ||
add: function(timeseriesName, value) { | ||
add: function(timeseriesName) { | ||
assert.string(timeseriesName, "timeseriesName"); | ||
assert.number(value, "value"); | ||
values.push({timeseriesName: timeseriesName, value: value}); | ||
lines.push(Array.prototype.slice.call(arguments).join(",") + CSV_LINE_END); | ||
return this; | ||
}, | ||
_insert: function(cb) { | ||
if (values.length === 0) { | ||
if (lines.length === 0) { | ||
return cb(); | ||
} | ||
var buffers = chunked(values, MAX_BODY_SIZE); | ||
var buffers = chunked(lines, MAX_BODY_SIZE); | ||
async.eachLimit(buffers, 4, function(buffer, cb) { | ||
@@ -91,3 +99,3 @@ request({ | ||
"Authorization": "Token " + self.token, | ||
"Content-type": "application/json" | ||
"Content-Type": "text/csv" | ||
}, | ||
@@ -94,0 +102,0 @@ timeout: self.timeout, |
@@ -1,8 +0,2 @@ | ||
var JSON_DELIMITER = ","; | ||
var BYTE_LENGTH_KLAMMER_AUF = Buffer.byteLength("[", "utf8"); | ||
var BYTE_LENGTH_KLAMMER_ZU = Buffer.byteLength("]", "utf8"); | ||
var BYTE_LENGTH_DELIMITER = Buffer.byteLength(JSON_DELIMITER, "utf8"); | ||
module.exports = function(values, maxChunkSize) { | ||
module.exports = function(lines, maxChunkSize) { | ||
"use strict"; | ||
@@ -12,3 +6,2 @@ var buffers = []; | ||
var currentBytes = 0; | ||
function write(str, bytes) { | ||
@@ -19,31 +12,13 @@ workingBuffer.write(str, currentBytes, bytes, "utf8"); | ||
function push() { | ||
write("]", BYTE_LENGTH_KLAMMER_ZU); | ||
var buffer = new Buffer(currentBytes); | ||
workingBuffer.copy(buffer, 0, 0, currentBytes); | ||
buffers.push(buffer); | ||
currentBytes = 0; | ||
} | ||
write("[", BYTE_LENGTH_KLAMMER_AUF); | ||
values.forEach(function(value) { | ||
var json = JSON.stringify(value); | ||
var jsonBytes = Buffer.byteLength(json, "utf8"); | ||
var jsonWithPossibleDelimiter; | ||
var jsonWithPossibleDelimiterBytes; | ||
if (currentBytes !== BYTE_LENGTH_KLAMMER_AUF) { | ||
jsonWithPossibleDelimiter = JSON_DELIMITER + json; | ||
jsonWithPossibleDelimiterBytes = BYTE_LENGTH_DELIMITER + jsonBytes; | ||
} else { | ||
jsonWithPossibleDelimiter = json; | ||
jsonWithPossibleDelimiterBytes = jsonBytes; | ||
} | ||
if ((currentBytes + jsonWithPossibleDelimiterBytes + BYTE_LENGTH_KLAMMER_ZU) > maxChunkSize) { | ||
lines.forEach(function(line) { | ||
var bytes = Buffer.byteLength(line, "utf8"); | ||
if ((currentBytes + bytes) > maxChunkSize) { | ||
push(); | ||
currentBytes = 0; | ||
write("[", BYTE_LENGTH_KLAMMER_AUF); | ||
jsonWithPossibleDelimiter = json; | ||
jsonWithPossibleDelimiterBytes = jsonBytes; | ||
} | ||
write(jsonWithPossibleDelimiter, jsonWithPossibleDelimiterBytes); | ||
write(line, bytes); | ||
}); | ||
@@ -50,0 +25,0 @@ push(); |
{ | ||
"name": "node-tsguru", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "TimeSeries.Guru Node.js client", | ||
@@ -5,0 +5,0 @@ "keywords": ["time", "series", "timeseries", "time-series", "guru", "timeseriesguru", "timeseries.guru", "client"], |
@@ -8,34 +8,23 @@ var assert = require("assert-plus"); | ||
it("one chunk, less than max", function() { | ||
var buffers = chunked([1], 5); | ||
var buffers = chunked(["a\n", "b\n"], 5); | ||
assert.equal(1, buffers.length); | ||
assert.equal("[1]", buffers[0].toString("utf8")); | ||
assert.equal("a\nb\n", buffers[0].toString("utf8")); | ||
}); | ||
it("one chunk, exactly max", function() { | ||
var buffers = chunked([1, 2], 5); | ||
var buffers = chunked(["ab\n", "c\n"], 5); | ||
assert.equal(1, buffers.length); | ||
assert.equal("[1,2]", buffers[0].toString("utf8")); | ||
assert.equal("ab\nc\n", buffers[0].toString("utf8")); | ||
}); | ||
it("two chunks, less than max", function() { | ||
var buffers = chunked([1, 2, 3], 5); | ||
var buffers = chunked(["a\n", "b\n", "c\n"], 5); | ||
assert.equal(2, buffers.length); | ||
assert.equal("[1,2]", buffers[0].toString("utf8")); | ||
assert.equal("[3]", buffers[1].toString("utf8")); | ||
assert.equal("a\nb\n", buffers[0].toString("utf8")); | ||
assert.equal("c\n", buffers[1].toString("utf8")); | ||
}); | ||
it("two chunks, exactly max", function() { | ||
var buffers = chunked([1, 2, 3, 4], 5); | ||
var buffers = chunked(["ab\n", "c\n", "d\n", "ef\n"], 5); | ||
assert.equal(2, buffers.length); | ||
assert.equal("[1,2]", buffers[0].toString("utf8")); | ||
assert.equal("[3,4]", buffers[1].toString("utf8")); | ||
assert.equal("ab\nc\n", buffers[0].toString("utf8")); | ||
assert.equal("d\nef\n", buffers[1].toString("utf8")); | ||
}); | ||
it("one chunk, less than max", function() { | ||
var buffers = chunked([1, 2, 3], 7); | ||
assert.equal(1, buffers.length); | ||
assert.equal("[1,2,3]", buffers[0].toString("utf8")); | ||
}); | ||
it("one chunk, less than max", function() { | ||
var buffers = chunked([1, 2, 3], 6); | ||
assert.equal(2, buffers.length); | ||
assert.equal("[1,2]", buffers[0].toString("utf8")); | ||
assert.equal("[3]", buffers[1].toString("utf8")); | ||
}); | ||
}); |
@@ -19,3 +19,4 @@ var mountebank = require("mountebank"); | ||
"headers": { | ||
"statusCodeontent-Type": "application/json" | ||
"Authorization": "Token XXX", | ||
"Content-Type": "application/json" | ||
}, | ||
@@ -39,5 +40,9 @@ "body": "{\"region\": \"eu-west-1\"}" | ||
"equals": { | ||
"path": "/databases/YYY/timeseries/sensor01/data", | ||
"path": "/databases/YYY/data", | ||
"method": "POST", | ||
"body": "{\"value\":123.45}" | ||
"headers": { | ||
"Authorization": "Token XXX", | ||
"Content-Type": "text/csv" | ||
}, | ||
"body": "sensor01,123.45\nsensor02,234.56\nsensor03,345.67\nsensor04,456.78\n" | ||
} | ||
@@ -55,5 +60,60 @@ }] | ||
"method": "POST", | ||
"body": "[{\"timeseriesName\":\"sensor01\",\"value\":123.45},{\"timeseriesName\":\"sensor02\",\"value\":234.56},{\"timeseriesName\":\"sensor03\",\"value\":345.67},{\"timeseriesName\":\"sensor04\",\"value\":456.78}]" | ||
"headers": { | ||
"Authorization": "Token XXX", | ||
"Content-Type": "text/csv" | ||
}, | ||
"body": "sensor05,a,123.45\nsensor06,b,234.56\nsensor07,c,345.67\nsensor08,d,456.78\n" | ||
} | ||
}] | ||
}, { | ||
"responses": [{ | ||
"is": { | ||
"statusCode": 204 | ||
} | ||
}], | ||
"predicates": [{ | ||
"equals": { | ||
"path": "/databases/YYY/data", | ||
"method": "POST", | ||
"headers": { | ||
"Authorization": "Token XXX", | ||
"Content-Type": "text/csv" | ||
}, | ||
"body": "sensor09,123.45\nsensor10,a,234.56\nsensor11,b,345.67\nsensor12,456.78\n" | ||
} | ||
}] | ||
}, { | ||
"responses": [{ | ||
"is": { | ||
"statusCode": 204 | ||
} | ||
}], | ||
"predicates": [{ | ||
"equals": { | ||
"path": "/databases/YYY/timeseries/sensor01/data", | ||
"method": "POST", | ||
"headers": { | ||
"Authorization": "Token XXX", | ||
"Content-Type": "text/csv" | ||
}, | ||
"body": "123.45" | ||
} | ||
}] | ||
}, { | ||
"responses": [{ | ||
"is": { | ||
"statusCode": 204 | ||
} | ||
}], | ||
"predicates": [{ | ||
"equals": { | ||
"path": "/databases/YYY/timeseries/sensor02/data", | ||
"method": "POST", | ||
"headers": { | ||
"Authorization": "Token XXX", | ||
"Content-Type": "text/csv" | ||
}, | ||
"body": "a,123.45" | ||
} | ||
}] | ||
}] | ||
@@ -80,7 +140,3 @@ } | ||
configreMountebank(2525, function(err) { | ||
if (err) { | ||
throw err; | ||
} else { | ||
done(); | ||
} | ||
done(err); | ||
}); | ||
@@ -98,18 +154,17 @@ }); | ||
tsguru({token: "XXX", databaseID: "YYY", apiBase: "http://localhost:4545"}, function(err, c) { | ||
if (err) { | ||
throw err; | ||
} | ||
client = c; | ||
done(); | ||
done(err); | ||
}); | ||
}); | ||
it("insert", function(done) { | ||
it("insert simpel", function(done) { | ||
client.insert("sensor01", 123.45, function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
done(); | ||
done(err); | ||
}); | ||
}); | ||
it("bulk", function(done) { | ||
it("insert symbol", function(done) { | ||
client.insert("sensor02", "a", 123.45, function(err) { | ||
done(err); | ||
}); | ||
}); | ||
it("bulk simpel", function(done) { | ||
client.bulk() | ||
@@ -121,8 +176,25 @@ .add("sensor01", 123.45) | ||
.insert(function(err) { | ||
if (err) { | ||
throw err; | ||
} | ||
done(); | ||
done(err); | ||
}); | ||
}); | ||
it("bulk symbol", function(done) { | ||
client.bulk() | ||
.add("sensor05", "a", 123.45) | ||
.add("sensor06", "b", 234.56) | ||
.add("sensor07", "c", 345.67) | ||
.add("sensor08", "d", 456.78) | ||
.insert(function(err) { | ||
done(err); | ||
}); | ||
}); | ||
it("bulk mixed", function(done) { | ||
client.bulk() | ||
.add("sensor09", 123.45) | ||
.add("sensor10", "a", 234.56) | ||
.add("sensor11", "b", 345.67) | ||
.add("sensor12", 456.78) | ||
.insert(function(err) { | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
@@ -135,5 +207,5 @@ describe("promise style", function() { | ||
client = c; | ||
done(); | ||
done(); | ||
}) | ||
.catch(function(err) {throw err;}); | ||
.catch(function(err) { done(err); }); | ||
}); | ||
@@ -143,3 +215,3 @@ it("insert", function(done) { | ||
.then(done) | ||
.catch(function(err) {throw err;}); | ||
.catch(function(err) { done(err); }); | ||
}); | ||
@@ -154,5 +226,5 @@ it("bulk", function(done) { | ||
.then(done) | ||
.catch(function(err) {throw err;}); | ||
.catch(function(err) { done(err); }); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
23703
494