Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
var ErrEventJobFilter = errors.New("invalid job status or cursor")
|
|
|
|
type eventJobCursor struct {
|
|
Time int64 `json:"time"`
|
|
UUID string `json:"uuid"`
|
|
}
|
|
|
|
// EventJobPage filters before limiting so long-running jobs cannot be hidden by
|
|
// newer completed work. Keyset pagination avoids scanning an increasing OFFSET.
|
|
func (s *Store) EventJobPage(ctx context.Context, status, cursor string, limit int) ([]EventJob, string, error) {
|
|
if limit < 1 || limit > 100 {
|
|
limit = 50
|
|
}
|
|
// Listing is metadata-only: import and preview payloads can be large and
|
|
// must not be read or sent for each dashboard refresh.
|
|
columns := strings.Replace(eventJobColumns, "payload,", "'' AS payload,", 1)
|
|
table := "event_jobs"
|
|
if status == "history" {
|
|
// SQLite otherwise prefers the status index for IN and sorts all
|
|
// completed rows before LIMIT. This partial index already has the order.
|
|
table += " INDEXED BY idx_event_jobs_history"
|
|
}
|
|
query := `SELECT ` + columns + ` FROM ` + table + ` WHERE 1=1`
|
|
args := []any{}
|
|
switch status {
|
|
case "":
|
|
case "history":
|
|
query += ` AND status IN ('succeeded','skipped','failed','cancelled')`
|
|
case "running", "queued", "succeeded", "skipped", "failed", "cancelled":
|
|
query += ` AND status=?`
|
|
args = append(args, status)
|
|
default:
|
|
return nil, "", ErrEventJobFilter
|
|
}
|
|
if cursor != "" {
|
|
if len(cursor) > 344 {
|
|
return nil, "", ErrEventJobFilter
|
|
}
|
|
var c eventJobCursor
|
|
data, err := base64.RawURLEncoding.DecodeString(cursor)
|
|
if err != nil || len(data) > 256 || json.Unmarshal(data, &c) != nil || c.Time <= 0 || c.UUID == "" {
|
|
return nil, "", ErrEventJobFilter
|
|
}
|
|
query += ` AND (created_at_ms,uuid) < (?,?)`
|
|
args = append(args, c.Time, c.UUID)
|
|
}
|
|
query += ` ORDER BY created_at_ms DESC,uuid DESC LIMIT ?`
|
|
args = append(args, limit+1)
|
|
rows, err := s.DB.QueryContext(ctx, query, args...)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
defer rows.Close()
|
|
jobs := make([]EventJob, 0)
|
|
for rows.Next() {
|
|
job, err := scanEventJob(rows)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
jobs = append(jobs, *job)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, "", err
|
|
}
|
|
next := ""
|
|
if len(jobs) > limit {
|
|
jobs = jobs[:limit]
|
|
last := jobs[len(jobs)-1]
|
|
data, _ := json.Marshal(eventJobCursor{last.CreatedAt, last.UUID})
|
|
next = base64.RawURLEncoding.EncodeToString(data)
|
|
}
|
|
return jobs, next, nil
|
|
}
|