Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replaced `getSince` function of the sampledb

  • Loading branch information...
commit 8d52ab6774269880169ed18f7fc401807174baa0 1 parent 4372fe7
@DamonOehlman authored
Showing with 70 additions and 11 deletions.
  1. +59 −5 lib/sampledb.js
  2. +11 −6 test/takesample.js
View
64 lib/sampledb.js
@@ -9,13 +9,67 @@ SampleDB.prototype.close = function() {
this.storage.close();
}; // close
+SampleDB.prototype.getSince = function(time, callback, maxItems) {
+ var targetKey = time.toString(),
+ iterator = this.storage.newIterator({}),
+ itemCount = maxItems || 1000,
+ startTick,
+ lastTick,
+ items = {};
+
+ // seek to the first item after the specified tick count
+ iterator.seek(new Buffer(targetKey));
+
+ // while we have items, and we haven't returned more than max items
+ while (iterator.valid() && itemCount--) {
+ var keyParts = (iterator.key() || '').toString().split('_'),
+ tick = keyParts[0],
+ type = keyParts[1] || 'misc',
+ rawValue = (iterator.value() || '').toString(),
+ item;
+
+ if (rawValue) {
+ item = JSON.parse(rawValue);
+
+ // ensure we have a start value
+ startTick = startTick || tick;
+
+ // check that we have a types array for the current type
+ if (! items[type]) {
+ items[type] = [];
+ } // if
+
+ // calculate the item tick diff
+ item.delta = tick - startTick;
+
+ // add the item to the items array
+ items[type].push(item);
+
+ // update the last tick
+ lastTick = tick;
+ } // if
+
+ // get the next value
+ iterator.next();
+ } // while
+
+ // trigger the callback
+ callback({
+ start: startTick,
+ end: lastTick,
+ more: itemCount <= 0,
+
+ items: items
+ });
+}; // getSince
+
SampleDB.prototype.save = function(type, data, details) {
var tick = new Date().getTime(),
- serialized = {
- data: JSON.stringify(data),
- details: details ? JSON.stringify(details) : undefined
- },
+ entry = JSON.stringify({
+ data: data,
+ details: details
+ }),
entryKey = tick + '_' + type;
- this.storage.put({}, new Buffer(entryKey), new Buffer(serialized));
+ this.storage.put({}, new Buffer(entryKey), new Buffer(entry));
};
View
17 test/takesample.js
@@ -1,11 +1,16 @@
var collector = require('../lib/collector'),
- collectionProcess;
+ collectionProcess,
+ startTick = new Date().getTime();
console.log('Collecting 5 seconds worth of data');
collectionProcess = collector.collectToDB();
-collectionProcess.collector.on('data', function(agentId, data, details) {
- console.log(agentId, data);
-});
-
-setTimeout(collectionProcess.stop, 5000);
+setTimeout(function() {
+ collectionProcess.db.getSince(startTick, function(results) {
+ for (var key in results.items) {
+ console.log(key, results.items[key].length);
+ } // for
+ });
+
+ collectionProcess.stop();
+}, 5000);
Please sign in to comment.
Something went wrong with that request. Please try again.