Add Task Sorting and Pagination
This commit is contained in:
parent
1c48a43738
commit
1a46eaf51b
4 changed files with 119 additions and 9 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -20,6 +21,11 @@ type TasksData struct {
|
|||
FfmpegCommands []FfmpegCommand
|
||||
Tasks []TaskDisplay
|
||||
Stats TaskStats
|
||||
OrderBy string
|
||||
OrderAsc bool
|
||||
Limit uint
|
||||
Page uint
|
||||
Count uint
|
||||
}
|
||||
|
||||
type TaskStats struct {
|
||||
|
@ -56,6 +62,8 @@ type TaskDB struct {
|
|||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
var taskColumns = []string{"id", "library", "worker", "type", "ffmpeg_command", "status", "file", "updated_at"}
|
||||
|
||||
func handleTasks(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
err := createTask(r.Context(), r)
|
||||
|
@ -114,7 +122,52 @@ func handleTasks(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
data.FfmpegCommands = ffmpegCommands
|
||||
|
||||
rows, err = db.Query(r.Context(), "SELECT t.id AS id, l.id AS library, t.worker_id AS worker, t.type AS type, fc.name AS ffmpeg_command, t.status AS status, f.path AS file, t.updated_at AS updated_at FROM tasks t INNER JOIN files f ON f.id = t.file_id INNER JOIN libraries l ON l.id = f.library_id INNER JOIN ffmpeg_commands fc ON fc.id = t.ffmpeg_command_id ORDER BY CASE t.type WHEN 3 THEN -1 ELSE t.type END, t.id")
|
||||
query := r.URL.Query()
|
||||
data.OrderBy = "updated_at"
|
||||
if query.Has(constants.SORT_ORDER_BY_PARAM) {
|
||||
if slices.Contains(taskColumns, query.Get(constants.SORT_ORDER_BY_PARAM)) {
|
||||
data.OrderBy = query.Get(constants.SORT_ORDER_BY_PARAM)
|
||||
} else {
|
||||
http.Error(w, "Error Unknown Column in order_by: "+query.Get(constants.SORT_ORDER_BY_PARAM), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
orderDir := "DESC"
|
||||
if query.Has(constants.SORT_ORDER_ASC_PARAM) {
|
||||
orderDir = "ASC"
|
||||
data.OrderAsc = true
|
||||
}
|
||||
|
||||
data.Limit = 100
|
||||
if query.Has(constants.QUERY_LIMIT_PARAM) {
|
||||
limit, err := strconv.Atoi(query.Get(constants.QUERY_LIMIT_PARAM))
|
||||
if err != nil {
|
||||
http.Error(w, "Error Parsing query limit: "+query.Get(constants.QUERY_LIMIT_PARAM), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if limit < 0 {
|
||||
http.Error(w, "Error query limit smaller than 0: "+query.Get(constants.QUERY_LIMIT_PARAM), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
data.Limit = uint(limit)
|
||||
}
|
||||
|
||||
data.Page = 0
|
||||
if query.Has(constants.QUERY_PAGE_PARAM) {
|
||||
page, err := strconv.Atoi(query.Get(constants.QUERY_PAGE_PARAM))
|
||||
if err != nil {
|
||||
http.Error(w, "Error Parsing query page: "+query.Get(constants.QUERY_PAGE_PARAM), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if page < 0 {
|
||||
http.Error(w, "Error query limit smaller than 0: "+query.Get(constants.QUERY_PAGE_PARAM), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
data.Page = uint(page)
|
||||
}
|
||||
|
||||
rows, err = db.Query(r.Context(), "SELECT t.id AS id, l.id AS library, t.worker_id AS worker, t.type AS type, fc.name AS ffmpeg_command, t.status AS status, f.path AS file, t.updated_at AS updated_at FROM tasks t INNER JOIN files f ON f.id = t.file_id INNER JOIN libraries l ON l.id = f.library_id INNER JOIN ffmpeg_commands fc ON fc.id = t.ffmpeg_command_id "+fmt.Sprintf("ORDER BY %v %v LIMIT %d OFFSET %d", data.OrderBy, orderDir, data.Limit, data.Page*data.Limit))
|
||||
if err != nil {
|
||||
slog.ErrorContext(r.Context(), "Query Tasks", "err", err)
|
||||
http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError)
|
||||
|
@ -138,6 +191,7 @@ func handleTasks(w http.ResponseWriter, r *http.Request) {
|
|||
UpdatedAt: tasks[i].UpdatedAt.Format(time.DateTime),
|
||||
})
|
||||
}
|
||||
data.Count = uint(len(tasks))
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
err = templates.ExecuteTemplate(&buf, constants.TASKS_TEMPLATE_NAME, data)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue