MCPcopy Create free account
hub / github.com/Flagsmith/flagsmith / _get_usage_trends_postgres

Function _get_usage_trends_postgres

api/platform_hub/services.py:432–471  ·  view source on GitHub ↗
(
    organisations: QuerySet[Organisation],
    date_start: object,
)

Source from the content-addressed store, hash-verified

430
431
432def _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
474def _get_usage_trends_influx(

Callers 1

get_usage_trendsFunction · 0.85

Calls 5

ResourceClass · 0.90
UsageTrendDataClass · 0.90
sortedFunction · 0.85
appendMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…