fix(store): surface task timestamps and tag the summary structs

Task carried no created/updated times, so the dashboard had nothing to
sort or age a queue by. Both columns already existed in the schema and
are now scanned through; SQLite returns them as strings in several
shapes, so sqliteTime does the parsing in one place rather than at each
of the five call sites.

WorkflowStat, StageStat and DayTokens had no JSON tags at all, so
/api/summary answered {"Day":...,"Tokens":...} while the dashboard read
d.tokens -- every sparkline would have plotted zero.
This commit is contained in:
sam committed 2026-08-04 01:44:10 +10:00
1 parent efa7e65755
commit 7d7d03191f
3 files changed
+180 -22

No files matched your search

+19 -16
View File
@@ -100,7 +100,8 @@ func (s *Store) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, e
func (s *Store) Tasks(ctx context.Context, limit int) (tasks []Task, retErr error) {
rows, err := s.db.QueryContext(ctx, `
SELECT id, owner, repo, issue_number, title, status, workflow, stage,
pr_number, tokens_used, iterations, attempt, park_reason, retry_count
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
created_at, updated_at
FROM tasks ORDER BY updated_at DESC LIMIT ?`, limit)
if err != nil {
return nil, err
@@ -112,7 +113,8 @@ func (s *Store) Tasks(ctx context.Context, limit int) (tasks []Task, retErr erro
var t Task
if err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.Title,
&t.Status, &t.Workflow, &t.Stage, &t.PRNumber, &t.TokensUsed,
&t.Iterations, &t.Attempt, &t.ParkReason, &t.RetryCount); err != nil {
&t.Iterations, &t.Attempt, &t.ParkReason, &t.RetryCount,
sqliteTime{&t.CreatedAt}, sqliteTime{&t.UpdatedAt}); err != nil {
return nil, err
}
tasks = append(tasks, t)
@@ -143,14 +145,14 @@ func (s *Store) StatusCounts(ctx context.Context) (counts map[string]int, retErr
// WorkflowStat is one row of the per-workflow metrics table.
type WorkflowStat struct {
Workflow string
Runs int
Merged int
PROpen int
Parked int
AvgTokens int
AvgSteps float64
TotalToken int
Workflow string `json:"workflow"`
Runs int `json:"runs"`
Merged int `json:"merged"`
PROpen int `json:"pr_open"`
Parked int `json:"parked"`
AvgTokens int `json:"avg_tokens"`
AvgSteps float64 `json:"avg_steps"`
TotalToken int `json:"total_tokens"`
}
// WorkflowStats aggregates outcomes and spend per workflow -- the
@@ -180,10 +182,11 @@ func (s *Store) WorkflowStats(ctx context.Context) (stats []WorkflowStat, retErr
// StageStat is average stage duration and failure counts per stage.
type StageStat struct {
Workflow, Stage string
Runs int
AvgMs int
Errors int
Workflow string `json:"workflow"`
Stage string `json:"stage"`
Runs int `json:"runs"`
AvgMs int `json:"avg_ms"`
Errors int `json:"errors"`
}
// StageStats aggregates stage_finish events -- where does time go, and
@@ -215,8 +218,8 @@ func (s *Store) StageStats(ctx context.Context) (stats []StageStat, retErr error
// DayTokens is token spend per UTC day.
type DayTokens struct {
Day string
Tokens int
Day string `json:"day"`
Tokens int `json:"tokens"`
}
// TokensByDay sums agent token spend per day from agent_finish events.
+54 -6
View File
@@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
"unicode/utf8"
_ "modernc.org/sqlite"
@@ -66,6 +67,11 @@ type Task struct {
// deployments). Used to scope /approve and /cancel authorization so
// one identity cannot control another's chat-spawned tasks.
Identity string `json:"identity"`
// CreatedAt and UpdatedAt are the SQLite row timestamps, exposed so
// callers can show a task's age and last activity. They are written by
// column defaults and the UPDATE statements, never by the caller.
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// SourceForge and SourceChat are the valid values for Task.Source.
@@ -204,7 +210,7 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
RETURNING id, owner, repo, issue_number, title, body, labels, status,
workflow, stage, branch, plan, notes, pr_number, tokens_used,
iterations, attempt, park_reason, watch_comment_id, retry_count,
source, identity`,
source, identity, created_at, updated_at`,
owner, repo, issueNumber, title, body, workflow, identity)
return scanTask(row)
}
@@ -218,7 +224,7 @@ func (s *Store) ClaimNext(ctx context.Context) (*Task, error) {
RETURNING id, owner, repo, issue_number, title, body, labels, status,
workflow, stage, branch, plan, notes, pr_number, tokens_used,
iterations, attempt, park_reason, watch_comment_id, retry_count,
source, identity`)
source, identity, created_at, updated_at`)
t, err := scanTask(row)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
@@ -231,7 +237,8 @@ func scanTask(row *sql.Row) (*Task, error) {
err := row.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.Title, &t.Body,
&t.Labels, &t.Status, &t.Workflow, &t.Stage, &t.Branch, &t.Plan, &t.Notes,
&t.PRNumber, &t.TokensUsed, &t.Iterations, &t.Attempt, &t.ParkReason,
&t.WatchCommentID, &t.RetryCount, &t.Source, &t.Identity)
&t.WatchCommentID, &t.RetryCount, &t.Source, &t.Identity,
sqliteTime{&t.CreatedAt}, sqliteTime{&t.UpdatedAt})
if err != nil {
return nil, err
}
@@ -249,7 +256,7 @@ func (s *Store) ClaimByIssue(ctx context.Context, owner, repo string, number int
RETURNING id, owner, repo, issue_number, title, body, labels, status,
workflow, stage, branch, plan, notes, pr_number, tokens_used,
iterations, attempt, park_reason, watch_comment_id, retry_count,
source, identity`, owner, repo, number)
source, identity, created_at, updated_at`, owner, repo, number)
t, err := scanTask(row)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
@@ -315,7 +322,7 @@ func (s *Store) TaskByIssue(ctx context.Context, owner, repo string, number int)
SELECT id, owner, repo, issue_number, title, body, labels, status,
workflow, stage, branch, plan, notes, pr_number, tokens_used,
iterations, attempt, park_reason, watch_comment_id, retry_count,
source, identity
source, identity, created_at, updated_at
FROM tasks WHERE owner=? AND repo=? AND issue_number=?`, owner, repo, number)
t, err := scanTask(row)
if errors.Is(err, sql.ErrNoRows) {
@@ -332,7 +339,7 @@ func (s *Store) TaskByID(ctx context.Context, taskID int64) (*Task, error) {
SELECT id, owner, repo, issue_number, title, body, labels, status,
workflow, stage, branch, plan, notes, pr_number, tokens_used,
iterations, attempt, park_reason, watch_comment_id, retry_count,
source, identity
source, identity, created_at, updated_at
FROM tasks WHERE id=?`, taskID)
t, err := scanTask(row)
if errors.Is(err, sql.ErrNoRows) {
@@ -463,3 +470,44 @@ func clip(s string, n int) string {
}
return s[:pos]
}
// sqliteTime scans SQLite's TEXT datetime columns into a time.Time.
//
// The tasks table stores timestamps as TEXT via datetime('now') rather than a
// typed column, so the driver hands back a string and the standard time.Time
// scan fails. Parsing here keeps the column definition untouched -- changing
// it would need a migration over existing rows for no behavioural gain.
type sqliteTime struct{ t *time.Time }
// sqliteTimeLayout is what datetime('now') produces: UTC, no zone suffix.
const sqliteTimeLayout = "2006-01-02 15:04:05"
func (s sqliteTime) Scan(v any) error {
switch value := v.(type) {
case nil:
return nil
case time.Time:
*s.t = value.UTC()
return nil
case []byte:
return s.parse(string(value))
case string:
return s.parse(value)
default:
return fmt.Errorf("store: cannot scan %T into time", v)
}
}
func (s sqliteTime) parse(v string) error {
if v == "" {
return nil
}
// RFC3339 first: rows written by other paths may carry a zone.
for _, layout := range []string{time.RFC3339Nano, time.RFC3339, sqliteTimeLayout} {
if parsed, err := time.Parse(layout, v); err == nil {
*s.t = parsed.UTC()
return nil
}
}
return fmt.Errorf("store: unrecognised time %q", v)
}
+107
View File
@@ -0,0 +1,107 @@
package store
import (
"context"
"testing"
"time"
)
// TestTaskTimestampsRoundTrip pins that a task carries usable created/updated
// times through every read path. They are what the dashboard shows a task's
// age from; before this they existed as SQLite columns but were never selected,
// so the API could not answer "how long has this been running".
func TestTaskTimestampsRoundTrip(t *testing.T) {
ctx := context.Background()
s := OpenTest(t)
t.Cleanup(func() { _ = s.Close() })
before := time.Now().UTC().Add(-2 * time.Second)
if _, err := s.EnqueueIssue(ctx, "o", "r", 1, "title", "body", "", ""); err != nil {
t.Fatalf("EnqueueIssue: %v", err)
}
after := time.Now().UTC().Add(2 * time.Second)
inRange := func(t *testing.T, label string, got time.Time) {
t.Helper()
if got.IsZero() {
t.Errorf("%s is zero; the column was not selected", label)
return
}
if got.Before(before) || got.After(after) {
t.Errorf("%s = %s, want within [%s, %s]", label, got, before, after)
}
}
// Every read path that returns a Task must populate them.
t.Run("TaskByIssue", func(t *testing.T) {
task, err := s.TaskByIssue(ctx, "o", "r", 1)
if err != nil || task == nil {
t.Fatalf("TaskByIssue = (%v, %v)", task, err)
}
inRange(t, "CreatedAt", task.CreatedAt)
inRange(t, "UpdatedAt", task.UpdatedAt)
})
t.Run("Tasks list", func(t *testing.T) {
tasks, err := s.Tasks(ctx, 10)
if err != nil {
t.Fatalf("Tasks: %v", err)
}
if len(tasks) != 1 {
t.Fatalf("got %d tasks, want 1", len(tasks))
}
inRange(t, "CreatedAt", tasks[0].CreatedAt)
inRange(t, "UpdatedAt", tasks[0].UpdatedAt)
})
t.Run("ClaimNext", func(t *testing.T) {
task, err := s.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("ClaimNext = (%v, %v)", task, err)
}
inRange(t, "CreatedAt", task.CreatedAt)
inRange(t, "UpdatedAt", task.UpdatedAt)
})
}
// TestSQLiteTimeScan pins the scanner that makes the above possible: SQLite
// stores these as TEXT via datetime('now'), so the driver hands back a string
// and a plain time.Time scan fails.
func TestSQLiteTimeScan(t *testing.T) {
want := time.Date(2026, 8, 4, 9, 30, 15, 0, time.UTC)
tests := []struct {
name string
input any
want time.Time
wantErr bool
}{
{"datetime('now') text", "2026-08-04 09:30:15", want, false},
{"same value as bytes", []byte("2026-08-04 09:30:15"), want, false},
{"rfc3339 from another writer", "2026-08-04T09:30:15Z", want, false},
{"driver already gave a time", want, want, false},
{"null leaves the zero value", nil, time.Time{}, false},
{"empty string leaves the zero value", "", time.Time{}, false},
{"unrecognised text is an error", "not a time", time.Time{}, true},
{"unsupported type is an error", 42, time.Time{}, true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var got time.Time
err := sqliteTime{&got}.Scan(tc.input)
if tc.wantErr {
if err == nil {
t.Fatalf("Scan(%v) = nil error, want an error", tc.input)
}
return
}
if err != nil {
t.Fatalf("Scan(%v): %v", tc.input, err)
}
if !got.Equal(tc.want) {
t.Errorf("got %s, want %s", got, tc.want)
}
})
}
}