Skip to content

Commit a793b7a

Browse files
committed
[db] count optimizations
1 parent 077023c commit a793b7a

File tree

10 files changed

+20
-21
lines changed

10 files changed

+20
-21
lines changed

api/parts/data/fetch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ fetch.getTotalUsersObjWithOptions = function(metric, params, options, callback)
14441444
}
14451445

14461446
if (groupBy === "users") {
1447-
options.db.collection("app_users" + params.app_id).find(match).count(function(error, appUsersDbResult) {
1447+
options.db.collection("app_users" + params.app_id).count(match, function(error, appUsersDbResult) {
14481448
if (!error && appUsersDbResult) {
14491449
callback([{ "_id": "users", "u": appUsersDbResult }]);
14501450
}
@@ -2045,8 +2045,8 @@ fetch.alljobs = async function(metric, params) {
20452045
fetch.jobDetails = async function(metric, params) {
20462046
const columns = ["schedule", "next", "finished", "status", "data", "duration"];
20472047
let sort = {};
2048+
const total = await common.db.collection('jobs').count({ name: params.qstring.name });
20482049
const cursor = common.db.collection('jobs').find({ name: params.qstring.name });
2049-
const total = await cursor.count();
20502050
sort[columns[params.qstring.iSortCol_0 || 0]] = (params.qstring.sSortDir_0 === "asc") ? 1 : -1;
20512051
cursor.sort(sort);
20522052
cursor.skip(Number(params.qstring.iDisplayStart || 0));

api/parts/mgmt/app_users.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ usersApi.count = function(app_id, query, callback) {
330330
query: query
331331
});
332332

333-
common.db.collection('app_users' + app_id).find(query).count(callback);
333+
common.db.collection('app_users' + app_id).count(query, callback);
334334
};
335335

336336
/**

api/parts/mgmt/users.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ usersApi.deleteOwnAccount = function(params) {
666666
verifyMemberArgon2Hash(params.member.email, params.qstring.password, (err, member) => {
667667
if (member) {
668668
if (member.global_admin) {
669-
common.db.collection('members').find({'global_admin': true}).count(function(err2, count) {
669+
common.db.collection('members').count({'global_admin': true}, function(err2, count) {
670670
if (err2) {
671671
console.log(err2);
672672
common.returnMessage(params, 400, 'Mongo error');
@@ -972,7 +972,7 @@ usersApi.fetchNotes = async function(params) {
972972
log.e(' got error while paring query notes request', e);
973973
}
974974
let count = 0;
975-
common.db.collection('notes').find(query).count(function(error, noteCount) {
975+
common.db.collection('notes').count(query, function(error, noteCount) {
976976
if (!error && noteCount) {
977977
count = noteCount;
978978
common.db.collection('notes').find(query)

api/utils/taskmanager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ taskmanager.getTableQueryResult = async function(options, callback) {
641641
catch (e) {
642642
log.e(' got error while process task request parse', e);
643643
}
644-
const count = await options.db.collection("long_tasks").find(options.query, options.projection).count();
644+
const count = await options.db.collection("long_tasks").count(options.query);
645645
return options.db.collection("long_tasks").find(options.query, options.projection).sort(sortBy).skip(skip).limit(limit).toArray((err, list) => {
646646
if (!err) {
647647
callback(null, {list, count});

plugins/assistant/api/assistantJob.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const _ = require('underscore');
9595
},
9696
function(parallelCallback) {
9797
// (1.5) Share dashboard
98-
db.collection('members').find({user_of: apc.app_id}).count(function(err1, userCount) {
98+
db.collection('members').count({user_of: apc.app_id}, function(err1, userCount) {
9999
const anc = assistant.prepareNotificationSpecificFields(apc, "assistant.share-dashboard", assistant.NOTIF_TYPE_QUICK_TIPS, 3, NOTIFICATION_VERSION);
100100
const not_enough_users = (userCount < 3);
101101
const max_show_time_not_exceeded = anc.showAmount < 1;

plugins/compliance-hub/api/api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const FEATURE_NAME = 'compliance_hub';
150150
query = {};
151151
}
152152
}
153-
common.db.collection("consent_history" + params.qstring.app_id).find(query).count(function(err, total) {
153+
common.db.collection("consent_history" + params.qstring.app_id).count(query, function(err, total) {
154154
if (err) {
155155
common.returnMessage(params, 400, err);
156156
}

plugins/crashes/api/api.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ plugins.setConfigs("crashes", {
184184
}
185185
}
186186
if (shouldRecalculate) {
187-
common.db.collection('app_crashusers' + params.app_id).find({"group": 0, crashes: { $gt: 0 }}).count(function(crashErr, userCount) {
188-
common.db.collection('app_crashusers' + params.app_id).find({"group": 0, crashes: { $gt: 0 }, fatal: { $gt: 0 }}).count(function(crashUsersErr, fatalCount) {
187+
common.db.collection('app_crashusers' + params.app_id).count({"group": 0, crashes: { $gt: 0 }}, function(crashErr, userCount) {
188+
common.db.collection('app_crashusers' + params.app_id).count({"group": 0, crashes: { $gt: 0 }, fatal: { $gt: 0 }}, function(crashUsersErr, fatalCount) {
189189
var set = {};
190190
set.users = userCount;
191191
set.usersfatal = fatalCount;
@@ -1569,8 +1569,8 @@ plugins.setConfigs("crashes", {
15691569
});
15701570
}, function() {
15711571
//recalculate users
1572-
common.db.collection('app_crashusers' + params.qstring.app_id).find({"group": 0, crashes: { $gt: 0 }}).count(function(crashUsersErr, userCount) {
1573-
common.db.collection('app_crashusers' + params.qstring.app_id).find({"group": 0, crashes: { $gt: 0 }, fatal: { $gt: 0 }}).count(function(crashGroupsErr, fatalCount) {
1572+
common.db.collection('app_crashusers' + params.qstring.app_id).count({"group": 0, crashes: { $gt: 0 }}, function(crashUsersErr, userCount) {
1573+
common.db.collection('app_crashusers' + params.qstring.app_id).count({"group": 0, crashes: { $gt: 0 }, fatal: { $gt: 0 }}, function(crashGroupsErr, fatalCount) {
15741574
var update = {};
15751575
update.$set = {};
15761576
update.$set.users = userCount;

plugins/push/api/api-message.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ async function generateDemoData(msg, demo) {
667667
await common.db.collection('apps').updateOne({_id: msg.app, 'plugins.push.h._id': {$exists: false}}, {$set: {'plugins.push.h._id': 'demo'}});
668668

669669
let app = await common.db.collection('apps').findOne({_id: msg.app}),
670-
count = await common.db.collection('app_users' + msg.app).find().count(),
670+
count = await common.db.collection('app_users' + msg.app).count(),
671671
events = [],
672672
result = msg.result || new Result();
673673

plugins/slipping-away-users/api/api.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,12 @@ catch (ex) {
115115

116116
conditions.forEach((condition) => {
117117
tasks.push(new BPromise(function(resolve, reject) {
118-
countlyDb.collection('app_users' + app_id).find(condition)
119-
.count(function(err, count) {
120-
if (err) {
121-
return reject(err);
122-
}
123-
return resolve(count);
124-
});
118+
countlyDb.collection('app_users' + app_id).count(condition, function(err, count) {
119+
if (err) {
120+
return reject(err);
121+
}
122+
return resolve(count);
123+
});
125124
}));
126125
});
127126

plugins/views/api/api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ const escapedViewSegments = { "name": true, "segment": true, "height": true, "wi
692692
}
693693
else {
694694
var qq = settings.count_query || {};
695-
common.db.collection("app_viewsmeta" + app_id).find(qq).count(function(err, total) {
695+
common.db.collection("app_viewsmeta" + app_id).count(qq, function(err, total) {
696696
common.db.collection(collectionName).aggregate(pipeline, {allowDiskUse: true}, function(err1, res) {
697697
if (err1) {
698698
log.e(err1);

0 commit comments

Comments
 (0)