schema.py 829 B

123456789101112131415161718192021222324252627
  1. from datetime import datetime
  2. from typing import Literal
  3. from ninja import Schema
  4. from ninja.errors import ValidationError
  5. from pydantic import Field, model_validator
  6. class StatsV2Schema(Schema):
  7. category: Literal["error", "transaction"]
  8. interval: Literal["1d", "1h", "1m"] | None = "1h"
  9. project: list[int] | None = Field(default=None, ge=-1)
  10. field: Literal["sum(quantity)", "sum(times_seen)"]
  11. start: datetime
  12. end: datetime
  13. @model_validator(mode="after")
  14. def validate(self):
  15. series_quantity = (self.end - self.start).days
  16. if self.interval == "1h":
  17. series_quantity *= 24
  18. elif self.interval == "1m":
  19. series_quantity *= 1440
  20. if series_quantity > 1000:
  21. raise ValidationError([{"end": "Too many intervals"}])
  22. return self