mirror of https://github.com/statping/statping
notifier testing js
parent
6864cd067e
commit
0f208c979f
|
@ -71,7 +71,7 @@ func checkinDB() *gorm.DB {
|
||||||
// HitsBetween returns the gorm database query for a collection of service hits between a time range
|
// 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 {
|
func (s *Service) HitsBetween(t1, t2 time.Time) *gorm.DB {
|
||||||
selector := Dbtimestamp(3600)
|
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() {
|
func CloseDB() {
|
||||||
|
|
|
@ -32,7 +32,7 @@ type BasicEvents interface {
|
||||||
|
|
||||||
// Tester interface will include a function to Test users settings before saving
|
// Tester interface will include a function to Test users settings before saving
|
||||||
type Tester interface {
|
type Tester interface {
|
||||||
OnTest(Notification) (bool, error)
|
OnTest() error
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceEvents are events for Services
|
// ServiceEvents are events for Services
|
||||||
|
|
|
@ -253,9 +253,9 @@ func testNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
notifer.Enabled = enabled == "on"
|
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"))
|
w.Write([]byte("ok"))
|
||||||
} else {
|
} else {
|
||||||
w.Write([]byte(err.Error()))
|
w.Write([]byte(err.Error()))
|
||||||
|
|
|
@ -17,10 +17,11 @@ package notifiers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hunterlong/statup/core/notifier"
|
"github.com/hunterlong/statup/core/notifier"
|
||||||
"github.com/hunterlong/statup/types"
|
"github.com/hunterlong/statup/types"
|
||||||
"github.com/hunterlong/statup/utils"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
@ -97,11 +98,18 @@ func (u *Slack) Select() *notifier.Notification {
|
||||||
return u.Notification
|
return u.Notification
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Slack) OnTest(n notifier.Notification) (bool, error) {
|
func (u *Slack) OnTest() error {
|
||||||
utils.Log(1, "Slack notifier loaded")
|
client := new(http.Client)
|
||||||
msg := fmt.Sprintf("You're Statup Slack Notifier is working correctly!")
|
res, err := client.Post(u.Host, "application/json", bytes.NewBuffer([]byte(`{"text":"testing message"}`)))
|
||||||
err := parseSlackMessage(SLACK_TEXT, msg)
|
if err != nil {
|
||||||
return true, err
|
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
|
// OnFailure will trigger failing service
|
||||||
|
|
|
@ -26,18 +26,20 @@ $('.service_li').on('click', function() {
|
||||||
$('.test_notifier').on('click', function(e) {
|
$('.test_notifier').on('click', function(e) {
|
||||||
var form = $(this).parents('form:first');
|
var form = $(this).parents('form:first');
|
||||||
var values = form.serialize();
|
var values = form.serialize();
|
||||||
|
var notifier = form.find('input[name=notifier]').val();
|
||||||
console.log(form);
|
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: form.attr("action")+"/test",
|
url: form.attr("action")+"/test",
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
data: values,
|
data: values,
|
||||||
success: function(data) {
|
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();
|
e.preventDefault();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -184,6 +184,7 @@
|
||||||
<form method="POST" class="{{underscore $n.Method }}" action="/settings/notifier/{{ $n.Method }}">
|
<form method="POST" class="{{underscore $n.Method }}" action="/settings/notifier/{{ $n.Method }}">
|
||||||
{{if $n.Title}}<h4>{{$n.Title}}</h4>{{end}}
|
{{if $n.Title}}<h4>{{$n.Title}}</h4>{{end}}
|
||||||
{{if $n.Description}}<p class="small text-muted">{{safe $n.Description}}</p>{{end}}
|
{{if $n.Description}}<p class="small text-muted">{{safe $n.Description}}</p>{{end}}
|
||||||
|
|
||||||
{{range .Form}}
|
{{range .Form}}
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="text-capitalize" for="{{underscore .Title}}">{{.Title}}</label>
|
<label class="text-capitalize" for="{{underscore .Title}}">{{.Title}}</label>
|
||||||
|
@ -212,6 +213,8 @@
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<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">
|
<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>
|
<button type="submit" class="btn btn-primary btn-block text-capitalize">Save</button>
|
||||||
</div>
|
</div>
|
||||||
|
@ -220,6 +223,18 @@
|
||||||
<div class="col-12 col-sm-12">
|
<div class="col-12 col-sm-12">
|
||||||
<button class="test_notifier btn btn-secondary btn-block text-capitalize col-12 float-right">Test</button>
|
<button class="test_notifier btn btn-secondary btn-block text-capitalize col-12 float-right">Test</button>
|
||||||
</div>
|
</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}}
|
{{end}}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue