Problem/Motivation
The AI Metering dashboard lists one row per editor. On sites with hundreds or
thousands of users this is impractical: the table is too long to scan and the
per-user detail is often not what administrators need. What matters for budget
attribution and planning is a role-level view: "how much did our editorial team
spend on AI this month, combined?" or "which role is driving the most cloud calls?"
The per-user breakdown remains useful for quota governance; the per-role breakdown
is the right view for cost reporting and capacity planning. Both should be available.
Steps to reproduce
- Install ai_metering on a site with multiple roles (e.g. editor, senior editor, administrator).
- Open
/admin/reports/ai-metering. - The only available view lists individual users; there is no way to see aggregated spend by role.
Environment
- Drupal version: 11.3.11
- ai_metering version: 1.0.0-alpha1
- AI provider module + version: any (e.g. ai_provider_anthropic 1.2.2)
- AI feature module tested: N/A
Proposed resolution
Add a second dashboard page at /admin/reports/ai-metering/by-role
that aggregates usage data by Drupal role: one row per role sorted by total token
volume, with columns for role label, number of users in role, call count
(cloud vs. local split), input tokens, output tokens, total cost, and combined
budget. The page includes the same Chart.js charts (doughnut for token split, bar
chart for cost per role), a month selector, and CSV + JSON export endpoints,
consistent with the per-user dashboard. A menu link is added under Reports
alongside "AI Usage by Editor", and each view links to the other.
The implementation reuses the existing dashboard library and CSS classes and adds
no new theme hooks, JS files, or CSS files.
Remaining tasks
- Commit
src/Controller/AiMeteringByRoleController.php(already written locally). - Add menu link
ai_metering.dashboard_by_roletoai_metering.links.menu.yml. - Add a cross-link between the per-user and by-role dashboards.
- Add functional test asserting role aggregation and export endpoints respond correctly.
- PHPCS.
- Review.
User interface changes
Yes. New page at /admin/reports/ai-metering/by-role with role-level breakdown, charts (token split doughnut, cost per role bar chart), month filter, and CSV/JSON export. Menu link added under Reports.
API changes
N/A
Data model changes
N/A. The by-role view aggregates the existing ai_metering_usage and ai_metering_quota tables; no new columns or tables.
AI assistance
N/A
Comments
Comment #12
codeitwisely commented