Skip to content

Commit 04db151

Browse files
authored
Merge pull request #3192 from Countly/fixes/nextt
[cache] fixes
2 parents 7121121 + a793b7a commit 04db151

File tree

13 files changed

+79
-34
lines changed

13 files changed

+79
-34
lines changed

api/parts/data/cache.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DataStore {
5454
constructor(size, age, dispose, Cls) {
5555
this.size = size;
5656
this.age = age;
57-
this.lru = new LRU({max: size || (Math.pow(2, 32) - 1), ttl: age || Number.MAX_SAFE_INTEGER, dispose: dispose, noDisposeOnSet: true, updateAgeOnGet: true});
57+
this.lru = new LRU({max: size || 10000, ttl: age || Number.MAX_SAFE_INTEGER, dispose: dispose, noDisposeOnSet: true, updateAgeOnGet: true});
5858
if (Cls) {
5959
this.Cls = Cls;
6060
this.Clas = require('../../../' + Cls[0])[Cls[1]];
@@ -588,7 +588,7 @@ class CacheMaster {
588588
this.operators[group] = {init, Cls, read, write, update, remove};
589589

590590
if (!size && size !== 0) {
591-
size = config.api && config.api.cache && config.api.cache[group] && config.api.cache[group].size !== undefined ? config.api.cache[group].size : Number.MAX_SAFE_INTEGER;
591+
size = config.api && config.api.cache && config.api.cache[group] && config.api.cache[group].size !== undefined ? config.api.cache[group].size : 10000;
592592
}
593593

594594
if (!age && age !== 0) {
@@ -1052,4 +1052,4 @@ class TestDataClass extends Jsonable {
10521052
}
10531053

10541054

1055-
module.exports = {CacheMaster, CacheWorker, TestDataClass};
1055+
module.exports = {CacheMaster, CacheWorker, TestDataClass};

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/compare/api/api.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ const FEATURE_NAME = 'compare';
5050
}
5151

5252
if (!params.qstring.events || params.qstring.events.length === 0) {
53-
return common.returnMessage(params, 400, 'Missing parameter: events');
53+
common.returnMessage(params, 400, 'Missing parameter: events');
54+
return true;
5455
}
5556

5657
if (params.qstring.events.length > 20) {
57-
return common.returnMessage(params, 400, 'Maximum length for parameter events is 20');
58+
common.returnMessage(params, 400, 'Maximum length for parameter events is 20');
59+
return true;
5860
}
5961

6062
validateRead(params, FEATURE_NAME, function() {
@@ -132,17 +134,20 @@ const FEATURE_NAME = 'compare';
132134
}
133135

134136
if (!params.qstring.apps || params.qstring.apps.length === 0) {
135-
return common.returnMessage(params, 400, 'Missing parameter: apps');
137+
common.returnMessage(params, 400, 'Missing parameter: apps');
138+
return true;
136139
}
137140

138141
if (params.qstring.apps.length > 20) {
139-
return common.returnMessage(params, 400, 'Maximum length for parameter apps is 20');
142+
common.returnMessage(params, 400, 'Maximum length for parameter apps is 20');
143+
return true;
140144
}
141145

142146
var appsToFetch = params.qstring.apps;
143147
for (var appsFetchIndex = 0; appsFetchIndex < appsToFetch.length; appsFetchIndex++) {
144148
if (appsToFetch[appsFetchIndex].length !== 24) {
145-
return common.returnMessage(params, 400, 'Invalid app id length in apps parameter, each app id should be 24 characters long');
149+
common.returnMessage(params, 400, 'Invalid app id length in apps parameter, each app id should be 24 characters long');
150+
return true;
146151
}
147152
}
148153
params.qstring.app_id = appsToFetch[0];
@@ -153,11 +158,13 @@ const FEATURE_NAME = 'compare';
153158
for (var i = 0; i < appsToFetch.length; i++) {
154159
if (params.member && userApps) {
155160
if (userApps.indexOf(appsToFetch[i]) === -1) {
156-
return common.returnMessage(params, 401, 'User does not have view rights for one or more apps provided in apps parameter');
161+
common.returnMessage(params, 401, 'User does not have view rights for one or more apps provided in apps parameter');
162+
return true;
157163
}
158164
}
159165
else {
160-
return common.returnMessage(params, 401, 'User does not have view rights for one or more apps provided in apps parameter');
166+
common.returnMessage(params, 401, 'User does not have view rights for one or more apps provided in apps parameter');
167+
return true;
161168
}
162169
}
163170
}

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

+7-7
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;
@@ -1599,7 +1599,7 @@ plugins.setConfigs("crashes", {
15991599

16001600
plugins.register("/i/apps/create", function(ob) {
16011601
var appId = ob.appId;
1602-
common.db.collection('app_crashgroups' + appId).insert({_id: "meta"}, function() {});
1602+
common.db.collection('app_crashgroups' + appId).updateOne({_id: "meta"}, {$set: {_id: "meta"}}, {upsert: true}, function() {});
16031603
common.db.collection('app_crashgroups' + appId).ensureIndex({"name": 1}, {background: true}, function() {});
16041604
common.db.collection('app_crashgroups' + appId).ensureIndex({"os": 1}, {background: true}, function() {});
16051605
common.db.collection('app_crashgroups' + appId).ensureIndex({"reports": 1}, {background: true}, function() {});
@@ -1651,7 +1651,7 @@ plugins.setConfigs("crashes", {
16511651
common.db.collection('app_crashusers' + appId).ensureIndex({"uid": 1}, {background: true}, function() {});
16521652
});
16531653
common.db.collection('app_crashgroups' + appId).drop(function() {
1654-
common.db.collection('app_crashgroups' + appId).insert({_id: "meta"}, function() {});
1654+
common.db.collection('app_crashgroups' + appId).updateOne({_id: "meta"}, {$set: {_id: "meta"}}, {upsert: true}, function() {});
16551655
common.db.collection('app_crashgroups' + appId).ensureIndex({"name": 1}, {background: true}, function() {});
16561656
common.db.collection('app_crashgroups' + appId).ensureIndex({"os": 1}, {background: true}, function() {});
16571657
common.db.collection('app_crashgroups' + appId).ensureIndex({"reports": 1}, {background: true}, function() {});
@@ -1681,7 +1681,7 @@ plugins.setConfigs("crashes", {
16811681
common.db.collection('app_crashusers' + appId).ensureIndex({"uid": 1}, {background: true}, function() {});
16821682
});
16831683
common.db.collection('app_crashgroups' + appId).drop(function() {
1684-
common.db.collection('app_crashgroups' + appId).insert({_id: "meta"}, function() {});
1684+
common.db.collection('app_crashgroups' + appId).updateOne({_id: "meta"}, {$set: {_id: "meta"}}, {upsert: true}, function() {});
16851685
common.db.collection('app_crashgroups' + appId).ensureIndex({"name": 1}, {background: true}, function() {});
16861686
common.db.collection('app_crashgroups' + appId).ensureIndex({"os": 1}, {background: true}, function() {});
16871687
common.db.collection('app_crashgroups' + appId).ensureIndex({"reports": 1}, {background: true}, function() {});

plugins/pluginManager.js

+39
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,17 @@ var pluginManager = function pluginManager() {
14401440
return;
14411441
}
14421442
return function(err, res) {
1443+
if (res) {
1444+
if (!res.result) {
1445+
res.result = {};
1446+
}
1447+
if (!res.result.ok) {
1448+
res.result.ok = !err;
1449+
}
1450+
if (!res.result.nModified) {
1451+
res.result.nModified = res.modifiedCount || 0;
1452+
}
1453+
}
14431454
if (err) {
14441455
if (retry && err.code === 11000) {
14451456
if (typeof retry === "function") {
@@ -1740,10 +1751,17 @@ var pluginManager = function pluginManager() {
17401751
logDbRead.d("aggregate " + collection + " %j %j" + at, query, options);
17411752
logDbRead.d("From connection %j", countlyDb._cly_debug);
17421753
var cursor = this._aggregate(query, options);
1754+
cursor._count = cursor.count;
1755+
cursor.count = function(...countArgs) {
1756+
return ob.countDocuments.call(ob, query, ...countArgs);
1757+
};
17431758
cursor._toArray = cursor.toArray;
17441759
cursor.toArray = function(cb) {
17451760
return handlePromiseErrors(cursor._toArray(logForReads(cb, e, copyArguments(args, "aggregate"))), e, copyArguments(arguments, "aggregate"));
17461761
};
1762+
cursor.isClosed = function() {
1763+
return cursor.closed || cursor.killed;
1764+
};
17471765
if (typeof callback === "function") {
17481766
return cursor.toArray(callback);
17491767
}
@@ -1782,10 +1800,17 @@ var pluginManager = function pluginManager() {
17821800
logDbRead.d("find " + collection + " %j %j" + at, query, options);
17831801
logDbRead.d("From connection %j", countlyDb._cly_debug);
17841802
var cursor = this._find(query, options);
1803+
cursor._count = cursor.count;
1804+
cursor.count = function(...countArgs) {
1805+
return ob.countDocuments.call(ob, query, ...countArgs);
1806+
};
17851807
cursor._toArray = cursor.toArray;
17861808
cursor.toArray = function(callback) {
17871809
return handlePromiseErrors(cursor._toArray(logForReads(callback, e, copyArguments(args, "find"))), e, copyArguments(arguments, "find"));
17881810
};
1811+
cursor.isClosed = function() {
1812+
return cursor.closed || cursor.killed;
1813+
};
17891814
return cursor;
17901815
};
17911816

@@ -1841,6 +1866,20 @@ var pluginManager = function pluginManager() {
18411866
return ob.findOneAndDelete(query, options, callback);
18421867
};
18431868

1869+
ob._drop = ob.drop;
1870+
ob.drop = function() {
1871+
if (!arguments.length) {
1872+
return ob._drop().catch(function(ex) {
1873+
if (ex.code !== 26) {
1874+
throw ex;
1875+
}
1876+
});
1877+
}
1878+
else {
1879+
return ob._drop.apply(ob, arguments);
1880+
}
1881+
};
1882+
18441883

18451884
countlyDb._collection_cache[collection] = ob;
18461885

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)