diff --git a/apps/backend/internal/analytics/dto/dto.go b/apps/backend/internal/analytics/dto/dto.go index 5d85134690..8fa0d29936 100644 --- a/apps/backend/internal/analytics/dto/dto.go +++ b/apps/backend/internal/analytics/dto/dto.go @@ -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"` } diff --git a/apps/backend/internal/analytics/handlers/stats_handlers.go b/apps/backend/internal/analytics/handlers/stats_handlers.go index 42ed510c75..f704b1c8c8 100644 --- a/apps/backend/internal/analytics/handlers/stats_handlers.go +++ b/apps/backend/internal/analytics/handlers/stats_handlers.go @@ -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 diff --git a/apps/backend/internal/analytics/models/models.go b/apps/backend/internal/analytics/models/models.go index b5c04056a4..459236e0d4 100644 --- a/apps/backend/internal/analytics/models/models.go +++ b/apps/backend/internal/analytics/models/models.go @@ -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 diff --git a/apps/backend/internal/analytics/repository/interface.go b/apps/backend/internal/analytics/repository/interface.go index 7681b0c22d..2fe0cc6012 100644 --- a/apps/backend/internal/analytics/repository/interface.go +++ b/apps/backend/internal/analytics/repository/interface.go @@ -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) diff --git a/apps/backend/internal/analytics/repository/sqlite/repository_test.go b/apps/backend/internal/analytics/repository/sqlite/repository_test.go index a90e85ddc2..a227922e9b 100644 --- a/apps/backend/internal/analytics/repository/sqlite/repository_test.go +++ b/apps/backend/internal/analytics/repository/sqlite/repository_test.go @@ -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) diff --git a/apps/backend/internal/analytics/repository/sqlite/stats.go b/apps/backend/internal/analytics/repository/sqlite/stats.go index abe63883dd..16af7baef2 100644 --- a/apps/backend/internal/analytics/repository/sqlite/stats.go +++ b/apps/backend/internal/analytics/repository/sqlite/stats.go @@ -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 diff --git a/apps/backend/internal/analytics/service/service_test.go b/apps/backend/internal/analytics/service/service_test.go index cbda806c3e..cb9b664e70 100644 --- a/apps/backend/internal/analytics/service/service_test.go +++ b/apps/backend/internal/analytics/service/service_test.go @@ -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 } diff --git a/apps/web/app/stats/stats-charts.tsx b/apps/web/app/stats/stats-charts.tsx index 855a680596..10a3286610 100644 --- a/apps/web/app/stats/stats-charts.tsx +++ b/apps/web/app/stats/stats-charts.tsx @@ -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 ( -