From 0971908da73aff4bc0c3ea2926cdedf592cd9fc1 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 15 Mar 2020 20:14:03 +0000 Subject: [PATCH] Implement client side enable, disable, edit --- public/css/water.css | 8 ++--- public/index.html | 3 +- public/js/js.js | 55 ++++++++++++++++++++++++++++---- scheduler/job.go | 2 ++ scheduler/job_test.go | 3 ++ server/list.go | 1 + server/routes.go | 6 ++-- server/{upserts.go => upsert.go} | 3 +- 8 files changed, 63 insertions(+), 18 deletions(-) rename server/{upserts.go => upsert.go} (92%) diff --git a/public/css/water.css b/public/css/water.css index 84d0ee8..58f41ee 100755 --- a/public/css/water.css +++ b/public/css/water.css @@ -10,19 +10,15 @@ tbody tr:nth-child(2n+1) { background-color: #161f27; } -#upserts { +#upsert { width: 100%; } -#upserts > textarea { +#upsert > textarea { width: 100%; resize: vertical; } -#upserts > input[name="id"] { - display: none; -} - #jobs > tbody > tr > td { padding: 0; } diff --git a/public/index.html b/public/index.html index 5426681..aa66f16 100755 --- a/public/index.html +++ b/public/index.html @@ -8,8 +8,9 @@
Upsert Job -
+ + diff --git a/public/js/js.js b/public/js/js.js index fdc0062..1e1f287 100755 --- a/public/js/js.js +++ b/public/js/js.js @@ -12,11 +12,11 @@ function http(method, remote, callback, body) { xmlhttp.send(body); } -function upserts() { +function upsert() { function cb(body, status) { console.log(status, body) } - http("POST", "/upserts", cb, jsonifyForm("upserts")) + http("POST", "/api/job/upsert", cb, jsonifyForm("upsert")) } function jsonifyForm(id) { @@ -33,8 +33,6 @@ function init() { jobs.forEach(function(job) { var s = format(job) inject(s) - console.log("job=", job) - console.log("s=", s) }) } function format(job) { @@ -55,12 +53,12 @@ function init() { btns.forEach(function(e) { buttons += ` - + ` }) return `
- + ${job.title} ${passing} ${buttons} @@ -87,7 +85,50 @@ function init() { var table = document.getElementById("jobs").getElementsByTagName("tbody")[0] table.innerHTML += s } - http("GET", "/list", cb, null) + http("GET", "/api/job/list", cb, null) } init() + +function jobdisable(input) { + jobmodify(input) + getField("disabled").checked = true +} + +function jobenable(input) { + jobmodify(input) + getField("disabled").checked = false +} + +function jobmodify(input) { + var form = getForm() + var job = jobFromInput(input) + var fields = ["id", "language", "cron", "script", "disabled"] + fields.forEach(function(field) { + var e = getField(field) + e.checked = job[field] + e.value = job[field] + }) + var details = form.parentElement + details.setAttribute("open", "") +} + +function jobdelete(input) { + http("DELETE", "/api/job/delete", cb, null) +} + +function jobFromInput(input) { + var b64 = input.getAttribute("job") + var json = atob(b64) + return JSON.parse(json) +} + +function getForm() { + return document.getElementById("upsert") +} + +function getField(name) { + var form = getForm() + var matches = form.elements[name] + return matches +} diff --git a/scheduler/job.go b/scheduler/job.go index 6359c1b..73de80b 100755 --- a/scheduler/job.go +++ b/scheduler/job.go @@ -25,6 +25,7 @@ type Job struct { LastOutput string LastRuntime time.Duration LastRun time.Time + Disabled bool } func NewJob(runner Runner, schedule, raw string) (*Job, error) { @@ -100,6 +101,7 @@ func (j *Job) Decode(b []byte) error { k.LastOutput = j.LastOutput k.LastRuntime = j.LastRuntime k.LastRun = j.LastRun + k.Disabled = j.Disabled *j = *k } return err diff --git a/scheduler/job_test.go b/scheduler/job_test.go index 116145b..227ebd1 100755 --- a/scheduler/job_test.go +++ b/scheduler/job_test.go @@ -89,6 +89,9 @@ func TestJobEncodeDecode(t *testing.T) { if k.Name != j.Name { t.Error(k.Name, "vs", j.Name) } + if k.Disabled != j.Disabled { + t.Error(k.Disabled, "vs", j.Disabled) + } if k.Title != j.Title { t.Error(k.Title, "vs", j.Title) } diff --git a/server/list.go b/server/list.go index e5b01ca..3a9b8c4 100755 --- a/server/list.go +++ b/server/list.go @@ -33,6 +33,7 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err) } + out[i]["disabled"] = j.Disabled out[i]["id"] = j.Name out[i]["title"] = j.Title out[i]["cron"] = j.Schedule diff --git a/server/routes.go b/server/routes.go index 097b7c0..aae54c5 100755 --- a/server/routes.go +++ b/server/routes.go @@ -13,11 +13,11 @@ func (s *Server) Routes() error { handler http.HandlerFunc }{ { - path: fmt.Sprintf("/upserts"), - handler: s.gzip(s.authenticate(s.upserts)), + path: fmt.Sprintf("/api/job/upsert"), + handler: s.gzip(s.authenticate(s.upsert)), }, { - path: fmt.Sprintf("/list"), + path: fmt.Sprintf("/api/job/list"), handler: s.gzip(s.authenticate(s.list)), }, { diff --git a/server/upserts.go b/server/upsert.go similarity index 92% rename from server/upserts.go rename to server/upsert.go index 26e6ad1..34fe3b1 100755 --- a/server/upserts.go +++ b/server/upsert.go @@ -19,6 +19,7 @@ type upsertRequest struct { Language string `json:"language"` Cron string `json:"cron"` Script string `json:"script"` + Disabled bool `json:"disabled"` } func newUpsertRequest(r io.Reader) (upsertRequest, error) { @@ -51,7 +52,7 @@ func (u *upsertRequest) validate() error { return nil } -func (s *Server) upserts(w http.ResponseWriter, r *http.Request) { +func (s *Server) upsert(w http.ResponseWriter, r *http.Request) { upsert, err := newUpsertRequest(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest)