serializers.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from rest_framework import serializers
  2. class StatsV2Serializer(serializers.Serializer):
  3. category = serializers.ChoiceField(
  4. choices=(("error", "error"), ("transaction", "transaction"))
  5. )
  6. interval = serializers.ChoiceField(
  7. choices=(("1d", "1 day"), ("1h", "1 hour"), ("1m", "1 minute")),
  8. default="1h",
  9. required=False,
  10. )
  11. project = serializers.ListField(
  12. child=serializers.IntegerField(min_value=-1), required=False
  13. )
  14. field = serializers.ChoiceField(
  15. choices=(
  16. ("sum(quantity)", "sum(quantity)"),
  17. ("sum(times_seen", "sum(times_seen"),
  18. ),
  19. )
  20. start = serializers.DateTimeField()
  21. end = serializers.DateTimeField()
  22. def validate(self, data):
  23. start = data.get("start")
  24. end = data.get("end")
  25. interval = data.get("interval")
  26. series_quantity = (end - start).days
  27. if interval == "1h":
  28. series_quantity *= 24
  29. elif interval == "1m":
  30. series_quantity *= 1440
  31. if series_quantity > 1000:
  32. raise serializers.ValidationError({"end": "Too many intervals"})
  33. return data