-
Notifications
You must be signed in to change notification settings - Fork 0
/
postFlightReport.js
335 lines (308 loc) · 10.8 KB
/
postFlightReport.js
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
import * as fs from "fs";
import { error, info } from "@empirica/core/console";
import { pushPostFlightReportToGithub } from "../providers/github";
function valueCounts(arr) {
return arr.reduce((acc, cur) => {
if (acc[cur] === undefined) {
acc[cur] = 1;
} else {
acc[cur] += 1;
}
return acc;
}, {});
}
function valuePercentages(arr) {
const counts = valueCounts(arr);
const total = arr.length;
return Object.fromEntries(
Object.entries(counts).map(([key, value]) => [key, value / total])
);
}
export async function postFlightReport({ batch }) {
const report = {};
// load data
const preregistrationsFound = fs.existsSync(
batch.get("preregistrationDataFilename")
);
const preregistrations = preregistrationsFound
? fs // if the file exists, load it
.readFileSync(batch.get("preregistrationDataFilename"))
.toString()
.split("\n")
.filter((line) => line !== "")
.map((line, index) => {
try {
return JSON.parse(line);
} catch (err) {
error(`Failed to parse preregistration line ${index} :`, line);
return undefined;
}
})
.filter((line) => line !== undefined)
: []; // otherwise, just use an empty array
const scienceDataFilename = batch.get("scienceDataFilename");
let scienceData = [];
if (scienceDataFilename && fs.existsSync(scienceDataFilename)) {
scienceData = fs
.readFileSync(scienceDataFilename)
.toString()
.split("\n")
.filter((line) => line !== "")
.map((line, index) => {
try {
return JSON.parse(line);
} catch (err) {
error(`Failed to parse science data line ${index}:`, line);
return undefined;
}
})
.filter((line) => line !== undefined);
} else {
error("No science data file found with filename:", scienceDataFilename);
}
// const serverLogs = fs
// .readFileSync(`${process.env.DATA_DIR}/empirica.log`)
// .toString()
// .split("\n");
// one error is the test error, so if the error count is minus one, then we know that there was an error read issue
// only listing the error count here, rather than the error lines themselves
// because we won't get the full error message anyways, and can't guarantee
// that the full error message won't contain sensitive information
// report.errorCount =
// serverLogs.filter((line) => line.includes("[1mERR")).length - 1;
// preregistration rates
report.preregistrations = {};
report.preregistrations.total = preregistrations.length;
report.preregistrations.treatmentBreakdown = valueCounts(
preregistrations.map((line) => line.treatmentMetadata.name)
);
const preregisteredSampleIds = new Set(
preregistrations.map((line) => line.sampleId)
);
const completedSampleIds = new Set(
scienceData
.filter((line) => line.exitStatus === "complete")
.map((line) => line.sampleId)
);
report.preregistrations.percentComplete =
(completedSampleIds.size / preregisteredSampleIds.size) * 100;
// participant completion rates
report.participants = {};
report.participants.total = scienceData.length;
report.participants.completeIntroSteps = scienceData.filter(
(line) =>
line.timeEnteredCountdown !== "missing" ||
line.timeIntroDone !== "missing"
).length;
report.participants.enterLobby = scienceData.filter(
(line) => line.timeIntroDone !== "missing"
).length;
report.participants.beginGame = scienceData.filter(
(line) => line.timeGameStarted !== "missing"
).length;
report.participants.finishGame = scienceData.filter(
(line) => line.timeGameEnded !== "missing"
).length;
report.participants.complete = scienceData.filter(
(line) => line.exitStatus === "complete"
).length;
// timezone and country breakdown
report.participants.ipTimezoneBreakdown = valueCounts(
scienceData.map((line) => line.connectionInfo.timezone)
);
report.participants.ipCountryBreakdown = valueCounts(
scienceData.map((line) => line.connectionInfo.country)
);
report.participants.possibleVPN = valueCounts(
scienceData.map(
(line) => line.connectionInfo.timezone !== line.browserInfo.timezone
)
);
report.participants.browserLanguageBreakdown = valueCounts(
scienceData.map((line) => line.browserInfo.language)
);
report.participants.browserTimezoneBreakdown = valueCounts(
scienceData.map((line) => line.browserInfo.timezone)
);
report.participants.knownVPN = valueCounts(
scienceData.map((line) => line.connectionInfo.isKnownVpn)
);
// section timings
report.timings = { intro: {}, countdown: {}, lobby: {}, game: {}, exit: {} };
const introTimings = scienceData
.filter(
(line) =>
line.times.playerEnteredCountdown !== "missing" ||
line.times.playerIntroDone !== "missing"
)
.map(
(line) =>
((line.times.playerEnteredCountdown !== "missing"
? Date.parse(line.times.playerEnteredCountdown)
: Date.parse(line.times.playerIntroDone)) -
Date.parse(line.times.playerArrived)) /
1000
);
report.timings.intro.max = Math.max(...introTimings);
report.timings.intro.min = Math.min(...introTimings);
report.timings.intro.mean =
introTimings.reduce((acc, cur) => acc + cur, 0) / introTimings.length;
report.timings.intro.median =
introTimings.sort()[Math.floor(introTimings.length / 2)];
const countdownTimings = scienceData
.filter(
(line) =>
line.times.playerIntroDone !== "missing" &&
line.times.playerEnteredCountdown !== "missing"
)
.map(
(line) =>
(Date.parse(line.times.playerIntroDone) -
Date.parse(line.times.playerEnteredCountdown)) /
1000
);
report.timings.countdown.max = Math.max(...countdownTimings);
report.timings.countdown.min = Math.min(...countdownTimings);
report.timings.countdown.mean =
countdownTimings.reduce((acc, cur) => acc + cur, 0) /
countdownTimings.length;
report.timings.countdown.median =
countdownTimings.sort()[Math.floor(countdownTimings.length / 2)];
const lobbyTimings = scienceData
.filter(
(line) =>
line.times.playerIntroDone !== "missing" &&
line.times.gameStarted !== "missing"
)
.map(
(line) =>
(Date.parse(line.times.gameStarted) -
Date.parse(line.times.playerIntroDone)) /
1000
);
report.timings.lobby.max = Math.max(...lobbyTimings);
report.timings.lobby.min = Math.min(...lobbyTimings);
report.timings.lobby.mean =
lobbyTimings.reduce((acc, cur) => acc + cur, 0) / lobbyTimings.length;
report.timings.lobby.median =
lobbyTimings.sort()[Math.floor(lobbyTimings.length / 2)];
const gameTimings = scienceData
.filter(
(line) =>
line.times.gameEnded !== "missing" &&
line.times.gameStarted !== "missing"
)
.map(
(line) =>
(Date.parse(line.times.gameEnded) -
Date.parse(line.times.gameStarted)) /
1000
);
report.timings.game.max = Math.max(...gameTimings);
report.timings.game.min = Math.min(...gameTimings);
report.timings.game.mean =
gameTimings.reduce((acc, cur) => acc + cur, 0) / gameTimings.length;
report.timings.game.median =
gameTimings.sort()[Math.floor(gameTimings.length / 2)];
const exitTimings = scienceData
.filter(
(line) =>
line.times.gameEnded !== "missing" &&
line.times.playerComplete !== "missing"
)
.map(
(line) =>
(Date.parse(line.times.playerComplete) -
Date.parse(line.times.gameEnded)) /
1000
);
report.timings.exit.max = Math.max(...exitTimings);
report.timings.exit.min = Math.min(...exitTimings);
report.timings.exit.mean =
exitTimings.reduce((acc, cur) => acc + cur, 0) / exitTimings.length;
report.timings.exit.median =
exitTimings.sort()[Math.floor(exitTimings.length / 2)];
// disconnection/reconnection rates
const connectionEvents = scienceData
.filter((line) => line.connectionHistory !== "missing")
.map(
(line) =>
line.connectionHistory.filter((event) => event.connected === true)
.length
);
report.connections = {};
report.connections.min = Math.min(...connectionEvents);
report.connections.max = Math.max(...connectionEvents);
report.connections.mean =
connectionEvents.reduce((acc, cur) => acc + cur, 0) /
connectionEvents.length;
report.connections.median =
connectionEvents.sort()[Math.floor(connectionEvents.length / 2)];
// QC stats
report.QC = {};
const QCSurveyResponses = scienceData
.map((line) => line.QCSurvey?.responses)
.filter((responses) => responses !== undefined);
report.QC.participateAgain = valuePercentages(
QCSurveyResponses.map((response) => response.participateAgain)
);
report.QC.adequateCompensation = valuePercentages(
QCSurveyResponses.map((response) => response.adequateCompensation)
);
report.QC.adequateTime = valuePercentages(
QCSurveyResponses.map((response) => response.adequateTime)
);
report.QC.clearInstructions = valuePercentages(
QCSurveyResponses.map((response) => response.clearInstructions)
);
report.QC.videoQuality = valuePercentages(
QCSurveyResponses.map((response) => response.videoQuality)
);
report.QC.joiningProblems = valuePercentages(
QCSurveyResponses.map((response) => response.joiningProblems)
);
report.QC.technicalProblems = valuePercentages(
QCSurveyResponses.map((response) => response.technicalProblems)
);
report.QC.textExpansion = QCSurveyResponses.map(
(response) => response.textExpansion
).filter(
(text) =>
!["no", "nan", "none", "nothing", "undefined"].includes(
text?.toLowerCase().trim()
)
);
report.QC.technicalDetail = QCSurveyResponses.map(
(response) => response.technicalDetail
).filter(
(text) =>
!["no", "nan", "none", "nothing", "undefined"].includes(
text?.toLowerCase().trim()
)
);
report.QC.joiningDetail = QCSurveyResponses.map(
(response) => response.joiningDetail
).filter(
(text) =>
!["no", "nan", "none", "nothing", "undefined"].includes(
text?.toLowerCase().trim()
)
);
// count of players reporting discussion problems at least once
report.participants.reportingDiscussionProblems = scienceData.filter(
(line) => line.reports.length > 0
).length;
// count of players checking in at least once
report.participants.checkingIn = scienceData.filter(
(line) => line.checkIns.length > 0
).length;
// todo: check that the expected video files are saved in S3
info("Post-flight report:", report);
// write report to file
fs.writeFileSync(
batch.get("postFlightReportFilename"),
JSON.stringify(report, null, 2)
);
// push report to github
await pushPostFlightReportToGithub({ batch });
}