Skip to content

Commit

Permalink
Events - REST support and other assorted goodies
Browse files Browse the repository at this point in the history
- Add EventsHandler REST API
- Add Events REST API groovy ITest
- Fix up Json [de-]serialization
  - don't try an overload the use of the category field by using
    it for polymorphic serialization (i.e. via JsonTypeInfo annotation)
  - add explicit Event.eventType field to be used by FasterXml
    - note: an implicit field didn't work because we send serialized json
      out to clients via Rest, and the client (e.g. groovy) would
      try to deserialize the field.
- Let Trigger define an explicit eventCategory and eventText.
- Rename DefinitionsEvent.EventType to DefinitionsEvent.Type to
  remove some confusing naming.
-
  • Loading branch information
jshaughn committed Oct 5, 2015
1 parent 2694169 commit 923abc2
Show file tree
Hide file tree
Showing 19 changed files with 582 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.hawkular.alerts.api.model.condition.ConditionEval;
import org.hawkular.alerts.api.model.dampening.Dampening;
import org.hawkular.alerts.api.model.trigger.Trigger;
import org.hawkular.alerts.api.model.trigger.TriggerType;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
Expand All @@ -42,11 +41,20 @@
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "category")
property = "eventType",
// set default impl for events injected via REST API
defaultImpl = Event.class)
@JsonSubTypes({
@Type(value = Alert.class, name = "ALERT") })
@Type(name = "EVENT", value = Event.class),
@Type(name = "ALERT", value = Alert.class) })
public class Event {

// This field will be controlled vi fasterxml. We need to make this an explicit field because we ship the
// serialized json to clients via rest. Since we serialize via fasterxml the field will be added to the json.
// Using "visible=false" in JsonTypeInfo only works if we control the deserialization.
@JsonInclude
protected String eventType;

@JsonInclude
protected String tenantId;

Expand Down Expand Up @@ -101,6 +109,7 @@ public Event(String tenantId, String id, String category, String text, Map<Strin
Map<String, String> tags) {
this.tenantId = tenantId;
this.id = id;
this.category = category;
this.text = text;
this.context = context;
this.tags = tags;
Expand All @@ -125,13 +134,29 @@ public Event(String tenantId, Trigger trigger, Dampening dampening, List<Set<Con
this.ctime = System.currentTimeMillis();

this.id = trigger.getId() + "-" + this.ctime;
this.category = TriggerType.ALERT == trigger.getType() ?
EventCategory.ALERT.name() : EventCategory.TRIGGER.name();
this.text = isEmpty(trigger.getDescription()) ? trigger.getName() : trigger.getDescription();
this.context = trigger.getContext();
if (!isEmpty(trigger.getEventCategory())) {
this.category = trigger.getEventCategory();
} else {
this.category = (EventType.ALERT == trigger.getEventType()) ?
EventCategory.ALERT.name() : EventCategory.TRIGGER.name();
}
if (!isEmpty(trigger.getEventText())) {
this.text = trigger.getEventText();
} else {
this.text = isEmpty(trigger.getDescription()) ? trigger.getName() : trigger.getDescription();
}
this.tags = trigger.getTags();
}

public String getEventType() {
return eventType;
}

public void setEventType(String eventType) {
this.eventType = eventType;
}

public String getTenantId() {
return tenantId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.alerts.api.model.trigger;
package org.hawkular.alerts.api.model.event;

/**
* What the trigger produces.
* The type of events a trigger can produce.
*
* @author jay shaughnessy
* @author lucas ponce
*/
public enum TriggerType {
public enum EventType {
ALERT, EVENT
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.UUID;

import org.hawkular.alerts.api.model.Severity;
import org.hawkular.alerts.api.model.event.EventType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand All @@ -43,17 +44,22 @@ public class Trigger {
@JsonInclude
private String id;

@JsonInclude
private TriggerType type;

/** For display */
@JsonInclude
private String name;

@JsonInclude(Include.NON_EMPTY)
private String description;

// defaults to non-null trigger description, otherwise trigger name
/** The type of event produced by the trigger. Defaults to EventType.ALERT */
@JsonInclude
private EventType eventType;

/** Defaults to EventCategory.ALERT if the Trigger EventType is ALERT, otherwise EventType.TRIGGER */
@JsonInclude
private String eventCategory;

/** Defaults to the Trigger Description if not null, otherwise the trigger name. */
@JsonInclude
private String eventText;

Expand Down Expand Up @@ -153,7 +159,6 @@ public Trigger(String tenantId, String id, String name, Map<String, String> cont
}
this.tenantId = tenantId;
this.id = id;
this.type = TriggerType.ALERT; // Is this an OK default, or should it be a parameter?
this.name = name;
this.context = context;
this.tags = tags;
Expand All @@ -164,6 +169,9 @@ public Trigger(String tenantId, String id, String name, Map<String, String> cont
this.autoResolve = false;
this.autoResolveAlerts = true;
this.autoResolveMatch = Match.ALL;
this.eventCategory = null;
this.eventText = null;
this.eventType = EventType.ALERT; // Is this an OK default, or should it be a constructor parameter?
this.memberOf = null;
this.description = null;
this.enabled = false;
Expand Down Expand Up @@ -196,14 +204,6 @@ public void setId(String id) {
this.id = id;
}

public TriggerType getType() {
return type;
}

public void setType(TriggerType type) {
this.type = type;
}

public String getName() {
return name;
}
Expand All @@ -223,6 +223,22 @@ public void setDescription(String description) {
this.description = description;
}

public EventType getEventType() {
return eventType;
}

public void setEventType(EventType eventType) {
this.eventType = eventType;
}

public String getEventCategory() {
return eventCategory;
}

public void setEventCategory(String eventCategory) {
this.eventCategory = eventCategory;
}

public String getEventText() {
return eventText;
}
Expand Down Expand Up @@ -475,13 +491,14 @@ public int hashCode() {

@Override
public String toString() {
return "Trigger [tenantId=" + tenantId + ", id=" + id + ", type=" + type.name() + ", name=" + name
+ ", description=" + description
+ ", eventType=" + type + ", eventText=" + eventText + ", severity=" + severity + ", context="
+ context + ", actions=" + actions + ", autoDisable=" + autoDisable + ", autoEnable=" + autoEnable
+ ", autoResolve=" + autoResolve + ", autoResolveAlerts=" + autoResolveAlerts + ", autoResolveMatch="
+ autoResolveMatch + ", memberOf=" + memberOf + ", enabled=" + enabled + ", firingMatch="
+ firingMatch + ", orphan=" + orphan + ", group=" + group + ", mode=" + mode + ", tags=" + tags + "]";
return "Trigger [tenantId=" + tenantId + ", id=" + id + ", triggerType=" + eventType.name()
+ ", name=" + name + ", description=" + description + ", eventType=" + eventType
+ ", eventCategory=" + eventCategory + ", eventText=" + eventText + ", severity=" + severity
+ ", context=" + context + ", actions=" + actions + ", autoDisable=" + autoDisable
+ ", autoEnable=" + autoEnable + ", autoResolve=" + autoResolve + ", autoResolveAlerts="
+ autoResolveAlerts + ", autoResolveMatch=" + autoResolveMatch + ", memberOf=" + memberOf
+ ", enabled=" + enabled + ", firingMatch=" + firingMatch + ", orphan=" + orphan + ", group=" + group
+ ", mode=" + mode + ", tags=" + tags + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@
*/
public class DefinitionsEvent {

public enum EventType {
public enum Type {
CONDITION_CHANGE,
DAMPENING_CHANGE,
TRIGGER_CREATE,
TRIGGER_REMOVE,
TRIGGER_UPDATE
};

private EventType eventType;
private Type type;

public DefinitionsEvent(EventType eventType) {
public DefinitionsEvent(Type type) {
super();
this.eventType = eventType;
this.type = type;
}

public EventType getEventType() {
return eventType;
public Type getType() {
return type;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.hawkular.alerts.api.model.dampening.Dampening;
import org.hawkular.alerts.api.model.trigger.Mode;
import org.hawkular.alerts.api.model.trigger.Trigger;
import org.hawkular.alerts.api.services.DefinitionsEvent.EventType;
import org.hawkular.alerts.api.services.DefinitionsEvent.Type;

/**
* A interface used to create new triggers, conditions and init new notifiers.
Expand Down Expand Up @@ -604,5 +604,5 @@ void updateAction(String tenantId, String actionPlugin, String actionId, Map<Str

Map<String, String> getAction(String tenantId, String actionPlugin, String actionId) throws Exception;

void registerListener(DefinitionsListener listener, EventType eventType, EventType... eventTypes);
void registerListener(DefinitionsListener listener, Type eventType, Type... eventTypes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public void jsonAlertTest() throws Exception {
public void jsonToAlertTest() throws Exception {
String jsonAlert = "{\"tenantId\":\"jdoe\"," +
"\"id\":\"trigger-test|1436964192878\"," +
"\"eventType\":\"ALERT\"," +
"\"trigger\":{\"tenantId\":\"jdoe\"," +
"\"id\":\"trigger-test\"," +
"\"name\":\"trigger-test\"," +
Expand All @@ -142,7 +143,6 @@ public void jsonToAlertTest() throws Exception {
"}," +
"\"ctime\":1436964192878," +
"\"context\":{\"n1\":\"v1\",\"n2\":\"v2\"}," +
"\"category\":\"ALERT\"," +
"\"text\":\"trigger-test\"," +
"\"evalSets\":[" +
"[{\"evalTimestamp\":1436964294055," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void init() {
public void onChange(DefinitionsEvent event) {
updateActiveIds();
}
}, DefinitionsEvent.EventType.CONDITION_CHANGE);
}, DefinitionsEvent.Type.CONDITION_CHANGE);
}

public Set<String> getActiveDataIds() {
Expand Down

0 comments on commit 923abc2

Please sign in to comment.