Skip to content

Commit

Permalink
Fixed issue with wrongly ignored folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Mircea Danila Dumitrescu committed May 16, 2014
1 parent 9deeb02 commit 588e66e
Show file tree
Hide file tree
Showing 12 changed files with 1,163 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
.jshintrc
atlassian-ide-plugin.xml
bower_components
lib
node_modules
tmp
public/lib
Expand Down
8 changes: 8 additions & 0 deletions lib/analytics/aggregation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
var mongodb = require("mongodb"),
genericGetter = require("./genericGetter.js");

exports.getterInit = function (aggregationDB) {
return genericGetter(aggregationDB);
};

476 changes: 476 additions & 0 deletions lib/analytics/aggregator.js

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions lib/analytics/collector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";
var endpoint = require("./endpoint.js");

var headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
};

function post(putter) {
return function (request, response) {
var content = "";
request.on("data", function (chunk) {
content += chunk;
});
request.on("end", function () {
try {
JSON.parse(content).forEach(putter);
} catch (e) {
response.writeHead(400, headers);
response.end(JSON.stringify({error: e.toString()}));
return;
}
response.writeHead(200, headers);
response.end("{}");
});
};
}

//noinspection JSLint
exports.register = function (dbs, endpoints, options) {
var db = dbs.events,
putter = require("./event.js").putterInit(db, options),
poster = post(putter);

endpoints.ws.push(
endpoint("/1.0/event/put", putter)
);

endpoints.http.push(
endpoint("POST", "/1.0/event/put", poster)
);

endpoints.udp = putter;
};
43 changes: 43 additions & 0 deletions lib/analytics/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";
var mongodb = require("mongodb"),
mongoConfigs = require("../../bin/databases-config"),
database = module.exports = {},
myConnect = function (url, options, name, callback) {
mongodb.Db.connect(url, options, function (error, db) {
callback(error, db, name);
});
};

database.openConnections = function (callback) {
var mongoDBType,
url,
options,
mongoConfig;

for (mongoDBType in mongoConfigs) {
if (mongoConfigs.hasOwnProperty(mongoDBType)) {
mongoConfig = mongoConfigs[mongoDBType];
url = database.configurl(mongoConfig);
options = mongoConfig["mongo-options"] || database.configOptions(mongoConfig);
myConnect(url, options, mongoDBType, callback);
}
}
};

database.configurl = function (config) {
var user = config["mongo-username"],
pass = config["mongo-password"],
host = config["mongo-host"] || "localhost",
port = config["mongo-port"] || 27017,
name = config["mongo-database"] || "analytics",
auth = user ? user + ":" + pass + "@" : "";
return "mongodb://" + auth + host + ":" + port + "/" + name;
};

database.configOptions = function (config) {
return {
db: config["mongo-database-options"] || { safe: false },
server: config["mongo-server-options"] || { auto_reconnect: true },
replSet: { read_secondary: true }
};
};
86 changes: 86 additions & 0 deletions lib/analytics/disperser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"use strict";
var endpoint = require("./endpoint.js"),
url = require("url"),
LIMIT_MAX = 1e4,
headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
};

//noinspection JSLint
exports.register = function (dbs, endpoints, options) {
var eventsDB = dbs.events,
aggregationsDB = dbs.aggregations,
eventGetter = require("./event.js").getterInit(eventsDB),
aggregationGetter = require("./aggregation.js").getterInit(aggregationsDB);

endpoints.ws.push(
endpoint("/1.0/event/get", eventGetter),
endpoint("/1.0/aggregation/get", aggregationGetter)
);

function eventGet(request, response) {
request = url.parse(request.url, true).query;
var data = [];

if (!request.hasOwnProperty('start')) {
request.start = 0;
}
if ((request.hasOwnProperty('limit') && (request.limit >= LIMIT_MAX))) {
request.limit = LIMIT_MAX;
}

function documentHandler(dataElem) {
if (dataElem === null) {
response.end(JSON.stringify(data.reverse()));
}
else {
data.push(dataElem);
}
}

if (eventGetter(request, documentHandler) < 0) {
response.writeHead(400, headers);
response.end(JSON.stringify(data[0]));
} else {
response.writeHead(200, headers);
}

}

function aggregationGet(request, response) {
request = url.parse(request.url, true).query;
var data = [];

if (!request.hasOwnProperty('start')) {
request.start = 0;
}
if ((request.hasOwnProperty('limit') && (request.limit >= LIMIT_MAX))) {
request.limit = LIMIT_MAX;
}

function documentHandler(dataElem) {
if (dataElem === null) {
response.end(JSON.stringify(data.reverse()));
}
else {
data.push(dataElem);
}
}

if (aggregationGetter(request, documentHandler) < 0) {
response.writeHead(400, headers);
response.end(JSON.stringify(data[0]));
} else {
response.writeHead(200, headers);
}

}

endpoints.http.push(
endpoint("GET", "/1.0/event/get", eventGet),
endpoint("GET", "/1.0/aggregation/get", aggregationGet)
);

};

25 changes: 25 additions & 0 deletions lib/analytics/endpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
// creates an endpoint with given HTTP method, URL path and dispatch (function)
// (method argument is optional)
// endpoints are evaluated in server.js and
// dispatch(request, response) is called if path/method matches
module.exports = function (method, path, dispatch) {
var match;

if (arguments.length < 3) {
dispatch = path;
path = method;
match = function (p) {
return p === path;
};
} else {
match = function (p, m) {
return m === method && p === path;
};
}

return {
match: match,
dispatch: dispatch
};
};
96 changes: 96 additions & 0 deletions lib/analytics/event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"use strict";
var mongodb = require("mongodb"),
type_re = /^[a-z][a-zA-Z0-9_]+$/,
genericGetter = require("./genericGetter.js"),
myutils = require("./myutils.js");

exports.putterInit = function (db, options) {
var eventsCollectionCreated = 0,
eventsToSave = [],
event,
collectionSize = options.collectionSize;

if (myutils.isInt(collectionSize)) {
throw "Invalid collection size: " + collectionSize;
}
function handle(error) {
if (error) {
throw error;
}
}

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

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

function saveEvents() {
eventsToSave.forEach(function (event) {
save(event);
});
eventsToSave = [];
}

// validations
if (!type_re.test(request.type)) {
messageSenderCallback({error: "invalid type"});
return -1;
}
if (isNaN(time)) {
messageSenderCallback({error: "invalid time"});
return -1;
}

// 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 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;

// 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!

// First add the new event to the queue.
eventsToSave.push(event);

// If the events collection exists, then we assume the indexes do
// too. Otherwise, we must create the required collections and indexes.

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;
};

exports.getterInit = function (eventsDB) {
return genericGetter(eventsDB);
};
Loading

0 comments on commit 588e66e

Please sign in to comment.