Add Updated At to Tasks And Libraries

This commit is contained in:
speatzle 2024-05-28 21:32:57 +02:00
parent ec645f6162
commit c04aa74364
14 changed files with 74 additions and 38 deletions

View file

@ -0,0 +1,2 @@
ALTER TABLE tasks
DROP updated_at;

View file

@ -0,0 +1,2 @@
ALTER TABLE tasks
ADD updated_at TIMESTAMP NOT NULL DEFAULT current_timestamp;

View file

@ -0,0 +1 @@
DROP FUNCTION update_updated_at_column;

View file

@ -0,0 +1,7 @@
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = clock_timestamp();
RETURN NEW;
END;
$$ language 'plpgsql';

View file

@ -0,0 +1 @@
DROP TRIGGER update_tasks_timestamp;

View file

@ -0,0 +1 @@
CREATE TRIGGER update_tasks_timestamp BEFORE UPDATE ON tasks FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();

View file

@ -0,0 +1,2 @@
ALTER TABLE files
DROP updated_at;

View file

@ -0,0 +1,2 @@
ALTER TABLE files
ADD updated_at TIMESTAMP NOT NULL DEFAULT current_timestamp;

View file

@ -0,0 +1 @@
DROP TRIGGER update_files_timestamp;

View file

@ -0,0 +1 @@
CREATE TRIGGER update_files_timestamp BEFORE UPDATE ON files FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();

View file

@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"net/http"
"time"
"git.lastassault.de/speatzle/morffix/constants"
"github.com/jackc/pgx/v5"
@ -16,21 +17,23 @@ type LibraryData struct {
}
type File struct {
ID int `db:"id"`
Path string `db:"path"`
Size int64 `db:"size"`
Status constants.FileStatus `db:"status"`
Health constants.FileHealth `db:"health"`
MD5 []byte `db:"md5"`
ID int `db:"id"`
Path string `db:"path"`
Size int64 `db:"size"`
Status constants.FileStatus `db:"status"`
Health constants.FileHealth `db:"health"`
MD5 []byte `db:"md5"`
UpdatedAt time.Time `db:"updated_at"`
}
type FileDisplay struct {
ID int
Path string
Size int64
Status string
Health string
MD5 string
ID int
Path string
Size int64
Status string
Health string
MD5 string
UpdatedAt string `db:"updated_at"`
}
func handleLibrary(w http.ResponseWriter, r *http.Request) {
@ -62,7 +65,7 @@ func handleLibrary(w http.ResponseWriter, r *http.Request) {
// TODO
}
rows, err := db.Query(r.Context(), "SELECT id, path, size, status, health, md5 FROM files where library_id = $1", id)
rows, err := db.Query(r.Context(), "SELECT id, path, size, status, health, md5, updated_at FROM files where library_id = $1", id)
if err != nil {
slog.ErrorContext(r.Context(), "Query Files", "err", err)
http.Error(w, "Error Query Files: "+err.Error(), http.StatusInternalServerError)
@ -77,12 +80,13 @@ func handleLibrary(w http.ResponseWriter, r *http.Request) {
for i := range files {
data.Files = append(data.Files, FileDisplay{
ID: files[i].ID,
Path: files[i].Path,
Size: files[i].Size,
Status: files[i].Status.String(),
Health: files[i].Health.String(),
MD5: fmt.Sprintf("%x", files[i].MD5),
ID: files[i].ID,
Path: files[i].Path,
Size: files[i].Size,
Status: files[i].Status.String(),
Health: files[i].Health.String(),
MD5: fmt.Sprintf("%x", files[i].MD5),
UpdatedAt: files[i].UpdatedAt.Format(time.DateTime),
})
}

View file

@ -7,6 +7,7 @@ import (
"fmt"
"log/slog"
"net/http"
"time"
"git.lastassault.de/speatzle/morffix/constants"
"git.lastassault.de/speatzle/morffix/types"
@ -32,21 +33,23 @@ type TaskStats struct {
}
type TaskDisplay struct {
ID int `db:"id"`
Library int `db:"library"`
Worker *string `db:"worker"`
Type int `db:"type"`
Status string `db:"status"`
File string `db:"file"`
ID int `db:"id"`
Library int `db:"library"`
Worker *string `db:"worker"`
Type int `db:"type"`
Status string `db:"status"`
File string `db:"file"`
UpdatedAt string `db:"updated_at"`
}
type TaskDB struct {
ID int `db:"id"`
Library int `db:"library"`
Worker *string `db:"worker"`
Type int `db:"type"`
Status constants.TaskStatus `db:"status"`
File string `db:"file"`
ID int `db:"id"`
Library int `db:"library"`
Worker *string `db:"worker"`
Type int `db:"type"`
Status constants.TaskStatus `db:"status"`
File string `db:"file"`
UpdatedAt time.Time `db:"updated_at"`
}
func handleTasks(w http.ResponseWriter, r *http.Request) {
@ -93,7 +96,7 @@ func handleTasks(w http.ResponseWriter, r *http.Request) {
}
data.Libraries = libraries
rows, err = db.Query(r.Context(), "SELECT t.id AS id, l.id AS library, t.worker_id AS worker, t.type AS type, t.status AS status, f.path AS file FROM tasks t INNER JOIN files f ON f.id = t.file_id INNER JOIN libraries l ON l.id = f.library_id ORDER BY CASE t.type WHEN 3 THEN -1 ELSE t.type END, t.id")
rows, err = db.Query(r.Context(), "SELECT t.id AS id, l.id AS library, t.worker_id AS worker, t.type AS type, 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 ORDER BY CASE t.type WHEN 3 THEN -1 ELSE t.type END, t.id")
if err != nil {
slog.ErrorContext(r.Context(), "Query Tasks", "err", err)
http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError)
@ -107,12 +110,13 @@ func handleTasks(w http.ResponseWriter, r *http.Request) {
}
for i := range tasks {
data.Tasks = append(data.Tasks, TaskDisplay{
ID: tasks[i].ID,
Library: tasks[i].Library,
Worker: tasks[i].Worker,
Type: tasks[i].Type,
File: tasks[i].File,
Status: tasks[i].Status.String(),
ID: tasks[i].ID,
Library: tasks[i].Library,
Worker: tasks[i].Worker,
Type: tasks[i].Type,
File: tasks[i].File,
Status: tasks[i].Status.String(),
UpdatedAt: tasks[i].UpdatedAt.Format(time.DateTime),
})
}

View file

@ -14,6 +14,7 @@
<th>Status</th>
<th>Health</th>
<th>MD5</th>
<th>Updated At</th>
</tr>
{{range $f := .Files}}
<tr>
@ -35,6 +36,9 @@
<td>
{{ $f.MD5 }}
</td>
<td>
{{ $f.UpdatedAt }}
</td>
</tr>
{{end}}
</table>

View file

@ -44,6 +44,7 @@ Total: {{.Stats.TotalCount}}
<th>Type</th>
<th>Status</th>
<th>File</th>
<th>Updated At</th>
</tr>
{{range $t := .Tasks}}
<tr onclick="window.location='/tasks/{{ $t.ID }}';">
@ -65,6 +66,9 @@ Total: {{.Stats.TotalCount}}
<td>
{{ $t.File }}
</td>
<td>
{{ $t.UpdatedAt }}
</td>
</tr>
{{end}}
</table>