diff --git a/intelligence/signalWorker.js b/intelligence/signalWorker.js index c0ef82b..c6d23ef 100644 --- a/intelligence/signalWorker.js +++ b/intelligence/signalWorker.js @@ -61,23 +61,32 @@ async function runSignalWorker(archiveDb, intelligenceDb, config) { try { const companies = getCompanies.all(); - // pick the company with the oldest (or missing) signal + // pick the company with the oldest (or missing) signal that has enough predictions let target = null; let oldestTs = null; + const companiesByAge = []; for (const company of companies) { const row = getLastSignal.get(company.id); + companiesByAge.push({ company, ts: row ? row.generated_at : null }); + } - if (!row) { - // never generated — highest priority - target = company; + // null ts (never generated) first, then oldest + companiesByAge.sort((a, b) => { + if (a.ts === null && b.ts === null) return 0; + if (a.ts === null) return -1; + if (b.ts === null) return 1; + return a.ts < b.ts ? -1 : 1; + }); + + let predictions = null; + for (const entry of companiesByAge) { + const preds = getPredictions.all(entry.company.id); + if (preds.length >= 3) { + target = entry.company; + predictions = preds; break; } - - if (oldestTs === null || row.generated_at < oldestTs) { - oldestTs = row.generated_at; - target = company; - } } if (!target) { @@ -85,14 +94,6 @@ async function runSignalWorker(archiveDb, intelligenceDb, config) { continue; } - - const predictions = getPredictions.all(target.id); - - if (predictions.length < 3) { - await sleep(loopDelay); - continue; - } - const facts = getFacts.all(target.id); const relationships = getRelationships.all(target.id);