-
Notifications
You must be signed in to change notification settings - Fork 981
Expand file tree
/
Copy pathprocessing.js
More file actions
222 lines (211 loc) · 8.97 KB
/
processing.js
File metadata and controls
222 lines (211 loc) · 8.97 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
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
/**
* File contains core data aggregators
*/
var common = require('../utils/common.js');
const { dataBatchReader } = require('../parts/data/dataBatchReader');
const plugins = require('../../plugins/pluginManager.js');
var usage = require('./usage.js');
const log = require('../utils/log.js')('aggregator-core:api');
const {WriteBatcher} = require('../parts/data/batcher.js');
const {Cacher} = require('../parts/data/cacher.js');
//dataviews = require('./parts/data/dataviews.js');
var crypto = require('crypto');
(function() {
function determineType(value) {
var type = "l";
if (Array.isArray(value)) {
type = "a";
}
else if (common.isNumber(value)) {
if ((value + "").length < 16) {
if ((value + "").length === 10 || (value + "").length === 13) {
type = "d"; //timestamp
}
else {
type = "n";
}
}
}
return type;
}
//Core events data aggregator
plugins.register("/aggregator", function() {
new dataBatchReader(common.drillDb, {
pipeline: [
{"$match": {"e": "[CLY]_custom"}},
{
"$group": {
"_id": {
"a": "$a",
"e": "$n",
"h": {"$dateToString": {"date": {"$toDate": "$ts"}, "format": "%Y:%m:%d:%H", "timezone": "UTC"}},
},
"c": {"$sum": "$c"},
"s": {"$sum": "$s"},
"dur": {"$sum": "$dur"}
}
},
{"$sort": {"a": 1}},
{
"$project": {
"_id": 0,
"a": "$_id.a",
"e": "$_id.e",
"h": "$_id.h",
"c": 1,
"s": 1,
"dur": 1
}
}
],
"interval": 10000, //10 seconds
"name": "event-aggregation",
"collection": "drill_events",
}, async function(token, results) {
if (results && results.length > 0) {
await usage.processEventTotalsFromStream(token, results, common.manualWriteBatcher);
//Flush collected changes
await common.manualWriteBatcher.flush("countly", "events_data", token.cd);
}
// process next batch of documents
});
});
//processes session data and updates in aggregated data
plugins.register("/aggregator", function() {
new dataBatchReader(common.drillDb, {
pipeline: [
{"$match": {"e": "[CLY]_session"}},
],
"name": "session-ingestion"
}, async function(token, data) {
if (data.length > 0) {
for (var k = 0; k < data.length; k++) {
var currEvent = data[k];
if (currEvent && currEvent.a) {
//Record in session data
try {
var app = await common.readBatcher.getOne("apps", common.db.ObjectID(currEvent.a));
//record event totals in aggregated data
if (app) {
await usage.processSessionFromStream(token, currEvent, {"app_id": currEvent.a, "app": app, "time": common.initTimeObj(app.timezone, currEvent.ts), "appTimezone": (app.timezone || "UTC")});
}
}
catch (ex) {
log.e("Error processing session event", ex);
return;
}
}
}
await common.manualWriteBatcher.flush("countly", "users", token.cd);
}
});
});
plugins.register("/aggregator", function() {
var writeBatcher = new WriteBatcher(common.db, true);
new dataBatchReader(common.drillDb, {
pipeline: [{"$match": {"e": {"$in": ["[CLY]_session_update"]}}}],
"name": "session-updates",
"collection": "drill_events",
}, async function(token, docs) {
console.log("Processing session updates " + docs.length);
if (docs.length > 0) {
for (var z = 0; z < docs.length; z++) {
var next = docs[z];
if (next && next.a && next.e && next.e === "[CLY]_session_update" && next.ts) {
try {
var app = await common.readBatcher.getOne("apps", common.db.ObjectID(next.a));
if (app) {
var dur = 0;
dur = next.dur || 0;
await usage.processSessionDurationRange(writeBatcher, token, dur, next.did, {"app_id": next.a, "app": app, "time": common.initTimeObj(app.timezone, next.ts), "appTimezone": (app.timezone || "UTC")});
//}
}
}
catch (e) {
log.e(e);
return;
}
}
}
await writeBatcher.flush("countly", "users", token.cd);
}
});
});
//Drill meta aggregator
plugins.register("/aggregator", function() {
var drillMetaCache = new Cacher(common.drillDb); //Used for Apps info
new dataBatchReader(common.drillDb, {
pipeline: [
{
"$project": {
"a": "$a",
"e": "$e",
"n": "$n",
"sg": {"$ifNull": [{"$objectToArray": "$sg"}, [{"k": null, "v": null}]]}
}
},
{"$unwind": "$sg"},
{"$group": {"_id": {"a": "$a", "e": "$e", "n": "$n", "sgk": "$sg.k"}, "sgv": {"$first": "$sg.v"}}}],
"interval": 10000, ///default update interval
"name": "drill-meta",
"collection": "drill_events",
"onClose": async function(callback) {
if (callback) {
callback();
}
},
}, async function(token, results) {
var updates = {};
for (var z = 0; z < results.length; z++) {
if (results[z]._id && results[z]._id.a && results[z]._id.e) {
if (results[z]._id.e === "[CLY]_custom") {
results[z]._id.e = results[z]._id.n;
}
let event_hash = crypto.createHash("sha1").update(results[z]._id.e + results[z]._id.a).digest("hex");
var meta = await drillMetaCache.getOne("drill_meta", {_id: results[z]._id.a + "_meta_" + event_hash});
var app_id = results[z]._id.a;
if ((!meta || !meta._id) && !updates[app_id + "_meta_" + event_hash]) {
updates[app_id + "_meta_" + event_hash] = {
_id: app_id + "_meta_" + event_hash,
app_id: results[z]._id.a,
e: results[z]._id.e,
type: "e"
};
meta = {
_id: app_id + "_meta_" + event_hash,
app_id: results[z]._id.a,
e: results[z]._id.e,
type: "e"
};
}
if (results[z]._id.sgk) {
if (!meta.sg || !meta.sg[results[z]._id.sgk]) {
meta.sg = meta.sg || {};
var type = determineType(results[z].sgv);
meta.sg[results[z]._id.sgk] = {
type: type
};
updates[app_id + "_meta_" + event_hash] = updates[app_id + "_meta_" + event_hash] || {};
updates[app_id + "_meta_" + event_hash]["sg." + results[z]._id.sgk + ".type"] = type;
}
}
}
}
//trigger all updates.
if (Object.keys(updates).length > 0) {
//bulk operation
const bulkOps = Object.keys(updates).map(u => {
return {
updateOne: {
filter: {"_id": u},
update: {$set: updates[u]},
upsert: true
}
};
});
await common.drillDb.collection("drill_meta").bulkWrite(bulkOps);
}
// process next document
});
});
}());