mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
A sprint over 10,000 cards was refused at start and close. With the snapshot rows already outside the sprint document, the rest now is too: - The rollover plan of a close goes to its own collection, scrumRolloverRows, in chunks bounded by rows and bytes; the sprint keeps only rolloverPending: true. A chunk is applied, its cards read at once, and removed; the mark is cleared when none is left. A plan kept in the sprint document before this is still finished as it was. - A Scrum History batch too large for one row is recorded as several rows sharing a batchId, each at most 1,000 records and 3 MB a side. A record is in exactly one row, and undo and redo walk the whole batch, with or without a request ID, so one undo still reverses a whole close. - Daily burndown observations keep their rows in scrumSnapshotRows too. - No 10,000-card limit is left: snapshot inputs, daily capture, scope replay, the daily report and the transfer read every card. Undoing a batch re-hashed its whole History row and sent its whole plan as a selector before every record write, so a 1,500-card undo took four minutes. The row is now checked whole when the operation begins and by its identity fields after that, and the worker check uses the plan's identity and the worker's random id - nothing ever rewrites a pending plan. A 10,500-card sprint closes in 8 s and undoes in 44 s. Thanks to xet7 !
114 lines
7.2 KiB
JavaScript
114 lines
7.2 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Random } from 'meteor/random';
|
|
import { EJSON } from 'meteor/ejson';
|
|
import Boards from '/models/boards';
|
|
import Cards from '/models/cards';
|
|
import Lists from '/models/lists';
|
|
import Swimlanes from '/models/swimlanes';
|
|
import ScrumSprints from '/models/scrumSprints';
|
|
import ScrumReleases from '/models/scrumReleases';
|
|
import ScrumEvents from '/models/scrumEvents';
|
|
import ScrumDailySnapshots from '/models/scrumDailySnapshots';
|
|
import { ScrumImportPending, ScrumImportSteps } from './scrumImportJournal';
|
|
const { writeImportPlan, finishImportPlan } = require('/server/lib/scrumImportWriter');
|
|
const { dailyObservationId } = require('/server/lib/scrumDailyCapture');
|
|
const { normalizeScrumTransfer, remapScrumTransfer, normalizeScrumTransferLosses } = require('/models/lib/scrumTransfer');
|
|
const { chunkSnapshot } = require('/models/lib/scrumSnapshotRows');
|
|
const { reportTotals } = require('/models/lib/scrumReports');
|
|
import { ScrumSnapshotRows } from './scrumSnapshotStore';
|
|
|
|
function inputMap(rows) {
|
|
return new Map((rows || []).map(row => [row._id || row.id, row._id || row.id]));
|
|
}
|
|
// Run before creating users, boards or any other import side effects. Mapping
|
|
// once with source IDs proves structural references exist in the same file.
|
|
export function validateScrumImport(board) {
|
|
if (!board.scrumTransfer) return;
|
|
try {
|
|
const transfer = normalizeScrumTransfer(board.scrumTransfer);
|
|
normalizeScrumTransferLosses(board.scrumTransferLosses);
|
|
const maps = Object.fromEntries(['sprints','releases','events'].map(key => [key,inputMap(transfer[key])]));
|
|
for (const key of ['cards','lists','swimlanes','customFields','users']) maps[key] = inputMap(board[key]);
|
|
remapScrumTransfer(transfer, maps);
|
|
const estimateIds = new Set([transfer.settings.estimateCustomFieldId,
|
|
...transfer.sprints.flatMap(s => [s.startSnapshot?.estimateCustomFieldId,s.closeSnapshot?.estimateCustomFieldId]),
|
|
...transfer.dailyObservations.map(row => row.snapshot.estimateCustomFieldId),
|
|
].filter(Boolean));
|
|
for (const id of estimateIds) if (!(board.customFields || []).some(field => field._id === id && field.type === 'number')) {
|
|
throw new Error('Scrum estimates require a numeric custom field in the imported file');
|
|
}
|
|
} catch (error) { throw new Meteor.Error('invalid-scrum-transfer', error.message); }
|
|
}
|
|
|
|
export async function importScrumTransfer(creator, source, boardId) {
|
|
if (!source.scrumTransfer) return;
|
|
const board = await Boards.findOneAsync(boardId);
|
|
if (!board || !board.hasAdmin(Meteor.userId())) throw new Meteor.Error('not-authorized');
|
|
const normalized = normalizeScrumTransfer(source.scrumTransfer);
|
|
const maps = {};
|
|
for (const key of ['sprints','releases','events']) maps[key] = new Map(normalized[key].map(row => [row._id,Random.id()]));
|
|
for (const key of ['cards','lists','swimlanes','customFields']) maps[key] = new Map(Object.entries(creator[key]));
|
|
maps.users = new Map(Object.entries(creator.members));
|
|
const { transfer, losses } = remapScrumTransfer(normalized, maps);
|
|
const steps = [];
|
|
// All IDs have been allocated and validated before planning documents are
|
|
// persisted. Imported accountabilities are informational, never membership.
|
|
for (const key of ['sprints', 'releases', 'events']) {
|
|
const sourceIds = new Map([...maps[key]].map(([sourceId, targetId]) => [targetId, sourceId]));
|
|
for (const record of transfer[key]) {
|
|
const provenance = record.provenance || { system: 'wekan', recordId: sourceIds.get(record._id),
|
|
...(typeof source._id === 'string' && source._id.length <= 500 ? { projectId: source._id } : {}) };
|
|
// A fresh incarnation per imported record, chosen in the plan so a
|
|
// resumed import writes the same one (never the source board's).
|
|
// A sprint's snapshot rows go to their own chunks (scrumSnapshotStore.js),
|
|
// inserted before the sprint that names them; the sprint keeps headers
|
|
// and the report's totals, from all the rows.
|
|
const stored = { ...record };
|
|
if (key === 'sprints') {
|
|
for (const [field, kind] of [['startSnapshot', 'start'], ['closeSnapshot', 'close']]) {
|
|
if (!record[field]) continue;
|
|
const { header, docs } = chunkSnapshot({ boardId, sprintId: record._id, kind, snapshot: record[field] });
|
|
for (const doc of docs) steps.push({ kind: 'insert', collection: 'snapshotRows', after: doc });
|
|
stored[field] = header;
|
|
}
|
|
if (record.startSnapshot || record.closeSnapshot) stored.reportTotals = reportTotals(record);
|
|
}
|
|
steps.push({ kind: 'insert', collection: key, after: { ...stored, provenance, boardId, revision: 1,
|
|
incarnation: Random.id(), ...(key === 'sprints' ? { scrumImportPending: true } : {}) } });
|
|
}
|
|
}
|
|
for (const [key, collection] of [['cards',Cards],['lists',Lists],['swimlanes',Swimlanes]]) {
|
|
for (const record of transfer[key]) {
|
|
const current = await collection.findOneAsync({ _id: record._id, boardId }, { fields: { scrum: 1, scrumRevision: 1 } });
|
|
if (!current) {
|
|
throw new Meteor.Error('invalid-scrum-transfer', 'An imported item no longer belongs to the destination board');
|
|
}
|
|
const before = Object.fromEntries(['scrum', 'scrumRevision'].filter(field => Object.hasOwn(current, field)).map(field => [field, current[field]]));
|
|
steps.push({ kind: 'update', collection: key, id: record._id, boardId, before,
|
|
after: { scrum: record.scrum, scrumRevision: 1 } });
|
|
}
|
|
}
|
|
for (const row of transfer.dailyObservations) {
|
|
// A day's rows go to their own chunks, as the daily capture stores them.
|
|
const { header, docs } = chunkSnapshot({ boardId, sprintId: row.sprintId, kind: 'daily',
|
|
snapshot: row.snapshot });
|
|
for (const doc of docs) steps.push({ kind: 'insert', collection: 'snapshotRows', after: doc });
|
|
steps.push({ kind: 'insert', collection: 'dailyObservations', after: { ...row, boardId, snapshot: header,
|
|
_id: dailyObservationId(row.sprintId, row.startedAt, row.day) } });
|
|
}
|
|
const report = [...normalizeScrumTransferLosses(source.scrumTransferLosses), ...losses];
|
|
const before = Object.fromEntries(['scrum', 'scrumRevision', 'scrumImportLosses'].filter(field => Object.hasOwn(board, field)).map(field => [field, board[field]]));
|
|
steps.push({ kind: 'update', collection: 'boards', id: boardId, boardId, before,
|
|
after: { scrum: transfer.settings, scrumRevision: 1, scrumImportLosses: report } });
|
|
const identity = await writeImportPlan({ boardId, operationId: Random.id(), userId: Meteor.userId(), steps,
|
|
pending: ScrumImportPending, journal: ScrumImportSteps, equals: EJSON.equals,
|
|
collections: { boards: Boards, cards: Cards, lists: Lists, swimlanes: Swimlanes,
|
|
sprints: ScrumSprints, releases: ScrumReleases, events: ScrumEvents, dailyObservations: ScrumDailySnapshots,
|
|
snapshotRows: ScrumSnapshotRows } });
|
|
// A collector must never record a half-imported active sprint. Interrupted
|
|
// imports keep this marker and cannot be exported as complete transfers.
|
|
await finishImportPlan({ identity, total: steps.length, pending: ScrumImportPending, journal: ScrumImportSteps,
|
|
clearMarkers: () => ScrumSprints.updateAsync({ boardId, _id: { $in: transfer.sprints.map(row => row._id) } },
|
|
{ $unset: { scrumImportPending: '' } }, { multi: true }) });
|
|
}
|