Skip to content

Commit

Permalink
Add query parameter to only search for incidents of a specific severity
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrisbattarbee committed Apr 4, 2024
1 parent 3b89560 commit acee831
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
32 changes: 30 additions & 2 deletions apiserver/internal/server/incidents.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"sort"
"strconv"
"strings"
)

type IncidentsResponse struct {
Expand All @@ -19,6 +20,7 @@ type IncidentsResponse struct {

// incidents is a handler for the /incidents endpoint.
// It has a required query parameter of statusPageUrl
// 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
func (s *Server) incidents(context *gin.Context) {
ctx := context.Request.Context()
statusPageUrl := context.Query("statusPageUrl")
Expand All @@ -27,6 +29,20 @@ func (s *Server) incidents(context *gin.Context) {
return
}

impactQuery := context.Query("impact")
var impacts []api.Impact
if impactQuery != "" {
impactsStr := strings.Split(impactQuery, ",")
for _, impactStr := range impactsStr {
impact, err := api.ParseImpact(impactStr)
if err != nil {
context.JSON(http.StatusBadRequest, gin.H{"error": "invalid impact"})
return
}
impacts = append(impacts, impact)
}
}

var limit *int = nil
if limitStr := context.Query("limit"); limitStr != "" {
limitInt, err := strconv.Atoi(limitStr)
Expand Down Expand Up @@ -56,7 +72,7 @@ func (s *Server) incidents(context *gin.Context) {
}

// Attempt to get the incidents from the cache
incidents, found, err := s.getIncidentsFromCache(ctx, statusPageUrl)
incidents, found, err := s.getIncidentsFromCache(ctx, statusPageUrl, impacts)
if err != nil {
s.logger.Error("failed to get incidents from cache", zap.Error(err))
context.JSON(http.StatusInternalServerError, gin.H{"error": "failed to get incidents from cache"})
Expand Down Expand Up @@ -100,7 +116,7 @@ func sortIncidentsDescending(incidents []api.Incident) {
// getIncidentsFromCache attempts to get the incidents from the cache.
// If the incidents are found in the cache, it returns them.
// If the incidents are not found in the cache, it returns false for the second return value.
func (s *Server) getIncidentsFromCache(ctx context.Context, statusPageUrl string) ([]api.Incident, bool, error) {
func (s *Server) getIncidentsFromCache(ctx context.Context, statusPageUrl string, impacts []api.Impact) ([]api.Incident, bool, error) {
incidents, found := s.incidentCache.Get(statusPageUrl)
if !found {
return nil, false, nil
Expand All @@ -111,6 +127,18 @@ func (s *Server) getIncidentsFromCache(ctx context.Context, statusPageUrl string
return nil, false, errors.New("failed to cast incidents to []api.Incident")
}

if len(impacts) > 0 {
var filteredIncidents []api.Incident
for _, incident := range incidentsCasted {
for _, impact := range impacts {
if incident.Impact == impact {
filteredIncidents = append(filteredIncidents, incident)
}
}
}
incidentsCasted = filteredIncidents
}

return incidentsCasted, true, nil
}

Expand Down
20 changes: 20 additions & 0 deletions common/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"database/sql/driver"
"encoding/json"
"github.com/pkg/errors"
"time"
)

Expand All @@ -16,6 +17,25 @@ const (
ImpactNone Impact = "none"
)

var ErrInvalidImpact = errors.New("invalid impact")

func ParseImpact(impact string) (Impact, error) {
switch impact {
case "minor":
return ImpactMinor, nil
case "major":
return ImpactMajor, nil
case "critical":
return ImpactCritical, nil
case "maintenance":
return ImpactMaintenance, nil
case "none":
return ImpactNone, nil
default:
return "", ErrInvalidImpact
}
}

type IncidentEventArray []IncidentEvent

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

0 comments on commit acee831

Please sign in to comment.