Skip to content

Ensure project event collections have indexes before grouper writes - #604

Open
Kuchizu wants to merge 1 commit into
masterfrom
feat/grouper-ensure-event-indexes
Open

Kuchizu wants to merge 1 commit into
masterfrom
feat/grouper-ensure-event-indexes

Conversation

@Kuchizu

@Kuchizu Kuchizu commented Nov 10, 2026

Copy link
Copy Markdown
Member

Grouper now checks a project's events, repetitions and dailyEvents collections before handling an event, once per hour per project. A missing collection is created with the same indexes the API sets up for a new project. For an existing collection the indexes are only compared with the expected list and the missing ones are logged, nothing is built on live data. A failed check is logged and does not stop event processing.

This is the first step towards creating event collections on the first event instead of at project creation. About 76% of projects have never received an event, and their empty collections make up more than half of all tables in the database. The API still creates collections as before, so behaviour does not change yet: the logs will show whether the grouper's index list matches production.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The audit can miss index-definition drift that breaks hinted queries, while its cache retains expired successful checks and retries persistent failures on every event, causing memory growth and excess database load.

3 open issues
What changed in this PR

This PR adds grouper-side index checks ahead of event writes, preparing for collection creation on first ingestion while leaving API-side creation unchanged.

Changes:

  • Defines expected indexes and creates missing collections; existing collections are audited without building indexes.
  • Adds hourly per-project caching, failure logging, and timing instrumentation.
  • Adds integration tests for collection creation, uniqueness enforcement, and missing-index reporting.
File Description
workers/​grouper/​tests/​eventIndexes.test.ts Tests index creation and audit-only behavior.
workers/​grouper/​src/​utils/​eventIndexes.ts Defines expected indexes and implements collection checks.
workers/​grouper/​src/​metrics/​grouperMetrics.ts Adds the index-check timing step.
workers/​grouper/​src/​index.ts Runs cached checks before event processing and logs results.

Review effort: Balanced

Comment on lines +541 to +544
.catch((error) => {
this.eventIndexesChecks.delete(projectId);
this.logger.error(`[ensureEventIndexes] project=${projectId} failed: ${error.message}`);
});
Comment on lines +546 to +549
this.eventIndexesChecks.set(projectId, {
expiresAt: Date.now() + EVENT_INDEXES_CHECK_TTL,
done,
});

for (const spec of specs) {
const found = existing.some((index) => {
return JSON.stringify(index.key) === JSON.stringify(spec.key) && !!index.unique === !!spec.unique;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants