-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.js
115 lines (100 loc) · 3.17 KB
/
recorder.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
/**
* @file Defines a class for recording data and sending to the server.
* @author Michael Pascale
*/
/**
* Create an object for recording data.
* @constructor
* @param {String} id - Unique identifier of the participant.
* @param {String} url - URL of the server to POST data to.
* @param {Number} condition - Experimental condition number.
* @param {String} [begin=false] - Start the recorder upon creation.
*/
function Recorder(id, url, condition, begin = false) {
this.id = id;
this.url = url;
this.data = [];
this.condition = condition;
this.started = undefined;
this.finished = undefined;
if (begin)
this.begin();
/**
* Save a data object to the data array.
* @method
* @param {String} type - Name for the type of record.
* @param {Object} data - Object containing data to be recorded.
*/
this.record = function (type, data) {
if (this.started && !this.finished)
this.data.push({
condition: this.condition,
time: new Date() - this.started,
id: this.id,
type: type,
...data
});
else
throw Error('Data recorder is not active.');
}
/**
* Begin recording data and log start time.
* @method
*/
this.begin = function () {
if (!this.started)
this.started = new Date();
else
throw Error('Data recorder already started.');
}
/**
* Stop recording data and log end time.
* @method
*/
this.end = function () {
if (!this.finished)
this.finished = new Date();
else
throw Error('Data recorder already stopped.');
}
/**
* Send data to the server.
* @method
*/
this.send = function (callback) {
if (!this.finished)
this.end();
$.ajax({
type: 'post',
url: this.url,
data: JSON.stringify([
{
type: 'metadata',
id: this.id,
time: 0,
condition: this.condition,
metadata: JSON.stringify({
started: this.started.toISOString(),
finished: this.finished.toISOString(),
elapsed: this.finished - this.started,
...this.data.reduce((acc, cur)=>{
if (cur.type === 'probe') {
acc.probes++;
acc.q1.push(cur.q1);
} else if (cur.type === 'throw') {
acc.throws++;
if (cur.from === 'participant')
acc.byPart++;
}
acc.pct = acc.byPart / acc.throws;
return acc;
}, { throws: 0, byPart: 0, probes: 0, pct: 0, q1: []})
})
},
...this.data
]),
contentType: 'application/json',
complete: callback
});
}
}