Skip to content

Commit edf3383

Browse files
author
Diane LAKESTANI
committed
fix(outages): create a dedicated patch route for outages
1 parent 26e7fee commit edf3383

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ require (
3535

3636
require (
3737
cloud.google.com/go v0.56.0 // indirect
38-
dario.cat/mergo v1.0.1 // indirect
3938
github.com/Azure/azure-sdk-for-go v41.3.0+incompatible // indirect
4039
github.com/Azure/go-autorest/autorest v0.10.0 // indirect
4140
github.com/Azure/go-autorest/autorest/adal v0.8.3 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiy
2323
cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos=
2424
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk=
2525
contrib.go.opencensus.io/exporter/ocagent v0.4.12/go.mod h1:450APlNTSR6FrvC3CTRqYosuDstRB9un7SOx2k/9ckA=
26-
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
27-
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
2826
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
2927
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
3028
github.com/Azure/azure-sdk-for-go v32.4.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=

handlers/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func Router() *mux.Router {
128128
api.Handle("/api/services/{id}/failures", authenticated(servicesDeleteFailuresHandler, false)).Methods("DELETE")
129129
api.Handle("/api/services/{id}/hits", scoped(apiServiceHitsHandler)).Methods("GET")
130130
api.Handle("/api/services/{id}/hits", authenticated(apiServiceHitsDeleteHandler, false)).Methods("DELETE")
131+
api.Handle("/api/services/{id}/outages", authenticated(apiServiceOutagePatchHandler, false)).Methods("PATCH")
131132

132133
// API SERVICE CHART DATA Routes
133134
api.Handle("/api/services/{id}/hits_data", http.HandlerFunc(apiServiceDataHandler)).Methods("GET")

handlers/services.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/statping-ng/statping-ng/types/hits"
99
"github.com/statping-ng/statping-ng/types/services"
1010
"github.com/statping-ng/statping-ng/utils"
11-
"dario.cat/mergo"
1211
"net/http"
1312
)
1413

@@ -78,7 +77,12 @@ func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
7877
type servicePatchReq struct {
7978
Online bool `json:"online"`
8079
Issue string `json:"issue,omitempty"`
81-
Latency int64 `json:"latency,omitempty"`
80+
Latency int64 `json:"latency,omitempty"`
81+
}
82+
83+
type serviceOutagePatchReq struct {
84+
IsOutageEnabled bool `json:"is_outage_enabled"`
85+
OutageType string `json:"outage_type"`
8286
}
8387

8488
func apiServicePatchHandler(w http.ResponseWriter, r *http.Request) {
@@ -102,7 +106,7 @@ func apiServicePatchHandler(w http.ResponseWriter, r *http.Request) {
102106
}
103107

104108
if !req.Online {
105-
services.RecordFailure(service, issueDefault, "trigger", service.OutageType)
109+
services.RecordFailure(service, issueDefault, "trigger", "offline")
106110
} else {
107111
services.RecordSuccess(service)
108112
}
@@ -115,39 +119,55 @@ func apiServicePatchHandler(w http.ResponseWriter, r *http.Request) {
115119
sendJsonAction(service, "update", w, r)
116120
}
117121

118-
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
122+
func apiServiceOutagePatchHandler(w http.ResponseWriter, r *http.Request) {
119123
service, err := findService(r)
120124
if err != nil {
121125
sendErrorJson(err, w, r)
122126
return
123127
}
124-
125-
var updateData services.Service
126-
if err := DecodeJSON(r, &updateData); err != nil {
128+
var req serviceOutagePatchReq
129+
if err := DecodeJSON(r, &req); err != nil {
127130
sendErrorJson(err, w, r)
128131
return
129132
}
130133

131-
// Merge updateData in service by overriding the values
132-
if err := mergo.Merge(service, updateData, mergo.WithOverride, mergo.WithOverwriteWithEmptyValue); err != nil {
134+
service.IsOutageEnabled = req.IsOutageEnabled
135+
service.OutageType = req.OutageType
136+
137+
issueDefault := "Service was triggered to be outaged"
138+
139+
if req.IsOutageEnabled {
140+
services.RecordFailure(service, issueDefault, "trigger", service.OutageType)
141+
} else {
142+
services.RecordSuccess(service)
143+
}
144+
145+
if err := service.Update(); err != nil {
133146
sendErrorJson(err, w, r)
134147
return
135148
}
136149

150+
sendJsonAction(service, "update", w, r)
151+
}
152+
153+
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
154+
service, err := findService(r)
155+
if err != nil {
156+
sendErrorJson(err, w, r)
157+
return
158+
}
159+
if err := DecodeJSON(r, &service); err != nil {
160+
sendErrorJson(err, w, r)
161+
return
162+
}
137163
if err := service.Update(); err != nil {
138164
sendErrorJson(err, w, r)
139165
return
140166
}
141167
go service.CheckService(true)
142-
log.Info("Service updated: ", service)
143-
if service.IsOutageEnabled {
144-
services.RecordFailure(service, "Outage set ("+service.OutageType+")", "outage", service.OutageType)
145-
}
146-
147168
sendJsonAction(service, "update", w, r)
148169
}
149170

150-
151171
func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) {
152172
service, err := findService(r)
153173
if err != nil {

types/services/services_test_alerts_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func runNotifyTests(t *testing.T, notif *exampleNotifier, tests ...notifyTest) {
269269
if test.OnSuccess {
270270
RecordSuccess(test.Service)
271271
} else {
272-
RecordFailure(test.Service, "test issue", "lookup")
272+
RecordFailure(test.Service, "test issue", "lookup", "test")
273273
}
274274

275275
assert.Equal(t, test.ExpectedSuccess, notif.success)

0 commit comments

Comments
 (0)