Skip to content

Commit cee7700

Browse files
Merge branch 'next' into eslint-ecma-change
2 parents fec0272 + e032591 commit cee7700

File tree

3 files changed

+67
-78
lines changed

3 files changed

+67
-78
lines changed

plugins/logger/api/api.js

+19-25
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var exported = {},
44
automaticStateManager = require('./helpers/automaticStateManager'),
55
log = require('../../../api/utils/log.js')('logger:api'),
66
{ validateRead } = require('../../../api/utils/rights.js');
7-
7+
const JOB = require('../../../api/parts/jobs');
8+
const MAX_NUMBER_OF_LOG_ENTRIES = 1000;
89
const FEATURE_NAME = 'logger';
910

1011
var RequestLoggerStateEnum = {
@@ -20,6 +21,13 @@ plugins.setConfigs("logger", {
2021
});
2122

2223
(function() {
24+
plugins.register("/master", function() {
25+
setTimeout(() => {
26+
JOB.job('logger:clear', { max: MAX_NUMBER_OF_LOG_ENTRIES })
27+
.replace()
28+
.schedule("every 5 minutes");
29+
}, 10000);
30+
});
2331

2432
plugins.register("/permissions/features", function(ob) {
2533
ob.features.push(FEATURE_NAME);
@@ -345,46 +353,32 @@ plugins.setConfigs("logger", {
345353
return true;
346354
}
347355
if (params.qstring.method === 'collection_info') {
348-
validateRead(params, FEATURE_NAME, async function(parameters) {
349-
try {
350-
var stats = await common.db.collection('logs' + parameters.app_id).aggregate([ { $collStats: { storageStats: { } } } ]).toArray();
351-
common.returnOutput(parameters, {capped: stats?.[0]?.storageStats?.capped, max: stats?.[0]?.storageStats?.max});
352-
}
353-
catch (ex) {
354-
console.log("Failed fetching logs collection info: ", ex);
355-
common.returnMessage(parameters, 400, 'Error fetching collection info');
356-
}
356+
validateRead(params, FEATURE_NAME, function(parameters) {
357+
common.db.collection('logs' + parameters.app_id).stats(function(err, stats) {
358+
if (err) {
359+
console.log("Failed fetching logs collection info", err);
360+
return common.returnMessage(parameters, 400, 'Error fetching collection info');
361+
}
362+
common.returnOutput(parameters, stats && {size: stats.size, count: stats.count, max: MAX_NUMBER_OF_LOG_ENTRIES} || {});
363+
});
357364
});
358365
return true;
359366
}
360367
});
361368

362-
plugins.register("/i/apps/create", function(ob) {
363-
var appId = ob.appId;
364-
common.db.command({"convertToCapped": 'logs' + appId, size: 10000000, max: 1000}, function(err) {
365-
if (err) {
366-
common.db.createCollection('logs' + appId, {capped: true, size: 10000000, max: 1000}, function() {});
367-
}
368-
});
369-
});
370-
371369
plugins.register("/i/apps/delete", function(ob) {
372370
var appId = ob.appId;
373371
common.db.collection('logs' + appId).drop(function() {});
374372
});
375373

376374
plugins.register("/i/apps/reset", function(ob) {
377375
var appId = ob.appId;
378-
common.db.collection('logs' + appId).drop(function() {
379-
common.db.createCollection('logs' + appId, {capped: true, size: 10000000, max: 1000}, function() {});
380-
});
376+
common.db.collection('logs' + appId).drop(function() {});
381377
});
382378

383379
plugins.register("/i/apps/clear_all", function(ob) {
384380
var appId = ob.appId;
385-
common.db.collection('logs' + appId).drop(function() {
386-
common.db.createCollection('logs' + appId, {capped: true, size: 10000000, max: 1000}, function() {});
387-
});
381+
common.db.collection('logs' + appId).drop(function() {});
388382
});
389383
}(exported));
390384

plugins/logger/api/jobs/clear.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @typedef {import("mongodb").Db} Database
3+
* @typedef {import("mongodb").ObjectId} ObjectId
4+
*/
5+
const JOB = require("../../../../api/parts/jobs/job.js");
6+
const log = require("../../../../api/utils/log.js")("job:logger:clear");
7+
8+
const DEFAULT_MAX_ENTRIES = 1000;
9+
10+
/**
11+
* clears logs
12+
*/
13+
class ClearJob extends JOB.Job {
14+
/**
15+
* Cleans up the logs{APPID} collection
16+
* @param {Database} db mongodb database instance
17+
*/
18+
async run(db) {
19+
let max = this.data.max;
20+
if (typeof max !== "number") {
21+
log.e(
22+
"Maximum number of log entries required. Falling back to default value:",
23+
DEFAULT_MAX_ENTRIES
24+
);
25+
max = DEFAULT_MAX_ENTRIES;
26+
}
27+
28+
log.d("Started: cleaning logs before the last", max);
29+
const appIds = await db.collection("apps").find().project({ _id: 1 }).toArray();
30+
const loggerCollections = appIds.map(({_id}) => "logs" + _id.toString());
31+
32+
for (const colName of loggerCollections) {
33+
const col = db.collection(colName);
34+
// find the first entry we want to keep
35+
const firstEntry = await col.find({}).project({ _id: 1 }).sort({ _id: -1 })
36+
.skip(max - 1).limit(1).toArray();
37+
if (!firstEntry[0]) {
38+
continue;
39+
}
40+
// delete all previous entries
41+
const result = await col.deleteMany({ _id: { $lt: firstEntry[0]._id } });
42+
log.d("Cleaned up", result.deletedCount, "entries from", colName);
43+
}
44+
log.d("Finished cleaning logs");
45+
}
46+
}
47+
48+
module.exports = ClearJob;

plugins/logger/install.js

-53
Original file line numberDiff line numberDiff line change
@@ -1,53 +0,0 @@
1-
var async = require('async'),
2-
pluginManager = require('../pluginManager.js');
3-
4-
console.log("Installing logger plugin");
5-
pluginManager.dbConnection().then((countlyDb) => {
6-
countlyDb.collection('apps').find({}).toArray(function(err, apps) {
7-
8-
if (!apps || err) {
9-
countlyDb.close();
10-
return;
11-
}
12-
function upgrade(app, done) {
13-
console.log("Creating logs collection for " + app.name);
14-
function cb() {
15-
done();
16-
}
17-
18-
countlyDb.command({ "listCollections": 1, "filter": { "name": "logs" + app._id } }, function(err, res) {
19-
if (err) {
20-
console.log(err);
21-
cb();
22-
}
23-
else {
24-
//check if collection capped
25-
if (res && res.cursor && res.cursor.firstBatch && res.cursor.firstBatch.length > 0) {
26-
//collection exists
27-
if (!res.cursor.firstBatch[0].options.capped) {
28-
console.log("converting to the capped");
29-
countlyDb.command({ "convertToCapped": 'logs' + app._id, size: 10000000, max: 1000 },
30-
function(err) {
31-
if (err) {
32-
console.log(err);
33-
}
34-
cb();
35-
});
36-
}
37-
else {
38-
cb();
39-
}
40-
}
41-
else { //collection does not exist
42-
console.log("collection does not exist");
43-
countlyDb.createCollection('logs' + app._id, { capped: true, size: 10000000, max: 1000 }, cb);
44-
}
45-
}
46-
});
47-
}
48-
async.forEach(apps, upgrade, function() {
49-
console.log("Logger plugin installation finished");
50-
countlyDb.close();
51-
});
52-
});
53-
});

0 commit comments

Comments
 (0)