From a26b9cc63e306f15a5b15997e4690dbda01a4001 Mon Sep 17 00:00:00 2001 From: Finn Date: Sun, 24 Nov 2024 16:08:06 -0800 Subject: [PATCH] Show logs when editing a code --- db/helpers.go | 9 +++-- db/lock_log.sql.go | 76 ++++++++++++++++++++++++++++++++++++ db/queries/lock_log.sql | 6 +++ frontend/frontend.go | 4 +- frontend/lock-code-edit.html | 7 ++++ frontend/lock.html | 3 ++ httpserver/lock.go | 13 ++++++ 7 files changed, 113 insertions(+), 5 deletions(-) diff --git a/db/helpers.go b/db/helpers.go index a6c1062..85c135e 100644 --- a/db/helpers.go +++ b/db/helpers.go @@ -49,10 +49,11 @@ func Migrate() error { } func NullString(s string) sql.NullString { - return sql.NullString{ - Valid: s != "", - String: s, - } + return sql.NullString{Valid: s != "", String: s} +} + +func NullInt64(i int64) sql.NullInt64 { + return sql.NullInt64{Valid: true, Int64: i} } type loggingDBTX struct { diff --git a/db/lock_log.sql.go b/db/lock_log.sql.go index b777285..5507db2 100644 --- a/db/lock_log.sql.go +++ b/db/lock_log.sql.go @@ -25,6 +25,44 @@ func (q *Queries) AddLogEntry(ctx context.Context, arg AddLogEntryParams) error return err } +const getLastLogForSlot = `-- name: GetLastLogForSlot :many +SELECT lock, timestamp, state, code, issued_code FROM lock_log WHERE lock = ? AND code = ? ORDER BY timestamp DESC LIMIT 1 +` + +type GetLastLogForSlotParams struct { + Lock int64 + Code sql.NullInt64 +} + +func (q *Queries) GetLastLogForSlot(ctx context.Context, arg GetLastLogForSlotParams) ([]LockLog, error) { + rows, err := q.db.QueryContext(ctx, getLastLogForSlot, arg.Lock, arg.Code) + if err != nil { + return nil, err + } + defer rows.Close() + var items []LockLog + for rows.Next() { + var i LockLog + if err := rows.Scan( + &i.Lock, + &i.Timestamp, + &i.State, + &i.Code, + &i.IssuedCode, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getLogForLock = `-- name: GetLogForLock :many SELECT lock, timestamp, state, code, issued_code FROM lock_log WHERE lock = ? ORDER BY timestamp DESC ` @@ -57,3 +95,41 @@ func (q *Queries) GetLogForLock(ctx context.Context, lock int64) ([]LockLog, err } return items, nil } + +const getLogForSlot = `-- name: GetLogForSlot :many +SELECT lock, timestamp, state, code, issued_code FROM lock_log WHERE lock = ? AND code = ? ORDER BY timestamp DESC +` + +type GetLogForSlotParams struct { + Lock int64 + Code sql.NullInt64 +} + +func (q *Queries) GetLogForSlot(ctx context.Context, arg GetLogForSlotParams) ([]LockLog, error) { + rows, err := q.db.QueryContext(ctx, getLogForSlot, arg.Lock, arg.Code) + if err != nil { + return nil, err + } + defer rows.Close() + var items []LockLog + for rows.Next() { + var i LockLog + if err := rows.Scan( + &i.Lock, + &i.Timestamp, + &i.State, + &i.Code, + &i.IssuedCode, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/db/queries/lock_log.sql b/db/queries/lock_log.sql index adfe54f..6fda69a 100644 --- a/db/queries/lock_log.sql +++ b/db/queries/lock_log.sql @@ -3,3 +3,9 @@ INSERT INTO lock_log (lock, state, code) VALUES (?, ?, ?); -- name: GetLogForLock :many SELECT * FROM lock_log WHERE lock = ? ORDER BY timestamp DESC; + +-- name: GetLogForSlot :many +SELECT * FROM lock_log WHERE lock = ? AND code = ? ORDER BY timestamp DESC; + +-- name: GetLastLogForSlot :many +SELECT * FROM lock_log WHERE lock = ? AND code = ? ORDER BY timestamp DESC LIMIT 1; diff --git a/frontend/frontend.go b/frontend/frontend.go index a57ea99..526381c 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -5,6 +5,7 @@ import ( "fmt" "html/template" "io/fs" + "time" "git.janky.solutions/finn/lockserver/config" ) @@ -19,7 +20,8 @@ var ( Templates *template.Template funcs = template.FuncMap{ - "version": func() string { return fmt.Sprintf("better-zwave-locks %s", config.Version) }, + "version": func() string { return fmt.Sprintf("better-zwave-locks %s", config.Version) }, + "time_since": func(t time.Time) string { return time.Since(t).Round(time.Second).String() }, } ) diff --git a/frontend/lock-code-edit.html b/frontend/lock-code-edit.html index 4faa47c..e996ff3 100644 --- a/frontend/lock-code-edit.html +++ b/frontend/lock-code-edit.html @@ -8,4 +8,11 @@
+

+ + {{ template "footer.html" }} diff --git a/frontend/lock.html b/frontend/lock.html index 1da075a..af71153 100644 --- a/frontend/lock.html +++ b/frontend/lock.html @@ -20,4 +20,7 @@ {{ end }} + +

+ {{ template "footer.html" }} diff --git a/httpserver/lock.go b/httpserver/lock.go index c5ef1da..192fa38 100644 --- a/httpserver/lock.go +++ b/httpserver/lock.go @@ -1,6 +1,8 @@ package httpserver import ( + "database/sql" + "errors" "fmt" "net/http" "strconv" @@ -8,6 +10,7 @@ import ( "git.janky.solutions/finn/lockserver/db" "git.janky.solutions/finn/lockserver/zwavejs" echo "github.com/labstack/echo/v4" + "github.com/sirupsen/logrus" ) func lockHandler(c echo.Context) error { @@ -109,9 +112,19 @@ func lockCodeEditHandler(c echo.Context) error { } if c.Request().Method == http.MethodGet { + logrus.WithField("lock", lockID).WithField("code", code.ID).Debug("querying logs") + log, err := queries.GetLogForSlot(ctx, db.GetLogForSlotParams{ + Lock: lockID, + Code: db.NullInt64(code.ID), + }) + if err != nil && !errors.Is(err, sql.ErrNoRows) { + return err + } + return c.Render(http.StatusFound, "lock-code-edit.html", map[string]interface{}{ "lock": lock, "code": code, + "log": log, }) }