notifier testing js

pull/78/head
hunterlong 2018-09-26 08:26:16 -07:00
parent 6864cd067e
commit 0f208c979f
6 changed files with 41 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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();
});

View File

@ -184,6 +184,7 @@
<form method="POST" class="{{underscore $n.Method }}" action="/settings/notifier/{{ $n.Method }}">
{{if $n.Title}}<h4>{{$n.Title}}</h4>{{end}}
{{if $n.Description}}<p class="small text-muted">{{safe $n.Description}}</p>{{end}}
{{range .Form}}
<div class="form-group">
<label class="text-capitalize" for="{{underscore .Title}}">{{.Title}}</label>
@ -212,7 +213,9 @@
</span>
</div>
<div class="col-12 col-sm-4 mb-2 mb-sm-0 mt-2 mt-sm-0">
<input type="hidden" name="notifier" 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">Save</button>
</div>
@ -220,6 +223,18 @@
<div class="col-12 col-sm-12">
<button class="test_notifier btn btn-secondary btn-block text-capitalize col-12 float-right">Test</button>
</div>
<div class="col-12 col-sm-12 mt-2">
<div class="alert alert-danger d-none" id="{{underscore $n.Method}}-error" role="alert">
{{$n.Method}} has an error!
</div>
<div class="alert alert-success d-none" id="{{underscore $n.Method}}-success" role="alert">
The {{$n.Method}} notifier is working correctly!
</div>
</div>
</div>
{{end}}
</div>