Skip to content

Commit 45f8bc6

Browse files
keep older version
1 parent 03f1345 commit 45f8bc6

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,96 @@ func (q *FakeQuerier) getProvisionerJobsByIDsWithQueuePositionLocked(_ context.C
12591259
return results, nil
12601260
}
12611261

1262+
func (q *FakeQuerier) getProvisionerJobsByIDsWithQueuePositionLockedV0(_ context.Context, ids []uuid.UUID) ([]database.GetProvisionerJobsByIDsWithQueuePositionRow, error) {
1263+
// WITH pending_jobs AS (
1264+
// SELECT
1265+
// id, created_at
1266+
// FROM
1267+
// provisioner_jobs
1268+
// WHERE
1269+
// started_at IS NULL
1270+
// AND
1271+
// canceled_at IS NULL
1272+
// AND
1273+
// completed_at IS NULL
1274+
// AND
1275+
// error IS NULL
1276+
// ),
1277+
type pendingJobRow struct {
1278+
ID uuid.UUID
1279+
CreatedAt time.Time
1280+
}
1281+
pendingJobs := make([]pendingJobRow, 0)
1282+
for _, job := range q.provisionerJobs {
1283+
if job.StartedAt.Valid ||
1284+
job.CanceledAt.Valid ||
1285+
job.CompletedAt.Valid ||
1286+
job.Error.Valid {
1287+
continue
1288+
}
1289+
pendingJobs = append(pendingJobs, pendingJobRow{
1290+
ID: job.ID,
1291+
CreatedAt: job.CreatedAt,
1292+
})
1293+
}
1294+
1295+
// queue_position AS (
1296+
// SELECT
1297+
// id,
1298+
// ROW_NUMBER() OVER (ORDER BY created_at ASC) AS queue_position
1299+
// FROM
1300+
// pending_jobs
1301+
// ),
1302+
slices.SortFunc(pendingJobs, func(a, b pendingJobRow) int {
1303+
c := a.CreatedAt.Compare(b.CreatedAt)
1304+
return c
1305+
})
1306+
1307+
queuePosition := make(map[uuid.UUID]int64)
1308+
for idx, pj := range pendingJobs {
1309+
queuePosition[pj.ID] = int64(idx + 1)
1310+
}
1311+
1312+
// queue_size AS (
1313+
// SELECT COUNT(*) AS count FROM pending_jobs
1314+
// ),
1315+
queueSize := len(pendingJobs)
1316+
1317+
// SELECT
1318+
// sqlc.embed(pj),
1319+
// COALESCE(qp.queue_position, 0) AS queue_position,
1320+
// COALESCE(qs.count, 0) AS queue_size
1321+
// FROM
1322+
// provisioner_jobs pj
1323+
// LEFT JOIN
1324+
// queue_position qp ON pj.id = qp.id
1325+
// LEFT JOIN
1326+
// queue_size qs ON TRUE
1327+
// WHERE
1328+
// pj.id IN (...)
1329+
jobs := make([]database.GetProvisionerJobsByIDsWithQueuePositionRow, 0)
1330+
for _, job := range q.provisionerJobs {
1331+
if ids != nil && !slices.Contains(ids, job.ID) {
1332+
continue
1333+
}
1334+
// clone the Tags before appending, since maps are reference types and
1335+
// we don't want the caller to be able to mutate the map we have inside
1336+
// dbmem!
1337+
job.Tags = maps.Clone(job.Tags)
1338+
job := database.GetProvisionerJobsByIDsWithQueuePositionRow{
1339+
// sqlc.embed(pj),
1340+
ProvisionerJob: job,
1341+
// COALESCE(qp.queue_position, 0) AS queue_position,
1342+
QueuePosition: queuePosition[job.ID],
1343+
// COALESCE(qs.count, 0) AS queue_size
1344+
QueueSize: int64(queueSize),
1345+
}
1346+
jobs = append(jobs, job)
1347+
}
1348+
1349+
return jobs, nil
1350+
}
1351+
12621352
func (*FakeQuerier) AcquireLock(_ context.Context, _ int64) error {
12631353
return xerrors.New("AcquireLock must only be called within a transaction")
12641354
}
@@ -4240,7 +4330,7 @@ func (q *FakeQuerier) GetProvisionerJobsByOrganizationAndStatusWithQueuePosition
42404330
LIMIT
42414331
sqlc.narg('limit')::int;
42424332
*/
4243-
rowsWithQueuePosition, err := q.getProvisionerJobsByIDsWithQueuePositionLocked(ctx, nil)
4333+
rowsWithQueuePosition, err := q.getProvisionerJobsByIDsWithQueuePositionLockedV0(ctx, nil)
42444334
if err != nil {
42454335
return nil, err
42464336
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy