diff --git a/server/repository/repository.go b/server/repository/repository.go index e1180eb..866ad47 100644 --- a/server/repository/repository.go +++ b/server/repository/repository.go @@ -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 @@ -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 } @@ -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 } diff --git a/server/scheduler/scheduler.go b/server/scheduler/scheduler.go index b21d7e8..6ca788b 100644 --- a/server/scheduler/scheduler.go +++ b/server/scheduler/scheduler.go @@ -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) @@ -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 { diff --git a/server/web/ui/src/JobTable.js b/server/web/ui/src/JobTable.js index 1972726..aaf6e98 100644 --- a/server/web/ui/src/JobTable.js +++ b/server/web/ui/src/JobTable.js @@ -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); @@ -94,7 +91,7 @@ const JobTable = ({ token, setShowJobTable }) => { case 'failed': return 'red'; default: - return 'inherit'; + return 'grey'; } }; diff --git a/server/web/web.go b/server/web/web.go index 267e536..b735cc7 100644 --- a/server/web/web.go +++ b/server/web/web.go @@ -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 @@ -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")