-
Notifications
You must be signed in to change notification settings - Fork 980
Expand file tree
/
Copy pathping.js
More file actions
131 lines (121 loc) · 5.12 KB
/
ping.js
File metadata and controls
131 lines (121 loc) · 5.12 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
'use strict';
const job = require('../parts/jobs/job.js'),
plugins = require('../../plugins/pluginManager.js'),
tracker = require('../parts/mgmt/tracker.js');
/** Class for the job of pinging servers **/
class PingJob extends job.Job {
/**
* Run the ping job
* @param {Db} db connection
* @param {done} done callback
*/
run(db, done) {
plugins.loadConfigs(db, async function() {
const offlineMode = plugins.getConfig("api").offline_mode;
if (!offlineMode) {
var server = tracker.getBulkServer();
var user = tracker.getBulkUser(server);
if (!user) {
return done();
}
try {
var custom = await tracker.getAllData();
if (Object.keys(custom).length) {
user.user_details({"custom": custom });
}
}
catch (ex) {
console.log("Error collecting server data:", ex);
}
var days = 90;
var current_sync = Date.now();
// Atomically retrieve old last_sync value and set new one
var syncResult = await db.collection("plugins").findOneAndUpdate(
{_id: "version"},
{$set: {last_sync: current_sync}},
{
upsert: true,
returnDocument: 'before',
projection: {last_sync: 1}
}
);
var last_sync = syncResult.value ? syncResult.value.last_sync : null;
if (last_sync) {
days = Math.floor((new Date().getTime() - last_sync) / (1000 * 60 * 60 * 24));
}
if (days > 0) {
//calculate seconds timestamp of days before today
var startTs = Math.round((new Date().getTime() - (30 * 24 * 60 * 60 * 1000)) / 1000);
//sync server events - use aggregation pipeline to group by day and action on MongoDB side
var aggregationPipeline = [
// Match documents with timestamp greater than startTs and valid action
{
$match: {
ts: { $gt: startTs }
}
},
// Add calculated fields for day grouping
{
$addFields: {
// Convert timestamp to date and set to noon (12:00:00)
dayDate: {
$dateFromParts: {
year: { $year: { $toDate: { $multiply: ["$ts", 1000] } } },
month: { $month: { $toDate: { $multiply: ["$ts", 1000] } } },
day: { $dayOfMonth: { $toDate: { $multiply: ["$ts", 1000] } } },
hour: 12,
minute: 0,
second: 0
}
}
}
},
// Convert back to timestamp in seconds
{
$addFields: {
noonTimestamp: {
$divide: [{ $toLong: "$dayDate" }, 1000]
}
}
},
// Group by day and action
{
$group: {
_id: {
day: "$noonTimestamp",
action: "$a"
},
count: { $sum: 1 }
}
},
// Project to final format
{
$project: {
_id: 0,
action: "$_id.action",
timestamp: "$_id.day",
count: 1
}
}
];
var cursor = db.collection("systemlogs").aggregate(aggregationPipeline);
while (cursor && await cursor.hasNext()) {
let eventData = await cursor.next();
user.add_event({key: eventData.action, count: eventData.count, timestamp: eventData.timestamp});
}
server.start(function() {
server.stop();
done();
});
}
else {
done();
}
}
else {
done();
}
});
}
}
module.exports = PingJob;