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" "fmt"
"log/slog" "log/slog"
"net/http" "net/http"
"time"
"git.lastassault.de/speatzle/morffix/constants" "git.lastassault.de/speatzle/morffix/constants"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
@ -22,6 +23,7 @@ type File struct {
Status constants.FileStatus `db:"status"` Status constants.FileStatus `db:"status"`
Health constants.FileHealth `db:"health"` Health constants.FileHealth `db:"health"`
MD5 []byte `db:"md5"` MD5 []byte `db:"md5"`
UpdatedAt time.Time `db:"updated_at"`
} }
type FileDisplay struct { type FileDisplay struct {
@ -31,6 +33,7 @@ type FileDisplay struct {
Status string Status string
Health string Health string
MD5 string MD5 string
UpdatedAt string `db:"updated_at"`
} }
func handleLibrary(w http.ResponseWriter, r *http.Request) { func handleLibrary(w http.ResponseWriter, r *http.Request) {
@ -62,7 +65,7 @@ func handleLibrary(w http.ResponseWriter, r *http.Request) {
// TODO // 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 { if err != nil {
slog.ErrorContext(r.Context(), "Query Files", "err", err) slog.ErrorContext(r.Context(), "Query Files", "err", err)
http.Error(w, "Error Query Files: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Error Query Files: "+err.Error(), http.StatusInternalServerError)
@ -83,6 +86,7 @@ func handleLibrary(w http.ResponseWriter, r *http.Request) {
Status: files[i].Status.String(), Status: files[i].Status.String(),
Health: files[i].Health.String(), Health: files[i].Health.String(),
MD5: fmt.Sprintf("%x", files[i].MD5), MD5: fmt.Sprintf("%x", files[i].MD5),
UpdatedAt: files[i].UpdatedAt.Format(time.DateTime),
}) })
} }

View file

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"log/slog" "log/slog"
"net/http" "net/http"
"time"
"git.lastassault.de/speatzle/morffix/constants" "git.lastassault.de/speatzle/morffix/constants"
"git.lastassault.de/speatzle/morffix/types" "git.lastassault.de/speatzle/morffix/types"
@ -38,6 +39,7 @@ type TaskDisplay struct {
Type int `db:"type"` Type int `db:"type"`
Status string `db:"status"` Status string `db:"status"`
File string `db:"file"` File string `db:"file"`
UpdatedAt string `db:"updated_at"`
} }
type TaskDB struct { type TaskDB struct {
@ -47,6 +49,7 @@ type TaskDB struct {
Type int `db:"type"` Type int `db:"type"`
Status constants.TaskStatus `db:"status"` Status constants.TaskStatus `db:"status"`
File string `db:"file"` File string `db:"file"`
UpdatedAt time.Time `db:"updated_at"`
} }
func handleTasks(w http.ResponseWriter, r *http.Request) { func handleTasks(w http.ResponseWriter, r *http.Request) {
@ -93,7 +96,7 @@ func handleTasks(w http.ResponseWriter, r *http.Request) {
} }
data.Libraries = libraries 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 { if err != nil {
slog.ErrorContext(r.Context(), "Query Tasks", "err", err) slog.ErrorContext(r.Context(), "Query Tasks", "err", err)
http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError) http.Error(w, "Error Query Tasks: "+err.Error(), http.StatusInternalServerError)
@ -113,6 +116,7 @@ func handleTasks(w http.ResponseWriter, r *http.Request) {
Type: tasks[i].Type, Type: tasks[i].Type,
File: tasks[i].File, File: tasks[i].File,
Status: tasks[i].Status.String(), Status: tasks[i].Status.String(),
UpdatedAt: tasks[i].UpdatedAt.Format(time.DateTime),
}) })
} }

View file

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

View file

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