Skip to content

Commit

Permalink
Logging per event and we can now reveive an array of events
Browse files Browse the repository at this point in the history
  • Loading branch information
Mircea Danila Dumitrescu committed May 16, 2014
1 parent 588e66e commit 4618af6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
75 changes: 40 additions & 35 deletions lib/analytics/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ exports.putterInit = function (db, options) {

function save(event) {
db.collection("events").insert(event, {w: 0});
console.log("Got event: " + event);
}

function putter(request, messageSenderCallback) {
var time = new Date().getTime();
var time = new Date().getTime(),
i;

function saveEvents() {
eventsToSave.forEach(function (event) {
Expand All @@ -44,48 +46,51 @@ exports.putterInit = function (db, options) {
}

// If an id is specified, promote it to Mongo's primary key.
event = {t: time, d: request.data, type: request.type};
if (request.hasOwnProperty("id")) {
event._id = request.id;
}
// If eventsCollectionCreated, save immediately.
if (eventsCollectionCreated === 1) {
return save(event);
if (!Array.isArray(request)) {
request = [request];
}
for (i = 0; i < request.length; i++) {
event = {t: time, d: request[i].data, type: request[i].type};

// If someone is already creating the event collection
// then append this event to the queue for later save.
if (eventsCollectionCreated === 0.5) {
return eventsToSave.push(event);
}
eventsCollectionCreated = 0.5;
// If eventsCollectionCreated, save immediately.
if (eventsCollectionCreated === 1) {
save(event);
}

// Otherwise, it's up to us to see if the collection exists, verify the
// associated indexes and save
// any events that have queued up in the interim!
// If someone is already creating the event collection
// then append this event to the queue for later save.
if (eventsCollectionCreated === 0.5) {
eventsToSave.push(event);
}
eventsCollectionCreated = 0.5;

// First add the new event to the queue.
eventsToSave.push(event);
// Otherwise, it's up to us to see if the collection exists, verify the
// associated indexes and save
// any events that have queued up in the interim!

// If the events collection exists, then we assume the indexes do
// too. Otherwise, we must create the required collections and indexes.
// First add the new event to the queue.
eventsToSave.push(event);

db.collectionNames("events", {}, function (error, names) {
if (error) {
throw error;
}
if (names.length) {
eventsCollectionCreated = 1;
return saveEvents();
}
// If the events collection exists, then we assume the indexes do
// too. Otherwise, we must create the required collections and indexes.

// Events are indexed by time which is _id, which is natural order.
db.createCollection("events", {capped: true, autoIndexId: true, size: collectionSize}, function (error, result) {
handle(error);
eventsCollectionCreated = 1;
saveEvents();
db.collectionNames("events", {}, function (error, names) {
if (error) {
throw error;
}
if (names.length) {
eventsCollectionCreated = 1;
return saveEvents();
}

// Events are indexed by time which is _id, which is natural order.
db.createCollection("events", {capped: true, autoIndexId: true, size: collectionSize}, function (error, result) {
handle(error);
eventsCollectionCreated = 1;
saveEvents();
});
});
});
}
}

return putter;
Expand Down
2 changes: 0 additions & 2 deletions lib/analytics/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ module.exports = function (options) {
}

socket.on("message", function (message) {
console.log("Got message: " + message);
console.log("Current WS status is " + socket.readyState);
endpoint.dispatch(JSON.parse(message), messageSender);
});
return;
Expand Down

0 comments on commit 4618af6

Please sign in to comment.