diff --git a/Makefile b/Makefile index 3589077b..6e25cd2d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=0.79.7 +VERSION=0.79.8 BINARY_NAME=statup GOPATH:=$(GOPATH) GOCMD=go diff --git a/core/database.go b/core/database.go index bc8aa690..83fa1df4 100644 --- a/core/database.go +++ b/core/database.go @@ -99,14 +99,15 @@ func (db *DbConfig) Close() error { // AfterFind for Service will set the timezone func (s *Service) AfterFind() (err error) { s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone) + s.UpdatedAt = utils.Timezoner(s.UpdatedAt, CoreApp.Timezone) return } // AfterFind for Hit will set the timezone -//func (h *Hit) AfterFind() (err error) { -// h.CreatedAt = utils.Timezoner(h.CreatedAt, CoreApp.Timezone) -// return -//} +func (h *Hit) AfterFind() (err error) { + h.CreatedAt = utils.Timezoner(h.CreatedAt, CoreApp.Timezone) + return +} // AfterFind for failure will set the timezone func (f *failure) AfterFind() (err error) { @@ -160,6 +161,7 @@ func (u *user) BeforeCreate() (err error) { func (s *Service) BeforeCreate() (err error) { if s.CreatedAt.IsZero() { s.CreatedAt = time.Now().UTC() + s.UpdatedAt = time.Now().UTC() } return } @@ -168,6 +170,7 @@ func (s *Service) BeforeCreate() (err error) { func (c *Checkin) BeforeCreate() (err error) { if c.CreatedAt.IsZero() { c.CreatedAt = time.Now().UTC() + c.UpdatedAt = time.Now().UTC() } return } diff --git a/core/services.go b/core/services.go index 2da96a73..8b7724e0 100644 --- a/core/services.go +++ b/core/services.go @@ -270,10 +270,14 @@ func GraphDataRaw(service types.ServiceInterface, start, end time.Time, group st var createdAt string var value float64 var createdTime time.Time + var err error rows.Scan(&createdAt, &value) createdTime, _ = time.Parse(types.TIME, createdAt) if CoreApp.DbConnection == "postgres" { - createdTime, _ = time.Parse(types.TIME_NANO, createdAt) + createdTime, err = time.Parse(types.TIME_NANO, createdAt) + if err != nil { + utils.Log(4, fmt.Errorf("issue parsing time from database: %v to %v", createdAt, types.TIME_NANO)) + } } gd.CreatedAt = utils.Timezoner(createdTime, CoreApp.Timezone).Format(types.TIME) gd.Value = int64(value * 1000) diff --git a/handlers/messages.go b/handlers/messages.go index 1a48baf0..26599df3 100644 --- a/handlers/messages.go +++ b/handlers/messages.go @@ -16,13 +16,8 @@ package handlers import ( - "fmt" - "github.com/gorilla/mux" "github.com/hunterlong/statup/core" - "github.com/hunterlong/statup/types" - "github.com/hunterlong/statup/utils" "net/http" - "time" ) func messagesHandler(w http.ResponseWriter, r *http.Request) { @@ -33,115 +28,3 @@ func messagesHandler(w http.ResponseWriter, r *http.Request) { messages, _ := core.SelectMessages() executeResponse(w, r, "messages.html", messages, nil) } - -func deleteMessageHandler(w http.ResponseWriter, r *http.Request) { - if !IsAuthenticated(r) { - http.Redirect(w, r, "/", http.StatusSeeOther) - return - } - vars := mux.Vars(r) - id := utils.StringInt(vars["id"]) - message, err := core.SelectMessage(id) - if err != nil { - http.Redirect(w, r, "/messages", http.StatusSeeOther) - return - } - message.Delete() - http.Redirect(w, r, "/messages", http.StatusSeeOther) -} - -func viewMessageHandler(w http.ResponseWriter, r *http.Request) { - if !IsAuthenticated(r) { - http.Redirect(w, r, "/", http.StatusSeeOther) - return - } - vars := mux.Vars(r) - id := utils.StringInt(vars["id"]) - message, err := core.SelectMessage(id) - if err != nil { - w.WriteHeader(http.StatusNotFound) - return - } - executeResponse(w, r, "message.html", message, nil) -} - -func updateMessageHandler(w http.ResponseWriter, r *http.Request) { - if !IsAuthenticated(r) { - http.Redirect(w, r, "/", http.StatusSeeOther) - return - } - vars := mux.Vars(r) - id := utils.StringInt(vars["id"]) - message, err := core.SelectMessage(id) - if err != nil { - w.WriteHeader(http.StatusNotFound) - return - } - r.ParseForm() - - title := r.PostForm.Get("title") - description := r.PostForm.Get("description") - notifyMethod := r.PostForm.Get("notify_method") - notifyUsers := r.PostForm.Get("notify_users") - startOn := r.PostForm.Get("start_on") - endOn := r.PostForm.Get("end_on") - notifyBefore := r.PostForm.Get("notify_before") - serviceId := utils.StringInt(r.PostForm.Get("service_id")) - - start, err := time.Parse(utils.FlatpickrTime, startOn) - if err != nil { - utils.Log(3, err) - } - end, _ := time.Parse(utils.FlatpickrTime, endOn) - before, _ := time.ParseDuration(notifyBefore) - - message.Title = title - message.Description = description - message.NotifyUsers = types.NewNullBool(notifyUsers == "on") - message.NotifyMethod = notifyMethod - message.StartOn = start.UTC() - message.EndOn = end.UTC() - message.NotifyBefore = before - message.ServiceId = serviceId - - message.Update() - executeResponse(w, r, "messages.html", message, "/messages") -} - -func createMessageHandler(w http.ResponseWriter, r *http.Request) { - if !IsAuthenticated(r) { - http.Redirect(w, r, "/", http.StatusSeeOther) - return - } - r.ParseForm() - - title := r.PostForm.Get("title") - description := r.PostForm.Get("description") - notifyMethod := r.PostForm.Get("notify_method") - notifyUsers := r.PostForm.Get("notify_users") - startOn := r.PostForm.Get("start_on") - endOn := r.PostForm.Get("end_on") - notifyBefore := r.PostForm.Get("notify_before") - serviceId := utils.StringInt(r.PostForm.Get("service_id")) - - start, _ := time.Parse(utils.FlatpickrTime, startOn) - end, _ := time.Parse(utils.FlatpickrTime, endOn) - before, _ := time.ParseDuration(notifyBefore) - - message := core.ReturnMessage(&types.Message{ - Title: title, - Description: description, - StartOn: start.UTC(), - EndOn: end.UTC(), - ServiceId: serviceId, - NotifyUsers: types.NewNullBool(notifyUsers == "on"), - NotifyMethod: notifyMethod, - NotifyBefore: before, - }) - _, err := message.Create() - if err != nil { - utils.Log(3, fmt.Sprintf("Error creating message %v", err)) - } - messages, _ := core.SelectMessages() - executeResponse(w, r, "messages.html", messages, "/messages") -} diff --git a/handlers/services.go b/handlers/services.go index 59094fba..685ea401 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -24,7 +24,6 @@ import ( "github.com/hunterlong/statup/utils" "net" "net/http" - "strconv" "time" ) @@ -77,64 +76,6 @@ func reorderServiceHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -func createServiceHandler(w http.ResponseWriter, r *http.Request) { - if !IsAuthenticated(r) { - http.Redirect(w, r, "/", http.StatusSeeOther) - return - } - r.ParseForm() - name := r.PostForm.Get("name") - domain := r.PostForm.Get("domain") - method := r.PostForm.Get("method") - expected := r.PostForm.Get("expected") - status, _ := strconv.Atoi(r.PostForm.Get("expected_status")) - interval, _ := strconv.Atoi(r.PostForm.Get("interval")) - port, _ := strconv.Atoi(r.PostForm.Get("port")) - timeout, _ := strconv.Atoi(r.PostForm.Get("timeout")) - checkType := r.PostForm.Get("check_type") - postData := r.PostForm.Get("post_data") - order, _ := strconv.Atoi(r.PostForm.Get("order")) - - if checkType == "http" && status == 0 { - status = 200 - } - - service := core.ReturnService(&types.Service{ - Name: name, - Domain: domain, - Method: method, - Expected: types.NewNullString(expected), - ExpectedStatus: status, - Interval: interval, - Type: checkType, - Port: port, - PostData: types.NewNullString(postData), - Timeout: timeout, - Order: order, - }) - _, err := service.Create(true) - if err != nil { - utils.Log(3, fmt.Sprintf("Error starting %v check routine. %v", service.Name, err)) - } - //notifiers.OnNewService(core.ReturnService(service.Service)) - executeResponse(w, r, "services.html", core.CoreApp.Services, "/services") -} - -func servicesDeleteHandler(w http.ResponseWriter, r *http.Request) { - if !IsAuthenticated(r) { - http.Redirect(w, r, "/", http.StatusSeeOther) - return - } - vars := mux.Vars(r) - service := core.SelectService(utils.StringInt(vars["id"])) - if service == nil { - w.WriteHeader(http.StatusNotFound) - return - } - service.Delete() - executeResponse(w, r, "services.html", core.CoreApp.Services, "/services") -} - func servicesViewHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) fields := parseGet(r) @@ -174,43 +115,6 @@ func servicesViewHandler(w http.ResponseWriter, r *http.Request) { executeResponse(w, r, "service.html", out, nil) } -func servicesUpdateHandler(w http.ResponseWriter, r *http.Request) { - if !IsAuthenticated(r) { - http.Redirect(w, r, "/", http.StatusSeeOther) - return - } - vars := mux.Vars(r) - service := core.SelectService(utils.StringInt(vars["id"])) - r.ParseForm() - name := r.PostForm.Get("name") - domain := r.PostForm.Get("domain") - method := r.PostForm.Get("method") - expected := r.PostForm.Get("expected") - status, _ := strconv.Atoi(r.PostForm.Get("expected_status")) - interval, _ := strconv.Atoi(r.PostForm.Get("interval")) - port, _ := strconv.Atoi(r.PostForm.Get("port")) - timeout, _ := strconv.Atoi(r.PostForm.Get("timeout")) - checkType := r.PostForm.Get("check_type") - postData := r.PostForm.Get("post_data") - order, _ := strconv.Atoi(r.PostForm.Get("order")) - - service.Name = name - service.Domain = domain - service.Method = method - service.ExpectedStatus = status - service.Expected = types.NewNullString(expected) - service.Interval = interval - service.Type = checkType - service.Port = port - service.PostData = types.NewNullString(postData) - service.Timeout = timeout - service.Order = order - - service.Update(true) - go service.Check(true) - executeResponse(w, r, "service.html", service, "/services") -} - func servicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) { if !IsAuthenticated(r) { http.Redirect(w, r, "/", http.StatusSeeOther) diff --git a/source/js/main.js b/source/js/main.js index c6b771c7..007c55c3 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -76,6 +76,14 @@ function Spinner(btn, off = false) { } } +function SaveNotifier(data) { + console.log(data) + let button = data.element.find('button[type=submit]'); + button.text('Saved!') + button.removeClass('btn-primary') + button.addClass('btn-success') +} + $('select#service_type').on('change', function() { var selected = $('#service_type option:selected').val(); var typeLabel = $('#service_type_label'); @@ -204,7 +212,7 @@ $('form.ajax_form').on('submit', function() { Spinner(button, true); if (func) { let fn = window[func]; - if (typeof fn === "function") fn({form: newArr, data: data}); + if (typeof fn === "function") fn({element: form, form: newArr, data: data}); } if (redirect) { window.location.href = redirect; diff --git a/source/tmpl/form_message.html b/source/tmpl/form_message.html index 6292eb95..daf3d9c7 100644 --- a/source/tmpl/form_message.html +++ b/source/tmpl/form_message.html @@ -32,7 +32,7 @@
- {{range Services}} {{$s := .Select}} @@ -61,5 +61,6 @@
+ {{end}} diff --git a/source/tmpl/form_notifier.html b/source/tmpl/form_notifier.html index dddf6d22..36d4bb5c 100644 --- a/source/tmpl/form_notifier.html +++ b/source/tmpl/form_notifier.html @@ -1,6 +1,6 @@ {{define "form_notifier"}} {{$n := .Select}} -
+ {{if $n.Title}}

{{$n.Title}}

{{end}} {{if $n.Description}}

{{safe $n.Description}}

{{end}} @@ -33,12 +33,12 @@
- +
- +
@@ -67,5 +67,6 @@ {{$n.Title}} Notifier created by {{$n.Author}} {{ end }} + {{end}} diff --git a/source/tmpl/form_service.html b/source/tmpl/form_service.html index 2715274b..84549909 100644 --- a/source/tmpl/form_service.html +++ b/source/tmpl/form_service.html @@ -100,5 +100,6 @@
{{end}} + {{end}}