Skip to content

Commit acee831

Browse files
Add query parameter to only search for incidents of a specific severity
1 parent 3b89560 commit acee831

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

apiserver/internal/server/incidents.go

+30-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"sort"
1212
"strconv"
13+
"strings"
1314
)
1415

1516
type IncidentsResponse struct {
@@ -19,6 +20,7 @@ type IncidentsResponse struct {
1920

2021
// incidents is a handler for the /incidents endpoint.
2122
// It has a required query parameter of statusPageUrl
23+
// It has an optional query parameter of impact (default is all), which is an array of impacts e.g. impact=critical,major,minor,none to exclude maintenance
2224
func (s *Server) incidents(context *gin.Context) {
2325
ctx := context.Request.Context()
2426
statusPageUrl := context.Query("statusPageUrl")
@@ -27,6 +29,20 @@ func (s *Server) incidents(context *gin.Context) {
2729
return
2830
}
2931

32+
impactQuery := context.Query("impact")
33+
var impacts []api.Impact
34+
if impactQuery != "" {
35+
impactsStr := strings.Split(impactQuery, ",")
36+
for _, impactStr := range impactsStr {
37+
impact, err := api.ParseImpact(impactStr)
38+
if err != nil {
39+
context.JSON(http.StatusBadRequest, gin.H{"error": "invalid impact"})
40+
return
41+
}
42+
impacts = append(impacts, impact)
43+
}
44+
}
45+
3046
var limit *int = nil
3147
if limitStr := context.Query("limit"); limitStr != "" {
3248
limitInt, err := strconv.Atoi(limitStr)
@@ -56,7 +72,7 @@ func (s *Server) incidents(context *gin.Context) {
5672
}
5773

5874
// Attempt to get the incidents from the cache
59-
incidents, found, err := s.getIncidentsFromCache(ctx, statusPageUrl)
75+
incidents, found, err := s.getIncidentsFromCache(ctx, statusPageUrl, impacts)
6076
if err != nil {
6177
s.logger.Error("failed to get incidents from cache", zap.Error(err))
6278
context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get incidents from cache"})
@@ -100,7 +116,7 @@ func sortIncidentsDescending(incidents []api.Incident) {
100116
// getIncidentsFromCache attempts to get the incidents from the cache.
101117
// If the incidents are found in the cache, it returns them.
102118
// If the incidents are not found in the cache, it returns false for the second return value.
103-
func (s *Server) getIncidentsFromCache(ctx context.Context, statusPageUrl string) ([]api.Incident, bool, error) {
119+
func (s *Server) getIncidentsFromCache(ctx context.Context, statusPageUrl string, impacts []api.Impact) ([]api.Incident, bool, error) {
104120
incidents, found := s.incidentCache.Get(statusPageUrl)
105121
if !found {
106122
return nil, false, nil
@@ -111,6 +127,18 @@ func (s *Server) getIncidentsFromCache(ctx context.Context, statusPageUrl string
111127
return nil, false, errors.New("failed to cast incidents to []api.Incident")
112128
}
113129

130+
if len(impacts) > 0 {
131+
var filteredIncidents []api.Incident
132+
for _, incident := range incidentsCasted {
133+
for _, impact := range impacts {
134+
if incident.Impact == impact {
135+
filteredIncidents = append(filteredIncidents, incident)
136+
}
137+
}
138+
}
139+
incidentsCasted = filteredIncidents
140+
}
141+
114142
return incidentsCasted, true, nil
115143
}
116144

common/api/api.go

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"database/sql/driver"
55
"encoding/json"
6+
"github.com/pkg/errors"
67
"time"
78
)
89

@@ -16,6 +17,25 @@ const (
1617
ImpactNone Impact = "none"
1718
)
1819

20+
var ErrInvalidImpact = errors.New("invalid impact")
21+
22+
func ParseImpact(impact string) (Impact, error) {
23+
switch impact {
24+
case "minor":
25+
return ImpactMinor, nil
26+
case "major":
27+
return ImpactMajor, nil
28+
case "critical":
29+
return ImpactCritical, nil
30+
case "maintenance":
31+
return ImpactMaintenance, nil
32+
case "none":
33+
return ImpactNone, nil
34+
default:
35+
return "", ErrInvalidImpact
36+
}
37+
}
38+
1939
type IncidentEventArray []IncidentEvent
2040

2141
func (sla *IncidentEventArray) Scan(src interface{}) error {

0 commit comments

Comments
 (0)