Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Commit

Permalink
Provide default start & stop; custom limits.
Browse files Browse the repository at this point in the history
Now you can request the most recent event as a simple GET request:

  /1.0/event?expression=random&limit=1

Or, get the value and apply a filter:

  /1.0/event?expression=random(random).gt(random,0)&limit=1

Likewise you can get the most recent metric for a given expression:

  /1.0/metric?expression=sum(random)&step=1e4&limit=1

This is convenient if you want to generate alerts based on recent events.
  • Loading branch information
mbostock committed Apr 15, 2012
1 parent 3c34d0e commit 614573e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions lib/cube/server/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var endpoint = require("./endpoint"),
// To avoid running out of memory, the GET endpoints have a maximum number of
// values they can return. If the limit is exceeded, only the most recent
// results are returned.
var limit = 1e4;
var limitMax = 1e4;

var headers = {
"Content-Type": "application/json",
Expand All @@ -28,14 +28,15 @@ exports.register = function(db, endpoints) {

function eventGet(request, response) {
request = url.parse(request.url, true).query;
var data = [];

// A stop time is required for the REST API.
if (!("stop" in request)) {
response.writeHead(400, headers);
response.end(JSON.stringify({error: "invalid stop"}));
return;
}
var data = [],
limit = +request.limit;

// Provide default start and stop times for recent events.
// If the limit is not specified, or too big, use the maximum limit.
if (!("stop" in request)) request.stop = Date.now();
if (!("start" in request)) request.start = 0;
if (!(limit <= limitMax)) limit = limitMax;

if (event(request, callback) < 0) {
response.writeHead(400, headers);
Expand All @@ -52,15 +53,23 @@ exports.register = function(db, endpoints) {

function metricGet(request, response) {
request = url.parse(request.url, true).query;

var data = [],
start = new Date(request.start),
stop = new Date(request.stop),
limit = +request.limit,
step = +request.step;

// Provide default start and stop times for recent metrics.
// If the limit is not specified, or too big, use the maximum limit.
if (!("stop" in request)) request.stop = Math.floor(Date.now() / step) * step;
if (!("start" in request)) request.start = 0;
if (!(limit <= limitMax)) limit = limitMax;

// If the time between start and stop is too long, then bring the start time
// forward so that only the most recent results are returned. This is only
// approximate in the case of months, but why would you want to return
// exactly ten thousand months? Don't rely on exact limits!
var start = new Date(request.start),
stop = new Date(request.stop);
if ((stop - start) / step > limit) request.start = new Date(stop - step * limit);

if (metric(request, callback) < 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/cube/server/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports.getter = function(db) {
}

// Round start and stop to the appropriate time step.
var tier = tiers[request.step];
var tier = tiers[+request.step];
if (!tier) return callback({error: "invalid step"}), -1;
start = tier.floor(start);
stop = tier.ceil(stop);
Expand Down

0 comments on commit 614573e

Please sign in to comment.