Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timob 9986 #1

Merged
merged 9 commits into from
Sep 13, 2012
112 changes: 60 additions & 52 deletions lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var path = require('path'),
uuid = require('node-uuid'),
async = require('async'),
wrench = require('wrench'),
auth = require('./auth')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comma.

fs = require('fs'),
getOSInfo = require('./environ').getOSInfo,
afs = require('./fs'),
Expand All @@ -18,6 +19,7 @@ var path = require('path'),
interfaces = require('./net').interfaces,
events = [],
url = 'https://api.appcelerator.net/p/v2/mobile-track',
needsEnroll = false,
sessionTimeout = 60 * 60 * 1000; // 1 hour

exports.addEvent = function (name, data, type) {
Expand All @@ -34,19 +36,24 @@ exports.addEvent = function (name, data, type) {
exports.send = function (args) {
var child = require('child_process').fork(module.filename);
args.events = events;
child.send(args);
child.send(mix(args, { doEnroll: needsEnroll }));
needsEnroll = false;
};

exports.enroll = function() {
needsEnroll = true;
};

process.on('message', function(m) {
if (!m || !['appId', 'appName', 'appGuid', 'directory', 'version'].every(function (p) { return m.hasOwnProperty(p); })) {
if (!m || !['appId', 'appName', 'appGuid', 'directory', 'version', 'doEnroll'].every(function (p) { return m.hasOwnProperty(p); })) {
return;
}

var appId = m.appId,
appName = m.appName,
appGuid = m.appGuid,
directory = afs.resolvePath(m.directory),
authFile = path.join(directory, 'auth.json'),
sessionFile = path.join(directory, 'analytics_session.json'),
logFile = path.join(directory, 'analytics.json'),
version = m.version,
deployType = m.deployType,
Expand All @@ -55,7 +62,9 @@ process.on('message', function(m) {
seqId = 0,
sid,
mid,
ids = {};
sessionExpiration,
ids = {},
doEnroll = m.doEnroll;

function add(type, event, id, ts, data) {
ids[id] = 1;
Expand All @@ -77,57 +86,56 @@ process.on('message', function(m) {

async.series([
function (next) {
auth.getMID(function(fetchedMID) {
mid = fetchedMID;
next();
});
},

function (next) {

var analyticsSession,
restoredPreviousSession = false;

directory = afs.resolvePath(directory);
afs.exists(directory) || wrench.mkdirSyncRecursive(directory);

deployType = deployType || 'production';
// have we already done enroll?
if (afs.exists(authFile)) {

// Do we have a valid session
if (afs.exists(sessionFile)) {
try {
var x = JSON.parse(fs.readFileSync(authFile));
sid = x.sid;
mid = x.mid;
seqId = x.seqId;
} catch (e) {
// file was malformed, treat as if a new session
sid = uuid.v4();
}
}

if (mid) {
next();
} else {
interfaces(function (ifaces) {
// find the mac address of the local ethernet card
var macAddress,
names = Object.keys(ifaces).sort();
for (var i = 0; i < names.length; i++) {
var j = ifaces[names[i]];
if (j.macAddress) {
macAddress = j.macAddress;
if (/^eth|en|Local Area Connection/.test(j)) {
break;
}
}
var analyticsSession = JSON.parse(fs.readFileSync(sessionFile)).analytics;
sid = analyticsSession.sid;
seqId = analyticsSession.seqId;
sessionExpiration = analyticsSession.sessionExpiration;

// If the expiration has expired, create a new one
if (sessionExpiration > Date.now()) {
restoredPreviousSession = true;
}
macAddress || (macAddress = uuid.v4());

mid = crypto.createHash('md5').update(macAddress).digest("hex");
sid = sid || uuid.v4();
fs.writeFileSync(authFile, JSON.stringify({ sid: sid, mid: mid, seqId: seqId }));

getOSInfo(function (info) {
add('ti.enroll', 'ti.enroll', null, null, mix({
app_id: appId,
app_name: appName || 'node-appc',
deploytype: deployType,
platform: 'node.js'
}, info));

next();
});
} catch (e) {} // file was malformed, treat as if a new session
}

// If the previous session was not restored, create a new one
if (!restoredPreviousSession) {
sid = uuid.v4();
sessionExpiration = Date.now() + sessionTimeout;
}

// Enroll if need be
if (doEnroll) {
getOSInfo(function (info) {
add('ti.enroll', 'ti.enroll', null, null, mix({
app_id: appId,
app_name: appName || 'node-appc',
deploytype: deployType,
platform: 'node.js'
}, info));
next();
});
} else {
next();
}
},

Expand All @@ -141,7 +149,7 @@ process.on('message', function(m) {
// add the ti.end event
add('ti.end', 'ti.end', null, logMtime);
// need to generate a new session id
fs.writeFileSync(authFile, JSON.stringify({ sid: sid = uuid.v4(), mid: mid, seqId: seqId = 0 }));
fs.writeFileSync(sessionFile, JSON.stringify({ sid: sid = uuid.v4(), seqId: seqId = 0, sessionExpiration: Date.now() }));
}

getOSInfo(function (info) {
Expand Down Expand Up @@ -182,7 +190,7 @@ process.on('message', function(m) {
// TEMPORARY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// console.log(payload);
fs.writeFileSync(logFile, '[]');
fs.writeFileSync(authFile, JSON.stringify({ sid: sid, mid: mid, seqId: seqId }));
fs.writeFileSync(sessionFile, JSON.stringify({ sid: sid, seqId: seqId, sessionExpiration: sessionExpiration }));
/*
// send the request
request({
Expand All @@ -206,7 +214,7 @@ process.on('message', function(m) {
fs.writeFileSync(logFile, JSON.stringify(evts));

// make sure we save the latest seqId
fs.writeFileSync(authFile, JSON.stringify({ sid: sid, mid: mid, seqId: seqId }));
fs.writeFileSync(sessionFile, JSON.stringify({ sid: sid, mid: mid, seqId: seqId }));
}
});
*/
Expand Down