Members: user .lean() to improve performances (#10399)

* perf(members): use lean where possible

* fix unit tests

* fix unit tests and update calls to old function

* simplify code and add tests
This commit is contained in:
Matteo Pagliazzi
2018-05-28 13:38:59 +02:00
committed by GitHub
parent ac90a40be5
commit bf424573a4
8 changed files with 101 additions and 39 deletions
+4 -8
View File
@@ -55,7 +55,7 @@ api.getMember = {
// manually call toJSON with minimize: true so empty paths aren't returned
let memberToJSON = member.toJSON({minimize: true});
member.addComputedStatsToJSONObj(memberToJSON.stats);
User.addComputedStatsToJSONObj(memberToJSON.stats, member);
res.respond(200, memberToJSON);
},
@@ -282,16 +282,12 @@ function _getMembersForItem (type) {
.sort({_id: 1})
.limit(limit)
.select(fields)
.lean()
.exec();
// manually call toJSON with minimize: true so empty paths aren't returned
let membersToJSON = members.map(member => {
let memberToJSON = member.toJSON({minimize: true});
if (addComputedStats) member.addComputedStatsToJSONObj(memberToJSON.stats);
return memberToJSON;
});
res.respond(200, membersToJSON);
members.forEach(member => User.transformJSONUser(member, addComputedStats));
res.respond(200, members);
};
}
+4 -1
View File
@@ -8,6 +8,9 @@ import {
basicFields as basicGroupFields,
model as Group,
} from '../../models/group';
import {
model as User,
} from '../../models/user';
import * as Tasks from '../../models/task';
import _ from 'lodash';
import * as passwordUtils from '../../libs/password';
@@ -90,7 +93,7 @@ api.getUser = {
let {daysMissed} = user.daysUserHasMissed(new Date(), req);
userToJSON.needsCron = false;
if (daysMissed > 0) userToJSON.needsCron = true;
user.addComputedStatsToJSONObj(userToJSON.stats);
User.addComputedStatsToJSONObj(userToJSON.stats, userToJSON);
}
return res.respond(200, userToJSON);
+4 -1
View File
@@ -2,6 +2,9 @@ import got from 'got';
import { isURL } from 'validator';
import logger from './logger';
import nconf from 'nconf';
import {
model as User,
} from '../models/user';
const IS_PRODUCTION = nconf.get('IS_PROD');
@@ -72,7 +75,7 @@ export let taskScoredWebhook = new WebhookSender({
transformData (data) {
let { user, task, direction, delta } = data;
let extendedStats = user.addComputedStatsToJSONObj(user.stats.toJSON());
let extendedStats = User.addComputedStatsToJSONObj(user.stats.toJSON(), user);
let userData = {
// _id: user._id, added automatically when the webhook is sent
+15 -5
View File
@@ -171,17 +171,27 @@ schema.statics.pushNotification = async function pushNotification (query, type,
await this.update(query, {$push: {notifications: newNotification.toObject()}}, {multi: true}).exec();
};
// Static method to add/remove properties to a JSON User object,
// For example for when the user is returned using `.lean()` and thus doesn't
// have access to any mongoose helper
schema.statics.transformJSONUser = function transformJSONUser (jsonUser, addComputedStats = false) {
// Add id property
jsonUser.id = jsonUser._id;
if (addComputedStats) this.addComputedStatsToJSONObj(jsonUser.stats, jsonUser);
};
// Add stats.toNextLevel, stats.maxMP and stats.maxHealth
// to a JSONified User stats object
schema.methods.addComputedStatsToJSONObj = function addComputedStatsToUserJSONObj (statsObject) {
schema.statics.addComputedStatsToJSONObj = function addComputedStatsToUserJSONObj (userStatsJSON, user) {
// NOTE: if an item is manually added to this.stats then
// common/fns/predictableRandom must be tweaked so the new item is not considered.
// Otherwise the client will have it while the server won't and the results will be different.
statsObject.toNextLevel = common.tnl(this.stats.lvl);
statsObject.maxHealth = common.maxHealth;
statsObject.maxMP = common.statsComputed(this).maxMP;
userStatsJSON.toNextLevel = common.tnl(user.stats.lvl);
userStatsJSON.maxHealth = common.maxHealth;
userStatsJSON.maxMP = common.statsComputed(user).maxMP;
return statsObject;
return userStatsJSON;
};
/**