mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
Reported by email against 10.91: "Still slow on loading cards. Test server with me as only user." One user is the part that says what it is not: with nobody else on the server there is no contention, no queue and no lock, so the time is going into the queries themselves. It was. cards, activities, cardComments, checklists and checklistItems - every collection a board draws and an opened card pulls in - had NO index at all. "The cards of this list", "the comments of this card", "the newest activities of this board" each walked the whole collection. That is invisible on a demo board and expensive on a real one, and worse on FerretDB, whose SQLite backend has to walk the same documents. The activities publication even explains that it keeps its selector flat so it can push down to FerretDB's index; there was no index for it to push down to. Each index matches a selector the app really makes WITH the sort it really uses - boardId+archived, listId+sort, swimlaneId+sort, parentId, boardId+cardNumber for cards; cardId+createdAt and boardId+createdAt for activities and comments; cardId+sort and checklistId+sort for the checklists - because an index on the filter alone still leaves an in-memory sort of everything it matched, which on a board with a year of history is the slow half. They go through the same idempotent ensureIndex every other index here uses: it waits for the replica-set primary, does nothing when the index is already there, and logs a backend that refuses one instead of stopping the server. tests/cardQueryIndexes.test.cjs pins the set, and that each one carries the sort as well as the filter. Thanks to xet7 !
111 lines
3.8 KiB
JavaScript
111 lines
3.8 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import { findWhere, where } from '/imports/lib/collectionHelpers';
|
|
import { getFeatureFlags } from '/models/lib/featureFlags';
|
|
|
|
// Activities don't need a schema because they are always set from the a trusted
|
|
// environment - the server - and there is no risk that a user change the logic
|
|
// we use with this collection. Moreover using a schema for this collection
|
|
// would be difficult (different activities have different fields) and wouldn't
|
|
// bring any direct advantage.
|
|
//
|
|
// XXX The activities API is not so nice and need some functionalities. For
|
|
// instance if a user archive a card, and un-archive it a few seconds later we
|
|
// should remove both activities assuming it was an error the user decided to
|
|
// revert.
|
|
const Activities = new Mongo.Collection('activities');
|
|
|
|
Activities.helpers({
|
|
board() {
|
|
return ReactiveCache.getBoard(this.boardId);
|
|
},
|
|
oldBoard() {
|
|
return ReactiveCache.getBoard(this.oldBoardId);
|
|
},
|
|
user() {
|
|
return ReactiveCache.getUser(this.userId);
|
|
},
|
|
member() {
|
|
return ReactiveCache.getUser(this.memberId);
|
|
},
|
|
list() {
|
|
return ReactiveCache.getList(this.listId);
|
|
},
|
|
swimlane() {
|
|
return ReactiveCache.getSwimlane(this.swimlaneId);
|
|
},
|
|
oldSwimlane() {
|
|
return ReactiveCache.getSwimlane(this.oldSwimlaneId);
|
|
},
|
|
oldList() {
|
|
return ReactiveCache.getList(this.oldListId);
|
|
},
|
|
card() {
|
|
return ReactiveCache.getCard(this.cardId);
|
|
},
|
|
comment() {
|
|
return ReactiveCache.getCardComment(this.commentId);
|
|
},
|
|
// #3606: text to show for editComment/deleteComment activities — the stored
|
|
// commentText, else the live comment's text, else '' (never "undefined").
|
|
commentDisplayText() {
|
|
const { commentActivityDisplayText } = require('./lib/commentActivity');
|
|
return commentActivityDisplayText({
|
|
commentText: this.commentText,
|
|
comment: this.comment(),
|
|
});
|
|
},
|
|
attachment() {
|
|
return ReactiveCache.getAttachment(this.attachmentId);
|
|
},
|
|
checklist() {
|
|
return ReactiveCache.getChecklist(this.checklistId);
|
|
},
|
|
checklistItem() {
|
|
return ReactiveCache.getChecklistItem(this.checklistItemId);
|
|
},
|
|
subtasks() {
|
|
return ReactiveCache.getCard(this.subtaskId);
|
|
},
|
|
customField() {
|
|
return ReactiveCache.getCustomField(this.customFieldId);
|
|
},
|
|
label() {
|
|
// Label activity did not work yet, unable to edit labels when tried this.
|
|
return ReactiveCache.getCard(this.labelId);
|
|
},
|
|
});
|
|
|
|
Activities.before.update((userId, doc, fieldNames, modifier) => {
|
|
modifier.$set = modifier.$set || {};
|
|
modifier.$set.modifiedAt = new Date();
|
|
});
|
|
|
|
Activities.before.insert((userId, doc) => {
|
|
// Admin Panel / Features / Notifications (#5820): stop recording activities
|
|
// entirely when disabled. Returning false cancels the insert, so no activity-feed
|
|
// entry is stored and the notification after.insert hook never fires.
|
|
if (getFeatureFlags().disableActivities) {
|
|
return false;
|
|
}
|
|
doc.createdAt = new Date();
|
|
doc.modifiedAt = doc.createdAt;
|
|
});
|
|
|
|
if (Meteor.isServer) {
|
|
// The card's Activities feed and the board's sidebar both ask for one scope
|
|
// sorted by createdAt descending, with a limit - the publication says so in
|
|
// its own comment about pushing the selector down to an index. There was no
|
|
// index to push it down to, so every poll walked the whole collection, which
|
|
// on a board with a year of history is the slowest thing a card does.
|
|
const { ensureIndex } = require('/server/lib/mongoStartup');
|
|
Meteor.startup(async () => {
|
|
await ensureIndex(Activities, { cardId: 1, createdAt: -1 });
|
|
await ensureIndex(Activities, { boardId: 1, createdAt: -1 });
|
|
await ensureIndex(Activities, { checklistId: 1 });
|
|
});
|
|
}
|
|
|
|
export default Activities;
|