tenseleyflow/shithub / 2f1478a

Browse files

actions/sqlc: add workflow run list filters

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
2f1478a65a1d6ca4260d6b5dcbef634030883ba6
Parents
12d548f
Tree
ff9a121

3 changed files

StatusFile+-
M internal/actions/queries/workflow_runs.sql 43 7
M internal/actions/sqlc/querier.go 2 0
M internal/actions/sqlc/workflow_runs.sql.go 134 25
internal/actions/queries/workflow_runs.sqlmodified
@@ -96,10 +96,46 @@ FROM workflow_runs
96
 WHERE repo_id = $1;
96
 WHERE repo_id = $1;
97
 
97
 
98
 -- name: ListWorkflowRunsForRepo :many
98
 -- name: ListWorkflowRunsForRepo :many
99
-SELECT id, repo_id, run_index, workflow_file, workflow_name,
99
+SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
100
-       head_sha, head_ref, event, status, conclusion,
100
+       r.head_sha, r.head_ref, r.event, r.status, r.conclusion,
101
-       actor_user_id, started_at, completed_at, created_at
101
+       r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
102
-FROM workflow_runs
102
+       r.started_at, r.completed_at, r.created_at, r.updated_at
103
-WHERE repo_id = $1
103
+FROM workflow_runs r
104
-ORDER BY created_at DESC
104
+LEFT JOIN users u ON u.id = r.actor_user_id
105
-LIMIT $2 OFFSET $3;
105
+WHERE r.repo_id = sqlc.arg(repo_id)::bigint
106
+  AND (sqlc.narg(workflow_file)::text IS NULL OR r.workflow_file = sqlc.narg(workflow_file)::text)
107
+  AND (sqlc.narg(head_ref)::text IS NULL OR r.head_ref = sqlc.narg(head_ref)::text)
108
+  AND (sqlc.narg(event)::workflow_run_event IS NULL OR r.event = sqlc.narg(event)::workflow_run_event)
109
+  AND (sqlc.narg(status)::workflow_run_status IS NULL OR r.status = sqlc.narg(status)::workflow_run_status)
110
+  AND (sqlc.narg(conclusion)::check_conclusion IS NULL OR r.conclusion = sqlc.narg(conclusion)::check_conclusion)
111
+  AND (sqlc.narg(actor_username)::text IS NULL OR u.username = sqlc.narg(actor_username)::citext)
112
+ORDER BY r.created_at DESC, r.id DESC
113
+LIMIT sqlc.arg(page_limit) OFFSET sqlc.arg(page_offset);
114
+
115
+-- name: CountWorkflowRunsForRepo :one
116
+SELECT COUNT(*)::bigint
117
+FROM workflow_runs r
118
+LEFT JOIN users u ON u.id = r.actor_user_id
119
+WHERE r.repo_id = sqlc.arg(repo_id)::bigint
120
+  AND (sqlc.narg(workflow_file)::text IS NULL OR r.workflow_file = sqlc.narg(workflow_file)::text)
121
+  AND (sqlc.narg(head_ref)::text IS NULL OR r.head_ref = sqlc.narg(head_ref)::text)
122
+  AND (sqlc.narg(event)::workflow_run_event IS NULL OR r.event = sqlc.narg(event)::workflow_run_event)
123
+  AND (sqlc.narg(status)::workflow_run_status IS NULL OR r.status = sqlc.narg(status)::workflow_run_status)
124
+  AND (sqlc.narg(conclusion)::check_conclusion IS NULL OR r.conclusion = sqlc.narg(conclusion)::check_conclusion)
125
+  AND (sqlc.narg(actor_username)::text IS NULL OR u.username = sqlc.narg(actor_username)::citext);
126
+
127
+-- name: ListWorkflowRunWorkflowsForRepo :many
128
+WITH ranked AS (
129
+    SELECT workflow_file,
130
+           workflow_name,
131
+           (COUNT(*) OVER (PARTITION BY workflow_file))::bigint AS run_count,
132
+           ROW_NUMBER() OVER (PARTITION BY workflow_file ORDER BY created_at DESC, id DESC) AS rn
133
+    FROM workflow_runs
134
+    WHERE repo_id = $1
135
+)
136
+SELECT workflow_file,
137
+       COALESCE(NULLIF(workflow_name, ''), workflow_file) AS workflow_name,
138
+       run_count
139
+FROM ranked
140
+WHERE rn = 1
141
+ORDER BY lower(COALESCE(NULLIF(workflow_name, ''), workflow_file)), workflow_file;
internal/actions/sqlc/querier.gomodified
@@ -16,6 +16,7 @@ type Querier interface {
16
 	ClaimQueuedWorkflowJob(ctx context.Context, db DBTX, arg ClaimQueuedWorkflowJobParams) (ClaimQueuedWorkflowJobRow, error)
16
 	ClaimQueuedWorkflowJob(ctx context.Context, db DBTX, arg ClaimQueuedWorkflowJobParams) (ClaimQueuedWorkflowJobRow, error)
17
 	CompleteWorkflowRun(ctx context.Context, db DBTX, arg CompleteWorkflowRunParams) (WorkflowRun, error)
17
 	CompleteWorkflowRun(ctx context.Context, db DBTX, arg CompleteWorkflowRunParams) (WorkflowRun, error)
18
 	CountRunningJobsForRunner(ctx context.Context, db DBTX, runnerID int64) (int32, error)
18
 	CountRunningJobsForRunner(ctx context.Context, db DBTX, runnerID int64) (int32, error)
19
+	CountWorkflowRunsForRepo(ctx context.Context, db DBTX, arg CountWorkflowRunsForRepoParams) (int64, error)
19
 	DeleteExpiredArtifacts(ctx context.Context, db DBTX) ([]DeleteExpiredArtifactsRow, error)
20
 	DeleteExpiredArtifacts(ctx context.Context, db DBTX) ([]DeleteExpiredArtifactsRow, error)
20
 	DeleteExpiredRunnerJWTUses(ctx context.Context, db DBTX) error
21
 	DeleteExpiredRunnerJWTUses(ctx context.Context, db DBTX) error
21
 	DeleteOrgSecret(ctx context.Context, db DBTX, arg DeleteOrgSecretParams) error
22
 	DeleteOrgSecret(ctx context.Context, db DBTX, arg DeleteOrgSecretParams) error
@@ -70,6 +71,7 @@ type Querier interface {
70
 	ListRunners(ctx context.Context, db DBTX) ([]ListRunnersRow, error)
71
 	ListRunners(ctx context.Context, db DBTX) ([]ListRunnersRow, error)
71
 	ListStepLogChunks(ctx context.Context, db DBTX, arg ListStepLogChunksParams) ([]WorkflowStepLogChunk, error)
72
 	ListStepLogChunks(ctx context.Context, db DBTX, arg ListStepLogChunksParams) ([]WorkflowStepLogChunk, error)
72
 	ListStepsForJob(ctx context.Context, db DBTX, jobID int64) ([]ListStepsForJobRow, error)
73
 	ListStepsForJob(ctx context.Context, db DBTX, jobID int64) ([]ListStepsForJobRow, error)
74
+	ListWorkflowRunWorkflowsForRepo(ctx context.Context, db DBTX, repoID int64) ([]ListWorkflowRunWorkflowsForRepoRow, error)
73
 	ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error)
75
 	ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error)
74
 	LockRunnerByID(ctx context.Context, db DBTX, id int64) (WorkflowRunner, error)
76
 	LockRunnerByID(ctx context.Context, db DBTX, id int64) (WorkflowRunner, error)
75
 	// Companion to EnqueueWorkflowRun for the conflict path: when an
77
 	// Companion to EnqueueWorkflowRun for the conflict path: when an
internal/actions/sqlc/workflow_runs.sql.gomodified
@@ -63,6 +63,44 @@ func (q *Queries) CompleteWorkflowRun(ctx context.Context, db DBTX, arg Complete
63
 	return i, err
63
 	return i, err
64
 }
64
 }
65
 
65
 
66
+const countWorkflowRunsForRepo = `-- name: CountWorkflowRunsForRepo :one
67
+SELECT COUNT(*)::bigint
68
+FROM workflow_runs r
69
+LEFT JOIN users u ON u.id = r.actor_user_id
70
+WHERE r.repo_id = $1::bigint
71
+  AND ($2::text IS NULL OR r.workflow_file = $2::text)
72
+  AND ($3::text IS NULL OR r.head_ref = $3::text)
73
+  AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
74
+  AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
75
+  AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
76
+  AND ($7::text IS NULL OR u.username = $7::citext)
77
+`
78
+
79
+type CountWorkflowRunsForRepoParams struct {
80
+	RepoID        int64
81
+	WorkflowFile  pgtype.Text
82
+	HeadRef       pgtype.Text
83
+	Event         NullWorkflowRunEvent
84
+	Status        NullWorkflowRunStatus
85
+	Conclusion    NullCheckConclusion
86
+	ActorUsername pgtype.Text
87
+}
88
+
89
+func (q *Queries) CountWorkflowRunsForRepo(ctx context.Context, db DBTX, arg CountWorkflowRunsForRepoParams) (int64, error) {
90
+	row := db.QueryRow(ctx, countWorkflowRunsForRepo,
91
+		arg.RepoID,
92
+		arg.WorkflowFile,
93
+		arg.HeadRef,
94
+		arg.Event,
95
+		arg.Status,
96
+		arg.Conclusion,
97
+		arg.ActorUsername,
98
+	)
99
+	var column_1 int64
100
+	err := row.Scan(&column_1)
101
+	return column_1, err
102
+}
103
+
66
 const enqueueWorkflowRun = `-- name: EnqueueWorkflowRun :one
104
 const enqueueWorkflowRun = `-- name: EnqueueWorkflowRun :one
67
 INSERT INTO workflow_runs (
105
 INSERT INTO workflow_runs (
68
     repo_id, run_index, workflow_file, workflow_name,
106
     repo_id, run_index, workflow_file, workflow_name,
@@ -268,41 +306,110 @@ func (q *Queries) InsertWorkflowRun(ctx context.Context, db DBTX, arg InsertWork
268
 	return i, err
306
 	return i, err
269
 }
307
 }
270
 
308
 
309
+const listWorkflowRunWorkflowsForRepo = `-- name: ListWorkflowRunWorkflowsForRepo :many
310
+WITH ranked AS (
311
+    SELECT workflow_file,
312
+           workflow_name,
313
+           (COUNT(*) OVER (PARTITION BY workflow_file))::bigint AS run_count,
314
+           ROW_NUMBER() OVER (PARTITION BY workflow_file ORDER BY created_at DESC, id DESC) AS rn
315
+    FROM workflow_runs
316
+    WHERE repo_id = $1
317
+)
318
+SELECT workflow_file,
319
+       COALESCE(NULLIF(workflow_name, ''), workflow_file) AS workflow_name,
320
+       run_count
321
+FROM ranked
322
+WHERE rn = 1
323
+ORDER BY lower(COALESCE(NULLIF(workflow_name, ''), workflow_file)), workflow_file
324
+`
325
+
326
+type ListWorkflowRunWorkflowsForRepoRow struct {
327
+	WorkflowFile string
328
+	WorkflowName string
329
+	RunCount     int64
330
+}
331
+
332
+func (q *Queries) ListWorkflowRunWorkflowsForRepo(ctx context.Context, db DBTX, repoID int64) ([]ListWorkflowRunWorkflowsForRepoRow, error) {
333
+	rows, err := db.Query(ctx, listWorkflowRunWorkflowsForRepo, repoID)
334
+	if err != nil {
335
+		return nil, err
336
+	}
337
+	defer rows.Close()
338
+	items := []ListWorkflowRunWorkflowsForRepoRow{}
339
+	for rows.Next() {
340
+		var i ListWorkflowRunWorkflowsForRepoRow
341
+		if err := rows.Scan(&i.WorkflowFile, &i.WorkflowName, &i.RunCount); err != nil {
342
+			return nil, err
343
+		}
344
+		items = append(items, i)
345
+	}
346
+	if err := rows.Err(); err != nil {
347
+		return nil, err
348
+	}
349
+	return items, nil
350
+}
351
+
271
 const listWorkflowRunsForRepo = `-- name: ListWorkflowRunsForRepo :many
352
 const listWorkflowRunsForRepo = `-- name: ListWorkflowRunsForRepo :many
272
-SELECT id, repo_id, run_index, workflow_file, workflow_name,
353
+SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
273
-       head_sha, head_ref, event, status, conclusion,
354
+       r.head_sha, r.head_ref, r.event, r.status, r.conclusion,
274
-       actor_user_id, started_at, completed_at, created_at
355
+       r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
275
-FROM workflow_runs
356
+       r.started_at, r.completed_at, r.created_at, r.updated_at
276
-WHERE repo_id = $1
357
+FROM workflow_runs r
277
-ORDER BY created_at DESC
358
+LEFT JOIN users u ON u.id = r.actor_user_id
278
-LIMIT $2 OFFSET $3
359
+WHERE r.repo_id = $1::bigint
360
+  AND ($2::text IS NULL OR r.workflow_file = $2::text)
361
+  AND ($3::text IS NULL OR r.head_ref = $3::text)
362
+  AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
363
+  AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
364
+  AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
365
+  AND ($7::text IS NULL OR u.username = $7::citext)
366
+ORDER BY r.created_at DESC, r.id DESC
367
+LIMIT $9 OFFSET $8
279
 `
368
 `
280
 
369
 
281
 type ListWorkflowRunsForRepoParams struct {
370
 type ListWorkflowRunsForRepoParams struct {
282
-	RepoID int64
371
+	RepoID        int64
283
-	Limit  int32
372
+	WorkflowFile  pgtype.Text
284
-	Offset int32
373
+	HeadRef       pgtype.Text
374
+	Event         NullWorkflowRunEvent
375
+	Status        NullWorkflowRunStatus
376
+	Conclusion    NullCheckConclusion
377
+	ActorUsername pgtype.Text
378
+	PageOffset    int32
379
+	PageLimit     int32
285
 }
380
 }
286
 
381
 
287
 type ListWorkflowRunsForRepoRow struct {
382
 type ListWorkflowRunsForRepoRow struct {
288
-	ID           int64
383
+	ID            int64
289
-	RepoID       int64
384
+	RepoID        int64
290
-	RunIndex     int64
385
+	RunIndex      int64
291
-	WorkflowFile string
386
+	WorkflowFile  string
292
-	WorkflowName string
387
+	WorkflowName  string
293
-	HeadSha      string
388
+	HeadSha       string
294
-	HeadRef      string
389
+	HeadRef       string
295
-	Event        WorkflowRunEvent
390
+	Event         WorkflowRunEvent
296
-	Status       WorkflowRunStatus
391
+	Status        WorkflowRunStatus
297
-	Conclusion   NullCheckConclusion
392
+	Conclusion    NullCheckConclusion
298
-	ActorUserID  pgtype.Int8
393
+	ActorUserID   pgtype.Int8
299
-	StartedAt    pgtype.Timestamptz
394
+	ActorUsername string
300
-	CompletedAt  pgtype.Timestamptz
395
+	StartedAt     pgtype.Timestamptz
301
-	CreatedAt    pgtype.Timestamptz
396
+	CompletedAt   pgtype.Timestamptz
397
+	CreatedAt     pgtype.Timestamptz
398
+	UpdatedAt     pgtype.Timestamptz
302
 }
399
 }
303
 
400
 
304
 func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error) {
401
 func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error) {
305
-	rows, err := db.Query(ctx, listWorkflowRunsForRepo, arg.RepoID, arg.Limit, arg.Offset)
402
+	rows, err := db.Query(ctx, listWorkflowRunsForRepo,
403
+		arg.RepoID,
404
+		arg.WorkflowFile,
405
+		arg.HeadRef,
406
+		arg.Event,
407
+		arg.Status,
408
+		arg.Conclusion,
409
+		arg.ActorUsername,
410
+		arg.PageOffset,
411
+		arg.PageLimit,
412
+	)
306
 	if err != nil {
413
 	if err != nil {
307
 		return nil, err
414
 		return nil, err
308
 	}
415
 	}
@@ -322,9 +429,11 @@ func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg List
322
 			&i.Status,
429
 			&i.Status,
323
 			&i.Conclusion,
430
 			&i.Conclusion,
324
 			&i.ActorUserID,
431
 			&i.ActorUserID,
432
+			&i.ActorUsername,
325
 			&i.StartedAt,
433
 			&i.StartedAt,
326
 			&i.CompletedAt,
434
 			&i.CompletedAt,
327
 			&i.CreatedAt,
435
 			&i.CreatedAt,
436
+			&i.UpdatedAt,
328
 		); err != nil {
437
 		); err != nil {
329
 			return nil, err
438
 			return nil, err
330
 		}
439
 		}