Permalink
Browse files

Merge branch 'metric-get'

  • Loading branch information...
2 parents 3588bbc + 499a0d0 commit 795b00bb99d80fdd43519d09071bb27ba3a6abda @mbostock mbostock committed Jan 18, 2012
@@ -27,7 +27,8 @@ cube.piece.type.area = function(board) {
.attr("class", "time")
.text("Time Range:");
- time.append("input");
+ time.append("input")
+ .property("value", 1440);
time.append("select").selectAll("option")
.data([
@@ -28,7 +28,8 @@ cube.piece.type.sum = function(board) {
.attr("class", "time")
.text("Time Range:");
- time.append("input");
+ time.append("input")
+ .property("value", 1440);
time.append("select").selectAll("option")
.data([
@@ -2,12 +2,14 @@ var endpoint = require("./endpoint"),
util = require("util");
exports.register = function(db, endpoints) {
- var putter = require("./event").putter(db);
+ var putter = require("./event").putter(db),
+ poster = post(putter);
endpoints.ws.push(
endpoint.exact("/1.0/event/put", putter)
);
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))
);
};
@@ -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) {
+ 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),
+ 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;
+}
@@ -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);
@@ -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;
};
};
View
@@ -1,14 +1,14 @@
{
"name": "cube",
- "version": "0.0.14",
+ "version": "0.0.15",
"description": "A system for time series visualization using MongoDB, Node and D3.",
"keywords": ["time series", "visualization"],
"homepage": "http://square.github.com/cube/",
"author": {"name": "Mike Bostock", "url": "http://bost.ocks.org/mike"},
"repository": {"type": "git", "url": "http://github.com/square/cube.git"},
"main": "./lib/cube",
"dependencies": {
- "d3": "2.6.0",
+ "d3": "2.7.2",
"mongodb": "0.9.7-1.3",
"pegjs": "0.6.2",
"vows": "0.5.11",

0 comments on commit 795b00b

Please sign in to comment.