From 72627cb3e722f8abf24979d6797642d93a1bb85e Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Sat, 2 May 2020 05:25:50 -0700 Subject: [PATCH] Revert "Notifier Update and other updates" --- .github/workflows/dev.yml | 7 +- .github/workflows/stale.yml | 5 +- CHANGELOG.md | 3 - frontend/src/pages/Settings.vue | 132 -------------------------------- go.mod | 2 - go.sum | 1 - notifiers/command.go | 6 +- notifiers/email.go | 34 ++++---- notifiers/email_test.go | 6 +- notifiers/notifiers.go | 38 +++------ notifiers/notifiers_test.go | 16 ---- notifiers/pushover.go | 6 +- notifiers/slack.go | 7 +- source/source_test.go | 5 +- types/notifications/methods.go | 1 - types/services/failures.go | 9 --- types/services/routine.go | 2 +- utils/replacer.go | 24 ++++++ utils/utils_test.go | 14 ++++ version.txt | 2 +- 20 files changed, 91 insertions(+), 229 deletions(-) delete mode 100644 notifiers/notifiers_test.go create mode 100644 utils/replacer.go diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 519d640a..0474fb42 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -85,9 +85,7 @@ jobs: - uses: actions/checkout@v2 - name: Install Global Dependencies - run: | - go get gotest.tools/gotestsum - npm install -g yarn sass newman cross-env wait-on @sentry/cli + run: npm install -g yarn sass newman cross-env wait-on @sentry/cli - name: Setting ENV's run: | @@ -112,7 +110,8 @@ jobs: - name: Go Tests run: | - SASS=`which sass` gotestsum --no-summary=skipped --format dots -- -covermode=count -coverprofile=coverage.out -p=1 ./... + go get gotest.tools/gotestsum + gotestsum --no-summary=skipped --format dots -- -covermode=count -coverprofile=coverage.out -p=1 ./... env: VERSION: ${{ env.VERSION }} DB_CONN: sqlite3 diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index 04466ec9..7cd36cf6 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -1,4 +1,4 @@ -name: Stalebot +name: Issues on: schedule: - cron: "0 0 * * *" @@ -12,7 +12,6 @@ jobs: with: repo-token: ${{ secrets.GITHUB_TOKEN }} stale-issue-message: "This issue hasn't had any updates in a while. If this is still a problem, please create a new issue." - stale-issue-label: "stale" days-before-stale: 30 - days-before-close: 7 + days-before-close: 5 exempt-issue-labels: "bug,urgent,feature,pinned,locked" diff --git a/CHANGELOG.md b/CHANGELOG.md index e1f00482..5b37531a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,3 @@ -# 0.90.36 (05-02-2020) -- Fixed Notifier golang templating func to use correct variables - # 0.90.35 (05-01-2020) - Fixed issue with API endpoints cannot accepting Authorization header - Fixed issue with sass executable not being found, SASS environment var re-implemented diff --git a/frontend/src/pages/Settings.vue b/frontend/src/pages/Settings.vue index 3de2739d..0c378147 100644 --- a/frontend/src/pages/Settings.vue +++ b/frontend/src/pages/Settings.vue @@ -25,9 +25,6 @@ {{notifier.title}} ON - - Variables -
Statping Links
@@ -119,135 +116,6 @@ -
-

Notifier Variables

- You can insert dynamic fields within the notifier payloads for some notifiers. - -

- Checkout the Service struct and the Failures struct and create variables in golang template format. -

- -

- For example, if you have {{"\{\{.Service.Name\}\}"}} it will return the service name. -

- -

Service Variables

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Additional variables within the Service struct -
VariableTrue Value
{{"\{\{.Service.Id\}\}"}}1
{{"\{\{.Service.Name\}\}"}}Example Service
{{"\{\{.Service.Domain\}\}"}}https://statping.com
{{"\{\{.Service.Port\}\}"}}8080
{{"\{\{.Service.DowntimeAgo\}\}"}}35 minutes ago
{{"\{\{.Service.LastStatusCode\}\}"}}404
{{"\{\{.Service.FailuresLast24Hours\}\}"}}38
- -

Failure Variables

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Additional variables within the Failures struct -
VariableTrue Value
{{"\{\{.Failure.Issue\}\}"}}Received 404 status code
{{"\{\{.Failure.ErrorCode\}\}"}}404
{{"\{\{.Failure.Service\}\}"}}1
{{"\{\{.Failure.PingTime\}\}"}}12482 (microseconds)
{{"\{\{.Failure.DowntimeAgo\}\}"}}35 minutes ago
{{"\{\{.Failure.CreatedAt\}\}"}}2020-05-02 09:14:43.66381 +0000 UTC
- -

Core Variables

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Additional variables within the Core struct -
VariableTrue Value
{{"\{\{.Core.Domain\}\}"}}http://localhost:8080
{{"\{\{.Core.Name\}\}"}}Statping Demo
{{"\{\{.Core.Description\}\}"}}Statping will monitor your stuff!
{{"\{\{.Core.Version\}\}"}}v0.90.34
{{"\{\{.Core.Started\}\}"}}2020-05-02 09:14:43.66381 +0000 UTC
- - -
-
diff --git a/go.mod b/go.mod index f7a30637..93495c75 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/GeertJohan/go.rice v1.0.0 github.com/ararog/timeago v0.0.0-20160328174124-e9969cf18b8d github.com/dgrijalva/jwt-go v3.2.0+incompatible - github.com/dustin/go-humanize v1.0.0 github.com/fatih/structs v1.1.0 github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/getsentry/sentry-go v0.5.1 @@ -19,7 +18,6 @@ require ( github.com/jinzhu/gorm v1.9.12 github.com/joho/godotenv v1.3.0 github.com/kataras/iris/v12 v12.0.1 - github.com/magiconair/properties v1.8.1 github.com/mattn/go-sqlite3 v2.0.3+incompatible github.com/mitchellh/mapstructure v1.2.2 // indirect github.com/pelletier/go-toml v1.7.0 // indirect diff --git a/go.sum b/go.sum index 271df335..8d5246b6 100755 --- a/go.sum +++ b/go.sum @@ -55,7 +55,6 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= -github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= diff --git a/notifiers/command.go b/notifiers/command.go index 9a9bb0c2..f68d3f6d 100644 --- a/notifiers/command.go +++ b/notifiers/command.go @@ -32,7 +32,7 @@ var Command = &commandLine{¬ifications.Notification{ Limits: 60, Form: []notifications.NotificationForm{{ Type: "text", - Title: "Executable Path", + Title: "Shell or Bash", Placeholder: "/usr/bin/curl", DbField: "host", SmallText: "You can use '/bin/sh', '/bin/bash', '/usr/bin/curl' or an absolute path for an application.", @@ -41,13 +41,13 @@ var Command = &commandLine{¬ifications.Notification{ Title: "Command to Run on OnSuccess", Placeholder: "http://localhost:8080/health", DbField: "var1", - SmallText: "Accepts Variables This Command will run when a service is receiving a Successful event.", + SmallText: "This Command will run when a service is receiving a Successful event.", }, { Type: "text", Title: "Command to Run on OnFailure", Placeholder: "http://localhost:8080/health", DbField: "var2", - SmallText: "Accepts Variables This Command will run when a service is receiving a Failing event.", + SmallText: "This Command will run when a service is receiving a Failing event.", }}}, } diff --git a/notifiers/email.go b/notifiers/email.go index 7c6f6129..5caa99ce 100644 --- a/notifiers/email.go +++ b/notifiers/email.go @@ -155,21 +155,25 @@ type emailOutgoing struct { Subject string Template string From string - Data replacer + Data emailData Source string Sent bool } +type emailData struct { + Service services.Service + Failure failures.Failure +} + // OnFailure will trigger failing service func (e *emailer) OnFailure(s *services.Service, f *failures.Failure) error { - subject := fmt.Sprintf("Service %s is Offline", s.Name) email := &emailOutgoing{ To: e.Var2, - Subject: subject, + Subject: fmt.Sprintf("Service %v is Failing", s.Name), Template: mainEmailTemplate, - Data: replacer{ - Service: s, - Failure: f, + Data: emailData{ + Service: *s, + Failure: *f, }, From: e.Var1, } @@ -178,14 +182,14 @@ func (e *emailer) OnFailure(s *services.Service, f *failures.Failure) error { // OnSuccess will trigger successful service func (e *emailer) OnSuccess(s *services.Service) error { - subject := fmt.Sprintf("Service %s is Back Online", s.Name) + msg := s.DownText email := &emailOutgoing{ To: e.Var2, - Subject: subject, + Subject: msg, Template: mainEmailTemplate, - Data: replacer{ - Service: s, - Failure: &failures.Failure{}, + Data: emailData{ + Service: *s, + Failure: failures.Failure{}, }, From: e.Var1, } @@ -213,9 +217,9 @@ func (e *emailer) OnTest() (string, error) { To: e.Var2, Subject: subject, Template: mainEmailTemplate, - Data: replacer{ - Service: &testService, - Failure: &failures.Failure{}, + Data: emailData{ + Service: testService, + Failure: failures.Failure{}, }, From: e.Var1, } @@ -235,7 +239,7 @@ func (e *emailer) dialSend(email *emailOutgoing) error { m.SetHeader("From", email.From) m.SetHeader("To", email.To) m.SetHeader("Subject", email.Subject) - m.SetBody("text/html", ReplaceTemplate(email.Template, email.Data)) + m.SetBody("text/html", utils.ReplaceTemplate(email.Template, email.Data)) if err := mailer.DialAndSend(m); err != nil { utils.Log.Errorln(fmt.Sprintf("email '%v' sent to: %v (size: %v) %v", email.Subject, email.To, len([]byte(email.Source)), err)) diff --git a/notifiers/email_test.go b/notifiers/email_test.go index 01283580..a69fa3aa 100644 --- a/notifiers/email_test.go +++ b/notifiers/email_test.go @@ -61,9 +61,9 @@ func TestEmailNotifier(t *testing.T) { To: email.GetValue("var2"), Subject: fmt.Sprintf("Service %v is Failing", exampleService.Name), Template: mainEmailTemplate, - Data: replacer{ - Service: exampleService, - Failure: exampleFailure, + Data: emailData{ + Service: *exampleService, + Failure: *exampleFailure, }, From: email.GetValue("var1"), } diff --git a/notifiers/notifiers.go b/notifiers/notifiers.go index 81f74f69..69b0df49 100644 --- a/notifiers/notifiers.go +++ b/notifiers/notifiers.go @@ -1,24 +1,15 @@ package notifiers import ( - "bytes" - "github.com/statping/statping/types/core" "github.com/statping/statping/types/failures" "github.com/statping/statping/types/null" "github.com/statping/statping/types/services" "github.com/statping/statping/utils" - "html/template" "time" ) var log = utils.Log.WithField("type", "notifier") -type replacer struct { - Core *core.Core - Service *services.Service - Failure *failures.Failure -} - func InitNotifiers() { Add( slacker, @@ -34,21 +25,6 @@ func InitNotifiers() { ) } -func ReplaceTemplate(tmpl string, data replacer) string { - buf := new(bytes.Buffer) - tmp, err := template.New("replacement").Parse(tmpl) - if err != nil { - log.Error(err) - return err.Error() - } - err = tmp.Execute(buf, data) - if err != nil { - log.Error(err) - return err.Error() - } - return buf.String() -} - func Add(notifs ...services.ServiceNotifier) { for _, n := range notifs { services.AddNotifier(n) @@ -58,8 +34,19 @@ func Add(notifs ...services.ServiceNotifier) { } } +func ToMap(srv *services.Service, f *failures.Failure) map[string]interface{} { + m := make(map[string]interface{}) + m["Service"] = srv + m["Failure"] = f + return m +} + func ReplaceVars(input string, s *services.Service, f *failures.Failure) string { - return ReplaceTemplate(input, replacer{Service: s, Failure: f, Core: core.App}) + input = utils.ReplaceTemplate(input, s) + if f != nil { + input = utils.ReplaceTemplate(input, f) + } + return input } var exampleService = &services.Service{ @@ -99,7 +86,6 @@ var exampleService = &services.Service{ LastOffline: utils.Now().Add(-10 * time.Minute), SecondsOnline: 4500, SecondsOffline: 300, - Redirect: null.NewNullBool(true), } var exampleFailure = &failures.Failure{ diff --git a/notifiers/notifiers_test.go b/notifiers/notifiers_test.go deleted file mode 100644 index d07ed631..00000000 --- a/notifiers/notifiers_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package notifiers - -import ( - "github.com/magiconair/properties/assert" - "testing" -) - -func TestReplaceTemplate(t *testing.T) { - temp := `{"id":{{.Service.Id}},"name":"{{.Service.Name}}"}` - replaced := ReplaceTemplate(temp, replacer{Service: exampleService}) - assert.Equal(t, `{"id":1,"name":"Statping"}`, replaced) - - temp = `{"id":{{.Service.Id}},"name":"{{.Service.Name}}","downtime":"{{.Service.DowntimeAgo}}","failure":"{{.Failure.Issue}}"}` - replaced = ReplaceTemplate(temp, replacer{Service: exampleService, Failure: exampleFailure}) - assert.Equal(t, `{"id":1,"name":"Statping","downtime":"15 seconds ago","failure":"HTTP returned a 500 status code"}`, replaced) -} diff --git a/notifiers/pushover.go b/notifiers/pushover.go index ae85eb13..c88d9f87 100644 --- a/notifiers/pushover.go +++ b/notifiers/pushover.go @@ -68,21 +68,21 @@ func (t *pushover) sendMessage(message string) (string, error) { // OnFailure will trigger failing service func (t *pushover) OnFailure(s *services.Service, f *failures.Failure) error { - msg := fmt.Sprintf("Your service '%s' is currently offline!", s.Name) + msg := fmt.Sprintf("Your service '%v' is currently offline!", s.Name) _, err := t.sendMessage(msg) return err } // OnSuccess will trigger successful service func (t *pushover) OnSuccess(s *services.Service) error { - msg := fmt.Sprintf("Your service '%s' is currently online!", s.Name) + msg := fmt.Sprintf("Your service '%v' is currently online!", s.Name) _, err := t.sendMessage(msg) return err } // OnTest will test the Pushover SMS messaging func (t *pushover) OnTest() (string, error) { - msg := fmt.Sprintf("Testing the Pushover Notifier, Your service '%s' is currently offline! Error: %s", exampleService.Name, exampleFailure.Issue) + msg := fmt.Sprintf("Testing the Pushover Notifier") content, err := t.sendMessage(msg) return content, err } diff --git a/notifiers/slack.go b/notifiers/slack.go index 2eb776cb..07fe0196 100644 --- a/notifiers/slack.go +++ b/notifiers/slack.go @@ -16,8 +16,8 @@ var _ notifier.Notifier = (*slack)(nil) const ( slackMethod = "slack" - failingTemplate = `{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": ":warning: The service {{.Service.Name}} is currently offline! :warning:" } }, { "type": "divider" }, { "type": "section", "fields": [ { "type": "mrkdwn", "text": "*Service:*\n{{.Service.Name}}" }, { "type": "mrkdwn", "text": "*URL:*\n{{.Service.Domain}}" }, { "type": "mrkdwn", "text": "*Status Code:*\n{{.Service.LastStatusCode}}" }, { "type": "mrkdwn", "text": "*When:*\n{{.Failure.CreatedAt}}" }, { "type": "mrkdwn", "text": "*Downtime:*\n{{.Service.DowntimeAgo}}" }, { "type": "plain_text", "text": "*Error:*\n{{.Failure.Issue}}" } ] }, { "type": "divider" }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Offline Service", "emoji": true }, "style": "danger", "url": "{{.Core.Domain}}/service/{{.Service.Id}}" }, { "type": "button", "text": { "type": "plain_text", "text": "Go to Statping", "emoji": true }, "url": "{{.Core.Domain}}" } ] } ] }` - successTemplate = `{ "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "The service {{.Service.Name}} is back online." } }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Service", "emoji": true }, "style": "primary", "url": "{{.Core.Domain}}/service/{{.Service.Id}}" }, { "type": "button", "text": { "type": "plain_text", "text": "Go to Statping", "emoji": true }, "url": "{{.Core.Domain}}" } ] } ] }` + failingTemplate = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is currently failing", "text": "Your Statping service <{{.Service.Domain}}|{{.Service.Name}}> has just received a Failure notification based on your expected results. {{.Service.Name}} responded with a HTTP Status code of {{.Service.LastStatusCode}}.", "fields": [ { "title": "Expected Status Code", "value": "{{.Service.ExpectedStatus}}", "short": true }, { "title": "Received Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ,{ "title": "Error Message", "value": "{{.Failure.Issue}}", "short": false } ], "color": "#FF0000", "thumb_url": "https://statping.com", "footer": "Statping", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }` + successTemplate = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is now back online", "text": "Your Statping service <{{.Service.Domain}}|{{.Service.Name}}> is now back online and meets your expected responses.", "color": "#00FF00", "thumb_url": "https://statping.com", "footer": "Statping", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }` ) type slack struct { @@ -59,8 +59,7 @@ func (s *slack) sendSlack(msg string) error { } func (s *slack) OnTest() (string, error) { - testMsg := ReplaceVars(failingTemplate, exampleService, exampleFailure) - contents, resp, err := utils.HttpRequest(s.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(testMsg)), time.Duration(10*time.Second), true) + contents, resp, err := utils.HttpRequest(s.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(`{"text":"testing message"}`)), time.Duration(10*time.Second), true) if err != nil { return "", err } diff --git a/source/source_test.go b/source/source_test.go index a0c848f8..cb4e7fe8 100644 --- a/source/source_test.go +++ b/source/source_test.go @@ -26,7 +26,7 @@ func TestCore_UsingAssets(t *testing.T) { func TestCreateAssets(t *testing.T) { CreateAllAssets(dir) assert.True(t, UsingAssets(dir)) - CompileSASS(DefaultScss...) + assert.Nil(t, CompileSASS(DefaultScss...)) assert.FileExists(t, dir+"/assets/css/main.css") assert.FileExists(t, dir+"/assets/css/style.css") assert.FileExists(t, dir+"/assets/css/vendor.css") @@ -41,7 +41,8 @@ func TestCreateAssets(t *testing.T) { //} func TestCompileSASS(t *testing.T) { - CompileSASS(DefaultScss...) + err := CompileSASS(DefaultScss...) + require.Nil(t, err) assert.True(t, UsingAssets(dir)) } diff --git a/types/notifications/methods.go b/types/notifications/methods.go index 6b2e527d..cec62d52 100644 --- a/types/notifications/methods.go +++ b/types/notifications/methods.go @@ -45,7 +45,6 @@ func (n *Notification) CanSend() bool { //fmt.Println("Last sent: ", n.lastSent.String()) //fmt.Println("Last count: ", n.lastSentCount) - //fmt.Println("Limits: ", n.Limits) //fmt.Println("Last sent before now: ", n.lastSent.Add(60*time.Second).Before(utils.Now())) // the last sent notification was past 1 minute (limit per minute) diff --git a/types/services/failures.go b/types/services/failures.go index e524d158..fe5badcf 100644 --- a/types/services/failures.go +++ b/types/services/failures.go @@ -2,7 +2,6 @@ package services import ( "fmt" - humanize "github.com/dustin/go-humanize" "github.com/statping/statping/types/failures" "strings" "time" @@ -21,14 +20,6 @@ func (s *Service) FailuresSince(t time.Time) failures.Failurer { return fails } -func (s *Service) DowntimeAgo() string { - last := s.LastOnline - if last.IsZero() { - return "Never been online" - } - return humanize.Time(last) -} - func (s *Service) DowntimeText() string { last := s.AllFailures().Last() if last == nil { diff --git a/types/services/routine.go b/types/services/routine.go index c7956e76..90204a98 100644 --- a/types/services/routine.go +++ b/types/services/routine.go @@ -379,7 +379,7 @@ func sendFailure(s *Service, f *failures.Failure) { return } - if s.notifyAfterCount > s.NotifyAfter { + if s.NotifyAfter == 0 || s.notifyAfterCount > s.NotifyAfter { for _, n := range allNotifiers { notif := n.Select() if notif.CanSend() { diff --git a/utils/replacer.go b/utils/replacer.go new file mode 100644 index 00000000..50d238f2 --- /dev/null +++ b/utils/replacer.go @@ -0,0 +1,24 @@ +package utils + +import ( + "bytes" + "text/template" +) + +func ReplaceTemplate(tmpl string, data interface{}) string { + buf := new(bytes.Buffer) + + tmp, err := template.New("replacement").Parse(tmpl) + if err != nil { + Log.Error(err) + return err.Error() + } + + err = tmp.Execute(buf, data) + if err != nil { + Log.Error(err) + return err.Error() + } + + return buf.String() +} diff --git a/utils/utils_test.go b/utils/utils_test.go index 55200c1a..207d607b 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -40,6 +40,20 @@ func TestCommand(t *testing.T) { assert.Contains(t, out, "statping") } +func TestReplaceTemplate(t *testing.T) { + type Object struct { + Id int64 + String string + Online bool + Example string + } + ex := &Object{ + 1, "this is an example", true, "it should work", + } + result := ReplaceTemplate(`{"id": {{.Id}} }`, ex) + assert.Equal(t, "{\"id\": 1 }", result) +} + func TestToInt(t *testing.T) { assert.Equal(t, int64(55), ToInt("55")) assert.Equal(t, int64(55), ToInt(55)) diff --git a/version.txt b/version.txt index 2df71c22..cc33bfef 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.90.36 +0.90.35