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-13908] Fixed several areas where config and session files are not... #48

Merged
merged 1 commit into from
Jun 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
0.1.31
-------------------
* Added try/catch around analytics processing with showErrors flag to display errors
* Added check to see if session file is writable when logging in or out of Appc network [TIMOB-13908]
* Improved AppcException to include a toString() function and improved dump() function
* Added isFileWritable() function to fs library

0.1.30
-------------------
* Fixed bug with the Android SDK path not being stored back in the Android detection results object after being converted to an absolute path [TIMOB-13549]
Expand Down
238 changes: 119 additions & 119 deletions lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exports.send = function (args) {
child.send(args);
};

process.on('message', function(m) {
process.on('message', function (m) {
if (!m || !['appId', 'appName', 'appGuid', 'directory', 'version'].every(function (p) { return m.hasOwnProperty(p); })) {
return;
}
Expand All @@ -63,103 +63,101 @@ process.on('message', function(m) {
});
}
}, function (err, results) {
var directory = afs.resolvePath(m.directory),
sessionFile = path.join(directory, 'analytics_session.json'),
logFile = path.join(directory, 'analytics.json'),
payload = [],
payloadRetry = [],
restoredPreviousSession = false,
now = (new Date).getTime(),
mid = results.mid,
sid,
sessionExpiration,
macAddr = '',
ipAddr = '',
ifaces = Object.keys(results.ifaces).sort();

// try to find us the mac address and ip address
// note: this finds the first physical interface which may not necessarily be the default gateway interface
for (var i = 0; i < ifaces.length; i++) {
if (results.ifaces[ifaces[i]].macAddress) {
var ips = results.ifaces[ifaces[i]].ipAddresses;
for (var j = 0; j < ips.length; j++) {
ipAddr = ips[j].address;
if (ips[j].family.toLowerCase() == 'ipv4') {
break;
try {
var directory = afs.resolvePath(m.directory),
sessionFile = path.join(directory, 'analytics_session.json'),
logFile = path.join(directory, 'analytics.json'),
payload = [],
payloadRetry = [],
restoredPreviousSession = false,
now = (new Date).getTime(),
mid = results.mid,
sid,
sessionExpiration,
macAddr = '',
ipAddr = '',
ifaces = Object.keys(results.ifaces).sort();

// try to find us the mac address and ip address
// note: this finds the first physical interface which may not necessarily be the default gateway interface
for (var i = 0; i < ifaces.length; i++) {
if (results.ifaces[ifaces[i]].macAddress) {
var ips = results.ifaces[ifaces[i]].ipAddresses;
for (var j = 0; j < ips.length; j++) {
ipAddr = ips[j].address;
if (ips[j].family.toLowerCase() == 'ipv4') {
break;
}
}
macAddr = results.ifaces[ifaces[i]].macAddress;
break;
}
macAddr = results.ifaces[ifaces[i]].macAddress;
break;
}
}

function add(type, event, id, ts, data) {
payload.push(mix({
event: event,
type: type,
sid: sid,
guid: m.appGuid,
mid: mid,
mac_addr: macAddr,
ip: ipAddr,
creator_user_id: m.uid,
app_name: m.appName,
app_version: m.version,
version: m.version,
tz: (new Date()).getTimezoneOffset(),
ver: '2',
un: m.email,
data: JSON.stringify(data),
id: id || uuid.v4(),
}, results.osinfo));
}
function add(type, event, id, ts, data) {
payload.push(mix({
event: event,
type: type,
sid: sid,
guid: m.appGuid,
mid: mid,
mac_addr: macAddr,
ip: ipAddr,
creator_user_id: m.uid,
app_name: m.appName,
app_version: m.version,
version: m.version,
tz: (new Date()).getTimezoneOffset(),
ver: '2',
un: m.email,
data: JSON.stringify(data),
id: id || uuid.v4(),
}, results.osinfo));
}

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

// Do we have a valid session
if (afs.exists(sessionFile)) {
try {
var analyticsSession = JSON.parse(fs.readFileSync(sessionFile));
// Do we have a valid session
if (afs.exists(sessionFile)) {
try {
var analyticsSession = JSON.parse(fs.readFileSync(sessionFile));

sid = analyticsSession.sid;
sessionExpiration = analyticsSession.sessionExpiration;
sid = analyticsSession.sid;
sessionExpiration = analyticsSession.sessionExpiration;

// If the expiration has expired, create a new one
if (sid && sessionExpiration && sessionExpiration > now) {
restoredPreviousSession = true;
} else {
// add the ti.end event
add('ti.end', 'ti.end', null, null);
}
} catch (e) {} // file was malformed, treat as if a new session
}
// If the expiration has expired, create a new one
if (sid && sessionExpiration && sessionExpiration > now) {
restoredPreviousSession = true;
} else {
// add the ti.end event
add('ti.end', 'ti.end', null, null);
}
} catch (e) {} // file was malformed, treat as if a new session
}

// If the previous session was not restored, create a new one
if (!restoredPreviousSession) {
// need to generate a new session id
fs.writeFileSync(sessionFile, JSON.stringify({
mid: mid,
sid: sid = uuid.v4(),
sessionExpiration: sessionExpiration = now + sessionTimeout
}));
// If the previous session was not restored, create a new one
if (!restoredPreviousSession) {
// need to generate a new session id
fs.writeFileSync(sessionFile, JSON.stringify({
mid: mid,
sid: sid = uuid.v4(),
sessionExpiration: sessionExpiration = now + sessionTimeout
}));

add('ti.start', 'ti.start', null, null, null);
}
add('ti.start', 'ti.start', null, null, null);
}

// add the list of app.feature events
m.events.forEach(function (evt) {
add(evt.type, evt.name, evt.id, evt.ts, evt.data);
});
// add the list of app.feature events
m.events.forEach(function (evt) {
add(evt.type, evt.name, evt.id, evt.ts, evt.data);
});

// append payload to disk
if (afs.exists(logFile)) {
try {
// append payload to disk
if (afs.exists(logFile)) {
payload = JSON.parse(fs.readFileSync(logFile)).concat(payload);
} catch (e) {}
}
}

function save() {
try {
function save() {
// save events that failed to send
fs.writeFileSync(logFile, JSON.stringify(payloadRetry));

Expand All @@ -169,43 +167,45 @@ process.on('message', function(m) {
sid: sid,
sessionExpiration: (new Date).getTime() + sessionTimeout
}));
} catch(e) {}
}
}

if (payload.length) {
// record the events before posting just in case of a problem
fs.writeFileSync(logFile, JSON.stringify(payload));

// console.log(payload);

var status = auth.status();

if (status.loggedIn && status.guid) {
var sessionCookie = status.cookie && status.cookie.match(/(PHPSESSID=[A-Za-z0-9]+)/),
cookie = (sessionCookie ? sessionCookie[1] + ';' : '') + 'uid=' + status.guid; // no, this is not a bug... it really is called uid and expects the guid

async.series(payload.map(function (data) {
return function (callback) {
request({
uri: url,
method: 'POST',
proxy: m.httpProxyServer || undefined,
headers: {
Cookie: cookie
},
body: urlEncode(data)
}, function (error, response, body) {
// Return the index if it failed and it needs to be written to the logfile again
if (error || response.statusCode != 204) {
payloadRetry.push(data);
}
callback();
});
};
}), save);
if (payload.length) {
// record the events before posting just in case of a problem
fs.writeFileSync(logFile, JSON.stringify(payload));

// console.log(payload);

var status = auth.status();

if (status.loggedIn && status.guid) {
var sessionCookie = status.cookie && status.cookie.match(/(PHPSESSID=[A-Za-z0-9]+)/),
cookie = (sessionCookie ? sessionCookie[1] + ';' : '') + 'uid=' + status.guid; // no, this is not a bug... it really is called uid and expects the guid

async.series(payload.map(function (data) {
return function (callback) {
request({
uri: url,
method: 'POST',
proxy: m.httpProxyServer || undefined,
headers: {
Cookie: cookie
},
body: urlEncode(data)
}, function (error, response, body) {
// Return the index if it failed and it needs to be written to the logfile again
if (error || response.statusCode != 204) {
payloadRetry.push(data);
}
callback();
});
};
}), save);
}
} else {
save();
}
} else {
save();
} catch (e) {
m.showErrors && console.error('\n' + (e.stack || e.toString()));
}
});
});
2 changes: 1 addition & 1 deletion lib/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports.detect = function (finished, sdkPath, ndkPath) {
sdkPath = result.sdkPath = findSDK();
}

if ( !sdkPath ) {
if (!sdkPath) {
finished && finished();
return;
}
Expand Down