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 !
252 lines
7.6 KiB
JavaScript
252 lines
7.6 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import Activities from '/models/activities';
|
|
const { SimpleSchema } = require('/imports/simpleSchema');
|
|
|
|
const ChecklistItems = new Mongo.Collection('checklistItems');
|
|
|
|
/**
|
|
* An item in a checklist
|
|
*/
|
|
ChecklistItems.attachSchema(
|
|
new SimpleSchema({
|
|
title: {
|
|
/**
|
|
* the text of the item
|
|
*/
|
|
type: String,
|
|
},
|
|
sort: {
|
|
/**
|
|
* the sorting field of the item
|
|
*/
|
|
type: Number,
|
|
},
|
|
isFinished: {
|
|
/**
|
|
* Is the item checked?
|
|
*/
|
|
type: Boolean,
|
|
defaultValue: false,
|
|
},
|
|
checklistId: {
|
|
/**
|
|
* the checklist ID the item is attached to
|
|
*/
|
|
type: String,
|
|
},
|
|
cardId: {
|
|
/**
|
|
* the card ID the item is attached to
|
|
*/
|
|
type: String,
|
|
},
|
|
boardId: {
|
|
/**
|
|
* The ID of the board the item's card is on. Denormalized from the card
|
|
* so the board publication can publish checklist items with a single
|
|
* reactive cursor filtered by boardId. Kept in sync on insert, on the
|
|
* item / its checklist moving to another card, and when the card moves to
|
|
* another board.
|
|
*/
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
createdAt: {
|
|
type: Date,
|
|
optional: true,
|
|
// eslint-disable-next-line consistent-return
|
|
autoValue() {
|
|
if (this.isInsert) {
|
|
return new Date();
|
|
} else if (this.isUpsert) {
|
|
return { $setOnInsert: new Date() };
|
|
} else {
|
|
this.unset();
|
|
}
|
|
},
|
|
},
|
|
modifiedAt: {
|
|
type: Date,
|
|
// eslint-disable-next-line consistent-return
|
|
autoValue() {
|
|
if (this.isInsert || this.isUpsert || this.isUpdate) {
|
|
return new Date();
|
|
} else {
|
|
this.unset();
|
|
}
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
|
|
ChecklistItems.before.insert((userId, doc) => {
|
|
if (!doc.userId) {
|
|
doc.userId = userId;
|
|
}
|
|
});
|
|
|
|
ChecklistItems.helpers({
|
|
async setTitle(title) {
|
|
return await ChecklistItems.updateAsync(this._id, { $set: { title } });
|
|
},
|
|
async check() {
|
|
return await ChecklistItems.updateAsync(this._id, { $set: { isFinished: true } });
|
|
},
|
|
async uncheck() {
|
|
return await ChecklistItems.updateAsync(this._id, { $set: { isFinished: false } });
|
|
},
|
|
async toggleItem() {
|
|
return await ChecklistItems.updateAsync(this._id, { $set: { isFinished: !this.isFinished } });
|
|
},
|
|
async move(checklistId, sortIndex) {
|
|
const checklist = await ReactiveCache.getChecklist(checklistId);
|
|
const cardId = checklist.cardId;
|
|
return await ChecklistItems.updateAsync(this._id, {
|
|
$set: { cardId, checklistId, sort: sortIndex },
|
|
});
|
|
},
|
|
});
|
|
|
|
// Activities helper
|
|
export async function itemCreation(userId, doc) {
|
|
const card = await ReactiveCache.getCard(doc.cardId);
|
|
if (!card) {
|
|
console.warn('[itemCreation] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
|
|
return;
|
|
}
|
|
const boardId = card.boardId;
|
|
await Activities.insertAsync({
|
|
userId,
|
|
activityType: 'addChecklistItem',
|
|
cardId: doc.cardId,
|
|
boardId,
|
|
checklistId: doc.checklistId,
|
|
checklistItemId: doc._id,
|
|
checklistItemName: doc.title,
|
|
listId: card.listId,
|
|
swimlaneId: card.swimlaneId,
|
|
});
|
|
}
|
|
|
|
export async function itemRemover(userId, doc) {
|
|
await Activities.removeAsync({
|
|
checklistItemId: doc._id,
|
|
});
|
|
}
|
|
|
|
export async function publishCheckActivity(userId, doc) {
|
|
const card = await ReactiveCache.getCard(doc.cardId);
|
|
if (!card) {
|
|
console.warn('[publishCheckActivity] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
|
|
return;
|
|
}
|
|
const boardId = card.boardId;
|
|
let activityType;
|
|
if (doc.isFinished) {
|
|
activityType = 'checkedItem';
|
|
} else {
|
|
activityType = 'uncheckedItem';
|
|
}
|
|
const act = {
|
|
userId,
|
|
activityType,
|
|
cardId: doc.cardId,
|
|
boardId,
|
|
checklistId: doc.checklistId,
|
|
checklistItemId: doc._id,
|
|
checklistItemName: doc.title,
|
|
listId: card.listId,
|
|
swimlaneId: card.swimlaneId,
|
|
};
|
|
await Activities.insertAsync(act);
|
|
}
|
|
|
|
export async function publishChekListCompleted(userId, doc) {
|
|
const card = await ReactiveCache.getCard(doc.cardId);
|
|
if (!card) {
|
|
console.warn('[publishChekListCompleted] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
|
|
return;
|
|
}
|
|
const boardId = card.boardId;
|
|
const checklistId = doc.checklistId;
|
|
const checkList = await ReactiveCache.getChecklist(checklistId);
|
|
if (!checkList) {
|
|
console.warn('[publishChekListCompleted] Checklist not found for checklistId:', checklistId, '— skipping activity insert.');
|
|
return;
|
|
}
|
|
const checklistItems = await ReactiveCache.getChecklistItems({ checklistId });
|
|
const isChecklistFinished = checkList.hideAllChecklistItems ||
|
|
(checklistItems.length > 0 && checklistItems.length === checklistItems.filter(i => i.isFinished).length);
|
|
if (isChecklistFinished) {
|
|
const act = {
|
|
userId,
|
|
activityType: 'completeChecklist',
|
|
cardId: doc.cardId,
|
|
boardId,
|
|
checklistId: doc.checklistId,
|
|
checklistName: checkList.title,
|
|
listId: card.listId,
|
|
swimlaneId: card.swimlaneId,
|
|
};
|
|
await Activities.insertAsync(act);
|
|
}
|
|
}
|
|
|
|
export async function publishChekListUncompleted(userId, doc) {
|
|
const card = await ReactiveCache.getCard(doc.cardId);
|
|
if (!card) {
|
|
console.warn('[publishChekListUncompleted] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
|
|
return;
|
|
}
|
|
const boardId = card.boardId;
|
|
const checklistId = doc.checklistId;
|
|
const checkList = await ReactiveCache.getChecklist(checklistId);
|
|
if (!checkList) {
|
|
console.warn('[publishChekListUncompleted] Checklist not found for checklistId:', checklistId, '— skipping activity insert.');
|
|
return;
|
|
}
|
|
// BUGS in IFTTT Rules: https://github.com/wekan/wekan/issues/1972
|
|
// Currently in checklist all are set as uncompleted/not checked,
|
|
// IFTTT Rule does not move card to other list.
|
|
// If following line is negated/changed to:
|
|
// if(!isChecklistFinished){
|
|
// then unchecking of any checkbox will move card to other list,
|
|
// even when all checkboxes are not yet unchecked.
|
|
// What is correct code for only moving when all in list is unchecked?
|
|
// TIPS: Finding files, ignoring some directories with grep -v:
|
|
// cd wekan
|
|
// find . | xargs grep 'count' -sl | grep -v .meteor | grep -v node_modules | grep -v .build
|
|
// Maybe something related here?
|
|
// wekan/client/components/rules/triggers/checklistTriggers.js
|
|
const uncheckItems = await ReactiveCache.getChecklistItems({ checklistId });
|
|
const isChecklistFinished = checkList.hideAllChecklistItems ||
|
|
(uncheckItems.length > 0 && uncheckItems.length === uncheckItems.filter(i => i.isFinished).length);
|
|
if (isChecklistFinished) {
|
|
const act = {
|
|
userId,
|
|
activityType: 'uncompleteChecklist',
|
|
cardId: doc.cardId,
|
|
boardId,
|
|
checklistId: doc.checklistId,
|
|
checklistName: checkList.title,
|
|
listId: card.listId,
|
|
swimlaneId: card.swimlaneId,
|
|
};
|
|
await Activities.insertAsync(act);
|
|
}
|
|
}
|
|
|
|
if (Meteor.isServer) {
|
|
// The items of one checklist in their order, and the per-card queries the
|
|
// minicard's checklist badge and the card's own list make.
|
|
const { ensureIndex } = require('/server/lib/mongoStartup');
|
|
Meteor.startup(async () => {
|
|
await ensureIndex(ChecklistItems, { checklistId: 1, sort: 1 });
|
|
await ensureIndex(ChecklistItems, { cardId: 1 });
|
|
});
|
|
}
|
|
|
|
export default ChecklistItems;
|