in backend/code_review_backend/issues/api.py [0:0]
def get_queryset(self):
# Count all the issues per day
queryset = (
Issue.objects.annotate(date=TruncDate("created"))
.values("date")
.annotate(total=Count("id"))
)
# Filter by repository
repository = self.request.query_params.get("repository")
if repository:
queryset = queryset.filter(
Q(diffs__revision__base_repository__slug=repository)
| Q(diffs__revision__head_repository__slug=repository)
)
# Filter by analyzer
analyzer = self.request.query_params.get("analyzer")
if analyzer:
queryset = queryset.filter(analyzer=analyzer)
# Filter by check
check = self.request.query_params.get("check")
if check:
queryset = queryset.filter(analyzer_check=check)
# Filter by date
since = self.request.query_params.get("since")
if since is not None:
try:
since = datetime.strptime(since, "%Y-%m-%d")
except ValueError:
raise APIException(detail="invalid since date - should be YYYY-MM-DD")
if "postgresql" in settings.DATABASES["default"]["ENGINE"]:
# Use a specific WHERE clause to compare the creation date.
# Casting the date as its natural data type (timestamptz) allow Postgres to perform an
# index scan on small data ranges (depending on available memory), which is much faster.
# Overall performance is improved in practice, even if the planned cost is higher when
# aggregating on the whole table.
since_date = since.strftime("%Y-%m-%d")
queryset = queryset.extra(
where=[f"created::timestamptz >= '{since_date}'::timestamptz"]
)
else:
queryset = queryset.filter(date__gte=since.date())
return queryset.order_by("date").distinct()