Skip to content

Commit

Permalink
rename status -> state for topics
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Mar 8, 2017
1 parent dc17567 commit 36814e9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 58 deletions.
4 changes: 2 additions & 2 deletions alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,14 +730,14 @@ 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.TopicEventState(a.anonTopic, id); ok {
if state, ok := a.et.tm.AlertService.EventState(a.anonTopic, id); ok {
anonTopicState = state
anonFound = true
}
}
// Check for previous state on topic.
if a.hasTopic() {
if state, ok := a.et.tm.AlertService.TopicEventState(a.topic, id); ok {
if state, ok := a.et.tm.AlertService.EventState(a.topic, id); ok {
topicState = state
topicFound = true
}
Expand Down
12 changes: 6 additions & 6 deletions alert/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,18 @@ func (s *Topics) ReplaceHandler(oldTopics, newTopics []string, oldH, newH Handle
}
}

// TopicStatus returns the max alert level for each topic matching 'pattern', not returning
// TopicState returns the max alert level for each topic matching 'pattern', not returning
// any topics with max alert levels less severe than 'minLevel'
func (s *Topics) TopicStatus(pattern string, minLevel Level) map[string]TopicStatus {
func (s *Topics) TopicState(pattern string, minLevel Level) map[string]TopicState {
s.mu.RLock()
res := make(map[string]TopicStatus, len(s.topics))
res := make(map[string]TopicState, len(s.topics))
for _, topic := range s.topics {
if !PatternMatch(pattern, topic.ID()) {
continue
}
level := topic.MaxLevel()
if level >= minLevel {
res[topic.ID()] = TopicStatus{
res[topic.ID()] = TopicState{
Level: level,
Collected: topic.Collected(),
}
Expand Down Expand Up @@ -226,8 +226,8 @@ func (t *Topic) ID() string {
return t.id
}

func (t *Topic) Status() TopicStatus {
return TopicStatus{
func (t *Topic) State() TopicState {
return TopicState{
Level: t.MaxLevel(),
Collected: t.Collected(),
}
Expand Down
2 changes: 1 addition & 1 deletion alert/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func ParseLevel(s string) (l Level, err error) {
return
}

type TopicStatus struct {
type TopicState struct {
Level Level
Collected int64
}
4 changes: 2 additions & 2 deletions client/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1438,9 +1438,9 @@ GET /kapacitor/v1preview/alerts/topics?min-level=WARNING
}
```

### Topic Status
### Topic State

To query the status of a topic make a GET request to `/kapacitor/v1preview/alerts/topics/<topic id>`.
To query the state of a topic make a GET request to `/kapacitor/v1preview/alerts/topics/<topic id>`.

#### Example

Expand Down
26 changes: 13 additions & 13 deletions services/alert/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ func (s *apiServer) handleListTopics(w http.ResponseWriter, r *http.Request) {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
statuses, err := s.Topics.ListTopicStatus(pattern, minLevel)
states, err := s.Topics.TopicStates(pattern, minLevel)
if err != nil {
httpd.HttpError(w, fmt.Sprint("failed to get topic statuses: ", err.Error()), true, http.StatusInternalServerError)
httpd.HttpError(w, fmt.Sprint("failed to get topic states: ", err.Error()), true, http.StatusInternalServerError)
return
}
list := make([]client.Topic, 0, len(statuses))
for topic, status := range statuses {
list = append(list, s.createClientTopic(topic, status))
list := make([]client.Topic, 0, len(states))
for topic, state := range states {
list = append(list, s.createClientTopic(topic, state))
}
sort.Sort(sortedTopics(list))

Expand Down Expand Up @@ -214,24 +214,24 @@ func (s *apiServer) topicHandlerLink(topic, handler string) client.Link {
return client.Link{Relation: client.Self, Href: path.Join(topicsBasePath, topic, topicHandlersPath, handler)}
}

func (s *apiServer) createClientTopic(topic string, status alert.TopicStatus) client.Topic {
func (s *apiServer) createClientTopic(topic string, state alert.TopicState) client.Topic {
return client.Topic{
ID: topic,
Link: s.topicLink(topic),
Level: status.Level.String(),
Collected: status.Collected,
Level: state.Level.String(),
Collected: state.Collected,
EventsLink: s.topicEventsLink(topic, eventsRelation),
HandlersLink: s.topicHandlersLink(topic, handlersRelation),
}
}

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

w.WriteHeader(http.StatusOK)
w.Write(httpd.MarshalJSON(topic, true))
Expand Down Expand Up @@ -271,7 +271,7 @@ func (s *apiServer) handleListTopicEvents(topic string, w http.ResponseWriter, r
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
events, err := s.Topics.TopicStatusEvents(topic, minLevel)
events, err := s.Topics.EventStates(topic, minLevel)
if err != nil {
httpd.HttpError(w, fmt.Sprintf("failed to get topic events: %s", err.Error()), true, http.StatusInternalServerError)
return
Expand All @@ -294,7 +294,7 @@ 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.TopicEventState(topic, eventID)
state, ok := s.Topics.EventState(topic, eventID)
if !ok {
httpd.HttpError(w, fmt.Sprintf("event %q does not exist for topic %q", eventID, topic), true, http.StatusNotFound)
return
Expand Down
41 changes: 21 additions & 20 deletions services/alert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,6 @@ func validatePattern(pattern string) error {
return err
}

func (s *Service) TopicEventState(topic, event string) (alert.EventState, bool) {
t, ok := s.topics.Topic(topic)
if !ok {
return alert.EventState{}, false
}
return t.EventState(event)
}

func (s *Service) Collect(event alert.Event) error {
s.mu.RLock()
closed := s.closedTopics[event.Topic]
Expand Down Expand Up @@ -464,31 +456,40 @@ func (s *Service) UpdateHandlerSpec(oldSpec, newSpec HandlerSpec) error {
return nil
}

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

// TopicStates returns the max alert level for each topic matching 'pattern', not returning
// any topics with max alert levels less severe than 'minLevel'
func (s *Service) TopicStates(pattern string, minLevel alert.Level) (map[string]alert.TopicState, error) {
return s.topics.TopicState(pattern, minLevel), nil
}

// EventState returns the current state of the event.
func (s *Service) EventState(topic, event string) (alert.EventState, bool) {
t, ok := s.topics.Topic(topic)
if !ok {
return alert.TopicStatus{}, fmt.Errorf("unknown topic %q", topic)
return alert.EventState{}, false
}
return t.Status(), nil
return t.EventState(event)
}

// TopicStatusEvents returns the current state of events for the specified topic.
// EventStates returns the current state of events for the specified topic.
// Only events greater or equal to minLevel will be returned
func (s *Service) TopicStatusEvents(topic string, minLevel alert.Level) (map[string]alert.EventState, error) {
func (s *Service) EventStates(topic string, minLevel alert.Level) (map[string]alert.EventState, error) {
t, ok := s.topics.Topic(topic)
if !ok {
return nil, fmt.Errorf("unknown topic %q", topic)
}
return t.EventStates(minLevel), nil
}

// ListTopicStatus returns the max alert level for each topic matching 'pattern', not returning
// any topics with max alert levels less severe than 'minLevel'
func (s *Service) ListTopicStatus(pattern string, minLevel alert.Level) (map[string]alert.TopicStatus, error) {
return s.topics.TopicStatus(pattern, minLevel), nil
}

func (s *Service) HandlerSpec(id string) (HandlerSpec, error) {
s.mu.RLock()
defer s.mu.RUnlock()
Expand Down
26 changes: 12 additions & 14 deletions services/alert/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ type HandlerSpecRegistrar interface {
HandlerSpecs(pattern string) ([]HandlerSpec, error)
}

// Topics is responsible for querying the status of topics and their events.
// Topics is responsible for querying the state of topics and their events.
type Topics interface {
// TopicStatus returns the status of the specified topic,
TopicStatus(topic string) (alert.TopicStatus, error)

// TopicStatusEvents returns the current state of events for the specified topic.
// TopicState returns the state of the specified topic,
TopicState(topic string) (alert.TopicState, 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)
// EventStates returns the current state of events for the specified topic.
// Only events greater or equal to minLevel will be returned
TopicStatusEvents(topic string, minLevel alert.Level) (map[string]alert.EventState, error)

// TopicEventState returns the current state of the event.
TopicEventState(topic, event string) (alert.EventState, bool)

// ListTopicStatus returns the status of all topics that match the pattern and have at least minLevel.
ListTopicStatus(pattern string, minLevel alert.Level) (map[string]alert.TopicStatus, error)
EventStates(topic string, minLevel alert.Level) (map[string]alert.EventState, error)
}

// AnonHandlerRegistrar is responsible for directly registering handlers for anonymous topics.
Expand All @@ -47,8 +45,8 @@ type Events interface {
Collect(event alert.Event) error
// UpdateEvent updates an existing event with a previously known state.
UpdateEvent(topic string, event alert.EventState) error
// TopicEventState returns the current events state.
TopicEventState(topic, event string) (alert.EventState, bool)
// EventState returns the current events state.
EventState(topic, event string) (alert.EventState, bool)
}

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

0 comments on commit 36814e9

Please sign in to comment.