Skip to content

Instantly share code, notes, and snippets.

@kcarnold
Last active February 14, 2016 22:13
Show Gist options
  • Save kcarnold/4170787 to your computer and use it in GitHub Desktop.
Save kcarnold/4170787 to your computer and use it in GitHub Desktop.
Logging
function logBrowserStats() {
function getAsObject(objectLike) {
var obj = {}, key;
for (key in objectLike) {
var val = objectLike[key];
if (typeof val === 'number' || typeof val === 'string') {
obj[key] = val;
}
}
return obj;
}
var browserStats = {
screen: getAsObject(screen),
window: {width: $(window).width(), height: $(window).height()},
navigator: getAsObject(navigator)
};
logEvent('browser stats', browserStats);
}
var events = [];
var maxSeq = 0;
var curUploadingSeq, curUploadingEvents = null;
function uploadEvents() {
if (curUploadingEvents === null && events.length !== 0) {
curUploadingSeq = maxSeq++;
curUploadingEvents = events.join('\n')+'\n';
events = [];
tryUpload();
}
}
setInterval(uploadEvents, 30*1000); // upload every 30 seconds
function tryUpload() {
$.ajax({
type: 'POST',
url: '/log_events/'+curUploadingSeq,
data: {events: curUploadingEvents},
timeout: 30000,
success: function() {
console.log('Successfully uploaded', curUploadingSeq);
$('#connectionTrouble').hide();
curUploadingEvents = null;
uploadEvents();
},
error: function(xhr, textStatus, errorThrown) {
// Try again in 5 seconds.
console.log('Upload failed', textStatus, xhr);
logEvent('trouble uploading');
$('#connectionTrouble').show();
setTimeout(tryUpload, 5000);
}
});
}
function logEvent(name) {
var args = Array.prototype.slice.call(arguments);
var event = {
time: new Date().getTime(),
lastMouseX: lastMouseX,
lastMouseY: lastMouseY,
name: name,
args: args.slice(1)
};
console.log(name, event);
events.push(JSON.stringify(event))
}
from flask import Flask, request, send_file, render_template, session
@app.route('/')
def index():
if 'participant' in request.args:
participant = request.args['participant']
elif 'participant' in session:
participant = session['participant']
else:
participant = ''.join(random.choice(string.ascii_letters) for i in xrange(4))
numTrials = int(request.args.get('numTrials', 120))
session['participant'] = participant
startTime, condition = getCondition(participant)
session['startTime'] = startTime.isoformat()
return render_template('index.html', condition=condition, numTrials=numTrials)
def get_remote_addr():
if 'X-Forwarded-For' in request.headers:
return request.headers['X-Forwarded-For']
else:
return request.remote_addr
@app.route('/log_events/<int:seq>', methods=['POST'])
def log_events(seq):
participant = session['participant']
startTime = session['startTime']
events = request.form['events']
sessionLogPath = getSessionLogPath(participant, startTime)
if not os.path.isdir(sessionLogPath):
os.makedirs(sessionLogPath)
with open(os.path.join(sessionLogPath, 'ip'), 'w') as f:
print >>f, get_remote_addr()
with open(os.path.join(sessionLogPath, '{0:03d}.txt'.format(seq)), 'wb') as f:
f.write(events)
return 'success'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment