-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathreporting.js
More file actions
424 lines (394 loc) · 13.9 KB
/
Copy pathreporting.js
File metadata and controls
424 lines (394 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import $ from 'jquery';
import _ from 'lodash';
import {getContainedLevelId} from '@cdo/apps/code-studio/levels/codeStudioLevels';
import {mergeResults} from '@cdo/apps/code-studio/progressRedux';
import {TestResults} from '@cdo/apps/constants';
import {getStore} from '@cdo/apps/redux';
import {PostMilestoneMode} from '@cdo/generated-scripts/sharedConstants';
var clientState = require('./clientState');
var lastAjaxRequest;
var lastServerResponse = {};
var reporting = module.exports;
reporting.getLastServerResponse = function () {
return lastServerResponse;
};
/**
* Validate that the provided field on our report object is one of the given
* type
* @param {string} key
* @param {*} value
* @param {string} type
*/
function validateType(key, value, type) {
let typeIsValid = false;
if (type === 'array') {
typeIsValid = typeIsValid || Array.isArray(value);
} else {
typeIsValid = typeIsValid || typeof value === type;
}
if (!typeIsValid) {
console.error(
`Expected ${key} to be of type '${type}'. Got '${typeof value}'`
);
}
}
/**
* Do some validation of our report object. Log console errors if we have any
* unexpected fields, or fields with different data than we expect.
* This is meant in part to serve as documentation of the existing behavior. In
* cases where I believe the behavior should potentially be different going
* forward, I've made notes.
* @param {MilestoneReport} report
*/
function validateReport(report) {
for (var key in report) {
if (!Object.prototype.hasOwnProperty.call(report, key)) {
continue;
}
const inLevelGroup = report.allowMultipleSends === true;
const isContainedLevel =
report.testResult === TestResults.CONTAINED_LEVEL_RESULT;
const value = report[key];
switch (key) {
case 'program':
if (report.app === 'match') {
validateType('program', value, 'array');
} else if (report.app === 'multi' && isContainedLevel) {
validateType('program', value, 'number');
} else if (report.app === 'multi' && !inLevelGroup) {
validateType('program', value, 'array');
} else {
validateType('program', value, 'string');
}
break;
case 'fallbackResponse':
// Sometimes we get an object, sometimes we get json for that object.
// When each is true depends on whether it's a contained level or not,
// whether it's in a script or not, and what type of level it is.
// Attempts to describe when each happens have fallen up short. What
// should likely happen is we consolidate around one, then make sure we
// always use that.}
break;
case 'callback':
if (value !== null) {
validateType('callback', value, 'string');
}
break;
case 'app':
validateType('app', value, 'string');
break;
case 'allowMultipleSends':
validateType('allowMultipleSends', value, 'boolean');
break;
case 'skipSuccessCallback':
validateType('skipSuccessCallback', value, 'boolean');
break;
case 'level':
if (value !== null) {
if (report.app === 'level_group' || isContainedLevel) {
// LevelGroups appear to report level as the position of this level
// within the script, which seems wrong.
validateType('level', value, 'number');
} else {
validateType('level', value, 'string');
}
}
break;
case 'result':
if (inLevelGroup) {
// A multi in an assessment seems to send an object here instead of a
// boolean (which may well be a bug).
validateType('result', value, 'object');
} else {
validateType('result', value, 'boolean');
}
break;
case 'pass':
if (inLevelGroup) {
// A multi in an assessment seems to send an object here instead of a
// boolean (which may well be a bug).
validateType('pass', value, 'object');
} else {
validateType('pass', value, 'boolean');
}
break;
case 'testResult':
validateType('testResult', value, 'number');
break;
case 'submitted':
if (
report.app === 'applab' ||
report.app === 'gamelab' ||
report.app === 'javalab' ||
report.app === 'spritelab' ||
report.app === 'weblab'
) {
validateType('submitted', value, 'boolean');
} else {
// In sendResultsCompletion this becomes either "true" (the string) or false (the boolean).
// Would probably be better long term if it was always a string or always a boolean.
if (value !== 'true' && value !== false) {
console.error(
'Expected submitted to be either string "true" or value false'
);
}
}
break;
case 'onComplete':
if (value !== undefined) {
validateType('onComplete', value, 'function');
}
break;
case 'time':
validateType('time', value, 'number');
break;
case 'timeSinceLastMilestone':
validateType('timeSinceLastMilestone', value, 'number');
break;
case 'lines':
validateType('lines', value, 'number');
break;
case 'attempt':
validateType('attempt', value, 'number');
break;
case 'image':
if (value !== null) {
validateType('image', value, 'string');
}
break;
case 'containedLevelResultsInfo':
validateType('containedLevelResultsInfo', value, 'object');
break;
case 'feedback':
if (value) {
validateType('feedback', value, 'string');
}
break;
default:
// Eventually we'd probably prefer to throw here, but I don't have enough
// confidence that this validation is 100% correct to start breaking things
// if it isnt.
console.error(
`Unexpected report key '${key}' of type '${typeof report[key]}'`
);
break;
}
}
}
/**
* @typedef {Object} MilestoneReport
* @property {string} callback - The url where the report should be sent.
* For studioApp-based levels, this is provided on initialization as
* appOptions.report.callback.
* @property {?} program - contents of submitted program.
* @property {string} app - The app name, as defined by its model.
* @property {string} level - The level name or number. Maybe deprecated?
* @property {number|boolean} result - Whether the attempt succeeded or failed.
* @property {TestResult} testResult - Additional detail on the outcome of the attempt.
* @property {onComplete} onComplete - Callback invoked when reporting is completed.
* @property {boolean} allowMultipleSends - ??
* @property {number} lines - number of lines of code written.
* @property {number} serverLevelId - ??
* @property {boolean} skipSuccessCallback - Whether we should ignore the success result from ajax
* @property {?} submitted - ??
* @property {?} time - ??
* @property {number} timeSinceLastMilestone- The time since navigating to this page or since the last
* milestone was recorded, whichever is more recent. It is used to calculated time spent on a level.
* @property {?} attempt - ??
* @property {?} image - ??
* @property {boolean} pass - true if the attempt is passing.
*/
/**
* @callback onComplete
* @param {LiveMilestoneResponse} response
*/
/**
* Notify the progression system of level attempt or completion.
* All labs/activities should call this function to report progress, typically
* when the "Run" or "Submit" button is clicked.
*
* Provides a response to a callback, which can provide a video to play
* and next/previous level URLs.
*
* The client posts the progress JSON to the URL specified by
* {@link MilestoneReport.callback} (e.g. /milestone).
* In the event of a failure or timeout, the client relies on
* report.fallbackResponse (if specified) to allow the user to progress.
*
* @param {MilestoneReport} report
*/
reporting.sendReport = function (report) {
// The list of report fields we want to send to the server
const serverFields = [
'program',
'app',
'allowMultipleSends',
'level',
'result',
'testResult',
'submitted',
'time',
'timeSinceLastMilestone',
'lines',
'attempt',
'image',
];
validateReport(report);
// Update Redux
const store = getStore();
store.dispatch(mergeResults({[appOptions.serverLevelId]: report.testResult}));
// If progress is not being saved in the database, save it locally.
// (Note: Either way, we still send a milestone report to the server so we can
// get information from the response.)
if (!store.getState().progress.usingDbProgress) {
saveReportLocally(report);
}
// jQuery can do this implicitly, but when url-encoding it, jQuery calls a method that
// shows the result dialog immediately
var queryItems = [];
const serverReport = _.pick(report, serverFields);
for (var key in serverReport) {
queryItems.push(key + '=' + report[key]);
}
const queryString = queryItems.join('&');
// Post milestone iff the server tells us.
// Check a second switch if we passed the last level of the script.
// Keep this logic in sync with ActivitiesController#milestone on the server.
const postMilestoneMode =
appOptions.postMilestoneMode || PostMilestoneMode.all;
let postMilestone;
switch (postMilestoneMode) {
case PostMilestoneMode.all:
postMilestone = true;
break;
case PostMilestoneMode.successful_runs_and_final_level_only:
postMilestone = report.pass || appOptions.level.isLastLevelInScript;
break;
case PostMilestoneMode.final_level_only:
postMilestone = appOptions.level.isLastLevelInScript;
break;
default:
console.error('Unexpected postMilestoneMode ' + postMilestoneMode);
postMilestone = true;
break;
}
if (postMilestone) {
var onNoSuccess = xhr => {
if (!report.allowMultipleSends && thisAjax !== lastAjaxRequest) {
return;
}
report.error = xhr.responseText;
reportComplete(report, getFallbackResponse(report));
};
var thisAjax = $.ajax({
type: 'POST',
url: report.callback,
contentType: 'application/x-www-form-urlencoded',
// Set a timeout of fifteen seconds so the user will get the fallback
// response even if the server is hung and unresponsive.
timeout: 15000,
data: queryString,
dataType: 'json',
jsonp: false,
beforeSend: function (xhr) {
xhr.setRequestHeader(
'X-CSRF-Token',
$('meta[name="csrf-token"]').attr('content')
);
},
success: function (response) {
if (report.skipSuccessCallback === true) {
onNoSuccess(response);
return;
}
if (appOptions.hasContainedLevels && !response.redirect) {
// for contained levels, we want to allow the user to Continue even
// if the answer was incorrect and nextRedirect was not supplied, so
// populate nextRedirect from the fallback if necessary
report.pass = true;
var fallback = getFallbackResponse(report) || {};
response.redirect = fallback.redirect;
}
if (appOptions.isBonusLevel) {
// Bonus levels might have to take students back to a different lesson,
// ignore the redirect in response and use the url from appOptions
// instead
response.redirect = appOptions.nextLevelUrl;
}
reportComplete(report, response);
},
error: xhr => onNoSuccess(xhr),
});
lastAjaxRequest = thisAjax;
} else {
//There's a potential race condition here - we show the dialog after animation completion, but also after the report
//is done posting. There is logic that says "don't show the dialog if we are animating" but if milestone posting
//is disabled then we might show the dialog before the animation starts. Putting a 1-sec delay works around this
setTimeout(function () {
reportComplete(report, getFallbackResponse(report));
}, 1000);
}
};
/**
* Save information in milestone report to session storage.
* @param report
*/
function saveReportLocally(report) {
clientState.trackProgress(
appOptions.scriptName,
appOptions.serverLevelId,
report.testResult
);
if (report.program && report.testResult !== TestResults.SKIPPED) {
const program = decodeURIComponent(report.program);
// If the program is the result for a contained level, store it with
// the contained level id
const levelId =
appOptions.hasContainedLevels && !appOptions.level.edit_blocks
? getContainedLevelId()
: appOptions.serverProjectLevelId || appOptions.serverLevelId;
clientState.writeSourceForLevel(
appOptions.scriptName,
levelId,
+new Date(),
program
);
}
}
reporting.cancelReport = function () {
if (lastAjaxRequest) {
lastAjaxRequest.abort();
}
lastAjaxRequest = null;
};
function getFallbackResponse(report) {
var fallbackResponse = maybeParse(report.fallbackResponse);
if (!fallbackResponse) {
return null;
}
return report.pass ? fallbackResponse.success : fallbackResponse.failure;
}
// TODO: sometimes fallback response is a string, not a parsed object
function maybeParse(data) {
if (typeof data === 'string') {
try {
return JSON.parse(data);
} catch (e) {}
}
return data;
}
function reportComplete(report, response) {
lastAjaxRequest = null;
if (response) {
lastServerResponse.report_error = report.error;
lastServerResponse.nextRedirect = response.redirect;
lastServerResponse.videoInfo = response.video_info;
lastServerResponse.endOfLessonExperience =
response.end_of_lesson_experience;
lastServerResponse.previousStageInfo =
response.lesson_changing && response.lesson_changing.previous;
}
if (report.onComplete) {
report.onComplete(response);
}
}