From 1a181980a3e562d2c2ff56db0e4042df8bc93029 Mon Sep 17 00:00:00 2001 From: speatzle Date: Tue, 25 Jun 2024 18:00:04 +0200 Subject: [PATCH] Improve Task Display --- constants/constants.go | 15 ++++++++++++++- server/task.go | 33 ++++++++++++++++++++++----------- tmpl/task.tmpl | 9 ++++++++- types/task.go | 10 +++++----- 4 files changed, 49 insertions(+), 18 deletions(-) diff --git a/constants/constants.go b/constants/constants.go index 24fdfd9..99736a3 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -25,11 +25,24 @@ const SORT_ORDER_ASC_PARAM = "order_asc" const QUERY_LIMIT_PARAM = "limit" const QUERY_PAGE_PARAM = "page" +type TaskType int + const ( - TASK_TYPE_HEALTHCHECK = iota + TASK_TYPE_HEALTHCHECK TaskType = iota TASK_TYPE_TRANSCODE ) +func (s TaskType) String() string { + switch s { + case TASK_TYPE_HEALTHCHECK: + return "Healthcheck" + case TASK_TYPE_TRANSCODE: + return "Transcode" + default: + return fmt.Sprintf("%d", int(s)) + } +} + type TaskStatus int // Non Append Changes Need Worker Version Bump diff --git a/server/task.go b/server/task.go index d536b1f..bf6a1f7 100644 --- a/server/task.go +++ b/server/task.go @@ -19,7 +19,7 @@ import ( type TasksData struct { Libraries []Library FfmpegCommands []FfmpegCommand - Tasks []TaskDisplay + Tasks []TasksDisplay Stats TaskStats OrderBy string OrderAsc bool @@ -40,7 +40,7 @@ type TaskStats struct { TotalCount int } -type TaskDisplay struct { +type TasksDisplay struct { ID int `db:"id"` Library int `db:"library"` Worker *string `db:"worker"` @@ -51,6 +51,17 @@ type TaskDisplay struct { UpdatedAt string `db:"updated_at"` } +type TaskDisplay struct { + ID int + Library string + Worker string + Type string + Status string + Filename string + Log []string + UpdatedAt time.Time +} + type TaskDB struct { ID int `db:"id"` Library int `db:"library"` @@ -167,7 +178,7 @@ func handleTasks(w http.ResponseWriter, r *http.Request) { 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)) + rows, err = db.Query(r.Context(), "SELECT t.id AS id, l.id AS library, w.name 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 INNER JOIN workers w ON t.worker_id = w.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) @@ -180,7 +191,7 @@ func handleTasks(w http.ResponseWriter, r *http.Request) { return } for i := range tasks { - data.Tasks = append(data.Tasks, TaskDisplay{ + data.Tasks = append(data.Tasks, TasksDisplay{ ID: tasks[i].ID, Library: tasks[i].Library, Worker: tasks[i].Worker, @@ -214,18 +225,18 @@ func handleTask(w http.ResponseWriter, r *http.Request) { return } - var log []string - err := db.QueryRow(r.Context(), "SELECT log FROM tasks WHERE id = $1", id).Scan(&log) + var typ constants.TaskType + var status constants.TaskStatus + t := TaskDisplay{} + err := db.QueryRow(r.Context(), "SELECT t.id, l.name, w.name, t.type, t.status, f.path, t.log, t.updated_at FROM tasks t INNER JOIN workers w ON w.id = t.worker_id INNER JOIN files f ON f.id = t.file_id INNER JOIN libraries l ON l.id = f.library_id WHERE t.id = $1", id).Scan(&t.ID, &t.Library, &t.Worker, &typ, &status, &t.Filename, &t.Log, &t.UpdatedAt) if err != nil { slog.ErrorContext(r.Context(), "Query Tasks", "err", err) http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError) return } - t := types.Task{ - Log: log, - } - + t.Type = typ.String() + t.Status = status.String() buf := bytes.Buffer{} err = templates.ExecuteTemplate(&buf, constants.TASK_TEMPLATE_NAME, t) if err != nil { @@ -311,7 +322,7 @@ func createTask(ctx context.Context, r *http.Request) error { type QueuedTask struct { ID int - Type int + Type constants.TaskType FileID int `json:"file_id"` FileMD5 []byte `json:"file_md5" db:"md5"` Data json.RawMessage diff --git a/tmpl/task.tmpl b/tmpl/task.tmpl index 09f2356..3fc3421 100644 --- a/tmpl/task.tmpl +++ b/tmpl/task.tmpl @@ -24,7 +24,14 @@ window.onload = function(e){ checkReloading(); } -

Task {{.ID}}

+

Task: {{.ID}}

+

Type: {{.Type}}

+

Status: {{.Status}}

+

Worker: {{.Worker}}

+

Library: {{.Library}}

+

Filename: {{.Filename}}

+

Updated At: {{.UpdatedAt}}

+
{{range $t := .Log}}

diff --git a/types/task.go b/types/task.go index 085fe20..766ba1b 100644 --- a/types/task.go +++ b/types/task.go @@ -7,10 +7,10 @@ import ( ) type TaskStart struct { - ID int `json:"id"` - FileID int `json:"file_id"` - FileMD5 []byte `json:"file_md5"` - Type int `json:"type"` + ID int `json:"id"` + FileID int `json:"file_id"` + FileMD5 []byte `json:"file_md5"` + Type constants.TaskType `json:"type"` Data json.RawMessage FfmpegCommand FFmpegCommand `json:"ffmpeg_command"` } @@ -19,7 +19,7 @@ type Task struct { ID int `json:"id"` FileID int `json:"file_id"` FileMD5 []byte `json:"md5"` - Type int `json:"type"` + Type constants.TaskType `json:"type"` Status constants.TaskStatus `json:"status"` FfmpegCommand FFmpegCommand `json:"ffmpeg_command"` Log []string `json:"log"`