-
Notifications
You must be signed in to change notification settings - Fork 123
/
workers.sql.go
97 lines (90 loc) · 2.62 KB
/
workers.sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.24.0
// source: workers.sql
package dbsqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const listWorkersWithStepCount = `-- name: ListWorkersWithStepCount :many
SELECT
workers.id, workers."createdAt", workers."updatedAt", workers."deletedAt", workers."tenantId", workers."lastHeartbeatAt", workers.name, workers.status, workers."dispatcherId", workers."maxRuns",
COUNT(runs."id") FILTER (WHERE runs."status" = 'RUNNING') AS "runningStepRuns"
FROM
"Worker" workers
LEFT JOIN
"StepRun" AS runs ON runs."workerId" = workers."id" AND runs."status" = 'RUNNING'
WHERE
workers."tenantId" = $1
AND (
$2::text IS NULL OR
workers."id" IN (
SELECT "_ActionToWorker"."B"
FROM "_ActionToWorker"
INNER JOIN "Action" ON "Action"."id" = "_ActionToWorker"."A"
WHERE "Action"."tenantId" = $1 AND "Action"."actionId" = $2::text
)
)
AND (
$3::timestamp IS NULL OR
workers."lastHeartbeatAt" > $3::timestamp
)
AND (
$4::boolean IS NULL OR
workers."maxRuns" IS NULL OR
($4::boolean AND workers."maxRuns" > (
SELECT COUNT(*)
FROM "StepRun"
WHERE runs."workerId" = workers."id" AND runs."status" = 'RUNNING'
))
)
GROUP BY
workers."id"
`
type ListWorkersWithStepCountParams struct {
Tenantid pgtype.UUID `json:"tenantid"`
ActionId pgtype.Text `json:"actionId"`
LastHeartbeatAfter pgtype.Timestamp `json:"lastHeartbeatAfter"`
Assignable pgtype.Bool `json:"assignable"`
}
type ListWorkersWithStepCountRow struct {
Worker Worker `json:"worker"`
RunningStepRuns int64 `json:"runningStepRuns"`
}
func (q *Queries) ListWorkersWithStepCount(ctx context.Context, db DBTX, arg ListWorkersWithStepCountParams) ([]*ListWorkersWithStepCountRow, error) {
rows, err := db.Query(ctx, listWorkersWithStepCount,
arg.Tenantid,
arg.ActionId,
arg.LastHeartbeatAfter,
arg.Assignable,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*ListWorkersWithStepCountRow
for rows.Next() {
var i ListWorkersWithStepCountRow
if err := rows.Scan(
&i.Worker.ID,
&i.Worker.CreatedAt,
&i.Worker.UpdatedAt,
&i.Worker.DeletedAt,
&i.Worker.TenantId,
&i.Worker.LastHeartbeatAt,
&i.Worker.Name,
&i.Worker.Status,
&i.Worker.DispatcherId,
&i.Worker.MaxRuns,
&i.RunningStepRuns,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}