Skip to content

Commit

Permalink
clarify state return values
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Mar 8, 2017
1 parent 36814e9 commit bb576c2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
10 changes: 8 additions & 2 deletions alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,14 +730,20 @@ func (a *AlertNode) restoreEventState(id string) (alert.Level, time.Time) {
var anonFound, topicFound bool
// Check for previous state on anonTopic
if a.hasAnonTopic() {
if state, ok := a.et.tm.AlertService.EventState(a.anonTopic, id); ok {
if state, ok, err := a.et.tm.AlertService.EventState(a.anonTopic, id); err != nil {
a.incrementErrorCount()
a.logger.Printf("E! failed to get event state for anonymous topic %s, event %s: %v", a.anonTopic, id, err)
} else if ok {
anonTopicState = state
anonFound = true
}
}
// Check for previous state on topic.
if a.hasTopic() {
if state, ok := a.et.tm.AlertService.EventState(a.topic, id); ok {
if state, ok, err := a.et.tm.AlertService.EventState(a.topic, id); err != nil {
a.incrementErrorCount()
a.logger.Printf("E! failed to get event state for topic %s, event %s: %v", a.topic, id, err)
} else if ok {
topicState = state
topicFound = true
}
Expand Down
14 changes: 11 additions & 3 deletions services/alert/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,15 @@ func (s *apiServer) createClientTopic(topic string, state alert.TopicState) clie
}

func (s *apiServer) handleTopic(id string, w http.ResponseWriter, r *http.Request) {
state, err := s.Topics.TopicState(id)
state, ok, err := s.Topics.TopicState(id)
if err != nil {
httpd.HttpError(w, fmt.Sprintf("failed to get topic state: %s", err.Error()), true, http.StatusInternalServerError)
return
}
if !ok {
httpd.HttpError(w, fmt.Sprintf("unknown topic: %q", id), true, http.StatusNotFound)
return
}
topic := s.createClientTopic(id, state)

w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -294,9 +298,13 @@ func (s *apiServer) handleListTopicEvents(topic string, w http.ResponseWriter, r

func (s *apiServer) handleTopicEvent(topic string, w http.ResponseWriter, r *http.Request) {
eventID := path.Base(r.URL.Path)
state, ok := s.Topics.EventState(topic, eventID)
state, ok, err := s.Topics.EventState(topic, eventID)
if err != nil {
httpd.HttpError(w, fmt.Sprintf("failed to get event state: %s", err.Error()), true, http.StatusInternalServerError)
return
}
if !ok {
httpd.HttpError(w, fmt.Sprintf("event %q does not exist for topic %q", eventID, topic), true, http.StatusNotFound)
httpd.HttpError(w, fmt.Sprintf("unknown event %q in topic %q", eventID, topic), true, http.StatusNotFound)
return
}
event := client.TopicEvent{
Expand Down
13 changes: 7 additions & 6 deletions services/alert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,12 @@ func (s *Service) UpdateHandlerSpec(oldSpec, newSpec HandlerSpec) error {
}

// TopicState returns the state for the specified topic.
func (s *Service) TopicState(topic string) (alert.TopicState, error) {
func (s *Service) TopicState(topic string) (alert.TopicState, bool, error) {
t, ok := s.topics.Topic(topic)
if !ok {
return alert.TopicState{}, fmt.Errorf("unknown topic %q", topic)
return alert.TopicState{}, false, nil
}
return t.State(), nil
return t.State(), true, nil
}

// TopicStates returns the max alert level for each topic matching 'pattern', not returning
Expand All @@ -472,12 +472,13 @@ func (s *Service) TopicStates(pattern string, minLevel alert.Level) (map[string]
}

// EventState returns the current state of the event.
func (s *Service) EventState(topic, event string) (alert.EventState, bool) {
func (s *Service) EventState(topic, event string) (alert.EventState, bool, error) {
t, ok := s.topics.Topic(topic)
if !ok {
return alert.EventState{}, false
return alert.EventState{}, false, nil
}
return t.EventState(event)
state, ok := t.EventState(event)
return state, ok, nil
}

// EventStates returns the current state of events for the specified topic.
Expand Down
8 changes: 4 additions & 4 deletions services/alert/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type HandlerSpecRegistrar interface {
HandlerSpecs(pattern string) ([]HandlerSpec, error)
}

// Topics is responsible for querying the state of topics and their events.
// Topics is responsible for querying the state of topics and their events.
type Topics interface {
// TopicState returns the state of the specified topic,
TopicState(topic string) (alert.TopicState, error)
TopicState(topic string) (alert.TopicState, bool, error)
// TopicStates returns the state of all topics that match the pattern and have at least minLevel.
TopicStates(pattern string, minLevel alert.Level) (map[string]alert.TopicState, error)

// EventState returns the current state of the event.
EventState(topic, event string) (alert.EventState, bool)
EventState(topic, event string) (alert.EventState, bool, error)
// EventStates returns the current state of events for the specified topic.
// Only events greater or equal to minLevel will be returned
EventStates(topic string, minLevel alert.Level) (map[string]alert.EventState, error)
Expand All @@ -46,7 +46,7 @@ type Events interface {
// UpdateEvent updates an existing event with a previously known state.
UpdateEvent(topic string, event alert.EventState) error
// EventState returns the current events state.
EventState(topic, event string) (alert.EventState, bool)
EventState(topic, event string) (alert.EventState, bool, error)
}

// TopicPersister is responsible for controlling the persistence of topic state.
Expand Down

0 comments on commit bb576c2

Please sign in to comment.