Skip to content

feat(admin): Add user engagement insights - #317

Merged
nfebe merged 2 commits into
devfrom
feat/admin-engagement-insights
Aug 19, 2026
Merged

feat(admin): Add user engagement insights#317
nfebe merged 2 commits into
devfrom
feat/admin-engagement-insights

Conversation

@nfebe

@nfebe nfebe commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Provides the admin console with searchable, paginated user engagement and AI usage data. Token totals now cover both routing and agent work, while activity comes from existing Sanctum and transaction records without a user schema change.

nfebe added 2 commits August 18, 2026 21:56
Token totals now include every user-facing AI path.
Administration reports therefore reflect the full user cost.
Administrators can page and search users, inspect engagement, and see token consumption.
@sourceant

sourceant Bot commented Aug 18, 2026

Copy link
Copy Markdown

Code Review Summary

This PR successfully introduces comprehensive user engagement and AI usage tracking for the admin console. It bridges the gap between agent usage and routing usage, providing a complete picture of LLM costs per user.

🚀 Key Improvements

  • Added token usage recording for all chat-related operations in AiRouter.
  • Optimized user list in UserController with engagement metrics using efficient subquery aggregations.
  • Implemented a new AgentUsageMetricProvider for the admin engagement dashboard.

💡 Minor Suggestions

  • Refactor the AgentUsageMetricProvider to use database-level aggregations (SUM and GROUP BY) instead of in-memory collection processing for better scalability.

@sourceant sourceant Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review complete. See the overview comment for a summary.

use Whilesmart\Engagement\Support\Period;

class AgentUsageMetricProvider implements MetricProvider
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fetching all records for a period into a collection to calculate a sum is inefficient. Use the database's sum() method directly and pass the period to sub-methods to allow for optimized database-level queries.

Suggested change
{
public function metrics(Period $period): array
{
$baseQuery = TokenUsage::query()
->whereBetween('created_at', [$period->start, $period->end]);
return [
Metric::sum('ai_tokens_used', 'AI tokens used', (float) $baseQuery->sum('total_tokens'), 'tokens'),
Metric::series('ai_tokens_series', 'AI tokens over time', $this->series($baseQuery->clone()->get(['created_at', 'total_tokens']), $period), 'tokens'),
Metric::ranking('ai_tokens_by_user', 'AI tokens by user', $this->ranking($period)),
];
}

->all();
}

private function ranking(iterable $usage): array

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Computing the user ranking in PHP by grouping all records is inefficient. This should be performed at the database level to significantly reduce memory usage and improve performance, especially when only the top 8 users are required.

Suggested change
private function ranking(iterable $usage): array
private function ranking(Period $period): array
{
$userType = (new User())->getMorphClass();
$totals = TokenUsage::query()
->where('owner_type', $userType)
->whereBetween('created_at', [$period->start, $period->end])
->groupBy('owner_id')
->selectRaw('owner_id, sum(total_tokens) as total')
->orderByDesc('total')
->take(8)
->pluck('total', 'owner_id');
$users = User::query()->whereIn('id', $totals->keys())->get()->keyBy('id');

@github-actions

Copy link
Copy Markdown

Coverage Report
PR coverage: 73.47%
Baseline: 73.1%
Change: ✅+0.4%

@nfebe
nfebe merged commit 0317292 into dev Aug 19, 2026
4 checks passed
@nfebe
nfebe deleted the feat/admin-engagement-insights branch August 19, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant