implement upsert, list in UI

master
bel 2020-03-15 16:41:41 +00:00
parent 052bb05331
commit a6ea7a79e8
4 changed files with 61 additions and 52 deletions

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"local/firestormy/config" "local/firestormy/config"
"local/firestormy/config/ns" "local/firestormy/config/ns"
"local/firestormy/logger"
"os/exec" "os/exec"
"strings" "strings"
"time" "time"
@ -58,7 +57,6 @@ func newBashJob(schedule, sh string) (*Job, error) {
if cmd != nil && cmd.ProcessState != nil { if cmd != nil && cmd.ProcessState != nil {
j.LastStatus = cmd.ProcessState.ExitCode() j.LastStatus = cmd.ProcessState.ExitCode()
} }
logger.New().Info(fmt.Sprintf("%+v", j))
b, err := j.Encode() b, err := j.Encode()
if err == nil { if err == nil {
// TODO webpage doenst load post SET despite this returning nil // TODO webpage doenst load post SET despite this returning nil

49
server/list.go Executable file
View File

@ -0,0 +1,49 @@
package server
import (
"encoding/json"
"local/firestormy/config"
"local/firestormy/config/ns"
"local/firestormy/scheduler"
"net/http"
"sort"
"time"
)
func (s *Server) list(w http.ResponseWriter, r *http.Request) {
jobs, err := config.Store.List(ns.Jobs)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sort.Strings(jobs)
out := make([]map[string]interface{}, len(jobs))
for i, job := range jobs {
out[i] = make(map[string]interface{})
b, err := config.Store.Get(job, ns.Jobs...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
j := &scheduler.Job{}
if err := j.Decode(b); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tz, err := time.LoadLocation("America/Denver")
if err != nil {
panic(err)
}
out[i]["id"] = j.Name
out[i]["cron"] = j.Schedule
out[i]["language"] = j.Runner.String()
out[i]["script"] = j.Raw
out[i]["last"] = map[string]interface{}{
"run": j.LastRun.In(tz).Format(`2006-01-02 15:04:05 MST`),
"runtime": j.LastRuntime.String(),
"output": string(j.LastOutput),
"status": j.LastStatus,
}
}
json.NewEncoder(w).Encode(out)
}

View File

@ -1,15 +1,9 @@
package server package server
import ( import (
"encoding/json"
"fmt" "fmt"
"local/firestormy/config"
"local/firestormy/config/ns"
"local/firestormy/scheduler"
"local/router" "local/router"
"log"
"net/http" "net/http"
"sort"
) )
func (s *Server) Routes() error { func (s *Server) Routes() error {
@ -43,47 +37,3 @@ func (s *Server) Routes() error {
func (s *Server) static(w http.ResponseWriter, r *http.Request) { func (s *Server) static(w http.ResponseWriter, r *http.Request) {
s.fileServer.ServeHTTP(w, r) s.fileServer.ServeHTTP(w, r)
} }
func (s *Server) upserts(w http.ResponseWriter, r *http.Request) {
upsert, err := newUpsertRequest(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Println("received", upsert)
http.Error(w, "not impl", http.StatusNotImplemented)
}
func (s *Server) list(w http.ResponseWriter, r *http.Request) {
jobs, err := config.Store.List(ns.Jobs)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sort.Strings(jobs)
out := make([]map[string]interface{}, len(jobs))
for i, job := range jobs {
out[i] = make(map[string]interface{})
b, err := config.Store.Get(job, ns.Jobs...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
j := &scheduler.Job{}
if err := j.Decode(b); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out[i]["id"] = j.Name
out[i]["cron"] = j.Schedule
out[i]["language"] = j.Runner.String()
out[i]["script"] = j.Raw
out[i]["last"] = map[string]interface{}{
"run": j.LastRun,
"runtime": j.LastRuntime,
"output": j.LastOutput,
"status": j.LastStatus,
}
}
json.NewEncoder(w).Encode(out)
}

View File

@ -7,6 +7,8 @@ import (
"io" "io"
"local/firestormy/config" "local/firestormy/config"
"local/firestormy/config/ns" "local/firestormy/config/ns"
"log"
"net/http"
"github.com/google/uuid" "github.com/google/uuid"
) )
@ -44,3 +46,13 @@ func (u *upsertRequest) validate() error {
} }
return nil return nil
} }
func (s *Server) upserts(w http.ResponseWriter, r *http.Request) {
upsert, err := newUpsertRequest(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Println("received", upsert)
http.Error(w, "not impl", http.StatusNotImplemented)
}