Problem/Motivation

The ai_metering_usage table grows without bound - one row per AI call, forever. Nothing ever deletes old rows. The retention_days setting only bounds the dashboard's display window (AiMeteringController); it does not prune storage. On a busy site the table reaches millions of rows, and since all reporting (CostReporter, QuotaManager) aggregates live over the raw log with GROUP BY, dashboards slow down as the table grows and PII (per-user call history) is retained indefinitely.

Proposed Resolution

  • Add a hook_cron purge that deletes ai_metering_usage rows older than a configurable retention period (new setting, e.g. usage_retention_days, with 0/empty meaning "keep forever"). Batch the delete so cron does not time out on large tables.
  • Before purging, roll raw rows up into a persisted monthly aggregate table (per user, provider, model: token sums and cost) so historical dashboards and totals survive the purge instead of dropping to zero. Point the reporting services at the rollup for past months and at the raw log for the current window.
  • If a lightweight aggregate already fits better as a Views-backed summary, reuse that rather than a new table - the key requirement is that purging raw rows must not erase historical totals.
  • Cover with a kernel test: insert dated rows, run the rollup + purge, assert old rows are gone and aggregated totals are preserved.

AI Assistance

Yes, AI was used to explore the issue.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

marcus_johansson created an issue. See original summary.

codeitwisely’s picture

Assigned: Unassigned » codeitwisely

  • 3b70c198 committed on 1.0.x
    Issue #3608701 by codeitwisely, marcus_johansson: Add monthly rollup and...
codeitwisely’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.