(
organisations: QuerySet[Organisation],
date_start: object,
)
| 430 | |
| 431 | |
| 432 | def _get_usage_trends_postgres( |
| 433 | organisations: QuerySet[Organisation], |
| 434 | date_start: object, |
| 435 | ) -> list[UsageTrendData]: |
| 436 | env_ids = list( |
| 437 | Environment.objects.filter( |
| 438 | project__organisation__in=organisations, |
| 439 | ).values_list("id", flat=True) |
| 440 | ) |
| 441 | |
| 442 | qs = ( |
| 443 | APIUsageBucket.objects.filter( |
| 444 | environment_id__in=env_ids, |
| 445 | bucket_size=analytics_constants.ANALYTICS_READ_BUCKET_SIZE, |
| 446 | created_at__date__gt=date_start, |
| 447 | ) |
| 448 | .values("created_at__date", "resource") |
| 449 | .annotate(total=Sum("total_count")) |
| 450 | .order_by("created_at__date") |
| 451 | ) |
| 452 | |
| 453 | daily: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int)) |
| 454 | for row in qs: |
| 455 | date_str = row["created_at__date"].isoformat() |
| 456 | resource = Resource(row["resource"]) |
| 457 | daily[date_str][resource.resource_name] = row["total"] |
| 458 | |
| 459 | results: list[UsageTrendData] = [] |
| 460 | for date_str in sorted(daily.keys()): |
| 461 | data = daily[date_str] |
| 462 | results.append( |
| 463 | UsageTrendData( |
| 464 | date=date_str, |
| 465 | api_calls=sum(data.values()), |
| 466 | flag_evaluations=data.get("flags", 0), |
| 467 | identity_requests=data.get("identities", 0), |
| 468 | ) |
| 469 | ) |
| 470 | |
| 471 | return results |
| 472 | |
| 473 | |
| 474 | def _get_usage_trends_influx( |
no test coverage detected
searching dependent graphs…