views.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from datetime import timedelta
  2. from django.db import connection
  3. from rest_framework import views
  4. from rest_framework.response import Response
  5. from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND
  6. from apps.organizations_ext.permissions import OrganizationPermission
  7. from apps.projects.models import Project
  8. from .serializers import StatsV2Serializer
  9. EVENT_TIME_SERIES_SQL = """
  10. SELECT gs.ts, sum(event_stat.count)
  11. FROM generate_series(%s, %s, %s::interval) gs (ts)
  12. LEFT JOIN projects_issueeventprojecthourlystatistic event_stat
  13. ON event_stat.date >= gs.ts AND event_stat.date < gs.ts + interval '1 hour'
  14. WHERE event_stat.project_id = ANY(%s) or event_stat is null
  15. GROUP BY gs.ts ORDER BY gs.ts;
  16. """
  17. TRANSACTION_TIME_SERIES_SQL = """
  18. SELECT gs.ts, sum(transaction_stat.count)
  19. FROM generate_series(%s, %s, %s::interval) gs (ts)
  20. LEFT JOIN projects_transactioneventprojecthourlystatistic transaction_stat
  21. ON transaction_stat.date >= gs.ts AND transaction_stat.date < gs.ts + interval '1 hour'
  22. WHERE transaction_stat.project_id = ANY(%s) or transaction_stat is null
  23. GROUP BY gs.ts ORDER BY gs.ts;
  24. """
  25. class StatsV2View(views.APIView):
  26. """
  27. Reverse engineered stats v2 endpoint. Endpoint in sentry not documented.
  28. Appears similar to documented sessions endpoint.
  29. Used by the Sentry Grafana integration.
  30. Used to return time series statistics.
  31. Submit query params start, end, and interval (defaults to 1h)
  32. Limits results to 1000 intervals. For example if using hours, max days would be 41
  33. """
  34. permission_classes = [OrganizationPermission]
  35. def get(self, *args, **kwargs):
  36. query_params = self.request.query_params
  37. data = {
  38. "category": query_params.get("category"),
  39. "project": query_params.getlist("project"),
  40. "field": query_params.get("field"),
  41. "start": query_params.get("start"),
  42. "end": query_params.get("end"),
  43. }
  44. if query_params.get("interval"):
  45. data["interval"] = query_params.get("interval")
  46. serializer = StatsV2Serializer(data=data)
  47. serializer.is_valid(raise_exception=True)
  48. category = serializer.validated_data["category"]
  49. start = serializer.validated_data["start"].replace(
  50. microsecond=0, second=0, minute=0
  51. )
  52. end = (serializer.validated_data["end"] + timedelta(hours=1)).replace(
  53. microsecond=0, second=0, minute=0
  54. )
  55. field = serializer.validated_data["field"]
  56. interval = serializer.validated_data["interval"]
  57. # Get projects that are authorized, filtered by organization, and selected by user
  58. # Intentionally separate SQL call to simplify raw SQL
  59. projects = Project.objects.filter(
  60. organization__slug=self.kwargs.get("organization_slug"),
  61. organization__users=self.request.user,
  62. )
  63. if serializer.validated_data.get("project"):
  64. projects = projects.filter(pk__in=serializer.validated_data["project"])
  65. project_ids = list(projects.values_list("id", flat=True))
  66. if not project_ids:
  67. return Response(status=HTTP_404_NOT_FOUND)
  68. if category == "error":
  69. with connection.cursor() as cursor:
  70. cursor.execute(
  71. EVENT_TIME_SERIES_SQL,
  72. [start, end, interval, project_ids],
  73. )
  74. series = cursor.fetchall()
  75. elif category == "transaction":
  76. with connection.cursor() as cursor:
  77. cursor.execute(
  78. TRANSACTION_TIME_SERIES_SQL,
  79. [start, end, interval, project_ids],
  80. )
  81. series = cursor.fetchall()
  82. else:
  83. return Response(status=HTTP_400_BAD_REQUEST)
  84. data = {
  85. "intervals": [row[0] for row in series],
  86. "groups": [
  87. {
  88. "series": {field: [row[1] for row in series]},
  89. }
  90. ],
  91. }
  92. return Response(data)