api fixes - notifier fixes

pull/99/head v0.79.8
Hunter Long 2018-11-13 15:38:25 -08:00
parent 2eb5444a4e
commit 8e31fb4e96
9 changed files with 29 additions and 224 deletions

View File

@ -1,4 +1,4 @@
VERSION=0.79.7
VERSION=0.79.8
BINARY_NAME=statup
GOPATH:=$(GOPATH)
GOCMD=go

View File

@ -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
}

View File

@ -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)

View File

@ -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")
}

View File

@ -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)

View File

@ -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;

View File

@ -32,7 +32,7 @@
<div class="form-group row">
<label for="service_id" class="col-sm-4 col-form-label">Service</label>
<div class="col-sm-8">
<select class="form-control" name="service_id" id="service_id">
<select class="form-control" name="service" id="service_id">
<option value="0" {{if eq (ToString .ServiceId) "0"}}selected{{end}}>Global Message</option>
{{range Services}}
{{$s := .Select}}
@ -61,5 +61,6 @@
<button type="submit" class="btn btn-primary btn-block">{{if ne .Id 0}}Update Message{{else}}Create Message{{end}}</button>
</div>
</div>
<div class="alert alert-danger d-none" id="alerter" role="alert"></div>
</form>
{{end}}

View File

@ -1,6 +1,6 @@
{{define "form_notifier"}}
{{$n := .Select}}
<form method="POST" class="{{underscore $n.Method }}" action="/settings/notifier/{{ $n.Method }}">
<form class="ajax_form {{underscore $n.Method }}" data-func="SaveNotifier" action="/api/notifier/{{ $n.Method }}" method="POST">
{{if $n.Title}}<h4 class="text-capitalize">{{$n.Title}}</h4>{{end}}
{{if $n.Description}}<p class="small text-muted">{{safe $n.Description}}</p>{{end}}
@ -33,12 +33,12 @@
<div class="col-3 col-sm-2 mt-1">
<span class="switch">
<input type="checkbox" name="enable" class="switch" id="switch-{{ $n.Method }}" {{if $n.Enabled.Bool}}checked{{end}}>
<input type="checkbox" name="enabled" class="switch" id="switch-{{ $n.Method }}" {{if $n.Enabled.Bool}}checked{{end}}>
<label for="switch-{{ $n.Method }}"></label>
</span>
</div>
<input type="hidden" name="notifier" value="{{underscore $n.Method }}">
<input type="hidden" name="method" value="{{underscore $n.Method }}">
<div class="col-12 col-sm-4 mb-2 mb-sm-0 mt-2 mt-sm-0">
<button type="submit" class="btn btn-primary btn-block text-capitalize"><i class="fa fa-check-circle"></i> Save</button>
@ -67,5 +67,6 @@
<span class="text-capitalize">{{$n.Title}}</span> Notifier created by <a href="{{$n.AuthorUrl}}" target="_blank">{{$n.Author}}</a>
</span>
{{ end }}
<div class="alert alert-danger d-none" id="alerter" role="alert"></div>
</form>
{{end}}

View File

@ -100,5 +100,6 @@
</div>
{{end}}
</div>
<div class="alert alert-danger d-none" id="alerter" role="alert"></div>
</form>
{{end}}