Permalink
Browse files

Don't collect unknown event types.

  • Loading branch information...
1 parent c000599 commit 0debb3dec05e69c3a0af8b60a4e0db20e2f36d99 @mbostock mbostock committed Nov 22, 2011
Showing with 25 additions and 12 deletions.
  1. +25 −12 lib/cube/server/event.js
View
@@ -12,35 +12,48 @@ var util = require("util"),
var type_re = /^[a-z][a-zA-Z0-9_]+$/,
invalidate = {$set: {i: true}},
multi = {multi: true},
- event_options = {sort: {t: -1}, batchSize: 1000};
+ event_options = {sort: {t: -1}, batchSize: 1000},
+ type_options = {safe: true};
exports.putter = function(db) {
var collection = types(db),
+ eventsByType = {},
flushInterval,
flushTypes = {},
flushDelay = 5000;
function endpoint(request) {
var time = new Date(request.time),
- type = request.type;
+ type = request.type,
+ events = eventsByType[type];
// Validate the date and type.
if (!type_re.test(type)) return util.log("invalid type: " + request.type);
if (isNaN(time)) return util.log("invalid time: " + request.time);
- // Create the event object, to control which fields are addressible.
- var event = {t: time, d: request.data};
+ // If this is a known event type, save immediately.
+ if (events) return save(events);
- // If an id is specified, promote it to Mongo's primary key.
- if ("id" in request) event._id = request.id;
+ // Otherwise, verify that the collection exists before saving.
+ db.collection(type + "_events", type_options, function(error, events) {
+ if (error) return util.log("unknown type: " + type);
+ save(eventsByType[type] = events);
+ });
- // Save the event.
- collection(type).events.save(event);
+ // Create and save the event object.
+ function save(events) {
+ var event = {t: time, d: request.data};
- // Queue invalidation of metrics for this type.
- var times = flushTypes[type] || (flushTypes[type] = [time, time]);
- if (time < times[0]) times[0] = time;
- if (time > times[1]) times[1] = time;
+ // If an id is specified, promote it to Mongo's primary key.
+ if ("id" in request) event._id = request.id;
+
+ events.save(event);
+
+ // Queue invalidation of metrics for this type.
+ var times = flushTypes[type] || (flushTypes[type] = [time, time]);
+ if (time < times[0]) times[0] = time;
+ if (time > times[1]) times[1] = time;
+ }
}
// Invalidate cached metrics.

0 comments on commit 0debb3d

Please sign in to comment.