From 0f208c979f75816c8a293e163308d5e30843734e Mon Sep 17 00:00:00 2001 From: hunterlong Date: Wed, 26 Sep 2018 08:26:16 -0700 Subject: [PATCH] notifier testing js --- core/database.go | 2 +- core/notifier/interfaces.go | 2 +- handlers/settings.go | 4 ++-- notifiers/slack.go | 20 ++++++++++++++------ source/js/main.js | 12 +++++++----- source/tmpl/settings.html | 17 ++++++++++++++++- 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/core/database.go b/core/database.go index 8a7751a9..6cc3ff57 100644 --- a/core/database.go +++ b/core/database.go @@ -71,7 +71,7 @@ func checkinDB() *gorm.DB { // HitsBetween returns the gorm database query for a collection of service hits between a time range func (s *Service) HitsBetween(t1, t2 time.Time) *gorm.DB { selector := Dbtimestamp(3600) - return DbSession.Debug().Model(&types.Hit{}).Select(selector).Where("service = ? AND created_at BETWEEN ? AND ?", s.Id, t1.UTC().Format(types.TIME), t2.UTC().Format(types.TIME)).Group("timeframe") + return DbSession.Model(&types.Hit{}).Select(selector).Where("service = ? AND created_at BETWEEN ? AND ?", s.Id, t1.UTC().Format(types.TIME), t2.UTC().Format(types.TIME)).Group("timeframe") } func CloseDB() { diff --git a/core/notifier/interfaces.go b/core/notifier/interfaces.go index 0581778a..4b0fa165 100644 --- a/core/notifier/interfaces.go +++ b/core/notifier/interfaces.go @@ -32,7 +32,7 @@ type BasicEvents interface { // Tester interface will include a function to Test users settings before saving type Tester interface { - OnTest(Notification) (bool, error) + OnTest() error } // ServiceEvents are events for Services diff --git a/handlers/settings.go b/handlers/settings.go index bb993e76..1940640a 100644 --- a/handlers/settings.go +++ b/handlers/settings.go @@ -253,9 +253,9 @@ func testNotificationHandler(w http.ResponseWriter, r *http.Request) { } notifer.Enabled = enabled == "on" - ok, err := notif.(notifier.Tester).OnTest(*notifer) + err = notif.(notifier.Tester).OnTest() - if ok { + if err == nil { w.Write([]byte("ok")) } else { w.Write([]byte(err.Error())) diff --git a/notifiers/slack.go b/notifiers/slack.go index 9648f680..e146ded1 100644 --- a/notifiers/slack.go +++ b/notifiers/slack.go @@ -17,10 +17,11 @@ package notifiers import ( "bytes" + "errors" "fmt" "github.com/hunterlong/statup/core/notifier" "github.com/hunterlong/statup/types" - "github.com/hunterlong/statup/utils" + "io/ioutil" "net/http" "text/template" "time" @@ -97,11 +98,18 @@ func (u *Slack) Select() *notifier.Notification { return u.Notification } -func (u *Slack) OnTest(n notifier.Notification) (bool, error) { - utils.Log(1, "Slack notifier loaded") - msg := fmt.Sprintf("You're Statup Slack Notifier is working correctly!") - err := parseSlackMessage(SLACK_TEXT, msg) - return true, err +func (u *Slack) OnTest() error { + client := new(http.Client) + res, err := client.Post(u.Host, "application/json", bytes.NewBuffer([]byte(`{"text":"testing message"}`))) + if err != nil { + return err + } + defer res.Body.Close() + contents, _ := ioutil.ReadAll(res.Body) + if string(contents) != "ok" { + return errors.New("incorrect url") + } + return err } // OnFailure will trigger failing service diff --git a/source/js/main.js b/source/js/main.js index 08a0133d..ca4e3d62 100644 --- a/source/js/main.js +++ b/source/js/main.js @@ -26,18 +26,20 @@ $('.service_li').on('click', function() { $('.test_notifier').on('click', function(e) { var form = $(this).parents('form:first'); var values = form.serialize(); - - console.log(form); - + var notifier = form.find('input[name=notifier]').val(); $.ajax({ url: form.attr("action")+"/test", type: 'POST', data: values, success: function(data) { - alert(data); + if (data === 'ok') { + $('#'+notifier+'-success').removeClass('d-none'); + } else { + $('#'+notifier+'-error').removeClass('d-none'); + $('#'+notifier+'-error').html(data); + } } }); - e.preventDefault(); }); diff --git a/source/tmpl/settings.html b/source/tmpl/settings.html index af50fa2f..48933ede 100644 --- a/source/tmpl/settings.html +++ b/source/tmpl/settings.html @@ -184,6 +184,7 @@
{{if $n.Title}}

{{$n.Title}}

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

{{safe $n.Description}}

{{end}} + {{range .Form}}
@@ -212,7 +213,9 @@
-
+ + +
@@ -220,6 +223,18 @@
+ +
+ + + +
+ +
{{end}}