From d2f30eb04f9655ca61ec9f10524ac8e61d8f0787 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Sat, 14 Jan 2023 10:39:06 +0000 Subject: [PATCH] added last value to index page --- main.go | 24 ++++++++++++++++++++++++ models.go | 1 + templates/index.html | 4 ++++ todo.md | 3 +-- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 205c496..07cea5f 100644 --- a/main.go +++ b/main.go @@ -309,6 +309,30 @@ func (web *Web) index(c *gin.Context) { watchMap[filter.WatchID].CronEntry = &entry } + // get the last value stored, also doesn't really work with multiple values but meh again + rows, err := web.db.Table("watches"). + Select("watches.id, max(filter_outputs.time) as time, filter_outputs.value"). + Joins("left join filter_outputs on filter_outputs.watch_id = watches.id"). + Order("filter_outputs.name"). + Group("watches.id"). + Rows() + + if err != nil { + log.Println(err) + } else { + for rows.Next() { + var watchID uint + var _time string + var value string + err := rows.Scan(&watchID, &_time, &value) + if err != nil { + log.Println(err) + continue + } + watchMap[watchID].LastValue = value + } + } + c.HTML(http.StatusOK, "index", gin.H{ "watches": watches, "warnings": web.startupWarnings, diff --git a/models.go b/models.go index fa17752..092424a 100644 --- a/models.go +++ b/models.go @@ -12,6 +12,7 @@ type Watch struct { ID uint `form:"watch_id" yaml:"watch_id"` Name string `form:"watch_name" yaml:"watch_name" binding:"required" validate:"min=1"` CronEntry *cron.Entry `gorm:"-:all"` + LastValue string `gorm:"-:all"` } type Filter struct { diff --git a/templates/index.html b/templates/index.html index ec333e8..c7aa796 100644 --- a/templates/index.html +++ b/templates/index.html @@ -21,6 +21,7 @@ GoWatch Name Last Run Next Run + Last Value Edit Delete @@ -36,6 +37,9 @@ GoWatch {{ else }} No schedule (Add cron filter) {{ end }} + + {{ .LastValue }} + Edit diff --git a/todo.md b/todo.md index 754771b..14f78f6 100644 --- a/todo.md +++ b/todo.md @@ -9,5 +9,4 @@ - json - add index to docs/compose to fix link in pages - safe escape {{ }} for pages -- xpath/css innerthtml option? -- display value(s) on index page? \ No newline at end of file +- xpath/css innerthtml option? \ No newline at end of file