Skip to content

Commit

Permalink
Allow HTTP GET for metrics.
Browse files Browse the repository at this point in the history
You can now issue a simple HTTP GET to retrieve metrics from Cube synchronously,
rather than fetching metrics asynchronously via WebSockets. The parameters are
identical to the WebSockets API. So,

    {
      "expression": "sum(random)",
      "start": "2012-01-09T01:23:00Z",
      "stop": "2012-01-10T04:56:00Z",
      "step": 300000
    }

Is equivalent to:

   http://localhost:1081/1.0/metric
     ?expression=sum(random)
     &start=2012-01-09T01:23:00Z
     &stop=2012-01-10T04:56:00Z
     &step=300000

The results are returned as a JSON array in chronological order. If an error
occurs, an empty array of results are returned. Fixes #24.
  • Loading branch information
mbostock committed Jan 13, 2012
1 parent 061ee59 commit 6796e23
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
36 changes: 33 additions & 3 deletions lib/cube/server/evaluator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
var endpoint = require("./endpoint");
var endpoint = require("./endpoint"),
url = require("url");

var headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
};

exports.register = function(db, endpoints) {
var event = require("./event").getter(db),
metric = require("./metric").getter(db);

endpoints.ws.push(
endpoint.exact("/1.0/event/get", require("./event").getter(db)),
endpoint.exact("/1.0/metric/get", require("./metric").getter(db))
endpoint.exact("/1.0/event/get", event),
endpoint.exact("/1.0/metric/get", metric)
);
endpoints.http.push(
endpoint.exact("GET", "/1.0/metric", metricGet)
);

function metricGet(request, response) {
var values = [],
expected = metric(url.parse(request.url, true).query, callback);

response.writeHead(expected < 0 ? 400 : 200, headers);
if (expected <= 0) response.end(JSON.stringify([]));

function callback(value) {
if (values.push(value) === expected) {
response.end(JSON.stringify(values.sort(chronological)));
}
}
}
};

function chronological(a, b) {
return a.time - b.time;
}
15 changes: 10 additions & 5 deletions lib/cube/server/metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,21 @@ exports.getter = function(db) {
var start = new Date(request.start),
stop = new Date(request.stop),
id = request.id;
if (isNaN(start)) return util.log("invalid start: " + request.start);
if (isNaN(stop)) return util.log("invalid stop: " + request.stop);
if (isNaN(start)) return util.log("invalid start: " + request.start), -1;
if (isNaN(stop)) return util.log("invalid stop: " + request.stop), -1;

// Parse the expression.
// TODO store expression as JSON object, or compute canonical form
var expression;
try {
expression = parser.parse(request.expression);
} catch (error) {
return util.log("invalid expression: " + error);
} catch (e) {
return util.log("invalid expression: " + e), -1;
}

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

Expand All @@ -299,5 +299,10 @@ exports.getter = function(db) {
measure(expression, start, stop, tier, expression.group
? ("id" in request ? callbackGroupId : callbackGroup)
: ("id" in request ? callbackValueId : callbackValue));

// Return the expected number of values.
var expected = 0;
while ((start = tier.step(start)) < stop) ++expected;
return expected;
};
};

0 comments on commit 6796e23

Please sign in to comment.