fix(postgres): tasks, events, steps and run stats stay in their org
This commit is contained in:
14 files changed
+230
-97
No files matched your search
@@ -680,29 +680,30 @@ func (q *Queries) InsertSource(ctx context.Context, arg InsertSourceParams) erro
|
||||
}
|
||||
|
||||
const insertToolCall = `-- name: InsertToolCall :exec
|
||||
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at, org_id, workspace_id)
|
||||
SELECT $1, t.id, $2, $3, $4, $5, $6, t.org_id, t.workspace_id
|
||||
FROM tasks t WHERE t.id = $7
|
||||
`
|
||||
|
||||
type InsertToolCallParams struct {
|
||||
ID string
|
||||
TaskID int64
|
||||
Attempt int64
|
||||
Tool string
|
||||
Result string
|
||||
Error string
|
||||
CalledAt time.Time
|
||||
TaskID int64
|
||||
}
|
||||
|
||||
func (q *Queries) InsertToolCall(ctx context.Context, arg InsertToolCallParams) error {
|
||||
_, err := q.db.Exec(ctx, insertToolCall,
|
||||
arg.ID,
|
||||
arg.TaskID,
|
||||
arg.Attempt,
|
||||
arg.Tool,
|
||||
arg.Result,
|
||||
arg.Error,
|
||||
arg.CalledAt,
|
||||
arg.TaskID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -1177,9 +1178,14 @@ func (q *Queries) SourceExists(ctx context.Context, path string) (bool, error) {
|
||||
|
||||
const taskToolCalls = `-- name: TaskToolCalls :many
|
||||
SELECT id, task_id, attempt, tool, result, error, called_at
|
||||
FROM tool_calls WHERE task_id = $1 ORDER BY called_at ASC
|
||||
FROM tool_calls WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2) ORDER BY called_at ASC
|
||||
`
|
||||
|
||||
type TaskToolCallsParams struct {
|
||||
TaskID int64
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
type TaskToolCallsRow struct {
|
||||
ID string
|
||||
TaskID int64
|
||||
@@ -1190,8 +1196,8 @@ type TaskToolCallsRow struct {
|
||||
CalledAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) TaskToolCalls(ctx context.Context, taskID int64) ([]TaskToolCallsRow, error) {
|
||||
rows, err := q.db.Query(ctx, taskToolCalls, taskID)
|
||||
func (q *Queries) TaskToolCalls(ctx context.Context, arg TaskToolCallsParams) ([]TaskToolCallsRow, error) {
|
||||
rows, err := q.db.Query(ctx, taskToolCalls, arg.TaskID, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ package postgresdb
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const insertEvent = `-- name: InsertEvent :one
|
||||
@@ -15,9 +17,11 @@ const insertEvent = `-- name: InsertEvent :one
|
||||
WITH append_lock AS (
|
||||
SELECT pg_advisory_xact_lock(hashtextextended('archie.events.insert', 0))
|
||||
)
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
|
||||
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13
|
||||
FROM append_lock
|
||||
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id)
|
||||
SELECT $1, $2, $3, $4, $5, $6, $7,
|
||||
$8, $9, $10, $11, $12, $13,
|
||||
COALESCE(t.org_id, $14::text), COALESCE(t.workspace_id, 'default')
|
||||
FROM append_lock LEFT JOIN tasks t ON t.id = $3
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
@@ -35,6 +39,7 @@ type InsertEventParams struct {
|
||||
PrincipalID string
|
||||
Detail string
|
||||
Data string
|
||||
OrgID string
|
||||
}
|
||||
|
||||
// Event-log queries. The (at, id) cursor is a wire contract: at is the
|
||||
@@ -60,6 +65,7 @@ func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) (int64
|
||||
arg.PrincipalID,
|
||||
arg.Detail,
|
||||
arg.Data,
|
||||
arg.OrgID,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
@@ -69,19 +75,25 @@ func (q *Queries) InsertEvent(ctx context.Context, arg InsertEventParams) (int64
|
||||
const listEventsAfter = `-- name: ListEventsAfter :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE at > $1 OR (at = $1 AND id > $2)
|
||||
WHERE (at > $1 OR (at = $1 AND id > $2)) AND ($3::text IS NULL OR org_id = $3)
|
||||
ORDER BY at, id
|
||||
LIMIT $3
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type ListEventsAfterParams struct {
|
||||
At time.Time
|
||||
ID int64
|
||||
Limit int32
|
||||
At time.Time
|
||||
ID int64
|
||||
ScopeOrg pgtype.Text
|
||||
PageLimit int32
|
||||
}
|
||||
|
||||
func (q *Queries) ListEventsAfter(ctx context.Context, arg ListEventsAfterParams) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, listEventsAfter, arg.At, arg.ID, arg.Limit)
|
||||
rows, err := q.db.Query(ctx, listEventsAfter,
|
||||
arg.At,
|
||||
arg.ID,
|
||||
arg.ScopeOrg,
|
||||
arg.PageLimit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -120,12 +132,18 @@ func (q *Queries) ListEventsAfter(ctx context.Context, arg ListEventsAfterParams
|
||||
const listEventsFromBeginning = `-- name: ListEventsFromBeginning :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE ($1::text IS NULL OR org_id = $1)
|
||||
ORDER BY at, id
|
||||
LIMIT $1
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
func (q *Queries) ListEventsFromBeginning(ctx context.Context, limit int32) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, listEventsFromBeginning, limit)
|
||||
type ListEventsFromBeginningParams struct {
|
||||
ScopeOrg pgtype.Text
|
||||
PageLimit int32
|
||||
}
|
||||
|
||||
func (q *Queries) ListEventsFromBeginning(ctx context.Context, arg ListEventsFromBeginningParams) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, listEventsFromBeginning, arg.ScopeOrg, arg.PageLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -164,10 +182,10 @@ func (q *Queries) ListEventsFromBeginning(ctx context.Context, limit int32) ([]E
|
||||
const stageStats = `-- name: StageStats :many
|
||||
SELECT workflow, stage,
|
||||
COUNT(*)::int AS runs,
|
||||
CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint) AS avg_ms,
|
||||
COALESCE(CAST(AVG((data::jsonb ->> 'duration_ms')::numeric) AS bigint), 0)::bigint AS avg_ms,
|
||||
COUNT(*) FILTER (WHERE (data::jsonb ->> 'error') IS NOT NULL)::int AS errors
|
||||
FROM events
|
||||
WHERE kind = 'stage_finish'
|
||||
WHERE kind = 'stage_finish' AND ($1::text IS NULL OR org_id = $1)
|
||||
GROUP BY workflow, stage
|
||||
ORDER BY workflow, stage
|
||||
`
|
||||
@@ -180,8 +198,8 @@ type StageStatsRow struct {
|
||||
Errors int32
|
||||
}
|
||||
|
||||
func (q *Queries) StageStats(ctx context.Context) ([]StageStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, stageStats)
|
||||
func (q *Queries) StageStats(ctx context.Context, scopeOrg pgtype.Text) ([]StageStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, stageStats, scopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -209,12 +227,17 @@ func (q *Queries) StageStats(ctx context.Context) ([]StageStatsRow, error) {
|
||||
const taskEventsByID = `-- name: TaskEventsByID :many
|
||||
SELECT id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data, org_id, workspace_id
|
||||
FROM events
|
||||
WHERE task_id = $1
|
||||
WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2)
|
||||
ORDER BY id
|
||||
`
|
||||
|
||||
func (q *Queries) TaskEventsByID(ctx context.Context, taskID int64) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, taskEventsByID, taskID)
|
||||
type TaskEventsByIDParams struct {
|
||||
TaskID int64
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) TaskEventsByID(ctx context.Context, arg TaskEventsByIDParams) ([]Event, error) {
|
||||
rows, err := q.db.Query(ctx, taskEventsByID, arg.TaskID, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -256,14 +279,14 @@ WITH event_totals AS (
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number'
|
||||
THEN (data::jsonb ->> 'tokens')::bigint ELSE 0 END) AS tokens,
|
||||
SUM(CASE WHEN jsonb_typeof(data::jsonb -> 'tokens') = 'number' THEN 1 ELSE 0 END) AS valid_tokens
|
||||
FROM events WHERE kind = 'agent_finish'
|
||||
FROM events WHERE kind = 'agent_finish' AND ($2::text IS NULL OR org_id = $2)
|
||||
GROUP BY task_id, day
|
||||
), usage AS (
|
||||
SELECT day, tokens FROM event_totals WHERE valid_tokens > 0
|
||||
UNION ALL
|
||||
SELECT to_char(t.updated_at AT TIME ZONE 'UTC', 'YYYY-MM-DD'), t.tokens_used
|
||||
FROM tasks t
|
||||
WHERE t.tokens_used != 0 AND NOT EXISTS (
|
||||
WHERE t.tokens_used != 0 AND ($2::text IS NULL OR t.org_id = $2) AND NOT EXISTS (
|
||||
SELECT 1 FROM event_totals e
|
||||
WHERE e.task_id = t.id AND e.valid_tokens > 0
|
||||
)
|
||||
@@ -272,13 +295,18 @@ SELECT day, SUM(tokens)::bigint AS tokens
|
||||
FROM usage GROUP BY day ORDER BY day DESC LIMIT $1
|
||||
`
|
||||
|
||||
type TokensByDayParams struct {
|
||||
Days int32
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
type TokensByDayRow struct {
|
||||
Day string
|
||||
Tokens int64
|
||||
}
|
||||
|
||||
func (q *Queries) TokensByDay(ctx context.Context, limit int32) ([]TokensByDayRow, error) {
|
||||
rows, err := q.db.Query(ctx, tokensByDay, limit)
|
||||
func (q *Queries) TokensByDay(ctx context.Context, arg TokensByDayParams) ([]TokensByDayRow, error) {
|
||||
rows, err := q.db.Query(ctx, tokensByDay, arg.Days, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -308,7 +336,7 @@ SELECT workflow,
|
||||
AVG(iterations)::float8 AS avg_steps,
|
||||
SUM(tokens_used)::bigint AS total_tokens
|
||||
FROM tasks
|
||||
WHERE workflow <> ''
|
||||
WHERE workflow <> '' AND ($1::text IS NULL OR org_id = $1)
|
||||
GROUP BY workflow
|
||||
ORDER BY COUNT(*) DESC
|
||||
`
|
||||
@@ -325,8 +353,8 @@ type WorkflowStatsRow struct {
|
||||
TotalTokens int64
|
||||
}
|
||||
|
||||
func (q *Queries) WorkflowStats(ctx context.Context) ([]WorkflowStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, workflowStats)
|
||||
func (q *Queries) WorkflowStats(ctx context.Context, scopeOrg pgtype.Text) ([]WorkflowStatsRow, error) {
|
||||
rows, err := q.db.Query(ctx, workflowStats, scopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -192,12 +192,14 @@ const listStepExecutions = `-- name: ListStepExecutions :many
|
||||
SELECT id, execution_id, attempt, parent_id, depth, kind, name, status, detail, tokens_used, started_at, finished_at, called_execution_id
|
||||
FROM step_executions
|
||||
WHERE execution_id = $1::bigint AND ($2::bigint = 0 OR attempt = $2::bigint)
|
||||
AND ($3::text IS NULL OR org_id = $3)
|
||||
ORDER BY attempt, id
|
||||
`
|
||||
|
||||
type ListStepExecutionsParams struct {
|
||||
ExecutionID int64
|
||||
Attempt int64
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
type ListStepExecutionsRow struct {
|
||||
@@ -220,7 +222,7 @@ type ListStepExecutionsRow struct {
|
||||
// (execution_id, attempt, id) makes both this and the single-attempt form a
|
||||
// straight index scan.
|
||||
func (q *Queries) ListStepExecutions(ctx context.Context, arg ListStepExecutionsParams) ([]ListStepExecutionsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listStepExecutions, arg.ExecutionID, arg.Attempt)
|
||||
rows, err := q.db.Query(ctx, listStepExecutions, arg.ExecutionID, arg.Attempt, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -14,13 +14,18 @@ import (
|
||||
|
||||
const activeTasksByOrigin = `-- name: ActiveTasksByOrigin :many
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks
|
||||
WHERE origin = $1 AND origin <> '' AND status IN ('queued', 'running')
|
||||
WHERE origin = $1 AND origin <> '' AND status IN ('queued', 'running') AND ($2::text IS NULL OR org_id = $2)
|
||||
ORDER BY id
|
||||
`
|
||||
|
||||
type ActiveTasksByOriginParams struct {
|
||||
Origin string
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
// The queued and running tasks one conversation created, for its /stop.
|
||||
func (q *Queries) ActiveTasksByOrigin(ctx context.Context, origin string) ([]Task, error) {
|
||||
rows, err := q.db.Query(ctx, activeTasksByOrigin, origin)
|
||||
func (q *Queries) ActiveTasksByOrigin(ctx context.Context, arg ActiveTasksByOriginParams) ([]Task, error) {
|
||||
rows, err := q.db.Query(ctx, activeTasksByOrigin, arg.Origin, arg.ScopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -306,7 +311,7 @@ func (q *Queries) ClearTerminalTasks(ctx context.Context) (int64, error) {
|
||||
}
|
||||
|
||||
const countTasksByStatus = `-- name: CountTasksByStatus :many
|
||||
SELECT status, count(*)::int AS count FROM tasks GROUP BY status
|
||||
SELECT status, count(*)::int AS count FROM tasks WHERE ($1::text IS NULL OR org_id = $1) GROUP BY status
|
||||
`
|
||||
|
||||
type CountTasksByStatusRow struct {
|
||||
@@ -314,8 +319,8 @@ type CountTasksByStatusRow struct {
|
||||
Count int32
|
||||
}
|
||||
|
||||
func (q *Queries) CountTasksByStatus(ctx context.Context) ([]CountTasksByStatusRow, error) {
|
||||
rows, err := q.db.Query(ctx, countTasksByStatus)
|
||||
func (q *Queries) CountTasksByStatus(ctx context.Context, scopeOrg pgtype.Text) ([]CountTasksByStatusRow, error) {
|
||||
rows, err := q.db.Query(ctx, countTasksByStatus, scopeOrg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -486,23 +491,24 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
|
||||
}
|
||||
|
||||
const insertTransition = `-- name: InsertTransition :exec
|
||||
INSERT INTO transitions (task_id, from_status, to_status, detail)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO transitions (task_id, from_status, to_status, detail, org_id, workspace_id)
|
||||
SELECT t.id, $1, $2, $3, t.org_id, t.workspace_id
|
||||
FROM tasks t WHERE t.id = $4
|
||||
`
|
||||
|
||||
type InsertTransitionParams struct {
|
||||
TaskID int64
|
||||
FromStatus string
|
||||
ToStatus string
|
||||
Detail string
|
||||
TaskID int64
|
||||
}
|
||||
|
||||
func (q *Queries) InsertTransition(ctx context.Context, arg InsertTransitionParams) error {
|
||||
_, err := q.db.Exec(ctx, insertTransition,
|
||||
arg.TaskID,
|
||||
arg.FromStatus,
|
||||
arg.ToStatus,
|
||||
arg.Detail,
|
||||
arg.TaskID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -566,14 +572,16 @@ SELECT id, owner, repo, issue_number, title, status, workflow,
|
||||
created_at, updated_at, plan, source, identity, binding_id, binding_version,
|
||||
outputs, review_gate, rereview_rounds
|
||||
FROM tasks
|
||||
WHERE (cardinality($1::text[]) = 0 OR status = ANY($1::text[]))
|
||||
AND ($2::timestamptz IS NULL
|
||||
OR (updated_at, id) < ($2::timestamptz, $3::bigint))
|
||||
WHERE ($1::text IS NULL OR org_id = $1)
|
||||
AND (cardinality($2::text[]) = 0 OR status = ANY($2::text[]))
|
||||
AND ($3::timestamptz IS NULL
|
||||
OR (updated_at, id) < ($3::timestamptz, $4::bigint))
|
||||
ORDER BY updated_at DESC, id DESC
|
||||
LIMIT $4
|
||||
LIMIT $5
|
||||
`
|
||||
|
||||
type ListTaskSummariesParams struct {
|
||||
ScopeOrg pgtype.Text
|
||||
Statuses []string
|
||||
BeforeUpdated pgtype.Timestamptz
|
||||
BeforeID int64
|
||||
@@ -617,6 +625,7 @@ type ListTaskSummariesRow struct {
|
||||
// answers"), and the round count is the cap's visible half.
|
||||
func (q *Queries) ListTaskSummaries(ctx context.Context, arg ListTaskSummariesParams) ([]ListTaskSummariesRow, error) {
|
||||
rows, err := q.db.Query(ctx, listTaskSummaries,
|
||||
arg.ScopeOrg,
|
||||
arg.Statuses,
|
||||
arg.BeforeUpdated,
|
||||
arg.BeforeID,
|
||||
@@ -919,11 +928,16 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
|
||||
}
|
||||
|
||||
const taskByID = `-- name: TaskByID :one
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks WHERE id = $1
|
||||
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
||||
`
|
||||
|
||||
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
|
||||
row := q.db.QueryRow(ctx, taskByID, id)
|
||||
type TaskByIDParams struct {
|
||||
ID int64
|
||||
ScopeOrg pgtype.Text
|
||||
}
|
||||
|
||||
func (q *Queries) TaskByID(ctx context.Context, arg TaskByIDParams) (Task, error) {
|
||||
row := q.db.QueryRow(ctx, taskByID, arg.ID, arg.ScopeOrg)
|
||||
var i Task
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
|
||||
Reference in new issue
Block a user