-
Notifications
You must be signed in to change notification settings - Fork 479
/
firehose.js
314 lines (293 loc) · 9.94 KB
/
firehose.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
/** @file Provides clients to AWS Firehose, whose data is imported into AWS Redshift. */
import AWS from 'aws-sdk/lib/core';
import 'aws-sdk/lib/config';
import Firehose from 'aws-sdk/clients/firehose';
import {
createUuid,
trySetLocalStorage,
tryGetLocalStorage
} from '@cdo/apps/utils';
import {getStore} from '@cdo/apps/redux';
/**
* A barebones client for posting data to an AWS Firehose stream.
* Usage:
* firehoseClient.putRecord(
* {
* study: 'underwater basket weaving', // REQUIRED
* study_group: 'control', // OPTIONAL
* event: 'drowning', // REQUIRED
* data_int: 2 // OPTIONAL
* data_float: 0.31 // OPTIONAL
* data_string: 'code.org rocks' // OPTIONAL
* data_json: JSON.stringify(x) // OPTIONAL
* }
* );
* Usage:
* firehoseClient.putRecordBatch(
* [
* {
* study: 'underwater basket weaving', // REQUIRED
* study_group: 'control', // OPTIONAL
* event: 'drowning', // REQUIRED
* data_int: 2 // OPTIONAL
* data_float: 0.31 // OPTIONAL
* data_string: 'code.org rocks' // OPTIONAL
* data_json: JSON.stringify(x) // OPTIONAL
* },
* {
* study: 'underwater basket weaving', // REQUIRED
* study_group: 'control', // OPTIONAL
* event: 'drowning', // REQUIRED
* data_int: 2 // OPTIONAL
* data_float: 0.31 // OPTIONAL
* data_string: 'code.org rocks' // OPTIONAL
* data_json: JSON.stringify(x) // OPTIONAL
* },
* ]
* );
*/
const deliveryStreamName = 'analysis-events';
// TODO(asher): Add the ability to queue records individually, to be submitted
// as a batch.
// TODO(asher): Determine whether any of the utility functions herein should be
// moved elsewhere, e.g., to apps/src/util.js.
class FirehoseClient {
/**
* Returns the current environment.
* @return {string} The current environment, e.g., "staging" or "production".
*/
getEnvironment() {
const hostname = window.location.hostname;
if (hostname.includes('adhoc')) {
// As adhoc hostnames may include other keywords, check it first.
return 'adhoc';
}
if (hostname.includes('test')) {
return 'test';
}
if (hostname.includes('levelbuilder')) {
return 'levelbuilder';
}
if (hostname.includes('staging')) {
return 'staging';
}
if (hostname.includes('localhost')) {
return 'development';
}
if (hostname.includes('code.org')) {
return 'production';
}
return 'unknown';
}
/**
* Returns whether the environment appears to be the test environment.
* @return {boolean} Whether the hostname includes "test".
*/
isTestEnvironment() {
return this.getEnvironment() === 'test';
}
/**
* Returns whether the environment appears to be a development environment.
* @return {boolean} Whether the hostname includes "localhost".
*/
isDevelopmentEnvironment() {
return this.getEnvironment() === 'development';
}
/**
* Returns whether the request should be sent through to AWS Firehose.
* @param {boolean} alwaysPut An override to default environment behavior.
* @return {boolean} Whether the request should be sent through to AWS
* Firehose.
*/
shouldPutRecord(alwaysPut) {
if (alwaysPut) {
return true;
}
if (this.isTestEnvironment() || this.isDevelopmentEnvironment()) {
return false;
}
return true;
}
/**
* Returns a unique user ID that is persisted across sessions through local
* storage.
* WARNING: Mutates local storage if an analyticsID has not already been set.
* @return {string} A unique user ID.
*/
getAnalyticsUuid() {
let analytics_uuid = tryGetLocalStorage('analyticsID', null);
if (!analytics_uuid) {
analytics_uuid = createUuid();
}
trySetLocalStorage('analyticsID', analytics_uuid);
return analytics_uuid;
}
/**
* Returns a hash containing user device information.
* storage.
* @return {hash} Hash of user device information.
*/
getDeviceInfo() {
let device_info = {
user_agent: window.navigator.userAgent,
window_width: window.innerWidth,
window_height: window.innerHeight,
hostname: window.location.hostname,
full_path: window.location.href
};
return device_info;
}
/**
* Merge various key-value pairs into data.
* @param {hash} data The data to add the key-value pairs to.
* @option {boolean} includeUserId Include userId in records, if signed in
* @return {hash} The data, including the newly added key-value pairs.
* NOTE: In scenarios where userId is not in pageConstants, such as in the
* project gallery, we can also directly pass user_id as a field on the data * object. In this case, includeUserId should be false to avoid overriding
* the manually set user_id.
*/
addCommonValues(data, includeUserId) {
data['created_at'] = new Date().toISOString();
data['environment'] = this.getEnvironment();
data['uuid'] = this.getAnalyticsUuid();
data['device'] = JSON.stringify(this.getDeviceInfo());
const state = getStore().getState();
if (state) {
if (includeUserId) {
const constants = state.pageConstants;
if (constants) {
data['user_id'] = constants.userId;
}
}
const progress = state.progress;
if (progress) {
data['script_id'] = progress.scriptId;
data['level_id'] = parseInt(progress.currentLevelId);
}
}
return data;
}
handleError(requestData, error) {
// Report the error via our own servers, in case reporting it directly
// to firehose was blocked by a network firewall.
$.ajax({
url: '/api/firehose_unreachable',
data: JSON.stringify({
original_data: requestData,
error_text: String(error)
}),
contentType: 'application/json; charset=utf-8',
method: 'PUT',
dataType: 'json'
});
}
/**
* Pushes one data record into the delivery stream.
* @param {hash} data The data to push.
* @param {hash} options Additional (optional) options.
* (default {alwaysPut: false})
* @option options [boolean] alwaysPut Forces the record to be sent.
* @option options [boolean] includeUserId Include userId in records, if signed in
* NOTE: In scenarios where userId is not in pageConstants, such as in the
* project gallery, we can also directly pass user_id as a field on the data * object. In this case, includeUserId should be false to avoid overriding
* the manually set user_id.
* @option options [function(err, data)] callback Invoked upon completion with error or data
*/
putRecord(
data,
options = {alwaysPut: false, includeUserId: false, callback: null}
) {
data = this.addCommonValues(data, options.includeUserId);
const handleError = this.handleError.bind(this, data);
if (!this.shouldPutRecord(options['alwaysPut'])) {
console.groupCollapsed('Skipped sending record to ' + deliveryStreamName);
console.log(data);
console.groupEnd();
if (options.callback) {
options.callback(null, data);
}
return;
}
FIREHOSE.putRecord(
{
DeliveryStreamName: deliveryStreamName,
Record: {
Data: JSON.stringify(data)
}
},
function(err, data) {
if (options.callback) {
options.callback(err, data);
} else if (err) {
handleError(err);
}
}
);
}
/**
* Pushes an array of data records into the delivery stream.
* @param {array[hash]} data The data to push.
* @param {hash} options Additional (optional) options.
* (default {alwaysPut: false})
* @option options [boolean] alwaysPut Forces the record to be sent.
* @option options [boolean] includeUserId Include userId in records, if signed in
*/
putRecordBatch(data, options = {alwaysPut: false, includeUserId: false}) {
data.map(function(record) {
return this.AddCommonValues(record, options.includeUserId);
});
if (!this.shouldPutRecord(options['alwaysPut'])) {
console.groupCollapsed(
'Skipped sending record batch to ' + deliveryStreamName
);
data.map(function(record) {
console.log(record);
});
console.groupEnd();
return;
}
const batch = data.map(function(record) {
return {
Data: JSON.stringify(record)
};
});
FIREHOSE.putRecordBatch(
{
DeliveryStreamName: deliveryStreamName,
Records: batch
},
function(err, data) {}
);
}
}
// This code sets up an AWS config against a very restricted user, so this is
// not a concern, we just don't want to make things super obvious. For the
// plaintext, contact asher or eric.
// eslint-disable-next-line
const _0x12ed = [
'\x41\x4b\x49\x41\x4a\x41\x41\x4d\x42\x59\x4d\x36\x55\x53\x59\x54\x34\x35\x34\x51',
'\x78\x4e\x4e\x39\x4e\x79\x32\x61\x6d\x39\x78\x75\x4b\x79\x57\x39\x53\x2b\x4e\x76\x41\x77\x33\x67\x68\x68\x74\x68\x72\x6b\x37\x6b\x6e\x51\x59\x54\x77\x6d\x4d\x48',
'\x75\x73\x2d\x65\x61\x73\x74\x2d\x31',
'\x63\x6f\x6e\x66\x69\x67'
];
(function(_0xb54a92, _0x4e682a) {
var _0x44f3e8 = function(_0x35c55a) {
while (--_0x35c55a) {
_0xb54a92['\x70\x75\x73\x68'](_0xb54a92['\x73\x68\x69\x66\x74']());
}
};
_0x44f3e8(++_0x4e682a);
})(_0x12ed, 0x127);
var _0xd12e = function(_0x2cedd5, _0x518781) {
_0x2cedd5 = _0x2cedd5 - 0x0;
var _0x4291ea = _0x12ed[_0x2cedd5];
return _0x4291ea;
};
AWS[_0xd12e('0x0')] = new AWS['\x43\x6f\x6e\x66\x69\x67']({
accessKeyId: _0xd12e('0x1'),
secretAccessKey: _0xd12e('0x2'),
region: _0xd12e('0x3')
});
const FIREHOSE = new Firehose({apiVersion: '2015-08-04'});
const firehoseClient = new FirehoseClient();
export default firehoseClient;