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],
)
| 476 | |
| 477 | |
| 478 | def 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 | |
| 530 | def build_filter_string(filter_expressions: typing.List[str]) -> str: |
searching dependent graphs…