fix(news): Conflicts reports of the same time come in a fixed order

GDELT exports are read in parallel, so two reports with the same time
arrived in whichever order their exports finished, and the feed test that
pages through them failed now and then. Same-time reports now order by
most mentions, then by id.
This commit is contained in:
Sameh Khamis committed 2026-11-10 22:40:15 -08:00
1 parent 9bca228340
commit bc786f8c55
1 file changed
+8 -1
+8 -1
View File
@@ -642,9 +642,16 @@ export function createKeyedNews({
await firstAnswer(inFlight.get('gdelt'));
if (!current()) return { failed: true };
const dayAgo = new Date(now() - DAY_MS).toISOString();
// Exports are read in parallel, so reports of one time arrive in any
// order: the most mentioned comes first, then by id.
const reports = [...gdelt.reports.values()]
.filter((r) => r.publishedAt >= dayAgo)
.sort((a, b) => b.publishedAt.localeCompare(a.publishedAt))
.sort(
(a, b) =>
b.publishedAt.localeCompare(a.publishedAt) ||
b.mentions - a.mentions ||
a.id.localeCompare(b.id),
)
.slice(0, MAX_GDELT_REPORTS);
if (reports.length !== gdelt.reports.size) {
const kept = new Set(reports);