Skip to content
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

feat(Price Stats): Stats grouped by #675

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

TTalex
Copy link
Collaborator

@TTalex TTalex commented Jan 11, 2025

What

Adds a new GET /api/v1/prices/grouped_stats endpoint, with mandatory param group_by matching an existing field.

For example, GET /api/v1/prices/grouped_stats?group_by=date&size=2 gives

{
  "items": [
    {
      "date": "2018-03-27",
      "price__count": 24,
      "price__min": 0.47,
      "price__max": 7.8,
      "price__avg": 2.55,
      "price__sum": 61.2
    },
    {
      "date": "2018-03-31",
      "price__count": 13,
      "price__min": 0.54,
      "price__max": 6.49,
      "price__avg": 3.19,
      "price__sum": 41.49
    }
  ],
  "page": 1,
  "pages": 55,
  "size": 2,
  "total": 110
}

Specific time fields are also supported, even though they are not part of the model. Users can group by month, week and year.

Results should be properly paginated.

By default, results are ordered following the group_by parameter (here by date), using the order_by params helps sorting the items with one of the computed param.

For example, GET /api/v1/prices/grouped_stats?group_by=date&size=2&order_by=-price__count gives

{
  "items": [
    {
      "date": "2021-01-11",
      "price__count": 37,
      "price__min": 0.4,
      "price__max": 5.99,
      "price__avg": 2.05,
      "price__sum": 75.8
    },
    {
      "date": "2018-08-09",
      "price__count": 37,
      "price__min": 0.59,
      "price__max": 12.15,
      "price__avg": 2.67,
      "price__sum": 98.71
    }
  ],
  "page": 1,
  "pages": 55,
  "size": 2,
  "total": 110
}

Fixes bug(s)

Notes

  • This is a pretty hacky implementation of a solution, I'm not really confident about it.
  • For example, I had to use OpenApiParameter for it to show up in the docs, which is weird.
  • I am also unsure about the performances of these groupings on a production database.

@TTalex
Copy link
Collaborator Author

TTalex commented Jan 11, 2025

Moving this to draft while we think of a better solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant