feat(service): add pagination to GetJobs endpoint

GetJobs now accepts page/page_size query parameters and returns JobListResponse instead of raw array. Uses in-memory pagination matching GetJobHistory pattern.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
dailz
2026-04-10 15:14:56 +08:00
parent 35a4017b8e
commit 2cb6fbecdd
5 changed files with 155 additions and 14 deletions

View File

@@ -82,8 +82,8 @@ func (s *JobService) SubmitJob(ctx context.Context, req *model.SubmitJobRequest)
return resp, nil
}
// GetJobs lists all current jobs from Slurm.
func (s *JobService) GetJobs(ctx context.Context) ([]model.JobResponse, error) {
// GetJobs lists all current jobs from Slurm with in-memory pagination.
func (s *JobService) GetJobs(ctx context.Context, query *model.JobListQuery) (*model.JobListResponse, error) {
s.logger.Debug("slurm API request",
zap.String("operation", "GetJobs"),
)
@@ -109,11 +109,36 @@ func (s *JobService) GetJobs(ctx context.Context) ([]model.JobResponse, error) {
zap.Any("body", result),
)
jobs := make([]model.JobResponse, 0, len(result.Jobs))
allJobs := make([]model.JobResponse, 0, len(result.Jobs))
for i := range result.Jobs {
jobs = append(jobs, mapJobInfo(&result.Jobs[i]))
allJobs = append(allJobs, mapJobInfo(&result.Jobs[i]))
}
return jobs, nil
total := len(allJobs)
page := query.Page
pageSize := query.PageSize
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 20
}
startIdx := (page - 1) * pageSize
end := startIdx + pageSize
if startIdx > total {
startIdx = total
}
if end > total {
end = total
}
return &model.JobListResponse{
Jobs: allJobs[startIdx:end],
Total: total,
Page: page,
PageSize: pageSize,
}, nil
}
// GetJob retrieves a single job by ID. If the job is not found in the active

View File

@@ -149,14 +149,17 @@ func TestGetJobs(t *testing.T) {
defer cleanup()
svc := NewJobService(client, zap.NewNop())
jobs, err := svc.GetJobs(context.Background())
result, err := svc.GetJobs(context.Background(), &model.JobListQuery{Page: 1, PageSize: 20})
if err != nil {
t.Fatalf("GetJobs: %v", err)
}
if len(jobs) != 1 {
t.Fatalf("expected 1 job, got %d", len(jobs))
if result.Total != 1 {
t.Fatalf("expected total 1, got %d", result.Total)
}
j := jobs[0]
if len(result.Jobs) != 1 {
t.Fatalf("expected 1 job, got %d", len(result.Jobs))
}
j := result.Jobs[0]
if j.JobID != 100 {
t.Errorf("expected JobID 100, got %d", j.JobID)
}
@@ -175,6 +178,12 @@ func TestGetJobs(t *testing.T) {
if j.Nodes != "node01" {
t.Errorf("expected Nodes node01, got %s", j.Nodes)
}
if result.Page != 1 {
t.Errorf("expected Page 1, got %d", result.Page)
}
if result.PageSize != 20 {
t.Errorf("expected PageSize 20, got %d", result.PageSize)
}
}
func TestGetJob(t *testing.T) {
@@ -621,7 +630,7 @@ func TestJobService_GetJobs_ErrorLog(t *testing.T) {
defer srv.Close()
svc, recorded := newJobServiceWithObserver(srv)
_, err := svc.GetJobs(context.Background())
_, err := svc.GetJobs(context.Background(), &model.JobListQuery{Page: 1, PageSize: 20})
if err == nil {
t.Fatal("expected error, got nil")
}