Skip to content

Commit

Permalink
[MergeDups] Add failsafe for duplicate guids and other bad merge sets (
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Jan 15, 2025
1 parent b454730 commit 0947624
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/goals/Redux/GoalActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,48 @@ function goalCleanup(goal: Goal): void {
}
}

// Returns goal data for some goal types.
/** Returns goal data for some goal types. */
export async function loadGoalData(goalType: GoalType): Promise<Word[][]> {
switch (goalType) {
case GoalType.MergeDups:
return await getDuplicates(5, maxNumSteps(goalType));
return checkMergeData(await getDuplicates(5, maxNumSteps(goalType)));
case GoalType.ReviewDeferredDups:
return await getGraylistEntries(maxNumSteps(goalType));
return checkMergeData(await getGraylistEntries(maxNumSteps(goalType)));
default:
return [];
}
}

/** Emergency failsafe for bad merge sets. */
function checkMergeData(goalData: Word[][]): Word[][] {
return goalData.filter((dups) => {
const errors: string[] = [];
if (dups.length < 2) {
errors.push("Set of duplicates doesn't have at least 2 words!");
}
const wordGuids = dups.map((w) => w.guid);
if (new Set(wordGuids).size < wordGuids.length) {
errors.push("Set of duplicates has multiple words with the same guid!");
}
if (dups.some((w) => !w.senses.length)) {
errors.push("Set of duplicates has a word with no senses!");
}
const senseGuids = dups.flatMap((w) => w.senses.map((s) => s.guid));
if (new Set(senseGuids).size < senseGuids.length) {
errors.push("Set of duplicates has multiple senses with the same guid!");
}
if (errors.length) {
errors.forEach((e) => {
console.error(e);
alert(e);
});
console.error(dups);
return false; // Skip bad merge set.
}
return true; // Include good merge set.
});
}

async function saveCurrentStep(goal: Goal): Promise<void> {
const userEditId = getUserEditId();
if (userEditId) {
Expand Down

0 comments on commit 0947624

Please sign in to comment.