Add Updated At to Tasks And Libraries
This commit is contained in:
parent
ec645f6162
commit
c04aa74364
14 changed files with 74 additions and 38 deletions
2
migrations/000005_alter_task_table_updated_at.down.sql
Normal file
2
migrations/000005_alter_task_table_updated_at.down.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE tasks
|
||||||
|
DROP updated_at;
|
2
migrations/000005_alter_task_table_updated_at.up.sql
Normal file
2
migrations/000005_alter_task_table_updated_at.up.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE tasks
|
||||||
|
ADD updated_at TIMESTAMP NOT NULL DEFAULT current_timestamp;
|
|
@ -0,0 +1 @@
|
||||||
|
DROP FUNCTION update_updated_at_column;
|
|
@ -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';
|
|
@ -0,0 +1 @@
|
||||||
|
DROP TRIGGER update_tasks_timestamp;
|
|
@ -0,0 +1 @@
|
||||||
|
CREATE TRIGGER update_tasks_timestamp BEFORE UPDATE ON tasks FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();
|
2
migrations/000008_alter_files_table_updated_at.down.sql
Normal file
2
migrations/000008_alter_files_table_updated_at.down.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE files
|
||||||
|
DROP updated_at;
|
2
migrations/000008_alter_files_table_updated_at.up.sql
Normal file
2
migrations/000008_alter_files_table_updated_at.up.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE files
|
||||||
|
ADD updated_at TIMESTAMP NOT NULL DEFAULT current_timestamp;
|
|
@ -0,0 +1 @@
|
||||||
|
DROP TRIGGER update_files_timestamp;
|
|
@ -0,0 +1 @@
|
||||||
|
CREATE TRIGGER update_files_timestamp BEFORE UPDATE ON files FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column();
|
|
@ -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"
|
||||||
|
@ -16,21 +17,23 @@ type LibraryData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
ID int `db:"id"`
|
ID int `db:"id"`
|
||||||
Path string `db:"path"`
|
Path string `db:"path"`
|
||||||
Size int64 `db:"size"`
|
Size int64 `db:"size"`
|
||||||
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 {
|
||||||
ID int
|
ID int
|
||||||
Path string
|
Path string
|
||||||
Size int64
|
Size int64
|
||||||
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)
|
||||||
|
@ -77,12 +80,13 @@ func handleLibrary(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
for i := range files {
|
for i := range files {
|
||||||
data.Files = append(data.Files, FileDisplay{
|
data.Files = append(data.Files, FileDisplay{
|
||||||
ID: files[i].ID,
|
ID: files[i].ID,
|
||||||
Path: files[i].Path,
|
Path: files[i].Path,
|
||||||
Size: files[i].Size,
|
Size: files[i].Size,
|
||||||
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),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
@ -32,21 +33,23 @@ type TaskStats struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type TaskDisplay struct {
|
type TaskDisplay struct {
|
||||||
ID int `db:"id"`
|
ID int `db:"id"`
|
||||||
Library int `db:"library"`
|
Library int `db:"library"`
|
||||||
Worker *string `db:"worker"`
|
Worker *string `db:"worker"`
|
||||||
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 {
|
||||||
ID int `db:"id"`
|
ID int `db:"id"`
|
||||||
Library int `db:"library"`
|
Library int `db:"library"`
|
||||||
Worker *string `db:"worker"`
|
Worker *string `db:"worker"`
|
||||||
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)
|
||||||
|
@ -107,12 +110,13 @@ func handleTasks(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
for i := range tasks {
|
for i := range tasks {
|
||||||
data.Tasks = append(data.Tasks, TaskDisplay{
|
data.Tasks = append(data.Tasks, TaskDisplay{
|
||||||
ID: tasks[i].ID,
|
ID: tasks[i].ID,
|
||||||
Library: tasks[i].Library,
|
Library: tasks[i].Library,
|
||||||
Worker: tasks[i].Worker,
|
Worker: tasks[i].Worker,
|
||||||
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),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Add table
Reference in a new issue