Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[api test] Added `._checkRange()` to Loggly searches to parse and val…

…idate timespans
  • Loading branch information...
commit 6a05387fe8def5a88314d1f40dda6f11fa250f3f 1 parent 913a832
Charlie Robbins indexzero authored
Showing with 70 additions and 3 deletions.
  1. +46 −2 lib/loggly/search.js
  2. +2 −1  package.json
  3. +22 −0 test/search-test.js
48 lib/loggly/search.js
View
@@ -7,6 +7,7 @@
*/
var qs = require('querystring'),
+ timespan = require('timespan'),
interns = require('./interns');
//
@@ -48,14 +49,22 @@ Search.prototype.context = function (context) {
};
//
-// function run (callback)
-//
+// ### function run (callback)
+// #### @callback {function} Continuation to respond to when complete
+// Runs the search query for for this instance with the query, metadata,
+// context, and other parameters that have been configured on it.
//
Search.prototype.run = function (callback) {
+ var rangeError;
+
+ //
// Trim the search query
+ //
this.query.trim();
+ //
// Update the callback for this instance if it's passed
+ //
this.callback = callback || this.callback;
if (!this.callback) {
throw new Error('Cannot run search without a callback function.');
@@ -69,6 +78,7 @@ Search.prototype.run = function (callback) {
// Set the context for the query string
this._context = this._context || {};
this._context.q = this.query;
+ this._checkRange();
var self = this, searchOptions = {
uri: this.client.logglyUrl(this.baseUrl + qs.stringify(this._context)),
@@ -80,4 +90,38 @@ Search.prototype.run = function (callback) {
});
return this;
+};
+
+//
+// ### function _checkRange ()
+// Checks if the range that has been configured for this
+// instance is valid and updates if it is not.
+//
+Search.prototype._checkRange = function () {
+ if (!this._context || (!this._context.until && !this._context.from)) {
+ return;
+ }
+
+ this._context.until = this._context.until || 'NOW';
+ this._context.from = this._context.from || 'NOW-24HOURS';
+
+ if (!timespan.parseDate(this._context.until)) {
+ this._context.until = 'NOW';
+ }
+
+ if (!timespan.parseDate(this._context.from)) {
+ this._context.from = 'NOW-24HOURS';
+ }
+
+ if (timespan.fromDates(this._context.from, this._context.until) < 0
+ || this._context.until === this._context.from) {
+ //
+ // If the length of the timespan for this Search instance is
+ // negative then set it to default values
+ //
+ this._context.until = 'NOW';
+ this._context.from = 'NOW-24HOURS';
+ }
+
+ return this;
};
3  package.json
View
@@ -14,7 +14,8 @@
},
"keywords": ["cloud computing", "api", "logging", "loggly"],
"dependencies": {
- "request": "1.9.x"
+ "request": "1.9.x",
+ "timespan": "2.x.x"
},
"devDependencies": {
"vows": "0.5.x"
22 test/search-test.js
View
@@ -57,6 +57,28 @@ vows.describe('node-loggly/search').addBatch({
helpers.assertSearch(err, results);
}
}
+ },
+ "the _checkRange() method": {
+ "with invalid options set": {
+ "should correct them": function () {
+ var search = loggly.search('logging message')
+ .context({ from: 'NOW', until: '1DAY' })
+ ._checkRange();
+
+ assert.equal(search._context.from, 'NOW-24HOURS');
+ assert.equal(search._context.until, 'NOW');
+ }
+ },
+ "with valid options set": {
+ "should not modify them": function () {
+ var search = loggly.search('logging message')
+ .context({ from: 'NOW-2MONTHS', until: 'NOW' })
+ ._checkRange();
+
+ assert.equal(search._context.from, 'NOW-2MONTHS');
+ assert.equal(search._context.until, 'NOW');
+ }
+ }
}
}
}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.