11'use strict' ;
22
33const job = require ( '../parts/jobs/job.js' ) ,
4- log = require ( '../utils/log.js' ) ( 'job:ping' ) ,
5- countlyConfig = require ( "../../frontend/express/config.js" ) ,
6- versionInfo = require ( '../../frontend/express/version.info' ) ,
74 plugins = require ( '../../plugins/pluginManager.js' ) ,
8- request = require ( 'countly-request' ) ( plugins . getConfig ( "security" ) ) ;
9-
5+ tracker = require ( '../parts/mgmt/tracker.js' ) ;
106
117/** Class for the job of pinging servers **/
128class PingJob extends job . Job {
@@ -16,86 +12,114 @@ class PingJob extends job.Job {
1612 * @param {done } done callback
1713 */
1814 run ( db , done ) {
19- request ( { strictSSL : false , uri : ( process . env . COUNTLY_CONFIG_PROTOCOL || "http" ) + "://" + ( process . env . COUNTLY_CONFIG_HOSTNAME || "localhost" ) + ( countlyConfig . path || "" ) + "/configs" } , function ( ) { } ) ;
20- var countlyConfigOrig = JSON . parse ( JSON . stringify ( countlyConfig ) ) ;
21- var url = "https://count.ly/configurations/ce/tracking" ;
22- if ( versionInfo . type !== "777a2bf527a18e0fffe22fb5b3e322e68d9c07a6" ) {
23- url = "https://count.ly/configurations/ee/tracking" ;
24- }
25- plugins . loadConfigs ( db , function ( ) {
15+ plugins . loadConfigs ( db , async function ( ) {
2616 const offlineMode = plugins . getConfig ( "api" ) . offline_mode ;
27- const { countly_tracking } = plugins . getConfig ( 'frontend' ) ;
2817 if ( ! offlineMode ) {
29- request ( url , function ( err , response , body ) {
30- if ( typeof body === "string" ) {
31- try {
32- body = JSON . parse ( body ) ;
33- }
34- catch ( ex ) {
35- body = null ;
36- }
18+ var server = tracker . getBulkServer ( ) ;
19+ var user = tracker . getBulkUser ( server ) ;
20+ if ( ! user ) {
21+ return done ( ) ;
22+ }
23+
24+ try {
25+ var custom = await tracker . getAllData ( ) ;
26+ if ( Object . keys ( custom ) . length ) {
27+ user . user_details ( { "custom" : custom } ) ;
3728 }
38- if ( body ) {
39- if ( countlyConfigOrig . web . use_intercom && typeof body . intercom !== "undefined" ) {
40- countlyConfig . web . use_intercom = body . intercom ;
41- }
42- if ( typeof countlyConfigOrig . web . track === "undefined" && typeof body . stats !== "undefined" ) {
43- if ( body . stats ) {
44- countlyConfig . web . track = null ;
45- }
46- else {
47- countlyConfig . web . track = "none" ;
48- }
49- }
29+ }
30+ catch ( ex ) {
31+ console . log ( "Error collecting server data:" , ex ) ;
32+ }
33+ var days = 90 ;
34+ var current_sync = Date . now ( ) ;
35+
36+ // Atomically retrieve old last_sync value and set new one
37+ var syncResult = await db . collection ( "plugins" ) . findOneAndUpdate (
38+ { _id : "version" } ,
39+ { $set : { last_sync : current_sync } } ,
40+ {
41+ upsert : true ,
42+ returnDocument : 'before' ,
43+ projection : { last_sync : 1 }
5044 }
51- log . d ( err , body , countlyConfigOrig , countlyConfig ) ;
52- if ( countly_tracking ) {
53- db . collection ( "members" ) . findOne ( { global_admin : true } , function ( err2 , member ) {
54- if ( ! err2 && member ) {
55- var date = new Date ( ) ;
56- let domain = plugins . getConfig ( 'api' ) . domain ;
45+ ) ;
5746
58- try {
59- // try to extract hostname from full domain url
60- const urlObj = new URL ( domain ) ;
61- domain = urlObj . hostname ;
62- }
63- catch ( _ ) {
64- // do nothing, domain from config will be used as is
65- }
47+ var last_sync = syncResult . value ? syncResult . value . last_sync : null ;
48+ if ( last_sync ) {
49+ days = Math . floor ( ( new Date ( ) . getTime ( ) - last_sync ) / ( 1000 * 60 * 60 * 24 ) ) ;
50+ }
6651
67- request ( {
68- uri : "https://stats.count.ly/i" ,
69- method : "GET" ,
70- timeout : 4E3 ,
71- qs : {
72- device_id : domain ,
73- app_key : "e70ec21cbe19e799472dfaee0adb9223516d238f" ,
74- timestamp : Math . floor ( date . getTime ( ) / 1000 ) ,
75- hour : date . getHours ( ) ,
76- dow : date . getDay ( ) ,
77- no_meta : true ,
78- events : JSON . stringify ( [
79- {
80- key : "PING" ,
81- count : 1
82- }
83- ] )
52+ if ( days > 0 ) {
53+ //calculate seconds timestamp of days before today
54+ var startTs = Math . round ( ( new Date ( ) . getTime ( ) - ( 30 * 24 * 60 * 60 * 1000 ) ) / 1000 ) ;
55+
56+ //sync server events - use aggregation pipeline to group by day and action on MongoDB side
57+ var aggregationPipeline = [
58+ // Match documents with timestamp greater than startTs and valid action
59+ {
60+ $match : {
61+ ts : { $gt : startTs }
62+ }
63+ } ,
64+ // Add calculated fields for day grouping
65+ {
66+ $addFields : {
67+ // Convert timestamp to date and set to noon (12:00:00)
68+ dayDate : {
69+ $dateFromParts : {
70+ year : { $year : { $toDate : { $multiply : [ "$ts" , 1000 ] } } } ,
71+ month : { $month : { $toDate : { $multiply : [ "$ts" , 1000 ] } } } ,
72+ day : { $dayOfMonth : { $toDate : { $multiply : [ "$ts" , 1000 ] } } } ,
73+ hour : 12 ,
74+ minute : 0 ,
75+ second : 0
8476 }
85- } , function ( a /*, c, b*/ ) {
86- log . d ( 'Done running ping job: %j' , a ) ;
87- done ( ) ;
88- } ) ;
77+ }
78+ }
79+ } ,
80+ // Convert back to timestamp in seconds
81+ {
82+ $addFields : {
83+ noonTimestamp : {
84+ $divide : [ { $toLong : "$dayDate" } , 1000 ]
85+ }
8986 }
90- else {
91- done ( ) ;
87+ } ,
88+ // Group by day and action
89+ {
90+ $group : {
91+ _id : {
92+ day : "$noonTimestamp" ,
93+ action : "$a"
94+ } ,
95+ count : { $sum : 1 }
9296 }
93- } ) ;
97+ } ,
98+ // Project to final format
99+ {
100+ $project : {
101+ _id : 0 ,
102+ action : "$_id.action" ,
103+ timestamp : "$_id.day" ,
104+ count : 1
105+ }
106+ }
107+ ] ;
108+
109+ var cursor = db . collection ( "systemlogs" ) . aggregate ( aggregationPipeline ) ;
110+
111+ while ( cursor && await cursor . hasNext ( ) ) {
112+ let eventData = await cursor . next ( ) ;
113+ user . add_event ( { key : eventData . action , count : eventData . count , timestamp : eventData . timestamp } ) ;
94114 }
95- else {
115+ server . start ( function ( ) {
116+ server . stop ( ) ;
96117 done ( ) ;
97- }
98- } ) ;
118+ } ) ;
119+ }
120+ else {
121+ done ( ) ;
122+ }
99123 }
100124 else {
101125 done ( ) ;
@@ -104,4 +128,4 @@ class PingJob extends job.Job {
104128 }
105129}
106130
107- module . exports = PingJob ;
131+ module . exports = PingJob ;
0 commit comments