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

Function get_platform_usage_trends

api/app_analytics/influxdb_wrapper.py:478–527  ·  view source on GitHub ↗

Query InfluxDB for platform-wide usage trends grouped by day and resource. Returns a dict of {date_str: {resource_name: count}}.

(
    date_start: datetime,
    date_stop: datetime,
    organisation_ids: list[int],
)

Source from the content-addressed store, hash-verified

476
477
478def get_platform_usage_trends(
479 date_start: datetime,
480 date_stop: datetime,
481 organisation_ids: list[int],
482) -> dict[str, dict[str, int]]:
483 """
484 Query InfluxDB for platform-wide usage trends grouped by day and resource.
485
486 Returns a dict of {date_str: {resource_name: count}}.
487 """
488 if not organisation_ids:
489 return {}
490
491 org_id_set = ", ".join(f'"{oid}"' for oid in organisation_ids)
492
493 bucket = InfluxDBWrapper.select_downsampled_bucket(date_start)
494 results = InfluxDBWrapper.influx_query_manager(
495 date_start=date_start,
496 date_stop=date_stop,
497 bucket=bucket,
498 filters=build_filter_string(
499 [
500 'r._measurement == "api_call"',
501 'r["_field"] == "request_count"',
502 f"contains(value: r.organisation_id, set: [{org_id_set}])",
503 ]
504 ),
505 drop_columns=(
506 "organisation",
507 "organisation_id",
508 "project",
509 "project_id",
510 "environment",
511 "environment_id",
512 "host",
513 ),
514 extra=(
515 '|> group(columns: ["resource"])'
516 ' |> aggregateWindow(every: 24h, fn: sum, timeSrc: "_start")'
517 ),
518 )
519
520 daily: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int))
521 for table in results:
522 for record in table.records:
523 date_str = record.values["_time"].strftime("%Y-%m-%d")
524 resource_name = record.values.get("resource", "unknown")
525 daily[date_str][resource_name] += record.get_value() or 0
526
527 return daily
528
529
530def build_filter_string(filter_expressions: typing.List[str]) -> str:

Calls 5

build_filter_stringFunction · 0.85
influx_query_managerMethod · 0.80
getMethod · 0.45
get_valueMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…