Skip to content

Commit 9248e37

Browse files
committed
refactored out Codeopticon learner interface
1 parent 0b923ae commit 9248e37

2 files changed

Lines changed: 76 additions & 57 deletions

File tree

‎v3/js/opt-frontend-common.js‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ var useCodeMirror = false; // true -> use CodeMirror, false -> use Ace
124124
var prevExecutionExceptionObjLst = [];
125125

126126

127-
var loggingSocketIO = undefined; // socket.io instance -- OPTIONAL: not all frontends use it
127+
var codeopticonSocketIO = undefined; // socket.io instance -- OPTIONAL: not all frontends use it
128128

129129
// From http://stackoverflow.com/a/8809472
130130
function generateUUID(){
@@ -284,7 +284,7 @@ function snapshotCodeDiff() {
284284
deltaObj.deltas.push(delta);
285285

286286
curCode = newCode;
287-
logEvent({type: 'editCode', delta: delta});
287+
logEventCodeopticon({type: 'editCode', delta: delta});
288288

289289
if (typeof TogetherJS !== 'undefined' && TogetherJS.running) {
290290
TogetherJS.send({type: "editCode", delta: delta});
@@ -1288,7 +1288,7 @@ function updateAppDisplay(newAppMode) {
12881288
$('#urlOutput,#embedCodeOutput').val(''); // clear to avoid stale values
12891289

12901290
// log at the end after appMode gets canonicalized
1291-
logEvent({type: 'updateAppDisplay', mode: appMode, appState: getAppState()});
1291+
logEventCodeopticon({type: 'updateAppDisplay', mode: appMode, appState: getAppState()});
12921292
}
12931293

12941294

@@ -1474,7 +1474,7 @@ function executeCodeAndCreateViz(codeToExec,
14741474
if (args.myViz.prevLineIsReturn) {
14751475
obj.prevLineIsReturn = true;
14761476
}
1477-
logEvent(obj);
1477+
logEventCodeopticon(obj);
14781478
});
14791479

14801480
// 2014-05-25: implemented more detailed tracing for surveys
@@ -1533,7 +1533,7 @@ function executeCodeAndCreateViz(codeToExec,
15331533
// and don't do it for iframe-embed.js since getAppState doesn't
15341534
// work in that case ...
15351535
if (originFrontendJsFile !== 'iframe-embed.js') {
1536-
logEvent({type: 'doneExecutingCode',
1536+
logEventCodeopticon({type: 'doneExecutingCode',
15371537
appState: getAppState(),
15381538
// enough to reconstruct the ExecutionVisualizer object
15391539
backendDataJSON: JSON.stringify(dataFromBackend), // for easier transport and compression
@@ -2128,7 +2128,7 @@ display a brief "Thanks!" note]
21282128
21292129
$.get('survey.py', myArgs, function(dat) {});
21302130
2131-
logEvent({type: 'survey',
2131+
logEventCodeopticon({type: 'survey',
21322132
appState: getAppState(),
21332133
surveyQuestion: myArgs.surveyQuestion,
21342134
surveyResponse: myArgs.surveyResponse,
@@ -2168,7 +2168,7 @@ display a brief "Thanks!" note]
21682168
$("#iJustLearnedThanks").fadeOut(1000);
21692169
});
21702170
2171-
logEvent({type: 'survey',
2171+
logEventCodeopticon({type: 'survey',
21722172
appState: getAppState(),
21732173
surveyQuestion: myArgs.surveyQuestion,
21742174
surveyResponse: myArgs.surveyResponse,
@@ -2181,27 +2181,27 @@ display a brief "Thanks!" note]
21812181

21822182

21832183
// using socket.io:
2184-
function logEvent(obj) {
2184+
function logEventCodeopticon(obj) {
21852185
//console.log(obj);
2186-
if (loggingSocketIO) {
2187-
if (supports_html5_storage()) {
2188-
obj.user_uuid = localStorage.getItem('opt_uuid');
2189-
}
2186+
if (codeopticonSocketIO) {
2187+
obj.user_uuid = supports_html5_storage() ? localStorage.getItem('opt_uuid') : undefined,
2188+
obj.session_uuid = sessionUUID;
2189+
21902190
// this probably won't match the server time due to time zones, etc.
21912191
obj.clientTime = new Date().getTime();
21922192

2193-
if (loggingSocketIO.connected) {
2194-
loggingSocketIO.emit('opt-client-event', obj);
2193+
if (codeopticonSocketIO.connected) {
2194+
codeopticonSocketIO.emit('opt-client-event', obj);
21952195
//console.log('emitted opt-client-event:', obj);
21962196
} else {
2197-
// TODO: be careful about this getting HUGE if loggingSocketIO
2197+
// TODO: be careful about this getting HUGE if codeopticonSocketIO
21982198
// never connects properly ...
21992199
logEventQueue.push(obj); // queue this up to be logged when the client
22002200
// finishes successfully connecting to the server
22012201

22022202
// we're not yet connected, or we've been disconnected by the
22032203
// server, so try to connect/reconnect first before emitting the event
2204-
loggingSocketIO.connect(); // will trigger the .on('connect', ...) handler
2204+
codeopticonSocketIO.connect(); // will trigger the .on('connect', ...) handler
22052205
}
22062206
}
22072207
}

‎v3/js/opt-frontend.js‎

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ var CPP_EXAMPLES = {
629629
cppVirtualLink: 'cpp-example-code/cpp-virtual-method.cpp',
630630
}
631631

632+
633+
// BEGIN -- Codeopticon learner interface
634+
632635
var chatBox = undefined;
633636
function createChatBox() {
634637
assert(!chatBox);
@@ -645,12 +648,18 @@ function createChatBox() {
645648

646649
function chatMsgSent(id, user, msg) {
647650
$("#chat_div").chatbox("option", "boxManager").addMsg(id, msg);
648-
logEvent({type: 'opt-client-chat', text: msg, sid: loggingSocketIO ? loggingSocketIO.id : undefined});
651+
logEventCodeopticon({type: 'opt-client-chat',
652+
text: msg,
653+
sid: codeopticonSocketIO ? codeopticonSocketIO.id : undefined
654+
});
649655
}
650656

651657
// only called when the user hits the X button to explicitly close the chat box
652658
function chatBoxClosed(id) {
653-
logEvent({type: 'opt-client-chat', text: '[closed chat box]', sid: loggingSocketIO ? loggingSocketIO.id : undefined});
659+
logEventCodeopticon({type: 'opt-client-chat',
660+
text: '[closed chat box]',
661+
sid: codeopticonSocketIO ? codeopticonSocketIO.id : undefined
662+
});
654663
}
655664

656665
// called when the user toggles the chat box open or close
@@ -659,9 +668,56 @@ function chatBoxToggled(visible) {
659668
if (visible) {
660669
msg = '[maximized chat box]';
661670
}
662-
logEvent({type: 'opt-client-chat', text: msg, sid: loggingSocketIO ? loggingSocketIO.id : undefined});
671+
logEventCodeopticon({type: 'opt-client-chat',
672+
text: msg,
673+
sid: codeopticonSocketIO ? codeopticonSocketIO.id : undefined
674+
});
675+
}
676+
677+
function initCodeopticon() {
678+
// connect on-demand in logEventCodeopticon(), not here
679+
//codeopticonSocketIO = io('http://104.237.139.253:5000/userlog'); // PRODUCTION_PORT
680+
//codeopticonSocketIO = io('http://104.237.139.253:5001/userlog'); // DEBUG_PORT
681+
codeopticonSocketIO = io('http://localhost:5000/userlog'); // localhost debugging
682+
683+
if (codeopticonSocketIO) {
684+
codeopticonSocketIO.on('connect', function() {
685+
//console.log('CONNECTED and emitting', logEventQueue.length, 'events');
686+
687+
if (logEventQueue.length > 0) {
688+
// the reconnectAttempts field that denotes how many times you've
689+
// attempted to reconnect (which is also how many times you've
690+
// been kicked off by the logging server for, say, being idle).
691+
// add this as an extra field on the FIRST event
692+
if (reconnectAttempts > 0) {
693+
logEventQueue[0].reconnectAttempts = reconnectAttempts;
694+
}
695+
696+
while (logEventQueue.length > 0) {
697+
codeopticonSocketIO.emit('opt-client-event', logEventQueue.pop());
698+
}
699+
}
700+
assert(logEventQueue.length === 0);
701+
702+
reconnectAttempts++;
703+
});
704+
705+
codeopticonSocketIO.on('opt-codeopticon-observer-chat', function(msg) {
706+
if (!chatBox) {
707+
createChatBox();
708+
} else {
709+
$("#chat_div").chatbox("option", "hidden", false);
710+
$("#chat_div").chatbox("showContent");
711+
}
712+
713+
$("#chat_div").chatbox("option", "boxManager").addMsg('Tutor', msg.text);
714+
});
715+
}
663716
}
664717

718+
// END -- Codeopticon learner interface
719+
720+
665721
$(document).ready(function() {
666722
setSurveyHTML();
667723

@@ -798,44 +854,7 @@ $(document).ready(function() {
798854

799855
initAceAndOptions(); // do this after genericOptFrontendReady
800856

801-
// connect on-demand in logEvent(), not here
802-
//loggingSocketIO = io('http://104.237.139.253:5000/userlog'); // PRODUCTION_PORT
803-
//loggingSocketIO = io('http://104.237.139.253:5001/userlog'); // DEBUG_PORT
804-
805-
806-
if (loggingSocketIO) {
807-
loggingSocketIO.on('connect', function() {
808-
//console.log('CONNECTED and emitting', logEventQueue.length, 'events');
809-
810-
if (logEventQueue.length > 0) {
811-
// the reconnectAttempts field that denotes how many times you've
812-
// attempted to reconnect (which is also how many times you've
813-
// been kicked off by the logging server for, say, being idle).
814-
// add this as an extra field on the FIRST event
815-
if (reconnectAttempts > 0) {
816-
logEventQueue[0].reconnectAttempts = reconnectAttempts;
817-
}
818-
819-
while (logEventQueue.length > 0) {
820-
loggingSocketIO.emit('opt-client-event', logEventQueue.pop());
821-
}
822-
}
823-
assert(logEventQueue.length === 0);
824-
825-
reconnectAttempts++;
826-
});
827-
828-
loggingSocketIO.on('opt-codeopticon-observer-chat', function(msg) {
829-
if (!chatBox) {
830-
createChatBox();
831-
} else {
832-
$("#chat_div").chatbox("option", "hidden", false);
833-
$("#chat_div").chatbox("showContent");
834-
}
835-
836-
$("#chat_div").chatbox("option", "boxManager").addMsg('Tutor', msg.text);
837-
});
838-
}
857+
initCodeopticon();
839858

840859
$("#createTestsLink").click(function() {
841860
initTestcasesPane('#testCasesPane');

0 commit comments

Comments
 (0)