diff options
Diffstat (limited to 'internal/storage/db.go')
| -rw-r--r-- | internal/storage/db.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/storage/db.go b/internal/storage/db.go index 1aac754..b3df696 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -409,6 +409,61 @@ func (s *DB) DeleteTask(id string) error { return nil } +// RecentExecution is returned by ListRecentExecutions (JOIN with tasks for name). +type RecentExecution struct { + ID string `json:"id"` + TaskID string `json:"task_id"` + TaskName string `json:"task_name"` + State string `json:"state"` + StartedAt time.Time `json:"started_at"` + FinishedAt *time.Time `json:"finished_at,omitempty"` + DurationMS *int64 `json:"duration_ms,omitempty"` + ExitCode int `json:"exit_code"` + CostUSD float64 `json:"cost_usd"` + StdoutPath string `json:"stdout_path"` +} + +// ListRecentExecutions returns executions since the given time, joined with task names. +// If taskID is non-empty, only executions for that task are returned. +func (s *DB) ListRecentExecutions(since time.Time, limit int, taskID string) ([]*RecentExecution, error) { + query := `SELECT e.id, e.task_id, t.name, e.status, e.start_time, e.end_time, e.exit_code, e.cost_usd, e.stdout_path + FROM executions e + JOIN tasks t ON e.task_id = t.id + WHERE e.start_time >= ?` + args := []interface{}{since.UTC()} + + if taskID != "" { + query += " AND e.task_id = ?" + args = append(args, taskID) + } + query += " ORDER BY e.start_time DESC LIMIT ?" + args = append(args, limit) + + rows, err := s.db.Query(query, args...) + if err != nil { + return nil, err + } + defer rows.Close() + + var results []*RecentExecution + for rows.Next() { + var re RecentExecution + var endTime time.Time + var stdoutPath sql.NullString + if err := rows.Scan(&re.ID, &re.TaskID, &re.TaskName, &re.State, &re.StartedAt, &endTime, &re.ExitCode, &re.CostUSD, &stdoutPath); err != nil { + return nil, err + } + re.StdoutPath = stdoutPath.String + if !endTime.IsZero() { + re.FinishedAt = &endTime + ms := endTime.Sub(re.StartedAt).Milliseconds() + re.DurationMS = &ms + } + results = append(results, &re) + } + return results, rows.Err() +} + // UpdateTaskQuestion stores the pending question JSON on a task. // Pass empty string to clear the question after it has been answered. func (s *DB) UpdateTaskQuestion(taskID, questionJSON string) error { |
