fix(stats): key Top Agents usage by model and stop dropping each range's first day (#2510)

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
João Salavisa
2026-08-11 20:43:46 +02:00
committed by GitHub
parent 1429fa1a9a
commit 8b21af3296
20 changed files with 458 additions and 147 deletions
+7 -9
View File
@@ -51,14 +51,12 @@ type CompletedTaskActivityDTO struct {
CompletedTasks int `json:"completed_tasks"`
}
// AgentUsageDTO represents usage statistics for a single agent profile
type AgentUsageDTO struct {
AgentProfileID string `json:"agent_profile_id"`
AgentProfileName string `json:"agent_profile_name"`
AgentModel string `json:"agent_model"`
SessionCount int `json:"session_count"`
TurnCount int `json:"turn_count"`
TotalDurationMs int64 `json:"total_duration_ms"`
// ModelUsageDTO represents usage statistics for a single model
type ModelUsageDTO struct {
Model string `json:"model"`
SessionCount int `json:"session_count"`
TurnCount int `json:"turn_count"`
TotalDurationMs int64 `json:"total_duration_ms"`
}
// RepositoryStatsDTO represents usage statistics for a repository
@@ -95,7 +93,7 @@ type StatsResponse struct {
TaskStatsHasMore bool `json:"task_stats_has_more"`
DailyActivity []DailyActivityDTO `json:"daily_activity"`
CompletedActivity []CompletedTaskActivityDTO `json:"completed_activity"`
AgentUsage []AgentUsageDTO `json:"agent_usage"`
ModelUsage []ModelUsageDTO `json:"model_usage"`
RepositoryStats []RepositoryStatsDTO `json:"repository_stats"`
GitStats GitStatsDTO `json:"git_stats"`
}
@@ -16,7 +16,7 @@ import (
// allTimeActivityDays is the number of days shown in the daily activity heatmap for the "all" range.
const allTimeActivityDays = 365
const taskStatsLimit = 200
const agentUsageLimit = 5
const modelUsageLimit = 5
type StatsHandlers struct {
repo repository.Repository
@@ -41,7 +41,7 @@ func (h *StatsHandlers) registerHTTP(router *gin.Engine) {
api.GET("/workspaces/:id/stats/tasks", h.httpGetTaskStats)
api.GET("/workspaces/:id/stats/daily-activity", h.httpGetDailyActivity)
api.GET("/workspaces/:id/stats/completed-activity", h.httpGetCompletedActivity)
api.GET("/workspaces/:id/stats/agent-usage", h.httpGetAgentUsage)
api.GET("/workspaces/:id/stats/model-usage", h.httpGetModelUsage)
api.GET("/workspaces/:id/stats/repositories", h.httpGetRepositoryStats)
api.GET("/workspaces/:id/stats/git", h.httpGetGitStats)
}
@@ -128,17 +128,17 @@ func (h *StatsHandlers) httpGetCompletedActivity(c *gin.Context) {
c.JSON(http.StatusOK, completedActivityToDTOs(activity))
}
func (h *StatsHandlers) httpGetAgentUsage(c *gin.Context) {
func (h *StatsHandlers) httpGetModelUsage(c *gin.Context) {
workspaceID, start, _, ok := h.parseRequest(c)
if !ok {
return
}
usage, err := h.repo.GetAgentUsage(c.Request.Context(), workspaceID, agentUsageLimit, start)
usage, err := h.repo.GetModelUsage(c.Request.Context(), workspaceID, modelUsageLimit, start)
if err != nil {
h.fail(c, workspaceID, "agent usage", err)
h.fail(c, workspaceID, "model usage", err)
return
}
c.JSON(http.StatusOK, agentUsageToDTOs(usage))
c.JSON(http.StatusOK, modelUsageToDTOs(usage))
}
func (h *StatsHandlers) httpGetRepositoryStats(c *gin.Context) {
@@ -241,16 +241,14 @@ func completedActivityToDTOs(items []*models.CompletedTaskActivity) []dto.Comple
return result
}
func agentUsageToDTOs(items []*models.AgentUsage) []dto.AgentUsageDTO {
result := make([]dto.AgentUsageDTO, 0, len(items))
for _, au := range items {
result = append(result, dto.AgentUsageDTO{
AgentProfileID: au.AgentProfileID,
AgentProfileName: au.AgentProfileName,
AgentModel: au.AgentModel,
SessionCount: au.SessionCount,
TurnCount: au.TurnCount,
TotalDurationMs: au.TotalDurationMs,
func modelUsageToDTOs(items []*models.ModelUsage) []dto.ModelUsageDTO {
result := make([]dto.ModelUsageDTO, 0, len(items))
for _, mu := range items {
result = append(result, dto.ModelUsageDTO{
Model: mu.Model,
SessionCount: mu.SessionCount,
TurnCount: mu.TurnCount,
TotalDurationMs: mu.TotalDurationMs,
})
}
return result
@@ -53,14 +53,18 @@ type CompletedTaskActivity struct {
CompletedTasks int `json:"completed_tasks"`
}
// AgentUsage represents usage statistics for a single agent profile
type AgentUsage struct {
AgentProfileID string `json:"agent_profile_id"`
AgentProfileName string `json:"agent_profile_name"`
AgentModel string `json:"agent_model"`
SessionCount int `json:"session_count"`
TurnCount int `json:"turn_count"`
TotalDurationMs int64 `json:"total_duration_ms"`
// ModelUsage represents usage statistics for a single model.
//
// Usage is keyed by model rather than by agent profile because a profile is
// mutable: it gets renamed and repointed at other models, so its sessions carry
// several different snapshots and no single one of them describes the group.
// A model string never changes meaning, so every row is exactly true and a
// short range's counts are always contained in the longer range around it.
type ModelUsage struct {
Model string `json:"model"`
SessionCount int `json:"session_count"`
TurnCount int `json:"turn_count"`
TotalDurationMs int64 `json:"total_duration_ms"`
}
// RepositoryStats represents aggregated statistics for a repository in a workspace
@@ -13,7 +13,7 @@ type Repository interface {
GetGlobalStats(ctx context.Context, workspaceID string, start *time.Time) (*models.GlobalStats, error)
GetDailyActivity(ctx context.Context, workspaceID string, days int) ([]*models.DailyActivity, error)
GetCompletedTaskActivity(ctx context.Context, workspaceID string, days int) ([]*models.CompletedTaskActivity, error)
GetAgentUsage(ctx context.Context, workspaceID string, limit int, start *time.Time) ([]*models.AgentUsage, error)
GetModelUsage(ctx context.Context, workspaceID string, limit int, start *time.Time) ([]*models.ModelUsage, error)
GetRepositoryStats(ctx context.Context, workspaceID string, start *time.Time) ([]*models.RepositoryStats, error)
GetGitStats(ctx context.Context, workspaceID string, start *time.Time) (*models.GitStats, error)
ListSessionCodeStats(ctx context.Context, filter models.SessionCodeStatsFilter) ([]*models.SessionCodeStats, error)
@@ -691,7 +691,7 @@ func TestGetCompletedTaskActivity_ExcludesEphemeralTasks(t *testing.T) {
}
}
func TestGetAgentUsage_ExcludesEphemeralTasks(t *testing.T) {
func TestGetModelUsage_ExcludesEphemeralTasks(t *testing.T) {
dbConn := createTestDB(t)
repo, err := NewWithDB(dbConn, dbConn)
if err != nil {
@@ -708,12 +708,12 @@ func TestGetAgentUsage_ExcludesEphemeralTasks(t *testing.T) {
execOrFatal(t, dbConn, `INSERT INTO tasks (id, workspace_id, board_id, workflow_step_id, title, is_ephemeral, created_at, updated_at) VALUES ('task-regular', 'ws-1', 'board-1', '', 'Regular', 0, ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO tasks (id, workspace_id, board_id, workflow_step_id, title, is_ephemeral, created_at, updated_at) VALUES ('task-ephemeral', 'ws-1', 'board-1', '', 'Quick Chat', 1, ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO task_sessions (id, task_id, agent_profile_id, agent_profile_snapshot, state, started_at, updated_at) VALUES ('sess-regular', 'task-regular', 'agent-1', '{"name":"Agent"}', 'COMPLETED', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO task_sessions (id, task_id, agent_profile_id, agent_profile_snapshot, state, started_at, updated_at) VALUES ('sess-ephemeral', 'task-ephemeral', 'agent-1', '{"name":"Agent"}', 'COMPLETED', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO task_sessions (id, task_id, agent_profile_id, agent_profile_snapshot, state, started_at, updated_at) VALUES ('sess-regular', 'task-regular', 'agent-1', '{"name":"Agent","model":"opus"}', 'COMPLETED', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO task_sessions (id, task_id, agent_profile_id, agent_profile_snapshot, state, started_at, updated_at) VALUES ('sess-ephemeral', 'task-ephemeral', 'agent-1', '{"name":"Agent","model":"opus"}', 'COMPLETED', ?, ?)`, nowStr, nowStr)
results, err := repo.GetAgentUsage(ctx, "ws-1", 5, nil)
results, err := repo.GetModelUsage(ctx, "ws-1", 5, nil)
if err != nil {
t.Fatalf("GetAgentUsage failed: %v", err)
t.Fatalf("GetModelUsage failed: %v", err)
}
totalSessions := 0
@@ -725,6 +725,258 @@ func TestGetAgentUsage_ExcludesEphemeralTasks(t *testing.T) {
}
}
// The reported bug: a shorter range advertised a model that the longer range
// containing it never mentioned, because usage was keyed by the agent profile
// and each range labelled the group from whichever session row it happened to
// read. Keyed by model, a range's rows are always a subset of the wider range's
// rows and every count only grows.
func TestGetModelUsage_CountsNestAcrossRanges(t *testing.T) {
dbConn := createTestDB(t)
repo, err := NewWithDB(dbConn, dbConn)
if err != nil {
t.Fatalf("NewWithDB failed: %v", err)
}
ctx := context.Background()
now := time.Now().UTC()
nowStr := now.Format(time.RFC3339)
execOrFatal(t, dbConn, `INSERT INTO workspaces (id, name, created_at, updated_at) VALUES ('ws-1', 'Test', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO boards (id, workspace_id, name, created_at, updated_at) VALUES ('board-1', 'ws-1', 'Board', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO tasks (id, workspace_id, board_id, workflow_step_id, title, is_ephemeral, created_at, updated_at) VALUES ('task-1', 'ws-1', 'board-1', '', 'Task', 0, ?, ?)`, nowStr, nowStr)
// One profile, repointed at a different model twice over the period.
sessions := []struct {
id string
snapshot string
age time.Duration
}{
{"sess-recent-a", `{"name":"Default (recommended)","model":"opus[1m]"}`, 24 * time.Hour},
{"sess-recent-b", `{"name":"Default (recommended)","model":"opus[1m]"}`, 3 * 24 * time.Hour},
{"sess-mid", `{"name":"Default (recommended)","model":"claude-fable-5[1m]"}`, 12 * 24 * time.Hour},
{"sess-old", `{"name":"Default","model":"sonnet"}`, 40 * 24 * time.Hour},
}
for _, s := range sessions {
startedAt := now.Add(-s.age).Format(time.RFC3339)
execOrFatal(t, dbConn, `INSERT INTO task_sessions (id, task_id, agent_profile_id, agent_profile_snapshot, state, started_at, updated_at) VALUES (?, 'task-1', 'agent-1', ?, 'COMPLETED', ?, ?)`,
s.id, s.snapshot, startedAt, startedAt)
}
weekStart := now.Add(-7 * 24 * time.Hour)
monthStart := now.Add(-30 * 24 * time.Hour)
cases := []struct {
name string
start *time.Time
want map[string]int
}{
{"last week", &weekStart, map[string]int{"opus[1m]": 2}},
{"last month", &monthStart, map[string]int{"opus[1m]": 2, "claude-fable-5[1m]": 1}},
{"all time", nil, map[string]int{"opus[1m]": 2, "claude-fable-5[1m]": 1, "sonnet": 1}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
results, err := repo.GetModelUsage(ctx, "ws-1", 5, tc.start)
if err != nil {
t.Fatalf("GetModelUsage failed: %v", err)
}
got := map[string]int{}
for _, r := range results {
got[r.Model] = r.SessionCount
}
if fmt.Sprint(got) != fmt.Sprint(tc.want) {
t.Errorf("expected %v, got %v", tc.want, got)
}
})
}
}
// Two profiles pointed at the same model are one line of usage, not two rows
// disagreeing about what ran.
func TestGetModelUsage_MergesProfilesSharingAModel(t *testing.T) {
dbConn := createTestDB(t)
repo, err := NewWithDB(dbConn, dbConn)
if err != nil {
t.Fatalf("NewWithDB failed: %v", err)
}
ctx := context.Background()
now := time.Now().UTC()
nowStr := now.Format(time.RFC3339)
execOrFatal(t, dbConn, `INSERT INTO workspaces (id, name, created_at, updated_at) VALUES ('ws-1', 'Test', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO boards (id, workspace_id, name, created_at, updated_at) VALUES ('board-1', 'ws-1', 'Board', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO tasks (id, workspace_id, board_id, workflow_step_id, title, is_ephemeral, created_at, updated_at) VALUES ('task-1', 'ws-1', 'board-1', '', 'Task', 0, ?, ?)`, nowStr, nowStr)
sessions := []struct{ id, profile, snapshot string }{
{"sess-a", "agent-default", `{"name":"Default (recommended)","model":"opus[1m]"}`},
{"sess-b", "agent-sonnet", `{"name":"Sonnet","model":"opus[1m]"}`},
// model_name and llm are the fallback snapshot keys for the same field.
{"sess-c", "agent-alt", `{"name":"Alt","model_name":"opus[1m]"}`},
{"sess-d", "agent-legacy", `{"name":"Legacy","llm":"opus[1m]"}`},
// No model recorded at all: counted nowhere rather than under a blank label.
{"sess-e", "agent-unknown", `{"name":"Unknown"}`},
}
for _, s := range sessions {
execOrFatal(t, dbConn, `INSERT INTO task_sessions (id, task_id, agent_profile_id, agent_profile_snapshot, state, started_at, updated_at) VALUES (?, 'task-1', ?, ?, 'COMPLETED', ?, ?)`,
s.id, s.profile, s.snapshot, nowStr, nowStr)
}
results, err := repo.GetModelUsage(ctx, "ws-1", 5, nil)
if err != nil {
t.Fatalf("GetModelUsage failed: %v", err)
}
if len(results) != 1 {
t.Fatalf("expected 1 model row, got %d: %+v", len(results), results)
}
if results[0].Model != "opus[1m]" || results[0].SessionCount != 4 {
t.Errorf("expected opus[1m] with 4 sessions, got %q with %d", results[0].Model, results[0].SessionCount)
}
}
// Rows tied on session count must come back in a stable order rather than
// shuffling between reloads of the same stats page.
func TestGetModelUsage_OrdersTiesDeterministically(t *testing.T) {
dbConn := createTestDB(t)
repo, err := NewWithDB(dbConn, dbConn)
if err != nil {
t.Fatalf("NewWithDB failed: %v", err)
}
ctx := context.Background()
now := time.Now().UTC()
nowStr := now.Format(time.RFC3339)
execOrFatal(t, dbConn, `INSERT INTO workspaces (id, name, created_at, updated_at) VALUES ('ws-1', 'Test', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO boards (id, workspace_id, name, created_at, updated_at) VALUES ('board-1', 'ws-1', 'Board', ?, ?)`, nowStr, nowStr)
execOrFatal(t, dbConn, `INSERT INTO tasks (id, workspace_id, board_id, workflow_step_id, title, is_ephemeral, created_at, updated_at) VALUES ('task-1', 'ws-1', 'board-1', '', 'Task', 0, ?, ?)`, nowStr, nowStr)
for _, model := range []string{"zeta", "alpha", "mu"} {
execOrFatal(t, dbConn, `INSERT INTO task_sessions (id, task_id, agent_profile_id, agent_profile_snapshot, state, started_at, updated_at) VALUES (?, 'task-1', 'agent-1', ?, 'COMPLETED', ?, ?)`,
"sess-"+model, fmt.Sprintf(`{"name":"Agent","model":%q}`, model), nowStr, nowStr)
}
want := []string{"alpha", "mu", "zeta"}
for attempt := 0; attempt < 3; attempt++ {
results, err := repo.GetModelUsage(ctx, "ws-1", 5, nil)
if err != nil {
t.Fatalf("GetModelUsage failed: %v", err)
}
got := make([]string, 0, len(results))
for _, r := range results {
got = append(got, r.Model)
}
if fmt.Sprint(got) != fmt.Sprint(want) {
t.Fatalf("attempt %d: expected %v, got %v", attempt, want, got)
}
}
}
// Rows dated on the range's first day used to be dropped wholesale: the
// boundary was bound as an RFC3339 string whose "T" separator sorts above the
// space in the layout SQLite actually stores, and the TIMESTAMP columns' NUMERIC
// affinity leaves a text parameter uncoerced, so ">=" was comparing strings.
// "Last 7 days" was really the last 6 days plus today.
//
// Every timestamp here is written the way production writes it — by handing the
// driver a time.Time — because the other tests in this file insert preformatted
// RFC3339 strings, which is exactly why none of them saw this.
func TestRangeStart_IncludesRowsLaterOnTheBoundaryDay(t *testing.T) {
dbConn := createTestDB(t)
repo, err := NewWithDB(dbConn, dbConn)
if err != nil {
t.Fatalf("NewWithDB failed: %v", err)
}
ctx := context.Background()
// Fixed clock: the boundary and both rows share one calendar day, so the
// separator mismatch is what decides the result, not the wall clock.
threshold := time.Date(2026, 3, 4, 12, 0, 0, 0, time.UTC)
before := threshold.Add(-6 * time.Hour)
after := threshold.Add(6 * time.Hour)
execOrFatal(t, dbConn, `INSERT INTO workspaces (id, name, created_at, updated_at) VALUES ('ws-1', 'Test', ?, ?)`, before, before)
execOrFatal(t, dbConn, `INSERT INTO boards (id, workspace_id, name, created_at, updated_at) VALUES ('board-1', 'ws-1', 'Board', ?, ?)`, before, before)
execOrFatal(t, dbConn, `INSERT INTO repositories (id, workspace_id, name, created_at, updated_at) VALUES ('repo-1', 'ws-1', 'Repo', ?, ?)`, before, before)
for _, row := range []struct {
suffix string
at time.Time
}{{"before", before}, {"after", after}} {
execOrFatal(t, dbConn, `INSERT INTO tasks (id, workspace_id, board_id, workflow_step_id, title, is_ephemeral, created_at, updated_at) VALUES (?, 'ws-1', 'board-1', '', 'Task', 0, ?, ?)`,
"task-"+row.suffix, row.at, row.at)
execOrFatal(t, dbConn, `INSERT INTO task_repositories (id, task_id, repository_id, created_at, updated_at) VALUES (?, ?, 'repo-1', ?, ?)`,
"task-repo-"+row.suffix, "task-"+row.suffix, row.at, row.at)
execOrFatal(t, dbConn, `INSERT INTO task_sessions (id, task_id, agent_profile_id, agent_profile_snapshot, repository_id, state, started_at, updated_at) VALUES (?, ?, 'agent-1', '{"name":"Agent","model":"opus"}', 'repo-1', 'COMPLETED', ?, ?)`,
"sess-"+row.suffix, "task-"+row.suffix, row.at, row.at)
execOrFatal(t, dbConn, `INSERT INTO task_session_commits (id, session_id, commit_sha, committed_at, files_changed, insertions, deletions, created_at) VALUES (?, ?, ?, ?, 1, 1, 0, ?)`,
"commit-"+row.suffix, "sess-"+row.suffix, "sha-"+row.suffix, row.at, row.at)
}
t.Run("model usage", func(t *testing.T) {
usage, err := repo.GetModelUsage(ctx, "ws-1", 5, &threshold)
if err != nil {
t.Fatalf("GetModelUsage failed: %v", err)
}
if len(usage) != 1 || usage[0].SessionCount != 1 {
t.Fatalf("expected the one session started after the boundary, got %+v", usage)
}
})
t.Run("global stats", func(t *testing.T) {
stats, err := repo.GetGlobalStats(ctx, "ws-1", &threshold)
if err != nil {
t.Fatalf("GetGlobalStats failed: %v", err)
}
if stats.TotalTasks != 1 {
t.Errorf("expected 1 task created after the boundary, got %d", stats.TotalTasks)
}
if stats.TotalSessions != 1 {
t.Errorf("expected 1 session started after the boundary, got %d", stats.TotalSessions)
}
})
t.Run("task stats", func(t *testing.T) {
stats, err := repo.GetTaskStats(ctx, "ws-1", &threshold, 10)
if err != nil {
t.Fatalf("GetTaskStats failed: %v", err)
}
if len(stats) != 1 || stats[0].TaskID != "task-after" || stats[0].SessionCount != 1 {
t.Fatalf("expected only the task created after the boundary, got %+v", stats)
}
})
t.Run("repository stats", func(t *testing.T) {
stats, err := repo.GetRepositoryStats(ctx, "ws-1", &threshold)
if err != nil {
t.Fatalf("GetRepositoryStats failed: %v", err)
}
if len(stats) != 1 || stats[0].TotalTasks != 1 || stats[0].SessionCount != 1 || stats[0].TotalCommits != 1 {
t.Fatalf("expected only boundary-day repository activity, got %+v", stats)
}
})
t.Run("git stats", func(t *testing.T) {
stats, err := repo.GetGitStats(ctx, "ws-1", &threshold)
if err != nil {
t.Fatalf("GetGitStats failed: %v", err)
}
if stats.TotalCommits != 1 {
t.Errorf("expected 1 commit made after the boundary, got %d", stats.TotalCommits)
}
})
t.Run("open ended range keeps both", func(t *testing.T) {
stats, err := repo.GetGlobalStats(ctx, "ws-1", nil)
if err != nil {
t.Fatalf("GetGlobalStats failed: %v", err)
}
if stats.TotalTasks != 2 || stats.TotalSessions != 2 {
t.Errorf("expected both rows with no range, got %d tasks / %d sessions", stats.TotalTasks, stats.TotalSessions)
}
})
}
func TestGetGitStats_ExcludesEphemeralTasks(t *testing.T) {
dbConn := createTestDB(t)
repo, err := NewWithDB(dbConn, dbConn)
@@ -46,6 +46,25 @@ func parseTimeString(s string) time.Time {
return time.Time{}
}
// rangeStartArg turns a range boundary into a value the timestamp columns can
// actually be compared against, and into an untyped nil for an open-ended range
// so the `? IS NULL` arm of each predicate matches.
//
// It has to bind the time.Time rather than a formatted string. These columns are
// written by handing the driver a time.Time, which SQLite stores as
// "2006-01-02 15:04:05.999999999-07:00", and their TIMESTAMP declaration gives
// them NUMERIC affinity — a text bound parameter is never coerced, so `>=` is a
// plain string comparison. Formatting the boundary as RFC3339 put a "T" where
// every stored value has a space, and "T" sorts above " ", so rows dated on the
// boundary day compared below the threshold whatever their time: each range
// silently lost its first day.
func rangeStartArg(start *time.Time) any {
if start == nil {
return nil
}
return start.UTC()
}
// GetTaskStats retrieves aggregated statistics for tasks in a workspace.
func (r *Repository) GetTaskStats(
ctx context.Context,
@@ -53,10 +72,7 @@ func (r *Repository) GetTaskStats(
start *time.Time,
limit int,
) ([]*models.TaskStats, error) {
var startArg any
if start != nil {
startArg = start.UTC().Format(time.RFC3339)
}
startArg := rangeStartArg(start)
if limit <= 0 {
limit = 200
}
@@ -175,10 +191,7 @@ const (
// index-only message-count subquery per qualifying turn (kept correlated to
// stay portable between the SQLite and Postgres dialects).
func (r *Repository) GetGlobalStats(ctx context.Context, workspaceID string, start *time.Time) (*models.GlobalStats, error) {
var startArg any
if start != nil {
startArg = start.UTC().Format(time.RFC3339)
}
startArg := rangeStartArg(start)
drv := r.ro.DriverName()
dur := dialect.DurationMs(drv, "turn.completed_at", "turn.started_at")
@@ -394,10 +407,7 @@ func (r *Repository) GetCompletedTaskActivity(ctx context.Context, workspaceID s
// GetRepositoryStats retrieves aggregated statistics for repositories in a workspace
func (r *Repository) GetRepositoryStats(ctx context.Context, workspaceID string, start *time.Time) ([]*models.RepositoryStats, error) {
var startArg any
if start != nil {
startArg = start.UTC().Format(time.RFC3339)
}
startArg := rangeStartArg(start)
query := buildRepositoryStatsQuery(r.ro.DriverName())
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query),
@@ -504,37 +514,47 @@ func buildRepositoryStatsQuery(drv string) string {
`, dur)
}
// GetAgentUsage retrieves usage statistics per agent profile
func (r *Repository) GetAgentUsage(ctx context.Context, workspaceID string, limit int, start *time.Time) ([]*models.AgentUsage, error) {
var startArg any
if start != nil {
startArg = start.UTC().Format(time.RFC3339)
}
// GetModelUsage retrieves usage statistics per model.
//
// Grouping on the model itself is what keeps two ranges consistent. Grouping on
// the agent profile did not: a profile's snapshot name and model change every
// time the user reconfigures it, so the group spans several of them and the
// engine was free to label it from any row it liked — the same profile then
// reported a different model per range, and per run.
//
// Sessions whose snapshot records no model are skipped rather than collected
// under a blank label, mirroring how sessions with no agent profile were
// already left out.
func (r *Repository) GetModelUsage(ctx context.Context, workspaceID string, limit int, start *time.Time) ([]*models.ModelUsage, error) {
startArg := rangeStartArg(start)
drv := r.ro.DriverName()
dur := dialect.DurationMs(drv, "turn.completed_at", "turn.started_at")
jeName := dialect.JSONExtract(drv, "s.agent_profile_snapshot", "name")
jeDisplay := dialect.JSONExtract(drv, "s.agent_profile_snapshot", "agent_display_name")
jeModel := dialect.JSONExtract(drv, "s.agent_profile_snapshot", "model")
jeModelName := dialect.JSONExtract(drv, "s.agent_profile_snapshot", "model_name")
jeLLM := dialect.JSONExtract(drv, "s.agent_profile_snapshot", "llm")
query := fmt.Sprintf(`
WITH scoped AS (
SELECT
s.id,
COALESCE(%s, %s, %s, '') as model
FROM task_sessions s
JOIN tasks t ON t.id = s.task_id
WHERE t.workspace_id = ? AND t.is_ephemeral = 0`+andNotAutomationOriginT+` AND (? IS NULL OR s.started_at >= ?)
)
SELECT
s.agent_profile_id,
COALESCE(%s, %s, s.agent_profile_id) as agent_profile_name,
COALESCE(%s, %s, %s, '') as agent_model,
COUNT(DISTINCT s.id) as session_count,
scoped.model,
COUNT(DISTINCT scoped.id) as session_count,
COUNT(DISTINCT turn.id) as turn_count,
COALESCE(SUM(CASE WHEN turn.completed_at IS NOT NULL THEN %s ELSE 0 END), 0) as total_duration_ms
FROM task_sessions s
JOIN tasks t ON t.id = s.task_id
LEFT JOIN task_session_turns turn ON turn.task_session_id = s.id
WHERE t.workspace_id = ? AND t.is_ephemeral = 0`+andNotAutomationOriginT+` AND s.agent_profile_id != '' AND (? IS NULL OR s.started_at >= ?)
GROUP BY s.agent_profile_id
ORDER BY session_count DESC
FROM scoped
LEFT JOIN task_session_turns turn ON turn.task_session_id = scoped.id
WHERE scoped.model != ''
GROUP BY scoped.model
ORDER BY session_count DESC, turn_count DESC, scoped.model ASC
LIMIT ?
`, jeName, jeDisplay, jeModel, jeModelName, jeLLM, dur)
`, jeModel, jeModelName, jeLLM, dur)
rows, err := r.ro.QueryContext(ctx, r.ro.Rebind(query), workspaceID, startArg, startArg, limit)
if err != nil {
@@ -542,13 +562,12 @@ func (r *Repository) GetAgentUsage(ctx context.Context, workspaceID string, limi
}
defer func() { _ = rows.Close() }()
var results []*models.AgentUsage
var results []*models.ModelUsage
for rows.Next() {
var usage models.AgentUsage
var usage models.ModelUsage
var totalDurationMs float64
err := rows.Scan(
&usage.AgentProfileID, &usage.AgentProfileName, &usage.AgentModel,
&usage.SessionCount, &usage.TurnCount, &totalDurationMs,
&usage.Model, &usage.SessionCount, &usage.TurnCount, &totalDurationMs,
)
if err != nil {
return nil, err
@@ -562,10 +581,7 @@ func (r *Repository) GetAgentUsage(ctx context.Context, workspaceID string, limi
// GetGitStats retrieves aggregated git statistics for a workspace
func (r *Repository) GetGitStats(ctx context.Context, workspaceID string, start *time.Time) (*models.GitStats, error) {
var startArg any
if start != nil {
startArg = start.UTC().Format(time.RFC3339)
}
startArg := rangeStartArg(start)
query := `
SELECT
@@ -35,7 +35,7 @@ func (f *fakeRepository) GetCompletedTaskActivity(context.Context, string, int)
return nil, nil
}
func (f *fakeRepository) GetAgentUsage(context.Context, string, int, *time.Time) ([]*models.AgentUsage, error) {
func (f *fakeRepository) GetModelUsage(context.Context, string, int, *time.Time) ([]*models.ModelUsage, error) {
return nil, nil
}
+10 -17
View File
@@ -3,7 +3,7 @@
import { useMemo, useState } from "react";
import { Button } from "@kandev/ui/button";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@kandev/ui/tooltip";
import type { DailyActivityDTO, AgentUsageDTO, CompletedTaskActivityDTO } from "@/lib/types/http";
import type { DailyActivityDTO, ModelUsageDTO, CompletedTaskActivityDTO } from "@/lib/types/http";
import { useTranslation } from "react-i18next";
function formatMonthLabel(date: Date): string {
@@ -163,37 +163,30 @@ export function ActivityHeatmap({ dailyActivity }: { dailyActivity: DailyActivit
);
}
export function AgentUsageList({ agentUsage }: { agentUsage: AgentUsageDTO[] }) {
export function ModelUsageList({ modelUsage }: { modelUsage: ModelUsageDTO[] }) {
const { t } = useTranslation();
if (!agentUsage || agentUsage.length === 0) {
if (!modelUsage || modelUsage.length === 0) {
return (
<div className="text-sm text-muted-foreground py-4">{t("stats:noAgentUsageDataYet")}</div>
<div className="text-sm text-muted-foreground py-4">{t("stats:noModelUsageDataYet")}</div>
);
}
const maxSessions = Math.max(...agentUsage.map((a) => a.session_count));
const maxSessions = Math.max(...modelUsage.map((m) => m.session_count));
return (
<div className="space-y-3">
{agentUsage.map((agent) => (
<div key={agent.agent_profile_id}>
{modelUsage.map((entry) => (
<div key={entry.model}>
<div className="flex items-center justify-between mb-1">
<div className="min-w-0">
<div className="text-sm truncate">{agent.agent_profile_name}</div>
{agent.agent_model && (
<div className="text-[11px] text-muted-foreground font-mono truncate">
{agent.agent_model}
</div>
)}
</div>
<div className="text-sm font-mono truncate min-w-0">{entry.model}</div>
<span className="text-xs text-muted-foreground tabular-nums ml-2">
{agent.session_count}
{entry.session_count}
</span>
</div>
<div className="h-1.5 bg-muted rounded-full overflow-hidden">
<div
className="h-full bg-primary/60 rounded-full"
style={{ width: `${(agent.session_count / maxSessions) * 100}%` }}
style={{ width: `${(entry.session_count / maxSessions) * 100}%` }}
/>
</div>
</div>
+9 -9
View File
@@ -2,7 +2,7 @@
import { useEffect, useState } from "react";
import type {
AgentUsageDTO,
ModelUsageDTO,
CompletedTaskActivityDTO,
DailyActivityDTO,
GitStatsDTO,
@@ -12,7 +12,7 @@ import type {
TaskStatsDTO,
} from "@/lib/types/http";
import {
fetchAgentUsage,
fetchModelUsage,
fetchCompletedActivity,
fetchDailyActivity,
fetchGitStats,
@@ -33,7 +33,7 @@ export type StatsSections = {
tasks: SectionStatus<TaskStatsResponse>;
daily: SectionStatus<DailyActivityDTO[]>;
completed: SectionStatus<CompletedTaskActivityDTO[]>;
agents: SectionStatus<AgentUsageDTO[]>;
models: SectionStatus<ModelUsageDTO[]>;
repos: SectionStatus<RepositoryStatsDTO[]>;
git: SectionStatus<GitStatsDTO>;
};
@@ -45,7 +45,7 @@ const INITIAL_SECTIONS: StatsSections = {
tasks: LOADING,
daily: LOADING,
completed: LOADING,
agents: LOADING,
models: LOADING,
repos: LOADING,
git: LOADING,
};
@@ -96,7 +96,7 @@ export function useStatsSections(workspaceId: string | undefined, range: RangeKe
run("tasks", fetchTaskStats(workspaceId, opts, range));
run("daily", fetchDailyActivity(workspaceId, opts, range));
run("completed", fetchCompletedActivity(workspaceId, opts, range));
run("agents", fetchAgentUsage(workspaceId, opts, range));
run("models", fetchModelUsage(workspaceId, opts, range));
run("repos", fetchRepositoryStats(workspaceId, opts, range));
run("git", fetchGitStats(workspaceId, opts, range));
@@ -111,7 +111,7 @@ export function readyGlobal(sections: StatsSections): GlobalStatsDTO | null {
}
// firstError returns the message of the first errored section in object-iteration
// order (insertion order: global → tasks → daily → completed → agents → repos →
// order (insertion order: global → tasks → daily → completed → models → repos →
// git). Callers use it only as a boolean "any section failed?" signal driving
// the header's "Failed to load stats" subtitle, so the deterministic ordering
// is fine — surface a specific section's error inside its own panel instead.
@@ -126,13 +126,13 @@ export function firstError(sections: StatsSections): string | null {
// composeStatsResponse builds a full StatsResponse when every section is ready.
// Returns null otherwise — used to gate the Copy Stats button.
export function composeStatsResponse(sections: StatsSections): StatsResponse | null {
const { global, tasks, daily, completed, agents, repos, git } = sections;
const { global, tasks, daily, completed, models, repos, git } = sections;
if (
global.kind !== "ready" ||
tasks.kind !== "ready" ||
daily.kind !== "ready" ||
completed.kind !== "ready" ||
agents.kind !== "ready" ||
models.kind !== "ready" ||
repos.kind !== "ready" ||
git.kind !== "ready"
) {
@@ -143,7 +143,7 @@ export function composeStatsResponse(sections: StatsSections): StatsResponse | n
task_stats: tasks.data.task_stats,
daily_activity: daily.data,
completed_activity: completed.data,
agent_usage: agents.data,
model_usage: models.data,
repository_stats: repos.data,
git_stats: git.data,
};
+9 -9
View File
@@ -10,7 +10,7 @@ import { useRouter, useSearchParams } from "@/lib/routing/client-router";
import { IconChartBar } from "@tabler/icons-react";
import { useCopyToClipboard } from "@/hooks/use-copy-to-clipboard";
import type {
AgentUsageDTO,
ModelUsageDTO,
CompletedTaskActivityDTO,
DailyActivityDTO,
GitStatsDTO,
@@ -27,7 +27,7 @@ import {
} from "./stats-sections";
import {
ActivityHeatmap,
AgentUsageList,
ModelUsageList,
CompletedTasksChart,
MostProductiveSummary,
} from "./stats-charts";
@@ -251,11 +251,11 @@ function CompletedPanel({ status }: { status: SectionStatus<CompletedTaskActivit
function ActivityPanel({
daily,
agents,
models,
rangeLabel,
}: {
daily: SectionStatus<DailyActivityDTO[]>;
agents: SectionStatus<AgentUsageDTO[]>;
models: SectionStatus<ModelUsageDTO[]>;
rangeLabel: string;
}) {
const { t } = useTranslation();
@@ -277,18 +277,18 @@ function ActivityPanel({
</Card>
),
})}
{renderSection(agents, {
{renderSection(models, {
skeleton: <ActivitySkeleton />,
errorTitle: t("stats:topAgents"),
errorTitle: t("stats:topModels"),
ready: (data) => (
<Card className="rounded-sm">
<CardHeader className="pb-2">
<CardTitle className="text-sm font-medium text-muted-foreground">
{t("stats:topAgents")}
{t("stats:topModels")}
</CardTitle>
</CardHeader>
<CardContent>
<AgentUsageList agentUsage={data} />
<ModelUsageList modelUsage={data} />
</CardContent>
</Card>
),
@@ -384,7 +384,7 @@ function StatsContent({
<OverviewPanel global={sections.global} git={sections.git} />
<SectionDivider id="telemetry" label={t("stats:telemetry")} />
<CompletedPanel status={sections.completed} />
<ActivityPanel daily={sections.daily} agents={sections.agents} rangeLabel={rangeLabel} />
<ActivityPanel daily={sections.daily} models={sections.models} rangeLabel={rangeLabel} />
<RepositoryActivityPanel status={sections.repos} />
<TopRepositoriesPanel status={sections.repos} />
<RepoLeadersPanel status={sections.repos} />
+36 -1
View File
@@ -9,6 +9,7 @@ import {
statsReducer,
toPanelState,
} from "./stats-utils";
import { composeStatsResponse, type StatsSections } from "./stats-data";
const emptyGlobal: StatsResponse["global"] = {
total_tasks: 0,
@@ -40,7 +41,7 @@ const sampleStats: StatsResponse = {
task_stats: [],
daily_activity: [],
completed_activity: [],
agent_usage: [],
model_usage: [],
repository_stats: [
{
repository_id: "r1",
@@ -68,6 +69,40 @@ const sampleStats: StatsResponse = {
},
};
function readySections(modelUsage: StatsResponse["model_usage"]): StatsSections {
return {
global: { kind: "ready", data: sampleStats.global },
tasks: {
kind: "ready",
data: { task_stats: sampleStats.task_stats, task_stats_has_more: false },
},
daily: { kind: "ready", data: sampleStats.daily_activity },
completed: { kind: "ready", data: sampleStats.completed_activity },
models: { kind: "ready", data: modelUsage },
repos: { kind: "ready", data: sampleStats.repository_stats },
git: { kind: "ready", data: sampleStats.git_stats },
};
}
describe("composeStatsResponse", () => {
it("waits for model usage before composing the response", () => {
const sections = readySections([]);
sections.models = { kind: "loading" };
expect(composeStatsResponse(sections)).toBeNull();
});
it("maps model usage into the composed response", () => {
const modelUsage = [{ model: "opus", session_count: 2, turn_count: 4, total_duration_ms: 100 }];
expect(composeStatsResponse(readySections(modelUsage))?.model_usage).toEqual(modelUsage);
});
it("preserves an empty model usage response", () => {
expect(composeStatsResponse(readySections([]))?.model_usage).toEqual([]);
});
});
describe("isRangeKey", () => {
it("accepts the three valid keys", () => {
expect(isRangeKey("week")).toBe(true);
+5 -13
View File
@@ -111,24 +111,16 @@ test.describe("Clarification flow", () => {
);
if (!task.session_id) throw new Error("createTaskWithAgent did not return a session_id");
// The mock agent reaches the clarification call asynchronously. Wait for
// the owning session to park before opening the page, then explicitly
// establish the Review state that the answer should move it out of.
await waitForSessionState(apiClient, {
taskId: task.id,
sessionId: task.session_id,
expectedState: "WAITING_FOR_INPUT",
message: "clarification session must park before the Review-state transition",
timeout: 30_000,
});
await apiClient.updateTaskState(task.id, "REVIEW");
await expect.poll(async () => (await apiClient.getTask(task.id)).state).toBe("REVIEW");
await testPage.goto(`/t/${task.id}`);
const session = new SessionPage(testPage);
await session.waitForLoad();
await expect(session.clarificationOverlay()).toBeVisible({ timeout: 30_000 });
// The pending overlay is durable while the session may legitimately stay
// RUNNING when the primary MCP response path remains connected.
await apiClient.updateTaskState(task.id, "REVIEW");
await expect.poll(async () => (await apiClient.getTask(task.id)).state).toBe("REVIEW");
const filters = new SidebarFilterPopoverPage(testPage);
await filters.open();
await filters.setGroup("State");
@@ -0,0 +1,25 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const fetchJsonMock = vi.hoisted(() => vi.fn());
vi.mock("../client", () => ({
fetchJson: fetchJsonMock,
}));
import { fetchModelUsage } from "./stats-api";
describe("fetchModelUsage", () => {
beforeEach(() => fetchJsonMock.mockReset());
it("requests the model-usage endpoint with the selected range", async () => {
const response = [{ model: "opus", session_count: 2, turn_count: 4, total_duration_ms: 100 }];
const options = { cache: "no-store" as const };
fetchJsonMock.mockResolvedValue(response);
await expect(fetchModelUsage("workspace-1", options, "month")).resolves.toEqual(response);
expect(fetchJsonMock).toHaveBeenCalledWith(
"/api/v1/workspaces/workspace-1/stats/model-usage?range=month",
options,
);
});
});
+3 -3
View File
@@ -1,6 +1,6 @@
import { fetchJson, type ApiRequestOptions } from "../client";
import type {
AgentUsageDTO,
ModelUsageDTO,
CompletedTaskActivityDTO,
DailyActivityDTO,
GitStatsDTO,
@@ -59,12 +59,12 @@ export function fetchCompletedActivity(
);
}
export function fetchAgentUsage(
export function fetchModelUsage(
workspaceId: string,
options?: ApiRequestOptions,
range?: StatsRange,
) {
return fetchJson<AgentUsageDTO[]>(statsUrl(workspaceId, "agent-usage", range), options);
return fetchJson<ModelUsageDTO[]>(statsUrl(workspaceId, "model-usage", range), options);
}
export function fetchRepositoryStats(
+3 -5
View File
@@ -411,10 +411,8 @@ export type CompletedTaskActivityDTO = {
completed_tasks: number;
};
export type AgentUsageDTO = {
agent_profile_id: string;
agent_profile_name: string;
agent_model: string;
export type ModelUsageDTO = {
model: string;
session_count: number;
turn_count: number;
total_duration_ms: number;
@@ -450,7 +448,7 @@ export type StatsResponse = {
task_stats: TaskStatsDTO[];
daily_activity: DailyActivityDTO[];
completed_activity: CompletedTaskActivityDTO[];
agent_usage: AgentUsageDTO[];
model_usage: ModelUsageDTO[];
repository_stats: RepositoryStatsDTO[];
git_stats: GitStatsDTO;
};
+2 -2
View File
@@ -35,11 +35,11 @@
"mostProductive": "Most Productive",
"mostTasks": "Most Tasks",
"mostTime": "Most Time",
"noAgentUsageDataYet": "No agent usage data yet.",
"noCompletedTaskDataYet": "No completed task data yet.",
"noCompletedTasksYet": "No completed tasks yet.",
"noDataYet": "No data yet.",
"noGitActivityYet": "No git activity yet.",
"noModelUsageDataYet": "No model usage data yet.",
"noRepositoryStatsYet": "No repository stats yet.",
"overview": "Overview",
"quickestTasks": "Quickest Tasks",
@@ -66,9 +66,9 @@
"timeSpent": "Time Spent",
"toolCalls": "Tool calls",
"toolShare": "Tool share",
"topAgents": "Top Agents",
"topByMessages": "Top By Messages",
"topByTurns": "Top By Turns",
"topModels": "Top Models",
"topRepositories": "Top Repositories",
"totalMessages": "Total messages",
"totalTurns": "Total turns",
+2 -2
View File
@@ -35,11 +35,11 @@
"mostProductive": "Ḿōśţ Ƥŕōďũćţĩvē",
"mostTasks": "Ḿōśţ Ţàśķś",
"mostTime": "Ḿōśţ Ţĩḿē",
"noAgentUsageDataYet": "Ńō àĝēńţ ũśàĝē ďàţà ŷēţ.",
"noCompletedTaskDataYet": "Ńō ćōḿƥĺēţēď ţàśķ ďàţà ŷēţ.",
"noCompletedTasksYet": "Ńō ćōḿƥĺēţēď ţàśķś ŷēţ.",
"noDataYet": "Ńō ďàţà ŷēţ.",
"noGitActivityYet": "Ńō ĝĩţ àćţĩvĩţŷ ŷēţ.",
"noModelUsageDataYet": "Ńō ḿōďēĺ ũśàĝē ďàţà ŷēţ.",
"noRepositoryStatsYet": "Ńō ŕēƥōśĩţōŕŷ śţàţś ŷēţ.",
"overview": "Ōvēŕvĩēŵ",
"quickestTasks": "Qũĩćķēśţ Ţàśķś",
@@ -66,9 +66,9 @@
"timeSpent": "Ţĩḿē Śƥēńţ",
"toolCalls": "Ţōōĺ ćàĺĺś",
"toolShare": "Ţōōĺ śĥàŕē",
"topAgents": "Ţōƥ Àĝēńţś",
"topByMessages": "Ţōƥ Ɓŷ Ḿēśśàĝēś",
"topByTurns": "Ţōƥ Ɓŷ Ţũŕńś",
"topModels": "Ţōƥ Ḿōďēĺś",
"topRepositories": "Ţōƥ Ŕēƥōśĩţōŕĩēś",
"totalMessages": "Ţōţàĺ ḿēśśàĝēś",
"totalTurns": "Ţōţàĺ ţũŕńś",
+2 -2
View File
@@ -35,11 +35,11 @@
"mostProductive": "Mais produtivo",
"mostTasks": "Mais tarefas",
"mostTime": "Mais tempo",
"noAgentUsageDataYet": "Ainda não há dados de utilização de agentes.",
"noCompletedTaskDataYet": "Ainda não há dados de tarefas concluídas.",
"noCompletedTasksYet": "Ainda não há tarefas concluídas.",
"noDataYet": "Ainda não há dados.",
"noGitActivityYet": "Ainda não há atividade do Git.",
"noModelUsageDataYet": "Ainda não há dados de utilização de modelos.",
"noRepositoryStatsYet": "Ainda não há estatísticas de repositórios.",
"overview": "Visão geral",
"quickestTasks": "Tarefas mais rápidas",
@@ -66,9 +66,9 @@
"timeSpent": "Tempo despendido",
"toolCalls": "Chamadas de ferramentas",
"toolShare": "Quota de ferramentas",
"topAgents": "Principais agentes",
"topByMessages": "Principais por mensagens",
"topByTurns": "Principais por turnos",
"topModels": "Modelos principais",
"topRepositories": "Principais repositórios",
"totalMessages": "Total de mensagens",
"totalTurns": "Total de turnos",
+2 -2
View File
@@ -35,11 +35,11 @@
"mostProductive": "最高产",
"mostTasks": "任务最多",
"mostTime": "耗时最多",
"noAgentUsageDataYet": "暂无代理使用数据。",
"noCompletedTaskDataYet": "暂无已完成任务数据。",
"noCompletedTasksYet": "暂无已完成任务。",
"noDataYet": "暂无数据。",
"noGitActivityYet": "暂无 Git 活动。",
"noModelUsageDataYet": "暂无模型使用数据。",
"noRepositoryStatsYet": "暂无仓库统计。",
"overview": "概览",
"quickestTasks": "最快的任务",
@@ -66,9 +66,9 @@
"timeSpent": "耗时",
"toolCalls": "工具调用",
"toolShare": "工具占比",
"topAgents": "热门代理",
"topByMessages": "按消息数",
"topByTurns": "按回合数",
"topModels": "热门模型",
"topRepositories": "热门仓库",
"totalMessages": "消息总数",
"totalTurns": "回合总数",
+3 -3
View File
@@ -17,9 +17,9 @@ Frontend Backend Database
├─ Overview cards └─ httpGetStats() ├─ tasks
├─ Heatmap ├─ GetGlobalStats() ├─ task_sessions
├─ Completed chart ├─ GetTaskStats() ├─ task_session_turns
├─ Agent usage ├─ GetDailyActivity() ├─ task_session_messages
├─ Model usage ├─ GetDailyActivity() ├─ task_session_messages
├─ Repo stats ├─ GetCompletedTaskActivity()├─ task_session_commits
└─ Workload ├─ GetAgentUsage() ├─ task_repositories
└─ Workload ├─ GetModelUsage() ├─ task_repositories
├─ GetRepositoryStats() ├─ repositories
└─ GetGitStats() └─ workflow_steps
```
@@ -67,7 +67,7 @@ The handler at `apps/backend/internal/analytics/handlers/stats_handlers.go` call
| Duration | `SUM((julianday(completed_at) - julianday(started_at)) * 86400000)` on turns |
| Tool calls | Messages with `type LIKE 'tool_%'` |
| User messages | Messages with `author_type = 'user'` |
| Agent name/model | `json_extract()` from `agent_profile_snapshot` on sessions |
| Model | `json_extract()` of `model` (then `model_name`, `llm`) from `agent_profile_snapshot` on sessions. Usage is grouped by this value rather than by agent profile: a profile gets renamed and repointed at other models, so its sessions carry several snapshots and no single one describes the group. |
| Git stats | Aggregated from `task_session_commits` (only in-session commits) |
### Time ranges