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

event subtyping #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/cube/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ exports.putter = function(db) {
type = request.type;

// Validate the date and type.
if (!type_re.test(type)) return callback({error: "invalid type"}), -1;
var keys = type.split('.');
keys.forEach(function(t) {
if (!type_re.test(t)) {
return callback({error: "invalid type"}), -1;
}
});
var type = keys[0];
var key = keys.slice(1).join('.');

if (isNaN(time)) return callback({error: "invalid time"}), -1;

// If an id is specified, promote it to Mongo's primary key.
var event = {t: time, d: request.data};
if ("id" in request) event._id = request.id;

// If this is a known event type, save immediately.
if (type in knownByType) return save(type, event);
if (type in knownByType) return save(type, event, key);

// If someone is already creating the event collection for this new type,
// then append this event to the queue for later save.
Expand Down Expand Up @@ -77,7 +85,7 @@ exports.putter = function(db) {
// Save any pending events to the new collection.
function saveEvents() {
knownByType[type] = true;
eventsToSaveByType[type].forEach(function(event) { save(type, event); });
eventsToSaveByType[type].forEach(function(event) { save(type, event, key); });
delete eventsToSaveByType[type];
}
});
Expand All @@ -91,8 +99,14 @@ exports.putter = function(db) {
// delay between saving the event and invalidating the metrics reduces the
// likelihood of a race condition between when the events are read by the
// evaluator and when the newly-computed metrics are saved.
function save(type, event) {
collection(type).events.save(event, handle);
function save(type, event, key) {
if (event._id !== undefined) {
var updater = {t: event.t};
updater[key !== '' ? 'd.' + key : 'd'] = event.d;
collection(type).events.update({_id: event._id}, {$set: updater}, {safe: true, upsert: true}, handle);
} else {
collection(type).events.save(event, handle);
}
queueInvalidation(type, event);
}

Expand Down