From 6dd611204072d654a458e41c84002431713296a5 Mon Sep 17 00:00:00 2001 From: nxshock Date: Fri, 21 Oct 2022 20:12:41 +0500 Subject: [PATCH] Add job categories --- README.md | 1 + httpserver.go | 28 +++++++++++++++++++++++++--- job.go | 2 ++ webui/index.htm | 7 ++++--- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b4c16ea..6e8caf6 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ 2. Create job config in `gron.d/job1.conf` ([TOML](https://en.wikipedia.org/wiki/TOML) format): ```toml Type = "cmd" # command execution + Category = "Test jobs" # jobs category name Cron = "* * * * *" # cron instructions Command = "echo Hello" # command to execute diff --git a/httpserver.go b/httpserver.go index 8a86a98..878191e 100644 --- a/httpserver.go +++ b/httpserver.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "os" + "sort" "time" log "github.com/sirupsen/logrus" @@ -40,13 +41,34 @@ func handler(w http.ResponseWriter, r *http.Request) { globalMutex.RLock() buf := new(bytes.Buffer) jobEntries := c.Entries() - var jobs []*Job + + jobs := make(map[string][]*Job) for _, jobEntry := range jobEntries { job := jobEntry.Job.(*Job) job.NextLaunch = jobEntry.Next.Format(config.TimeFormat) - jobs = append(jobs, job) + jobs[job.JobConfig.Category] = append(jobs[job.JobConfig.Category], job) } - _ = templates.ExecuteTemplate(buf, "index.htm", jobs) + + var keys []string + for k, v := range jobs { + keys = append(keys, k) + + sort.Slice(v, func(i, j int) bool { + return v[i].Name < v[j].Name + }) + } + + sort.Slice(keys, func(i, j int) bool { + return keys[i] < keys[j] + }) + + _ = templates.ExecuteTemplate(buf, "index.htm", struct { + Categories []string + Jobs map[string][]*Job + }{ + Categories: keys, + Jobs: jobs, + }) globalMutex.RUnlock() _, _ = buf.WriteTo(w) diff --git a/job.go b/job.go index f4c0dec..062abb3 100644 --- a/job.go +++ b/job.go @@ -21,6 +21,8 @@ import ( type JobConfig struct { Type JobType + Category string + // JobType = Cmd Command string // command for execution diff --git a/webui/index.htm b/webui/index.htm index 9d7163c..673a867 100644 --- a/webui/index.htm +++ b/webui/index.htm @@ -20,7 +20,8 @@ ⏻ Shutdown -

Job list

+ {{range .Categories}} +

{{if eq . ""}}Other jobs{{else}}{{.}}{{end}}

@@ -33,7 +34,7 @@ - {{range .}} + {{range (index $.Jobs .)}} {{end}} -
NameNext launch Details
@@ -50,7 +51,7 @@
{{.NextLaunch}} open
+ {{end}}