(
docIncRems: IncrementalRem[],
docCardInfos: CardPriorityInfo[],
allKbIncRems: IncrementalRem[],
allKbCardInfos: CardPriorityInfo[],
)
| 65 | * @param allKbCardInfos All card infos in the KB (for percentile calculation) |
| 66 | */ |
| 67 | export function computePriorityGraphData( |
| 68 | docIncRems: IncrementalRem[], |
| 69 | docCardInfos: CardPriorityInfo[], |
| 70 | allKbIncRems: IncrementalRem[], |
| 71 | allKbCardInfos: CardPriorityInfo[], |
| 72 | ): PriorityGraphData { |
| 73 | // Calculate percentiles against the ENTIRE KB |
| 74 | // Filter out inheritance-only rems (cardCount === 0) before percentile calculation |
| 75 | const validKbCardInfos = allKbCardInfos.filter(c => c.cardCount === undefined || c.cardCount > 0); |
| 76 | const kbIncRemPercentiles = calculateAllPercentiles(allKbIncRems); |
| 77 | // Per-card universe for cards: matches the Weighted Shield / kbPercentile |
| 78 | // semantics so the graph and the rem badges stay consistent. |
| 79 | const kbCardPercentiles = calculateCardRemPercentilesFromCards(validKbCardInfos); |
| 80 | |
| 81 | const binsAbsolute = createBins('integer'); |
| 82 | const binsKbRelative = createBins('range'); |
| 83 | const now = Date.now(); |
| 84 | |
| 85 | // Fill bins from document-scoped IncRems |
| 86 | for (const item of docIncRems) { |
| 87 | const isDue = item.nextRepDate <= now; |
| 88 | const pAbs = Math.max(0, Math.min(100, item.priority)); |
| 89 | const absIndex = Math.min(Math.floor(pAbs / 5), 19); |
| 90 | if (isDue) binsAbsolute[absIndex].incRemDue++; |
| 91 | else binsAbsolute[absIndex].incRemNotDue++; |
| 92 | |
| 93 | // KB-wide percentile for this item |
| 94 | const pKb = Math.max(0, Math.min(100, kbIncRemPercentiles[item.remId] ?? 100)); |
| 95 | const kbIndex = Math.min(Math.floor(pKb / 5), 19); |
| 96 | if (isDue) binsKbRelative[kbIndex].incRemDue++; |
| 97 | else binsKbRelative[kbIndex].incRemNotDue++; |
| 98 | } |
| 99 | |
| 100 | // Fill bins from document-scoped Cards |
| 101 | // Filter out inheritance-only rems (cardCount === 0) that hold the powerup |
| 102 | // only for child inheritance but have no actual cards themselves. |
| 103 | for (const item of docCardInfos) { |
| 104 | if (item.cardCount !== undefined && item.cardCount <= 0) continue; |
| 105 | const isDue = (item.dueCards ?? 0) > 0; |
| 106 | const pAbs = Math.max(0, Math.min(100, item.priority)); |
| 107 | const absIndex = Math.min(Math.floor(pAbs / 5), 19); |
| 108 | if (isDue) binsAbsolute[absIndex].cardDue++; |
| 109 | else binsAbsolute[absIndex].cardNotDue++; |
| 110 | |
| 111 | const pKb = Math.max(0, Math.min(100, kbCardPercentiles[item.remId] ?? 100)); |
| 112 | const kbIndex = Math.min(Math.floor(pKb / 5), 19); |
| 113 | if (isDue) binsKbRelative[kbIndex].cardDue++; |
| 114 | else binsKbRelative[kbIndex].cardNotDue++; |
| 115 | } |
| 116 | |
| 117 | return { |
| 118 | bins: binsAbsolute, |
| 119 | binsKbRelative, |
| 120 | lastUpdated: new Date().toISOString(), |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | /** |
no test coverage detected