Skip to content

Commit

Permalink
feat(server): Add pagination to get jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
pando85 committed Jan 21, 2024
1 parent ff3822a commit d2c9703
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
17 changes: 11 additions & 6 deletions server/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Repository interface {
PingServerUpdate(ctx context.Context, name string, ip string, queueName string) error
GetTimeoutJobs(ctx context.Context, timeout time.Duration) ([]*model.TaskEvent, error)
GetJob(ctx context.Context, uuid string) (*model.Video, error)
GetJobs(ctx context.Context) (*[]model.Video, error)
GetJobs(ctx context.Context, page int, pageSize int) (*[]model.Video, error)
GetJobByPath(ctx context.Context, path string) (*model.Video, error)
AddNewTaskEvent(ctx context.Context, event *model.TaskEvent) error
AddVideo(ctx context.Context, video *model.Video) error
Expand Down Expand Up @@ -181,12 +181,12 @@ func (S *SQLRepository) GetJob(ctx context.Context, uuid string) (video *model.V
return video, err
}

func (S *SQLRepository) GetJobs(ctx context.Context) (videos *[]model.Video, returnError error) {
func (S *SQLRepository) GetJobs(ctx context.Context, page int, pageSize int) (videos *[]model.Video, returnError error) {
db, err := S.getConnection(ctx)
if err != nil {
return nil, err
}
videos, err = S.getJobs(ctx, db)
videos, err = S.getJobs(ctx, db, page, pageSize)
return videos, err
}

Expand Down Expand Up @@ -231,18 +231,23 @@ func (S *SQLRepository) getJob(ctx context.Context, tx Transaction, uuid string)
return &video, nil
}

func (S *SQLRepository) getJobs(ctx context.Context, tx Transaction) (*[]model.Video, error) {
rows, err := tx.QueryContext(ctx, "SELECT id FROM videos")
func (S *SQLRepository) getJobs(ctx context.Context, tx Transaction, page int, pageSize int) (*[]model.Video, error) {
offset := (page - 1) * pageSize
query := fmt.Sprintf("SELECT id FROM videos LIMIT %d OFFSET %d", pageSize, offset)

rows, err := tx.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()

videos := []model.Video{}
for rows.Next() {
video := model.Video{}
rows.Scan(&video.Id)
videos = append(videos, video)
}
rows.Close()

return &videos, nil
}

Expand Down
6 changes: 3 additions & 3 deletions server/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Scheduler interface {
Run(wg *sync.WaitGroup, ctx context.Context)
ScheduleJobRequests(ctx context.Context, jobRequest *model.JobRequest) (*ScheduleJobRequestResult, error)
GetJob(ctx context.Context, uuid string) (videos *model.Video, err error)
GetJobs(ctx context.Context) (*[]model.Video, error)
GetJobs(ctx context.Context, page int, pageSize int) (*[]model.Video, error)
GetUploadJobWriter(ctx context.Context, uuid string) (*UploadJobStream, error)
GetDownloadJobWriter(ctx context.Context, uuid string) (*DownloadJobStream, error)
GetChecksum(ctx context.Context, uuid string) (string, error)
Expand Down Expand Up @@ -334,8 +334,8 @@ func (R *RuntimeScheduler) GetJob(ctx context.Context, uuid string) (videos *mod
return R.repo.GetJob(ctx, uuid)
}

func (R *RuntimeScheduler) GetJobs(ctx context.Context) (videos *[]model.Video, err error) {
return R.repo.GetJobs(ctx)
func (R *RuntimeScheduler) GetJobs(ctx context.Context, page int, pageSize int) (videos *[]model.Video, err error) {
return R.repo.GetJobs(ctx, page, pageSize)
}

func (R *RuntimeScheduler) CancelJob(ctx context.Context, uuid string) error {
Expand Down
7 changes: 2 additions & 5 deletions server/web/ui/src/JobTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ const JobTable = ({ token, setShowJobTable }) => {
params: { token, page },
});

// Set new page of jobs only if there are no jobs loaded yet
setJobs((prevJobs) =>
prevJobs.length === 0 ? response.data : prevJobs
);
setJobs((prevJobs) => [...prevJobs, ...response.data]);
} catch (error) {
console.error('Error fetching jobs:', error);
setShowJobTable(false);
Expand Down Expand Up @@ -94,7 +91,7 @@ const JobTable = ({ token, setShowJobTable }) => {
case 'failed':
return 'red';
default:
return 'inherit';
return 'grey';
}
};

Expand Down
10 changes: 9 additions & 1 deletion server/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func (w *WebServer) addJobs(c *gin.Context) {
}

func (w *WebServer) getAllJobs(c *gin.Context) {
videos, err := w.scheduler.GetJobs(w.ctx)
page, pageSize := getPageParams(c)

videos, err := w.scheduler.GetJobs(w.ctx, page, pageSize)
if err != nil {
webError(c, err, http.StatusInternalServerError)
return
Expand All @@ -69,6 +71,12 @@ func (w *WebServer) getAllJobs(c *gin.Context) {
c.JSON(http.StatusOK, videos)
}

func getPageParams(c *gin.Context) (int, int) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "20"))
return page, pageSize
}

func (w *WebServer) getJobs(c *gin.Context) {
uuid := c.Query("uuid")

Expand Down

0 comments on commit d2c9703

Please sign in to comment.