|
| 1 | +/** |
| 2 | + * Pure aggregation for the クラス・課題 俯瞰ダッシュボード (Admin, EPIC #1073). |
| 3 | + * |
| 4 | + * The operator wants to grasp "どんな課題がどれくらい作られているか" along three |
| 5 | + * axes (chosen in the design interview): creation trend, content richness and |
| 6 | + * theme tendency — NOT popularity/engagement. Kept pure so the whole shape is |
| 7 | + * unit-tested without DynamoDB. |
| 8 | + */ |
| 9 | + |
| 10 | +/** A classroom row as stored (only the fields we aggregate on). */ |
| 11 | +export interface ClassroomRow { |
| 12 | + classroomId?: unknown; |
| 13 | + className?: unknown; |
| 14 | + assignmentName?: unknown; |
| 15 | + teacherSub?: unknown; |
| 16 | + status?: unknown; // 'active' | 'archived' |
| 17 | + createdAt?: unknown; |
| 18 | + content?: { pages?: { text?: string; imageKey?: string }[]; starterKey?: string }; |
| 19 | +} |
| 20 | + |
| 21 | +/** Quota rows reuse the classrooms key space; never count them. */ |
| 22 | +export function isRealClassroom(row: ClassroomRow): boolean { |
| 23 | + return typeof row.classroomId === 'string' && !row.classroomId.includes('-quota#'); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Content richness score (0-4): 1 for having any page, +1 for multiple pages, |
| 28 | + * +1 for any page image, +1 for a starter project. Higher = more ready to |
| 29 | + * share as-is to みんなの課題. |
| 30 | + * @param row - classroom row |
| 31 | + * @returns {score, pageCount, hasImages, hasStarter} |
| 32 | + */ |
| 33 | +export function richness(row: ClassroomRow): { |
| 34 | + score: number; pageCount: number; hasImages: boolean; hasStarter: boolean; |
| 35 | +} { |
| 36 | + const pages = row.content?.pages || []; |
| 37 | + const pageCount = pages.length; |
| 38 | + const hasImages = pages.some(p => typeof p?.imageKey === 'string' && p.imageKey.length > 0); |
| 39 | + const hasStarter = typeof row.content?.starterKey === 'string' && row.content.starterKey.length > 0; |
| 40 | + let score = 0; |
| 41 | + if (pageCount >= 1) score += 1; |
| 42 | + if (pageCount >= 2) score += 1; |
| 43 | + if (hasImages) score += 1; |
| 44 | + if (hasStarter) score += 1; |
| 45 | + return { score, pageCount, hasImages, hasStarter }; |
| 46 | +} |
| 47 | + |
| 48 | +/** Split a Japanese/English title into rough keyword tokens for tallying. */ |
| 49 | +export function tokenizeTitle(title: string): string[] { |
| 50 | + return title |
| 51 | + .normalize('NFKC') |
| 52 | + .toLowerCase() |
| 53 | + // split on whitespace, punctuation and common separators (keep JP/EN/digits) |
| 54 | + .split(/[\s、。・,.\/|::;;\-—_()()「」『』[]\[\]{}!!??]+/u) |
| 55 | + .map(t => t.trim()) |
| 56 | + .filter(t => t.length >= 2 && !/^\d+$/.test(t)); |
| 57 | +} |
| 58 | + |
| 59 | +const MONTH_KEY = (iso: string): string => iso.slice(0, 7); // YYYY-MM |
| 60 | + |
| 61 | +/** |
| 62 | + * Build the whole overview payload from every classroom row. |
| 63 | + * @param rows - raw classroom items (quota rows are filtered out here) |
| 64 | + * @param sharedTitles - normalized assignment titles already on みんなの課題 |
| 65 | + * (to flag "likely already shared" candidates); pass [] if unknown |
| 66 | + * @param nowMs - current time (injectable for deterministic tests) |
| 67 | + * @returns the dashboard aggregation |
| 68 | + */ |
| 69 | +export function buildOverview( |
| 70 | + rows: ClassroomRow[], |
| 71 | + sharedTitles: string[], |
| 72 | + nowMs: number, |
| 73 | +): { |
| 74 | + summary: { total: number; active: number; archived: number; recent30d: number }; |
| 75 | + creationTrend: { month: string; count: number }[]; |
| 76 | + richnessDistribution: { score: number; count: number }[]; |
| 77 | + candidates: { |
| 78 | + classroomId: string; className: string; assignmentName: string; |
| 79 | + teacherSub: string; score: number; pageCount: number; |
| 80 | + hasImages: boolean; hasStarter: boolean; createdAt: string; likelyShared: boolean; |
| 81 | + }[]; |
| 82 | + themeKeywords: { keyword: string; count: number }[]; |
| 83 | +} { |
| 84 | + const classrooms = rows.filter(isRealClassroom); |
| 85 | + const sharedSet = new Set(sharedTitles.map(t => t.normalize('NFKC').trim().toLowerCase())); |
| 86 | + const cutoff30 = nowMs - 30 * 24 * 60 * 60 * 1000; |
| 87 | + |
| 88 | + let active = 0; |
| 89 | + let archived = 0; |
| 90 | + let recent30d = 0; |
| 91 | + const byMonth = new Map<string, number>(); |
| 92 | + const byScore = new Map<number, number>(); |
| 93 | + const keywordCount = new Map<string, number>(); |
| 94 | + const candidates: ReturnType<typeof buildOverview>['candidates'] = []; |
| 95 | + |
| 96 | + for (const row of classrooms) { |
| 97 | + const status = String(row.status || ''); |
| 98 | + if (status === 'archived') archived += 1; else active += 1; |
| 99 | + |
| 100 | + const createdAt = typeof row.createdAt === 'string' ? row.createdAt : ''; |
| 101 | + if (createdAt) { |
| 102 | + byMonth.set(MONTH_KEY(createdAt), (byMonth.get(MONTH_KEY(createdAt)) || 0) + 1); |
| 103 | + if (Date.parse(createdAt) >= cutoff30) recent30d += 1; |
| 104 | + } |
| 105 | + |
| 106 | + const r = richness(row); |
| 107 | + byScore.set(r.score, (byScore.get(r.score) || 0) + 1); |
| 108 | + |
| 109 | + const assignmentName = typeof row.assignmentName === 'string' ? row.assignmentName : ''; |
| 110 | + const className = typeof row.className === 'string' ? row.className : ''; |
| 111 | + for (const token of [...tokenizeTitle(assignmentName), ...tokenizeTitle(className)]) { |
| 112 | + keywordCount.set(token, (keywordCount.get(token) || 0) + 1); |
| 113 | + } |
| 114 | + |
| 115 | + // 有益候補: reasonably rich (>=3) content. Flag likely-already-shared so |
| 116 | + // the operator focuses on the unshared ones. |
| 117 | + if (r.score >= 3) { |
| 118 | + const likelyShared = sharedSet.has(assignmentName.normalize('NFKC').trim().toLowerCase()); |
| 119 | + candidates.push({ |
| 120 | + classroomId: String(row.classroomId), |
| 121 | + className, |
| 122 | + assignmentName, |
| 123 | + teacherSub: typeof row.teacherSub === 'string' ? row.teacherSub : '', |
| 124 | + score: r.score, |
| 125 | + pageCount: r.pageCount, |
| 126 | + hasImages: r.hasImages, |
| 127 | + hasStarter: r.hasStarter, |
| 128 | + createdAt, |
| 129 | + likelyShared, |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + const creationTrend = [...byMonth.entries()] |
| 135 | + .map(([month, count]) => ({ month, count })) |
| 136 | + .sort((a, b) => a.month.localeCompare(b.month)); |
| 137 | + |
| 138 | + const richnessDistribution = [0, 1, 2, 3, 4] |
| 139 | + .map(score => ({ score, count: byScore.get(score) || 0 })); |
| 140 | + |
| 141 | + // Unshared candidates first, then richer, then newer. |
| 142 | + candidates.sort((a, b) => |
| 143 | + Number(a.likelyShared) - Number(b.likelyShared) || |
| 144 | + b.score - a.score || |
| 145 | + b.createdAt.localeCompare(a.createdAt)); |
| 146 | + |
| 147 | + const themeKeywords = [...keywordCount.entries()] |
| 148 | + .map(([keyword, count]) => ({ keyword, count })) |
| 149 | + .filter(k => k.count >= 2) |
| 150 | + .sort((a, b) => b.count - a.count || a.keyword.localeCompare(b.keyword)) |
| 151 | + .slice(0, 30); |
| 152 | + |
| 153 | + return { |
| 154 | + summary: { total: classrooms.length, active, archived, recent30d }, |
| 155 | + creationTrend, |
| 156 | + richnessDistribution, |
| 157 | + candidates: candidates.slice(0, 50), |
| 158 | + themeKeywords, |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * Facet buckets for the restore tab (削除済みスナップショット). Groups a large |
| 164 | + * candidate list by deletion month and by teacher so the operator can narrow |
| 165 | + * down like the みんなの課題 catalog. |
| 166 | + * @param snapshots - matched snapshot summaries |
| 167 | + * @returns facet counts |
| 168 | + */ |
| 169 | +export function buildRestoreFacets( |
| 170 | + snapshots: { deletedAt?: string | null; teacherSub?: string }[], |
| 171 | +): { byMonth: { month: string; count: number }[]; byTeacher: { teacherSub: string; count: number }[] } { |
| 172 | + const byMonth = new Map<string, number>(); |
| 173 | + const byTeacher = new Map<string, number>(); |
| 174 | + for (const s of snapshots) { |
| 175 | + const month = s.deletedAt ? s.deletedAt.slice(0, 7) : '(不明)'; |
| 176 | + byMonth.set(month, (byMonth.get(month) || 0) + 1); |
| 177 | + const t = s.teacherSub || '(不明)'; |
| 178 | + byTeacher.set(t, (byTeacher.get(t) || 0) + 1); |
| 179 | + } |
| 180 | + return { |
| 181 | + byMonth: [...byMonth.entries()].map(([month, count]) => ({ month, count })) |
| 182 | + .sort((a, b) => b.month.localeCompare(a.month)), |
| 183 | + byTeacher: [...byTeacher.entries()].map(([teacherSub, count]) => ({ teacherSub, count })) |
| 184 | + .sort((a, b) => b.count - a.count), |
| 185 | + }; |
| 186 | +} |
0 commit comments