Skip to content
Permalink
Browse files

notify after x failures, variable replacer, 0.90.16

  • Loading branch information
hunterlong committed Mar 23, 2020
1 parent 11538c3 commit f331e9bbf9eabf09229901471012301adc6a21e6

This file was deleted.

@@ -1,3 +1,11 @@
# 0.90.16
- Added Notify After (int) field for Services. Will send notifications after x amount of failures.
- Added new method in utils package for replacing `{{.Service.*}}` and `{{.Failure.*}}` variables from string to it's true value
- Fixed Notifer get endpoint
- Cleaned Notifier methods
- Updated recommended changes from [sonarcloud.io](https://sonarcloud.io/organizations/statping/projects)
- Organized utils package files

# 0.90.15
- Fixed /dashboard authentication state to show admin tabs if your an admin. [Issue #438](https://github.com/statping/statping/issues/438)
- Fixed Cache JS error on Dashboard
@@ -16,4 +24,4 @@
- Fixed version from not being added into Core table. [Issue #436](https://github.com/statping/statping/issues/436)

# 0.90.12
- Fixed MySQL timestamp formatting. (issue #432)
- Fixed MySQL timestamp formatting. (issue #432)
@@ -469,7 +469,6 @@ HTML,BODY {
margin: 0;
font: inherit;
overflow: auto;
font-family: inherit;
display: block;
width: 100%;
padding: 0px;
@@ -485,7 +484,6 @@ HTML,BODY {
/* Code Mirror Settings */
font-family: monospace;
position: relative;
overflow: hidden;
height:80vh;
}

@@ -138,7 +138,7 @@
name: c.name, description: c.description, domain: c.domain,
timezone: c.timezone, using_cdn: c.using_cdn, footer: c.footer, update_notify: c.update_notify,
gh_client_id: c.github_clientId, gh_client_secret: c.github_clientSecret
}
};
await Api.core_save(coreForm)
const core = await Api.core()
this.$store.commit('setCore', core)
@@ -1,5 +1,5 @@
<template>
<form @submit.prevent="login">
<form @submit.prevent="login" autocomplete="on">
<div class="form-group row">
<label for="username" class="col-sm-2 col-form-label">Username</label>
<div class="col-sm-10">
@@ -47,7 +47,7 @@

<div class="col-12 col-sm-12 mt-3">
<button @click.prevent="testNotifier" class="btn btn-secondary btn-block text-capitalize col-12 float-right"><i class="fa fa-vial"></i>
{{loading ? "Loading..." : "Test Notifier"}}</button>
{{loadingTest ? "Loading..." : "Test Notifier"}}</button>
</div>

</div>
@@ -74,6 +74,7 @@ export default {
data() {
return {
loading: false,
loadingTest: false,
error: null,
saved: false,
ok: false,
@@ -104,10 +105,11 @@ export default {
},
async testNotifier() {
this.ok = false
this.loading = true
this.loadingTest = true
let form = {}
this.notifier.form.forEach((f) => {
form[f.field] = this.notifier[f.field]
let field = f.field.toLowerCase()
this.form[field] = this.notifier[field]
});
form.enabled = this.notifier.enabled
form.limits = parseInt(this.notifier.limits)
@@ -118,7 +120,7 @@ export default {
} else {
this.error = tested
}
this.loading = false
this.loadingTest = false
},
}
}
@@ -139,6 +139,13 @@
</span>
</div>
</div>
<div v-if="service.allow_notifications" class="form-group row">
<label class="col-sm-4 col-form-label">Notify After Failures</label>
<div class="col-sm-8">
<input v-model="service.notify_after" type="number" name="notify_after" class="form-control" id="notify_after" autocapitalize="none">
<small class="form-text text-muted">Send Notification after {{service.notify_after === 0 ? 'the first Failure' : service.notify_after+' Failures'}} </small>
</div>
</div>
<div v-if="service.allow_notifications" class="form-group row">
<label class="col-sm-4 col-form-label">Notify All Changes</label>
<div class="col-8 mt-1">
@@ -196,6 +203,7 @@
verify_ssl: true,
allow_notifications: true,
notify_all_changes: true,
notify_after: 2,
public: true,
},
groups: [],
@@ -1,7 +1,7 @@
<template>
<div class="container col-md-7 col-sm-12 mt-2 sm-container">
<div class="col-12 col-md-8 offset-md-2 mb-4">
<img class="col-12 mt-5 mt-md-0" style="max-width:680px" src="/banner.png">
<img alt="Statping Setup" class="col-12 mt-5 mt-md-0" style="max-width:680px" src="banner.png">
</div>

<div class="col-12">
@@ -2,7 +2,7 @@
<div class="container col-md-7 col-sm-12 mt-md-5 bg-light">
<div class="col-10 offset-1 col-md-8 offset-md-2 mt-md-2">
<div class="col-12 col-md-8 offset-md-2 mb-4">
<img class="col-12 mt-5 mt-md-0" style="max-width:680px" src="/banner.png">
<img alt="Statping Login" class="col-12 mt-5 mt-md-0" style="max-width:680px" src="banner.png">
</div>

<FormLogin/>
@@ -169,7 +169,7 @@ func sendJsonAction(obj interface{}, method string, w http.ResponseWriter, r *ht
objName = "incident_update"
objId = v.Id
default:
objName = "missing"
objName = fmt.Sprintf("%T", v)
}

output := apiResponse{
@@ -17,6 +17,7 @@ package handlers

import (
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
"github.com/statping/statping/types/notifications"
@@ -28,21 +29,21 @@ import (

func apiNotifiersHandler(w http.ResponseWriter, r *http.Request) {
notifiers := services.AllNotifiers()
var notifs []*notifications.Notification
for _, n := range notifiers {
notifs = append(notifs, notifications.SelectNotifier(n.Select()))
}
returnJson(notifs, w, r)
returnJson(notifiers, w, r)
}

func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
notifier, err := notifications.Find(vars["notifier"])
if err != nil {
sendErrorJson(err, w, r)
return
notifier := vars["notifier"]
notifiers := services.AllNotifiers()
for _, n := range notifiers {
notf := n.Select()
if notifier == notf.Method {
returnJson(n, w, r)
return
}
}
returnJson(notifier, w, r)
sendErrorJson(errors.New("notifier not found"), w, r)
}

func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
@@ -66,7 +67,7 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
return
}
//notifications.OnSave(notifer.Method)
sendJsonAction(notifer, "update", w, r)
sendJsonAction(vars["notifier"], "update", w, r)
}

func testNotificationHandler(w http.ResponseWriter, r *http.Request) {
@@ -2,6 +2,7 @@ package handlers

import (
"github.com/statping/statping/notifiers"
"github.com/statping/statping/types/services"
"github.com/stretchr/testify/assert"
"testing"
)
@@ -17,34 +18,35 @@ func TestApiNotifiersRoutes(t *testing.T) {
URL: "/api/notifiers",
Method: "GET",
ExpectedStatus: 200,
ResponseLen: len(services.AllNotifiers()),
BeforeTest: SetTestENV,
SecureRoute: true,
}, {
Name: "Statping Mobile Notifier",
URL: "/api/notifier/mobile",
Name: "Statping Slack Notifier",
URL: "/api/notifier/slack",
Method: "GET",
ExpectedStatus: 200,
BeforeTest: SetTestENV,
SecureRoute: true,
}, {
Name: "Statping Update Notifier",
URL: "/api/notifier/mobile",
URL: "/api/notifier/slack",
Method: "POST",
Body: `{
"method": "mobile",
"var1": "ExponentPushToken[ToBadIWillError123456]",
"method": "slack",
"host": "https://slack.api/example/12345",
"enabled": true,
"limits": 55
}`,
ExpectedStatus: 200,
BeforeTest: SetTestENV,
SecureRoute: true,
}, {
Name: "Statping Mobile Notifier",
URL: "/api/notifier/mobile",
Name: "Statping Slack Notifier",
URL: "/api/notifier/slack",
Method: "GET",
ExpectedStatus: 200,
ExpectedContains: []string{`"method":"mobile"`, `"var1":"ExponentPushToken[ToBadIWillError123456]"`, `"enabled":true`, `"limits":55`},
ExpectedContains: []string{`"method":"slack"`},
BeforeTest: SetTestENV,
SecureRoute: true,
}}
@@ -74,14 +74,16 @@ func runCommand(app string, cmd ...string) (string, string, error) {
// OnFailure for commandLine will trigger failing service
func (u *commandLine) OnFailure(s *services.Service, f *failures.Failure) error {
msg := u.GetValue("var2")
_, _, err := runCommand(u.Host, msg)
tmpl := ReplaceVars(msg, s, f)
_, _, err := runCommand(u.Host, tmpl)
return err
}

// OnSuccess for commandLine will trigger successful service
func (u *commandLine) OnSuccess(s *services.Service) error {
msg := u.GetValue("var1")
_, _, err := runCommand(u.Host, msg)
tmpl := ReplaceVars(msg, s, nil)
_, _, err := runCommand(u.Host, tmpl)
return err
}

@@ -54,41 +54,41 @@ var Discorder = &discord{&notifications.Notification{
}

// Send will send a HTTP Post to the discord API. It accepts type: []byte
func (u *discord) sendRequest(msg string) error {
func (d *discord) sendRequest(msg string) error {
_, _, err := utils.HttpRequest(Discorder.GetValue("host"), "POST", "application/json", nil, strings.NewReader(msg), time.Duration(10*time.Second), true)
return err
}

func (u *discord) Select() *notifications.Notification {
return u.Notification
func (d *discord) Select() *notifications.Notification {
return d.Notification
}

// OnFailure will trigger failing service
func (u *discord) OnFailure(s *services.Service, f *failures.Failure) error {
msg := fmt.Sprintf(`{"content": "Your service '%v' is currently failing! Reason: %v"}`, s.Name, f.Issue)
return u.sendRequest(msg)
func (d *discord) OnFailure(s *services.Service, f *failures.Failure) error {
msg := `{"content": "Your service '{{.Service.Name}}' is currently failing! Reason: {{.Failure.Issue}}"}`
return d.sendRequest(ReplaceVars(msg, s, f))
}

// OnSuccess will trigger successful service
func (u *discord) OnSuccess(s *services.Service) error {
msg := fmt.Sprintf(`{"content": "Your service '%s' is currently online!"}`, s.Name)
return u.sendRequest(msg)
func (d *discord) OnSuccess(s *services.Service) error {
msg := `{"content": "Your service '{{.Service.Name}}' is currently online!"}`
return d.sendRequest(ReplaceVars(msg, s, nil))
}

// OnSave triggers when this notifier has been saved
func (u *discord) OnTest() error {
func (d *discord) OnTest() error {
outError := errors.New("Incorrect discord URL, please confirm URL is correct")
message := `{"content": "Testing the discord notifier"}`
contents, _, err := utils.HttpRequest(Discorder.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(message)), time.Duration(10*time.Second), true)
if string(contents) == "" {
return nil
}
var d discordTestJson
err = json.Unmarshal(contents, &d)
var dtt discordTestJson
err = json.Unmarshal(contents, &dtt)
if err != nil {
return outError
}
if d.Code == 0 {
if dtt.Code == 0 {
return outError
}
fmt.Println("discord: ", string(contents))

0 comments on commit f331e9b

Please sign in to comment.
You can’t perform that action at this time.