89 lines
2.5 KiB
TypeScript
89 lines
2.5 KiB
TypeScript
import type { NormalizedItem, ScoredItem } from "./types.js";
|
|
|
|
const DEFAULT_TERMS = [
|
|
"health", "technology", "agriculture", "primary production", "horticulture",
|
|
"infrastructure", "economy", "employment", "trade", "climate", "energy",
|
|
"transport", "earthquake", "food", "housing", "business", "environment"
|
|
];
|
|
|
|
function watchTerms() {
|
|
return (process.env.WATCH_TERMS?.split(",") ?? DEFAULT_TERMS)
|
|
.map((v) => v.trim().toLowerCase())
|
|
.filter(Boolean);
|
|
}
|
|
|
|
export function scoreItem(item: NormalizedItem): ScoredItem {
|
|
let score = 0;
|
|
const reasons: string[] = [];
|
|
const haystack = `${item.title} ${item.summary} ${item.topics.join(" ")}`.toLowerCase();
|
|
|
|
const matches = watchTerms().filter((term) => haystack.includes(term)).slice(0, 5);
|
|
if (matches.length) {
|
|
score += Math.min(3, matches.length);
|
|
reasons.push(`Matches watch terms: ${matches.join(", ")}`);
|
|
}
|
|
|
|
if (item.sourceId === "geonet") {
|
|
const magnitude = Number(item.metrics?.magnitude ?? 0);
|
|
const mmi = Number(item.metrics?.mmi ?? 0);
|
|
if (magnitude >= 5) {
|
|
score += 4;
|
|
reasons.push(`Magnitude ${magnitude} earthquake`);
|
|
} else if (magnitude >= 4) {
|
|
score += 2;
|
|
reasons.push(`Magnitude ${magnitude} earthquake`);
|
|
}
|
|
if (mmi >= 4) {
|
|
score += 2;
|
|
reasons.push(`Potentially notable shaking (MMI ${mmi})`);
|
|
}
|
|
}
|
|
|
|
if (item.sourceId === "stats-nz") {
|
|
score += 1;
|
|
reasons.push("New or changed official statistical release");
|
|
}
|
|
|
|
if (item.sourceId === "parliament") {
|
|
score += 1;
|
|
reasons.push("New or changed parliamentary evidence/submission");
|
|
}
|
|
|
|
if (item.sourceId === "data-govt-nz") {
|
|
score += 1;
|
|
reasons.push("New or updated public dataset metadata");
|
|
}
|
|
|
|
if (item.sourceId === "treasury") {
|
|
score += 1;
|
|
reasons.push("New or updated Treasury publication");
|
|
}
|
|
|
|
if (item.sourceId === "rbnz") {
|
|
score += 1;
|
|
reasons.push("New or updated Reserve Bank statistical release");
|
|
}
|
|
|
|
if (item.sourceId === "mbie-jobs-online") {
|
|
score += 1;
|
|
reasons.push("New or updated MBIE labour-demand data");
|
|
}
|
|
|
|
if (item.sourceId === "companies-office") {
|
|
score += 1;
|
|
reasons.push("New or updated Companies Office business statistics");
|
|
}
|
|
|
|
if (item.sourceId === "nz-gazette") {
|
|
score += 1;
|
|
reasons.push("New official Gazette notice");
|
|
}
|
|
|
|
if (item.sourceId === "electricity-authority") {
|
|
score += 1;
|
|
reasons.push("New or updated Electricity Authority supply snapshot");
|
|
}
|
|
|
|
return { score, reasons };
|
|
}
|