From 9a5c14bf6598bb6b2ccc26f02ea88097e70c8b6f Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Fri, 7 Oct 2022 19:54:18 +0000 Subject: [PATCH] kinda working watch view page --- main.go | 46 ++++++- models.go | 9 +- templates/index.html | 14 ++- templates/watch/edit.html | 213 ++++++++++++++++++++++++++++++++ templates/watch/view.html | 249 ++++++++------------------------------ 5 files changed, 324 insertions(+), 207 deletions(-) create mode 100644 templates/watch/edit.html diff --git a/main.go b/main.go index f2b240f..eb651bf 100644 --- a/main.go +++ b/main.go @@ -65,7 +65,8 @@ func (web *Web) initRouter() { web.router.GET("/", web.index) - web.router.GET("/watch/:id", web.watchView) + web.router.GET("/watch/view/:id", web.watchView) + web.router.GET("/watch/edit/:id", web.watchEdit) web.router.GET("/watch/new", web.watchCreate) web.router.POST("/watch/create", web.watchCreatePost) web.router.POST("/watch/update", web.watchUpdate) @@ -80,6 +81,7 @@ func (web *Web) initTemplates() { web.templates.AddFromFiles("index", "templates/base.html", "templates/index.html") web.templates.AddFromFiles("watchCreate", "templates/base.html", "templates/watch/create.html") web.templates.AddFromFiles("watchView", "templates/base.html", "templates/watch/view.html") + web.templates.AddFromFiles("watchEdit", "templates/base.html", "templates/watch/edit.html") web.templates.AddFromFiles("cacheView", "templates/base.html", "templates/cache/view.html") @@ -107,11 +109,22 @@ func (web *Web) run() { web.router.Run("0.0.0.0:8080") } +type WatchEntry struct { + Watch *Watch + Entry *cron.Entry +} + func (web *Web) index(c *gin.Context) { //msg := tgbotapi.NewMessage(viper.GetInt64("telegram.chat"), message) //web.Bot.Send(msg) watches := []Watch{} web.db.Find(&watches) + + for i := 0; i < len(watches); i++ { + entry := web.cronWatch[watches[i].ID] + watches[i].CronEntry = &entry + } + c.HTML(http.StatusOK, "index", watches) } @@ -149,6 +162,33 @@ func (web *Web) deleteWatch(c *gin.Context) { func (web *Web) watchView(c *gin.Context) { id := c.Param("id") + var watch Watch + web.db.Model(&Watch{}).First(&watch, id) + entry, exists := web.cronWatch[watch.ID] + if !exists { + log.Println("Could not find entry for Watch", watch.ID) + c.HTML(http.StatusNotFound, "watchView", gin.H{"error": "Entry not found"}) + return + } + watch.CronEntry = &entry + + var values []FilterOutput + web.db.Model(&FilterOutput{}).Where("watch_id = ?", watch.ID).Find(&values) + + valueMap := make(map[string][]FilterOutput, len(values)) + for _, value := range values { + valueMap[value.Name] = append(valueMap[value.Name], value) + } + + c.HTML(http.StatusOK, "watchView", gin.H{ + "Watch": watch, + "ValueMap": valueMap, + }) +} + +func (web *Web) watchEdit(c *gin.Context) { + id := c.Param("id") + var watch Watch web.db.Model(&Watch{}).First(&watch, id) @@ -164,7 +204,7 @@ func (web *Web) watchView(c *gin.Context) { buildFilterTree(filters, connections) processFilters(filters, web, &watch, true, true) - c.HTML(http.StatusOK, "watchView", gin.H{ + c.HTML(http.StatusOK, "watchEdit", gin.H{ "Watch": watch, "Filters": filters, "Connections": connections, @@ -216,7 +256,7 @@ func (web *Web) watchUpdate(c *gin.Context) { web.db.Create(&newConnections) } - c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/%d", watch.ID)) + c.Redirect(http.StatusSeeOther, fmt.Sprintf("/watch/edit/%d", watch.ID)) } func (web *Web) cacheView(c *gin.Context) { diff --git a/models.go b/models.go index 7a126fe..bf09507 100644 --- a/models.go +++ b/models.go @@ -4,12 +4,15 @@ import ( "fmt" "html" "time" + + "github.com/robfig/cron/v3" ) type Watch struct { - ID uint `form:"watch_id" yaml:"watch_id"` - Name string `form:"watch_name" yaml:"watch_name" binding:"required" validate:"min=1"` - Interval int `form:"interval" yaml:"interval" binding:"required"` + ID uint `form:"watch_id" yaml:"watch_id"` + Name string `form:"watch_name" yaml:"watch_name" binding:"required" validate:"min=1"` + Interval int `form:"interval" yaml:"interval" binding:"required"` + CronEntry *cron.Entry `gorm:"-:all"` } type Filter struct { diff --git a/templates/index.html b/templates/index.html index d12eee5..530c8d7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,15 +3,21 @@ Name - Interval - + Last Run + Next Run + Edit + Delete {{ range . }} - + {{ .Name }} - {{ .Interval }} + {{ .CronEntry.Prev.Format "2006-01-02 15:04:05" }} + {{ .CronEntry.Next.Format "2006-01-02 15:04:05" }} + + Edit +
diff --git a/templates/watch/edit.html b/templates/watch/edit.html new file mode 100644 index 0000000..c19bbdc --- /dev/null +++ b/templates/watch/edit.html @@ -0,0 +1,213 @@ +{{define "head"}} + + +{{ end }} +{{define "content"}} +
+ + + +
+ +{{ end }} + +{{ define "left" }} + + + + + + + +
+ + + + + +
+ + +
+ + + + + + + +{{ end }} + +{{define "scripts"}} + +{{ end }} \ No newline at end of file diff --git a/templates/watch/view.html b/templates/watch/view.html index c19bbdc..640b759 100644 --- a/templates/watch/view.html +++ b/templates/watch/view.html @@ -1,213 +1,68 @@ -{{define "head"}} - - -{{ end }} {{define "content"}} -
- - - -
- +{{ if .error }} + Could not find entry {{ end }} -{{ define "left" }} - - - - - - - -
- - - - - -
- - -