Skip to content

Commit

Permalink
delete incidents and checkins when service is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterlong committed Jul 23, 2020
1 parent 848c46c commit 86d6a5a
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Modified node version from 10.x to 12.18.2
- Modified Notifier's struct values to be NullString and NullInt to allow empty values
- Added Search ability to Logs in UI
- Fixed issue with Incidents and Checkins not being deleted once service is deleted

# 0.90.60 (07-15-2020)
- Added LETSENCRYPT_ENABLE (boolean) env to enable/disable letsencrypt SSL
Expand Down
1 change: 1 addition & 0 deletions notifiers/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func TestCommandNotifier(t *testing.T) {
t.Parallel()
t.SkipNow()
err := utils.InitLogs()
require.Nil(t, err)
Expand Down
1 change: 1 addition & 0 deletions notifiers/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
)

func TestDiscordNotifier(t *testing.T) {
t.Parallel()
err := utils.InitLogs()
require.Nil(t, err)
DISCORD_URL = utils.Params.GetString("DISCORD_URL")
Expand Down
1 change: 1 addition & 0 deletions notifiers/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
)

func TestEmailNotifier(t *testing.T) {
t.Parallel()
err := utils.InitLogs()
require.Nil(t, err)

Expand Down
1 change: 1 addition & 0 deletions notifiers/gotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
)

func TestGotifyNotifier(t *testing.T) {
t.Parallel()
err := utils.InitLogs()
require.Nil(t, err)
GOTIFY_URL = utils.Params.GetString("GOTIFY_URL")
Expand Down
1 change: 1 addition & 0 deletions notifiers/mobile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
)

func TestMobileNotifier(t *testing.T) {
t.Parallel()
t.SkipNow()
err := utils.InitLogs()
require.Nil(t, err)
Expand Down
1 change: 1 addition & 0 deletions notifiers/notifiers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

func TestReplaceTemplate(t *testing.T) {
t.Parallel()
temp := `{"id":{{.Service.Id}},"name":"{{.Service.Name}}"}`
replaced := ReplaceTemplate(temp, replacer{Service: services.Example(true)})
assert.Equal(t, `{"id":6283,"name":"Statping Example"}`, replaced)
Expand Down
1 change: 1 addition & 0 deletions notifiers/pushover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
)

func TestPushoverNotifier(t *testing.T) {
t.Parallel()
err := utils.InitLogs()
require.Nil(t, err)

Expand Down
1 change: 1 addition & 0 deletions notifiers/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
)

func TestSlackNotifier(t *testing.T) {
t.Parallel()
err := utils.InitLogs()
require.Nil(t, err)
db, err := database.OpenTester()
Expand Down
1 change: 1 addition & 0 deletions notifiers/statping_emailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
)

func TestStatpingEmailerNotifier(t *testing.T) {
t.Parallel()
err := utils.InitLogs()
require.Nil(t, err)
db, err := database.OpenTester()
Expand Down
6 changes: 4 additions & 2 deletions notifiers/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
)

func TestTelegramNotifier(t *testing.T) {
t.Parallel()

telegramToken = utils.Params.GetString("TELEGRAM_TOKEN")
telegramChannel = utils.Params.GetString("TELEGRAM_CHANNEL")
if telegramToken == "" || telegramChannel == "" {
t.Log("Telegram notifier testing skipped, missing TELEGRAM_TOKEN and TELEGRAM_CHANNEL environment variable")
t.SkipNow()
Expand All @@ -28,8 +32,6 @@ func TestTelegramNotifier(t *testing.T) {
err := utils.InitLogs()
require.Nil(t, err)

telegramToken = utils.Params.GetString("TELEGRAM_TOKEN")
telegramChannel = utils.Params.GetString("TELEGRAM_CHANNEL")
Telegram.ApiSecret = null.NewNullString(telegramToken)
Telegram.Var1 = null.NewNullString(telegramChannel)

Expand Down
2 changes: 2 additions & 0 deletions notifiers/twilio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (
)

func TestTwilioNotifier(t *testing.T) {
t.Parallel()

err := utils.InitLogs()
require.Nil(t, err)

Expand Down
1 change: 1 addition & 0 deletions notifiers/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
)

func TestWebhookNotifier(t *testing.T) {
t.Parallel()
t.SkipNow()
err := utils.InitLogs()
require.Nil(t, err)
Expand Down
5 changes: 5 additions & 0 deletions types/incidents/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@ func (i *Incident) Update() error {
}

func (i *Incident) Delete() error {
for _, u := range i.Updates() {
if err := u.Delete(); err != nil {
return err
}
}
return db.Delete(i).Error()
}
3 changes: 3 additions & 0 deletions types/services/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (s *Service) Delete() error {
if err := s.DeleteCheckins(); err != nil {
return err
}
if err := s.DeleteIncidents(); err != nil {
return err
}
delete(allServices, s.Id)
q := db.Model(&Service{}).Delete(s)
return q.Error()
Expand Down
20 changes: 20 additions & 0 deletions types/services/incidents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package services

import (
"github.com/statping/statping/types/incidents"
)

func (s *Service) Incidents() []*incidents.Incident {
var i []*incidents.Incident
db.Where("service = ?", s.Id).Find(&i)
return i
}

func (s *Service) DeleteIncidents() error {
for _, i := range s.Incidents() {
if err := i.Delete(); err != nil {
return err
}
}
return nil
}
36 changes: 35 additions & 1 deletion types/services/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/statping/statping/types/checkins"
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/hits"
"github.com/statping/statping/types/incidents"
"github.com/statping/statping/types/null"
"github.com/statping/statping/utils"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -83,6 +84,20 @@ var fail2 = &failures.Failure{
CreatedAt: utils.Now().Add(-5 * time.Second),
}

var incident1 = &incidents.Incident{
Title: "Theres something going on",
Description: "this is an example",
ServiceId: 1,
CreatedAt: utils.Now().Add(-30 * time.Second),
}

var incidentUpdate1 = &incidents.IncidentUpdate{
IncidentId: 1,
Message: "This is an update",
Type: "pending",
CreatedAt: utils.Now().Add(-5 * time.Second),
}

type exampleGRPC struct {
pb.UnimplementedRouteGuideServer
}
Expand Down Expand Up @@ -168,9 +183,10 @@ func TestServices(t *testing.T) {
require.Nil(t, err)
db, err := database.OpenTester()
require.Nil(t, err)
db.AutoMigrate(&Service{}, &hits.Hit{}, &checkins.Checkin{}, &checkins.CheckinHit{}, &failures.Failure{})
db.AutoMigrate(&Service{}, &hits.Hit{}, &checkins.Checkin{}, &checkins.CheckinHit{}, &failures.Failure{}, &incidents.Incident{}, &incidents.IncidentUpdate{})
checkins.SetDB(db)
failures.SetDB(db)
incidents.SetDB(db)
hits.SetDB(db)
SetDB(db)

Expand All @@ -181,6 +197,8 @@ func TestServices(t *testing.T) {
db.Create(&exmapleCheckin)
db.Create(&fail1)
db.Create(&fail2)
db.Create(&incident1)
db.Create(&incidentUpdate1)

tlsCert := utils.Params.GetString("STATPING_DIR") + "/cert.pem"
tlsCertKey := utils.Params.GetString("STATPING_DIR") + "/key.pem"
Expand Down Expand Up @@ -499,6 +517,22 @@ func TestServices(t *testing.T) {
err = item.Delete()
require.Nil(t, err)

checkin := item.Checkins()
assert.Len(t, checkin, 0)
for _, c := range checkin {
assert.Len(t, c.Failures().List(), 0)
assert.Len(t, c.Hits(), 0)
}

assert.Len(t, item.AllFailures().List(), 0)
assert.Len(t, item.AllHits().List(), 0)

inc := item.Incidents()
assert.Len(t, inc, 0)
for _, i := range inc {
assert.Len(t, i.Updates(), 0)
}

all = All()
assert.Len(t, all, 1)
})
Expand Down

0 comments on commit 86d6a5a

Please sign in to comment.