-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtelemetry.ts
172 lines (154 loc) · 4.3 KB
/
telemetry.ts
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
const telemetryEnabled: boolean = _.has($.url().param(), 'telemetry') ? $.url().param() === 'false' : true
let interactions: Interaction[] = [];
function sendTelemetry() {
const is = interactions
interactions = []
if (_.isArray(is) && is.length > 0) {
try {
$.ajax({
type: 'POST',
data: {
interactions: is,
worker: $.url().param().worker,
segment: segment,
token: token,
browser: browser,
width: canvas.width,
height: canvas.height,
words: words,
selected: selected,
start: startS,
end: endS,
startTime: startTime,
startOffset: startOffset,
lastClick: lastClick,
date: new Date(),
annotations: _.map(annotations, cloneAnnotation)
},
dataType: 'application/json',
url: $.url().attr('protocol') + '://' + $.url().attr('host') + ':' + (parseInt($.url().attr('port')) + 1) + '/telemetry'
})
} catch (err) {
// We try our best to send back telemetry, but if it doesn't work, that's not an issue
}
}
}
if (telemetryEnabled)
// every 30 seconds
setInterval(sendTelemetry, 30000)
interface Interaction {
kind: string
}
interface Message extends Interaction {
level: string,
data: string
}
interface Click extends Interaction {
x: number,
y: number,
relativeX: number,
relativeY: number,
elements: any[]
}
interface DragStart extends Click {
}
interface DragEnd extends Click {
}
interface Resize extends Interaction {
pagex: number,
pagey: number,
}
interface Keypress extends Interaction {
key: string,
element?: string
}
interface Send extends Interaction {
data: any,
server: string,
port: number,
why: string
}
interface Receive extends Interaction {
response: any,
error: any,
status: string,
server: string
port: number,
why: string
}
function recordMessage(i: Omit<Message, 'kind'>) {
const j: Message = {
kind: 'message',
...i
}
interactions.push(j)
}
function recordSend(i: Omit<Send, 'kind'>) {
const j: Send = {
kind: 'send',
...i
}
interactions.push(j)
}
_.mixin({
deeply:
// @ts-ignore
function(obj, fn) {
if (_.isObjectLike(obj)) {
return _.mapValues(_.mapValues(obj, fn), function(v) {
// @ts-ignore
return _.isPlainObject(v) || _.isArray(v) ? _.deeply(v, fn) : v;
// return _.isPlainObject(v) ? _.deeply(v, fn) : _.isArray(v) ? v.map(function(x) {
// // @ts-ignore
// return _.deeply(x, fn);
// }) : v;
})
} else {
return fn(obj)
}
},
})
function recordReceive(i: Omit<Receive, 'kind'>) {
// @ts-ignore
i.response = _.deeply(i.response, function(val, key?) {
if (_.isArray(val) || _.isString(val) || _.isNumber(val) || _.isDate(val)) {
return val
}
if (_.isObjectLike(val)) {
val = _.omit(val, _.functions(val))
if (_.has(val, 'responseJSON')) {
val = _.omit(val, ['responseText'])
}
return val
}
return null
})
const j: Receive = {
kind: 'receive',
...i
}
interactions.push(j)
}
function recordKeypress(key: string, element?: string) {
const j: Keypress = {
kind: 'keypress',
key: key,
element: element
}
interactions.push(j)
}
function recordMouseClick(e: JQuery.Event, element: any, element2?: any) {
const j: Click = {
kind: 'mouse',
elements: _.isUndefined(element2) ? [element] : [element, element2],
// @ts-ignore
relativeX: e.offsetX, // d3.event.layerX,
// @ts-ignore
relativeY: e.offsetY, // d3.event.layerY,
// @ts-ignore
x: e.pageX, // d3.event.x,
// @ts-ignore
y: e.pageY // d3.event.y
}
interactions.push(j)
}