forked from fippo/rtcstats-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
58 lines (53 loc) · 1.49 KB
/
database.js
File metadata and controls
58 lines (53 loc) · 1.49 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
const AWS = require('aws-sdk');
const _ = require('lodash');
function lower(obj) {
let key, keys = Object.keys(obj);
let n = keys.length;
const newobj={};
while (n--) {
key = keys[n];
newobj[key.toLowerCase()] = obj[key];
}
return newobj;
}
module.exports = function(config) {
let firehose;
if (config.firehose && config.firehose.stream) {
AWS.config = config.firehose;
firehose = new AWS.Firehose();
} else {
console.log('No Firehose configuration present. Skipping firehose storage.')
}
return {
put: function(pageUrl, clientId, connectionId, clientFeatures, connectionFeatures) {
const d = new Date().getTime();
const item = {
Date: d - (d % (86400 * 1000)), // just the UTC day
DateTime: d,
ClientId: clientId,
ConnectionId: clientId + '_' + connectionId,
PageUrl: pageUrl,
};
_.forEach(clientFeatures, (value, key) => {
item[key] = value;
});
_.forEach(connectionFeatures, (value, key) => {
item[key] = value;
});
if (firehose) {
firehose.putRecord({
DeliveryStreamName: config.firehose.stream, /* required */
Record: {
Data: JSON.stringify(lower(item))
},
}, (err, data) => {
if (err) {
console.log("Error firehosing data: ", err, JSON.stringify(lower(item)));
} else {
console.log("Successfully firehosed data");
}
});
}
},
};
}