Skip to content

feat(Prices): API: stats: new country filter, distinct proof count & last 30 days summary. ref #854 - #1464

Open
rishiraj38 wants to merge 1 commit into
openfoodfacts:mainfrom
rishiraj38:fix/854-price-stats-country-filter
Open

rishiraj38 wants to merge 1 commit into
openfoodfacts:mainfrom
rishiraj38:fix/854-price-stats-country-filter

Conversation

@rishiraj38

@rishiraj38 rishiraj38 commented Oct 2, 2026 •

Copy link
Copy Markdown

Builds on the existing GET /api/v1/prices/stats endpoint, following the outline in the issue:

  • PriceFilter: new location__osm_address_country_code filter (exact match, same naming as the existing price tag filter). Without it, all countries are included. Also exposes price_per, so callers can keep price units apart the same way they already do with currency.
  • PriceQuerySet.calculate_stats(): adds proof__count (distinct proof_id) to the existing aggregate, so the filtered stats get their own proof count in the same query. Product.price__stats() gains the same key.
  • Response: the existing top-level keys are unchanged (all-time). A nested last_30_days object repeats the same fields for prices whose date is within the last 30 days.

Example: GET /api/v1/prices/stats?location__osm_address_country_code=FR

{
  "price__count": 3,
  "price__min": "10.00",
  "price__max": "30.00",
  "price__avg": "20.00",
  "proof__count": 2,
  "last_30_days": {
    "price__count": 2,
    "price__min": "10.00",
    "price__max": "30.00",
    "price__avg": "20.00",
    "proof__count": 2
  }
}

The OpenAPI schema picks up the new filters and the nested serializer automatically. No migration.

Fixes #854

Tests: PriceStatsApiTest now covers the country filter, the distinct proof count, the 30-day window boundary (day 30 in, day 31 out), combining with other filters, and the empty result. PriceQuerySetTest and ProductPropertyTest expectations updated for the new key. Full suite run locally: the new country test fails on main and passes here. Four challenge tests fail on main too (their fixtures use fixed 2024/2025 dates) and are untouched. ruff clean.

Summary by CodeRabbit

  • New Features
    • Price statistics now include a separate summary for the last 30 days, alongside overall totals for price count, minimum, maximum, and average.
    • Statistics now include the number of distinct proofs associated with matching prices.
    • Price searches can be filtered by location country code and exact price.
    • Country and price filters can be combined; statistics reflect the active filters.

@rishiraj38
rishiraj38 force-pushed the fix/854-price-stats-country-filter branch from 8ed4dfe to c005f86 Compare November 11, 2026 06:01
@coderabbitai

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The price API adds exact country and price-per filters. Its statistics include distinct proof counts and overall and last-30-days aggregates.

Changes: Price statistics API

Layer / File(s) Summary
Filters and proof aggregates
open_prices/api/prices/filters.py, open_prices/prices/models.py
Price filters add exact country-code and price-per lookups. Statistics add a distinct count of associated proofs.
Statistics response
open_prices/api/prices/serializers.py, open_prices/api/prices/views.py
The stats response includes overall statistics and statistics for prices dated within the last 30 days.
Statistics and filter tests
open_prices/api/prices/tests.py, open_prices/prices/tests.py, open_prices/products/tests.py
Tests cover proof counts, country-filtered results, overall and recent-period aggregates, and empty results.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant APIClient
  participant PriceViewSet
  participant PriceQuerySet
  participant PriceStatsSerializer
  APIClient->>PriceViewSet: Request price statistics
  PriceViewSet->>PriceQuerySet: Calculate overall and last-30-days statistics
  PriceQuerySet-->>PriceViewSet: Return aggregate values
  PriceViewSet->>PriceStatsSerializer: Serialize overall and nested period values
  PriceStatsSerializer-->>APIClient: Return statistics response
Loading

Possibly related PRs

Suggested labels: Prices, Price per, 📍 Location

Suggested reviewers: raphodn, ttalex

Merge Risk: Minimal · up to c005f

The country-filtered and recent price statistics are ready for normal merge checks; no concrete merge-blocking issue remains.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly summarizes the main changes: the country filter, distinct proof count, and last-30-days statistics.
Linked Issues check Passed Issue #854 requires product price statistics with average, minimum, maximum, proof count, a country filter, and global and recent views. PriceFilter exposes exact `location__osm_address_country_code…
Out of Scope Changes check Passed The changes support issue #854. The price_per filter keeps statistics for different price units separate. The serializer, model aggregate, view logic, and test updates implement or verify the reques…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

Issue #854 requires product price statistics with average, minimum, maximum, proof count, a country filter, and global and recent views. PriceFilter exposes exact location__osm_address_country_code filtering. PriceViewSet.stats calculates filtered all-time statistics and a last_30_days result. PriceQuerySet.calculate_stats() returns average, minimum, maximum, and distinct proof__count. The supplied test summary covers country filtering, proof counts, the 30-day boundary, combined filters, and empty results.

Full details: Out of Scope Changes check

Explanation

The changes support issue #854. The price_per filter keeps statistics for different price units separate. The serializer, model aggregate, view logic, and test updates implement or verify the requested statistics endpoint. No unrelated change is demonstrated.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 7 files.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 4d413b9 and c005f86.

Files selected for processing: 7

  • open_prices/api/prices/filters.py
  • open_prices/api/prices/serializers.py
  • open_prices/api/prices/tests.py
  • open_prices/api/prices/views.py
  • open_prices/prices/models.py
  • open_prices/prices/tests.py
  • open_prices/products/tests.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

API endpoint: Product statistics filterable by country

1 participant