-
Notifications
You must be signed in to change notification settings - Fork 157
/
events.sql.go
302 lines (283 loc) · 7.43 KB
/
events.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.24.0
// source: events.sql
package dbsqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const countEvents = `-- name: CountEvents :one
SELECT
count(*) OVER() AS total
FROM
"Event" as events
LEFT JOIN
"WorkflowRunTriggeredBy" as runTriggers ON events."id" = runTriggers."eventId"
LEFT JOIN
"WorkflowRun" as runs ON runTriggers."parentId" = runs."id"
LEFT JOIN
"WorkflowVersion" as workflowVersion ON workflowVersion."id" = runs."workflowVersionId"
LEFT JOIN
"Workflow" as workflow ON workflowVersion."workflowId" = workflow."id"
WHERE
events."tenantId" = $1 AND
(
$2::text[] IS NULL OR
events."key" = ANY($2::text[])
) AND
(
($3::text[])::uuid[] IS NULL OR
(workflow."id" = ANY($3::text[]::uuid[]))
) AND
(
$4::text IS NULL OR
jsonb_path_exists(events."data", cast(concat('$.** ? (@.type() == "string" && @ like_regex "', $4::text, '")') as jsonpath))
) AND
(
$5::text[] IS NULL OR
"status" = ANY(cast($5::text[] as "WorkflowRunStatus"[]))
)
`
type CountEventsParams struct {
TenantId pgtype.UUID `json:"tenantId"`
Keys []string `json:"keys"`
Workflows []string `json:"workflows"`
Search pgtype.Text `json:"search"`
Statuses []string `json:"statuses"`
}
func (q *Queries) CountEvents(ctx context.Context, db DBTX, arg CountEventsParams) (int64, error) {
row := db.QueryRow(ctx, countEvents,
arg.TenantId,
arg.Keys,
arg.Workflows,
arg.Search,
arg.Statuses,
)
var total int64
err := row.Scan(&total)
return total, err
}
const createEvent = `-- name: CreateEvent :one
INSERT INTO "Event" (
"id",
"createdAt",
"updatedAt",
"deletedAt",
"key",
"tenantId",
"replayedFromId",
"data"
) VALUES (
$1::uuid,
coalesce($2::timestamp, CURRENT_TIMESTAMP),
coalesce($3::timestamp, CURRENT_TIMESTAMP),
$4::timestamp,
$5::text,
$6::uuid,
$7::uuid,
$8::jsonb
) RETURNING id, "createdAt", "updatedAt", "deletedAt", key, "tenantId", "replayedFromId", data
`
type CreateEventParams struct {
ID pgtype.UUID `json:"id"`
CreatedAt pgtype.Timestamp `json:"createdAt"`
UpdatedAt pgtype.Timestamp `json:"updatedAt"`
Deletedat pgtype.Timestamp `json:"deletedat"`
Key string `json:"key"`
Tenantid pgtype.UUID `json:"tenantid"`
ReplayedFromId pgtype.UUID `json:"replayedFromId"`
Data []byte `json:"data"`
}
func (q *Queries) CreateEvent(ctx context.Context, db DBTX, arg CreateEventParams) (*Event, error) {
row := db.QueryRow(ctx, createEvent,
arg.ID,
arg.CreatedAt,
arg.UpdatedAt,
arg.Deletedat,
arg.Key,
arg.Tenantid,
arg.ReplayedFromId,
arg.Data,
)
var i Event
err := row.Scan(
&i.ID,
&i.CreatedAt,
&i.UpdatedAt,
&i.DeletedAt,
&i.Key,
&i.TenantId,
&i.ReplayedFromId,
&i.Data,
)
return &i, err
}
const getEventForEngine = `-- name: GetEventForEngine :one
SELECT
"id",
"key",
"data",
"tenantId"
FROM
"Event"
WHERE
"id" = $1::uuid
`
type GetEventForEngineRow struct {
ID pgtype.UUID `json:"id"`
Key string `json:"key"`
Data []byte `json:"data"`
TenantId pgtype.UUID `json:"tenantId"`
}
func (q *Queries) GetEventForEngine(ctx context.Context, db DBTX, id pgtype.UUID) (*GetEventForEngineRow, error) {
row := db.QueryRow(ctx, getEventForEngine, id)
var i GetEventForEngineRow
err := row.Scan(
&i.ID,
&i.Key,
&i.Data,
&i.TenantId,
)
return &i, err
}
const getEventsForRange = `-- name: GetEventsForRange :many
SELECT
date_trunc('hour', "createdAt") AS event_hour,
COUNT(*) AS event_count
FROM
"Event"
WHERE
"createdAt" >= NOW() - INTERVAL '1 week'
GROUP BY
event_hour
ORDER BY
event_hour
`
type GetEventsForRangeRow struct {
EventHour pgtype.Interval `json:"event_hour"`
EventCount int64 `json:"event_count"`
}
func (q *Queries) GetEventsForRange(ctx context.Context, db DBTX) ([]*GetEventsForRangeRow, error) {
rows, err := db.Query(ctx, getEventsForRange)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetEventsForRangeRow
for rows.Next() {
var i GetEventsForRangeRow
if err := rows.Scan(&i.EventHour, &i.EventCount); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listEvents = `-- name: ListEvents :many
SELECT
events.id, events."createdAt", events."updatedAt", events."deletedAt", events.key, events."tenantId", events."replayedFromId", events.data,
sum(case when runs."status" = 'PENDING' OR runs."status" = 'QUEUED' then 1 else 0 end) AS pendingRuns,
sum(case when runs."status" = 'RUNNING' then 1 else 0 end) AS runningRuns,
sum(case when runs."status" = 'SUCCEEDED' then 1 else 0 end) AS succeededRuns,
sum(case when runs."status" = 'FAILED' then 1 else 0 end) AS failedRuns
FROM
"Event" as events
LEFT JOIN
"WorkflowRunTriggeredBy" as runTriggers ON events."id" = runTriggers."eventId"
LEFT JOIN
"WorkflowRun" as runs ON runTriggers."parentId" = runs."id"
LEFT JOIN
"WorkflowVersion" as workflowVersion ON workflowVersion."id" = runs."workflowVersionId"
LEFT JOIN
"Workflow" as workflow ON workflowVersion."workflowId" = workflow."id"
WHERE
events."tenantId" = $1 AND
(
$2::text[] IS NULL OR
events."key" = ANY($2::text[])
) AND
(
($3::text[])::uuid[] IS NULL OR
(workflow."id" = ANY($3::text[]::uuid[]))
) AND
(
$4::text IS NULL OR
workflow.name like concat('%', $4::text, '%') OR
jsonb_path_exists(events."data", cast(concat('$.** ? (@.type() == "string" && @ like_regex "', $4::text, '")') as jsonpath))
) AND
(
$5::text[] IS NULL OR
"status" = ANY(cast($5::text[] as "WorkflowRunStatus"[]))
)
GROUP BY
events."id"
ORDER BY
case when $6 = 'createdAt ASC' THEN events."createdAt" END ASC ,
case when $6 = 'createdAt DESC' then events."createdAt" END DESC
OFFSET
COALESCE($7, 0)
LIMIT
COALESCE($8, 50)
`
type ListEventsParams struct {
TenantId pgtype.UUID `json:"tenantId"`
Keys []string `json:"keys"`
Workflows []string `json:"workflows"`
Search pgtype.Text `json:"search"`
Statuses []string `json:"statuses"`
Orderby interface{} `json:"orderby"`
Offset interface{} `json:"offset"`
Limit interface{} `json:"limit"`
}
type ListEventsRow struct {
Event Event `json:"event"`
Pendingruns int64 `json:"pendingruns"`
Runningruns int64 `json:"runningruns"`
Succeededruns int64 `json:"succeededruns"`
Failedruns int64 `json:"failedruns"`
}
func (q *Queries) ListEvents(ctx context.Context, db DBTX, arg ListEventsParams) ([]*ListEventsRow, error) {
rows, err := db.Query(ctx, listEvents,
arg.TenantId,
arg.Keys,
arg.Workflows,
arg.Search,
arg.Statuses,
arg.Orderby,
arg.Offset,
arg.Limit,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*ListEventsRow
for rows.Next() {
var i ListEventsRow
if err := rows.Scan(
&i.Event.ID,
&i.Event.CreatedAt,
&i.Event.UpdatedAt,
&i.Event.DeletedAt,
&i.Event.Key,
&i.Event.TenantId,
&i.Event.ReplayedFromId,
&i.Event.Data,
&i.Pendingruns,
&i.Runningruns,
&i.Succeededruns,
&i.Failedruns,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}