Repository navigation
feat(sync): record each vector's last chart day #1164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Last-charted dates on Qdrant points (own-episode-IDs P3b, plan section 10b item 4). | ||
| * | ||
| * Every point in both collections carries `charted_at`: the UTC day (epoch seconds) its show was | ||
| * last seen on a chart. The show's catalog row and state record go 7 days after it leaves the | ||
| * charts, so the date has to live on the points for the 90-day vector retention to read it. | ||
| * Nothing here deletes: the cleanup keeps its 7-day rule until the 90-day rule is switched on. | ||
| * | ||
| * Stamps come from the cleanup's chart clock (state `shows[id].s`, set daily by stage 6): | ||
| * - a show's points get the day of its last chart sighting; | ||
| * - a point is rewritten only when it has no date or its date is more than REFRESH_DAYS older | ||
| * than the show's, so dates only move forward and a charting show's points are rewritten | ||
| * about once a week, not daily; | ||
| * - new points get the day they were embedded (stage 4 and 5), since those shows chart. | ||
| */ | ||
|
|
||
| const cfg = require('./config'); | ||
|
|
||
| const FIELD = 'charted_at'; | ||
| /** Both collections: episodes name their show in podcast_id, show points in id. */ | ||
| const COLLECTIONS = [ | ||
| { name: cfg.EPISODES_COLLECTION, showKey: 'podcast_id' }, | ||
| { name: cfg.PODCASTS_COLLECTION, showKey: 'id' }, | ||
| ]; | ||
| const DAY_S = 86_400; | ||
| /** Shows per Qdrant filter call. */ | ||
| const CHUNK = 500; | ||
|
|
||
| /** UTC day start in epoch seconds for a time in ms. */ | ||
| function dayStartSec(ms) { | ||
| return Math.floor(Number(ms) / 1000 / DAY_S) * DAY_S; | ||
| } | ||
|
|
||
| /** | ||
| * Groups shows by stamp day into filter-sized batches. | ||
| * @param {{ id: string|number }[]} pods | ||
| * @param {object} shows state.shows (rec.s = last chart sighting, ms) | ||
| * @returns {{ value: number, ids: number[] }[]} newest day first; shows without a sighting are skipped | ||
| */ | ||
| function planBatches(pods, shows, chunk = CHUNK) { | ||
| const byDay = new Map(); | ||
| for (const pod of pods || []) { | ||
| const id = Number(pod.id); | ||
| const seen = shows?.[String(pod.id)]?.s; | ||
| if (!Number.isSafeInteger(id) || id <= 0 || !(seen > 0)) continue; | ||
| const day = dayStartSec(seen); | ||
| if (!byDay.has(day)) byDay.set(day, []); | ||
| byDay.get(day).push(id); | ||
| } | ||
| const out = []; | ||
| for (const day of [...byDay.keys()].sort((a, b) => b - a)) { | ||
| const ids = byDay.get(day).sort((a, b) => a - b); | ||
| for (let i = 0; i < ids.length; i += chunk) out.push({ value: day, ids: ids.slice(i, i + chunk) }); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * Points of these shows whose date is missing or more than refreshSec older than `value`. | ||
| * @param {'podcast_id'|'id'} showKey episodes use podcast_id, the podcasts collection id | ||
| */ | ||
| function staleFilter(showKey, ids, value, refreshSec) { | ||
| return { | ||
| must: [{ key: showKey, match: { any: ids } }], | ||
| should: [ | ||
| { is_empty: { key: FIELD } }, | ||
| { key: FIELD, range: { lt: value - refreshSec } }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Stamps both collections. Never throws: a failed batch is counted and the rest still run. | ||
| * @param {{ setPayloadByFilter: Function, countPoints: Function }} qdrant | ||
| * @param {{ value: number, ids: number[] }[]} batches | ||
| * @param {{ collections: { name: string, showKey: string }[], refreshSec: number, dry?: boolean, | ||
| * warn?: (msg: string) => void }} opts | ||
| * @returns {Promise<{ batches: number, failed: number, points: Record<string, number>, dry: boolean }>} | ||
| * points: per collection, points that needed a date (counted before writing) | ||
| */ | ||
| async function stamp(qdrant, batches, { collections, refreshSec, dry = false, warn = () => {} }) { | ||
| const out = { batches: batches.length, failed: 0, points: {}, dry }; | ||
| for (const c of collections) out.points[c.name] = 0; | ||
| for (const b of batches) { | ||
| for (const c of collections) { | ||
| const filter = staleFilter(c.showKey, b.ids, b.value, refreshSec); | ||
| try { | ||
| const n = await qdrant.countPoints(c.name, filter); | ||
| out.points[c.name] += n; | ||
| if (!dry && n > 0) await qdrant.setPayloadByFilter(c.name, { [FIELD]: b.value }, filter, true); | ||
| } catch (e) { | ||
| out.failed++; | ||
| warn(`[CHARTED_AT] ${c.name}: ${b.ids.length} shows at ${new Date(b.value * 1000).toISOString().slice(0, 10)} not stamped (${e.message})`); | ||
| } | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /** One line for the run report. */ | ||
| function summary(r) { | ||
| const pts = Object.entries(r.points).map(([name, n]) => `${name} ${n}`).join(', '); | ||
| return `charted_at ${r.dry ? 'dry run' : 'stamped'}: ${pts} points in ${r.batches} batches` + | ||
| (r.failed ? `, ${r.failed} failed calls` : ''); | ||
| } | ||
|
|
||
| module.exports = { FIELD, COLLECTIONS, CHUNK, DAY_S, dayStartSec, planBatches, staleFilter, stamp, summary }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.