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

Commit

Permalink
Merge branch 'metric-get'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 18, 2012
2 parents 3588bbc + 499a0d0 commit 795b00b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/cube/client/piece-area.js
Expand Up @@ -27,7 +27,8 @@ cube.piece.type.area = function(board) {
.attr("class", "time") .attr("class", "time")
.text("Time Range:"); .text("Time Range:");


time.append("input"); time.append("input")
.property("value", 1440);


time.append("select").selectAll("option") time.append("select").selectAll("option")
.data([ .data([
Expand Down
3 changes: 2 additions & 1 deletion lib/cube/client/piece-sum.js
Expand Up @@ -28,7 +28,8 @@ cube.piece.type.sum = function(board) {
.attr("class", "time") .attr("class", "time")
.text("Time Range:"); .text("Time Range:");


time.append("input"); time.append("input")
.property("value", 1440);


time.append("select").selectAll("option") time.append("select").selectAll("option")
.data([ .data([
Expand Down
6 changes: 4 additions & 2 deletions lib/cube/server/collector.js
Expand Up @@ -2,12 +2,14 @@ var endpoint = require("./endpoint"),
util = require("util"); util = require("util");


exports.register = function(db, endpoints) { exports.register = function(db, endpoints) {
var putter = require("./event").putter(db); var putter = require("./event").putter(db),
poster = post(putter);
endpoints.ws.push( endpoints.ws.push(
endpoint.exact("/1.0/event/put", putter) endpoint.exact("/1.0/event/put", putter)
); );
endpoints.http.push( endpoints.http.push(
endpoint.exact("POST", "/1.0/event/put", post(putter)), endpoint.exact("POST", "/1.0/event", poster),
endpoint.exact("POST", "/1.0/event/put", poster),
endpoint.exact("POST", "/collectd", require("./collectd").putter(putter)) endpoint.exact("POST", "/collectd", require("./collectd").putter(putter))
); );
}; };
Expand Down
37 changes: 34 additions & 3 deletions lib/cube/server/evaluator.js
@@ -1,8 +1,39 @@
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) { exports.register = function(db, endpoints) {
var event = require("./event").getter(db),
metric = require("./metric").getter(db);

endpoints.ws.push( endpoints.ws.push(
endpoint.exact("/1.0/event/get", require("./event").getter(db)), endpoint.exact("/1.0/event/get", event),
endpoint.exact("/1.0/metric/get", require("./metric").getter(db)) endpoint.exact("/1.0/metric/get", metric)
);
endpoints.http.push(
endpoint.exact("GET", "/1.0/metric", metricGet),
endpoint.exact("GET", "/1.0/metric/get", 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
Expand Up @@ -261,21 +261,21 @@ exports.getter = function(db) {
var start = new Date(request.start), var start = new Date(request.start),
stop = new Date(request.stop), stop = new Date(request.stop),
id = request.id; id = request.id;
if (isNaN(start)) return util.log("invalid start: " + request.start); if (isNaN(start)) return util.log("invalid start: " + request.start), -1;
if (isNaN(stop)) return util.log("invalid stop: " + request.stop); if (isNaN(stop)) return util.log("invalid stop: " + request.stop), -1;


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


// Round start and stop to the appropriate time step. // Round start and stop to the appropriate time step.
var tier = tiers[request.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); start = tier.floor(start);
stop = tier.ceil(stop); stop = tier.ceil(stop);


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

// Return the expected number of values.
var expected = 0;
while ((start = tier.step(start)) < stop) ++expected;
return expected;
}; };
}; };
4 changes: 2 additions & 2 deletions package.json
@@ -1,14 +1,14 @@
{ {
"name": "cube", "name": "cube",
"version": "0.0.14", "version": "0.0.15",
"description": "A system for time series visualization using MongoDB, Node and D3.", "description": "A system for time series visualization using MongoDB, Node and D3.",
"keywords": ["time series", "visualization"], "keywords": ["time series", "visualization"],
"homepage": "http://square.github.com/cube/", "homepage": "http://square.github.com/cube/",
"author": {"name": "Mike Bostock", "url": "http://bost.ocks.org/mike"}, "author": {"name": "Mike Bostock", "url": "http://bost.ocks.org/mike"},
"repository": {"type": "git", "url": "http://github.com/square/cube.git"}, "repository": {"type": "git", "url": "http://github.com/square/cube.git"},
"main": "./lib/cube", "main": "./lib/cube",
"dependencies": { "dependencies": {
"d3": "2.6.0", "d3": "2.7.2",
"mongodb": "0.9.7-1.3", "mongodb": "0.9.7-1.3",
"pegjs": "0.6.2", "pegjs": "0.6.2",
"vows": "0.5.11", "vows": "0.5.11",
Expand Down

0 comments on commit 795b00b

Please sign in to comment.