@@ -5,6 +5,7 @@ const common = require('./utils/common.js');
5
5
const { WriteBatcher} = require ( './parts/data/batcher.js' ) ;
6
6
const { Cacher} = require ( './parts/data/cacher.js' ) ;
7
7
const { changeStreamReader} = require ( './parts/data/changeStreamReader.js' ) ;
8
+ const usage = require ( './aggregator/usage.js' ) ;
8
9
var t = [ "countly:" , "aggregator" ] ;
9
10
t . push ( "node" ) ;
10
11
@@ -21,6 +22,7 @@ plugins.connectToAllDatabases(true).then(function() {
21
22
// common.writeBatcher = new WriteBatcher(common.db);
22
23
23
24
common . writeBatcher = new WriteBatcher ( common . db ) ;
25
+ common . secondaryWriteBatcher = new WriteBatcher ( common . db ) ;
24
26
common . readBatcher = new Cacher ( common . db ) ; //Used for Apps info
25
27
26
28
@@ -29,27 +31,47 @@ plugins.connectToAllDatabases(true).then(function() {
29
31
var changeStream = new changeStreamReader ( common . drillDb , {
30
32
pipeline : [
31
33
{ "$match" : { "operationType" : "insert" , "fullDocument.ce" : true } } ,
32
- { "$project" : { "a" : "$fullDocument.a" , "key" : "$fullDocument.e" , "ts" : "$fullDocument.ts" , "sg" : "$fullDocument.sg" , "count" : "$fullDocument.c" , "s" : "$fullDocument.s" , "dur" : "$fullDocument.dur" } }
34
+ { "$project" : { "__iid" : "$fullDocument._id" , "cd" : "$fullDocument.cd" , " a" : "$fullDocument.a" , "key" : "$fullDocument.e" , "ts" : "$fullDocument.ts" , "sg" : "$fullDocument.sg" , "count" : "$fullDocument.c" , "s" : "$fullDocument.s" , "dur" : "$fullDocument.dur" } }
33
35
] ,
34
36
"name" : "event-ingestion"
35
37
} , ( token , currEvent ) => {
36
38
if ( currEvent && currEvent . a && currEvent . e ) {
37
- common . readBatcher . getOne ( "apps" , currEvent . a , function ( err , app ) {
39
+ // usage.processEventFromStream(currEvent));
40
+ }
41
+ // process next document
42
+ } ) ;
43
+
44
+ common . writeBatcher . addFlushCallback ( "events_data" , function ( token ) {
45
+ console . log ( "flush callback" ) ;
46
+ changeStream . acknowledgeToken ( token ) ;
47
+ } ) ;
48
+ } ) ;
49
+
50
+ plugins . register ( "/aggregator" , function ( ) {
51
+ var changeStream = new changeStreamReader ( common . drillDb , {
52
+ pipeline : [
53
+ { "$match" : { "operationType" : "insert" , "fullDocument.e" : "[CLY]_session" } } ,
54
+ { "$addFields" : { "__id" : "$fullDocument._id" , "cd" : "$fullDocument.cd" } } ,
55
+ ] ,
56
+ "name" : "session-ingestion"
57
+ } , ( token , next ) => {
58
+ var currEvent = next . fullDocument ;
59
+ if ( currEvent && currEvent . a ) {
60
+ //Record in session data
61
+ common . readBatcher . getOne ( "apps" , common . db . ObjectID ( currEvent . a ) , function ( err , app ) {
38
62
//record event totals in aggregated data
39
- if ( currEvent . count && common . isNumber ( currEvent . count ) ) {
40
- currEvent . count = parseInt ( currEvent . count , 10 ) ;
63
+ if ( err ) {
64
+ log . e ( "Error getting app data for session" , err ) ;
65
+ return ;
41
66
}
42
- else {
43
- currEvent . count = 1 ;
67
+ if ( app ) {
68
+ usage . processSessionFromStream ( token , currEvent , { "app_id" : currEvent . a , "app" : app , "time" : common . initTimeObj ( app . timezone , currEvent . ts ) , "appTimezone" : ( app . timezone || "UTC" ) } ) ;
44
69
}
45
-
46
70
} ) ;
47
71
}
48
- // process next document
49
72
} ) ;
50
73
51
- common . writeBatcher . addFlushCallback ( "events_data" , function ( token ) {
52
- console . log ( "flush callback" ) ;
74
+ common . writeBatcher . addFlushCallback ( "users" , function ( token ) {
53
75
changeStream . acknowledgeToken ( token ) ;
54
76
} ) ;
55
77
@@ -157,8 +179,9 @@ plugins.connectToAllDatabases(true).then(function() {
157
179
*/
158
180
async function storeBatchedData ( code ) {
159
181
try {
160
- //await common.writeBatcher.flushAll();
161
- //await common.insertBatcher.flushAll();
182
+ await common . writeBatcher . flushAll ( ) ;
183
+ await common . secondaryWriteBatcher . flushAll ( ) ;
184
+ await common . insertBatcher . flushAll ( ) ;
162
185
console . log ( "Successfully stored batch state" ) ;
163
186
}
164
187
catch ( ex ) {
0 commit comments