schema.py 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from typing import Annotated
  2. from ninja import Field, ModelSchema
  3. from pydantic.functional_validators import BeforeValidator
  4. from glitchtip.schema import CamelSchema
  5. from .models import TransactionEvent, TransactionGroup
  6. def coerce_int(v) -> int:
  7. if isinstance(v, float):
  8. return int(v)
  9. return v
  10. FlexInt = Annotated[int, BeforeValidator(coerce_int)]
  11. class TransactionEventSchema(CamelSchema, ModelSchema):
  12. class Meta:
  13. model = TransactionEvent
  14. fields = (
  15. "event_id",
  16. "timestamp",
  17. "start_timestamp",
  18. # "transaction",
  19. # "op",
  20. # "method",
  21. )
  22. class TransactionGroupSchema(CamelSchema, ModelSchema):
  23. avg_duration: FlexInt | None
  24. transaction_count: int
  25. project: int = Field(validation_alias="project_id")
  26. class Meta:
  27. model = TransactionGroup
  28. fields = [
  29. "id",
  30. "transaction",
  31. "op",
  32. "method",
  33. ]