From 4618af65394934ed7ef3aee6e5c01ab2874be10d Mon Sep 17 00:00:00 2001 From: Mircea Danila Dumitrescu Date: Fri, 16 May 2014 16:55:15 +0100 Subject: [PATCH] Logging per event and we can now reveive an array of events --- lib/analytics/event.js | 75 ++++++++++++++++++++++------------------- lib/analytics/server.js | 2 -- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/lib/analytics/event.js b/lib/analytics/event.js index a1f2ca7..d84a86f 100644 --- a/lib/analytics/event.js +++ b/lib/analytics/event.js @@ -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) { @@ -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; diff --git a/lib/analytics/server.js b/lib/analytics/server.js index 95931b0..a330bec 100644 --- a/lib/analytics/server.js +++ b/lib/analytics/server.js @@ -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;