Skip to content
This repository
Browse code

Merge branch 'release'

  • Loading branch information...
commit f5412d4b008f17f8c081c3272f5a67ae24687612 2 parents 70e9ee6 + 0debb3d
Mike Bostock authored November 22, 2011
37  lib/cube/server/event.js
@@ -12,35 +12,48 @@ var util = require("util"),
12 12
 var type_re = /^[a-z][a-zA-Z0-9_]+$/,
13 13
     invalidate = {$set: {i: true}},
14 14
     multi = {multi: true},
15  
-    event_options = {sort: {t: -1}, batchSize: 1000};
  15
+    event_options = {sort: {t: -1}, batchSize: 1000},
  16
+    type_options = {safe: true};
16 17
 
17 18
 exports.putter = function(db) {
18 19
   var collection = types(db),
  20
+      eventsByType = {},
19 21
       flushInterval,
20 22
       flushTypes = {},
21 23
       flushDelay = 5000;
22 24
 
23 25
   function endpoint(request) {
24 26
     var time = new Date(request.time),
25  
-        type = request.type;
  27
+        type = request.type,
  28
+        events = eventsByType[type];
26 29
 
27 30
     // Validate the date and type.
28 31
     if (!type_re.test(type)) return util.log("invalid type: " + request.type);
29 32
     if (isNaN(time)) return util.log("invalid time: " + request.time);
30 33
 
31  
-    // Create the event object, to control which fields are addressible.
32  
-    var event = {t: time, d: request.data};
  34
+    // If this is a known event type, save immediately.
  35
+    if (events) return save(events);
33 36
 
34  
-    // If an id is specified, promote it to Mongo's primary key.
35  
-    if ("id" in request) event._id = request.id;
  37
+    // Otherwise, verify that the collection exists before saving.
  38
+    db.collection(type + "_events", type_options, function(error, events) {
  39
+      if (error) return util.log("unknown type: " + type);
  40
+      save(eventsByType[type] = events);
  41
+    });
36 42
 
37  
-    // Save the event.
38  
-    collection(type).events.save(event);
  43
+    // Create and save the event object.
  44
+    function save(events) {
  45
+      var event = {t: time, d: request.data};
39 46
 
40  
-    // Queue invalidation of metrics for this type.
41  
-    var times = flushTypes[type] || (flushTypes[type] = [time, time]);
42  
-    if (time < times[0]) times[0] = time;
43  
-    if (time > times[1]) times[1] = time;
  47
+      // If an id is specified, promote it to Mongo's primary key.
  48
+      if ("id" in request) event._id = request.id;
  49
+
  50
+      events.save(event);
  51
+
  52
+      // Queue invalidation of metrics for this type.
  53
+      var times = flushTypes[type] || (flushTypes[type] = [time, time]);
  54
+      if (time < times[0]) times[0] = time;
  55
+      if (time > times[1]) times[1] = time;
  56
+    }
44 57
   }
45 58
 
46 59
   // Invalidate cached metrics.
4  package.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "cube",
3  
-  "version": "0.0.11",
  3
+  "version": "0.0.12",
4 4
   "description": "A system for time series visualization using MongoDB, Node and D3.",
5 5
   "keywords": ["time series", "visualization"],
6 6
   "homepage": "http://square.github.com/cube/",
@@ -8,7 +8,7 @@
8 8
   "repository": {"type": "git", "url": "http://github.com/square/cube.git"},
9 9
   "main": "./lib/cube",
10 10
   "dependencies": {
11  
-    "d3": "2.5.1",
  11
+    "d3": "2.5.2",
12 12
     "mongodb": "0.9.6-15",
13 13
     "pegjs": "0.6.2",
14 14
     "vows": "0.5.11",

0 notes on commit f5412d4

Please sign in to comment.
Something went wrong with that request. Please try again.