Skip to content

add latest aggregate forecast json to question object#4908

Open
lsabor wants to merge 2 commits into
mainfrom
issue/4907/latest-agg-json-on-question
Open

add latest aggregate forecast json to question object#4908
lsabor wants to merge 2 commits into
mainfrom
issue/4907/latest-agg-json-on-question

Conversation

@lsabor

@lsabor lsabor commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

addresses the main push of #4907

Summary by CodeRabbit

  • New Features
    • Added a new field on questions to store the latest aggregate forecast snapshot.
  • Changes
    • Backfilled the field for currently open questions with their most recent aggregate forecast.
    • Updated forecast building to keep the field in sync with the latest available aggregate forecast prior to the current time.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ebfc2dcd-71de-498b-b82b-7243d53d4503

📥 Commits

Reviewing files that changed from the base of the PR and between 80bc458 and 48080be.

📒 Files selected for processing (1)
  • questions/services/forecasts.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • questions/services/forecasts.py

📝 Walkthrough

Walkthrough

Adds a latest_aggregate_forecast nullable JSONField to the Question model. A data migration backfills the field for currently open questions by serializing the most recent AggregateForecast row. The build_question_forecasts service function is updated to maintain this field at runtime using bisect_left on aggregation_history.

Changes

Latest Aggregate Forecast Cache

Layer / File(s) Summary
Model field definition and data migration
questions/models.py, questions/migrations/0038_question_latest_aggregate_forecast.py
Adds latest_aggregate_forecast as a nullable/blank JSONField on Question. The migration adds the column and backfills it for open questions (no actual_close_time, open_time__lte=now) by fetching the latest aggregate_forecasts row by start_time and serializing it with serialize_aggregate_forecast(..., full=True). Reverse operation is a no-op.
Runtime update in build_question_forecasts
questions/services/forecasts.py
Imports bisect_left and serialize_aggregate_forecast. After bulk-creating/updating AggregateForecast rows, uses bisect_left on start_time in aggregation_history to find the most recent aggregate before timezone.now() and assigns the full serialized form to question.latest_aggregate_forecast.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 A field so fresh, it gleams with JSON light,
The latest forecast cached, no need to search tonight.
Migration hops along, backfilling every row,
bisect_left finds the time—the rabbit's on the go!
The question knows its forecast, stored neat and tight. ✨

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a new JSONField for the latest aggregate forecast to the Question model, which is the primary objective of the pull request.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue/4907/latest-agg-json-on-question

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@questions/migrations/0038_question_latest_aggregate_forecast.py`:
- Line 6: Remove the import of serialize_aggregate_forecast from
questions.serializers.aggregate_forecasts at the top of the migration file.
Instead, copy the serialization logic directly into the migration file or
implement the serialization logic inline without relying on external application
code. Update any calls to serialize_aggregate_forecast within the migration to
use the local implementation instead. This ensures the migration is
self-contained and will not break if the external serializer changes in the
future.

In `@questions/services/forecasts.py`:
- Around line 584-591: The code only updates question.latest_aggregate_forecast
when latest_index > 0, but when no eligible aggregate exists (latest_index
equals 0 or less), the previous cached value is never cleared and remains stale.
Add an else clause after the if latest_index > 0 condition that sets
question.latest_aggregate_forecast to None to explicitly clear the cache when no
prior aggregate exists.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 66a72692-6abb-4827-899a-f040ee93e51f

📥 Commits

Reviewing files that changed from the base of the PR and between 23ee8aa and 80bc458.

📒 Files selected for processing (3)
  • questions/migrations/0038_question_latest_aggregate_forecast.py
  • questions/models.py
  • questions/services/forecasts.py

from django.db import migrations, models
from django.utils import timezone

from questions.serializers.aggregate_forecasts import serialize_aggregate_forecast

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Avoid runtime serializer imports inside migrations.

On Line 6, importing questions.serializers.aggregate_forecasts.serialize_aggregate_forecast makes this migration depend on mutable application code. If that serializer changes later, migration replay can fail on clean installs. Keep serialization logic snapshot-local inside this migration (or serialize from historical model fields directly).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@questions/migrations/0038_question_latest_aggregate_forecast.py` at line 6,
Remove the import of serialize_aggregate_forecast from
questions.serializers.aggregate_forecasts at the top of the migration file.
Instead, copy the serialization logic directly into the migration file or
implement the serialization logic inline without relying on external application
code. Update any calls to serialize_aggregate_forecast within the migration to
use the local implementation instead. This ensures the migration is
self-contained and will not break if the external serializer changes in the
future.

Comment thread questions/services/forecasts.py
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Preview Environment

Your preview environment is ready!

Resource Details
🌐 Preview URL https://metaculus-pr-4908-issue-4907-latest-agg-json-on-preview.mtcl.cc
📦 Docker Image ghcr.io/metaculus/metaculus:issue-4907-latest-agg-json-on-question-48080be
🗄️ PostgreSQL NeonDB branch preview/pr-4908-issue-4907-latest-agg-json-on
Redis Fly Redis mtc-redis-pr-4908-issue-4907-latest-agg-json-on

Details

  • Commit: 9aa83a002a949a84f51e34f1e3161bfc5c762638
  • Branch: issue/4907/latest-agg-json-on-question
  • Fly App: metaculus-pr-4908-issue-4907-latest-agg-json-on

ℹ️ Preview Environment Info

Isolation:

  • PostgreSQL and Redis are fully isolated from production
  • Each PR gets its own database branch and Redis instance
  • Changes pushed to this PR will trigger a new deployment

Limitations:

  • Background workers and cron jobs are not deployed in preview environments
  • If you need to test background jobs, use Heroku staging environments

Cleanup:

  • This preview will be automatically destroyed when the PR is closed

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.

1 participant