Skip to content

Commit

Permalink
Playing with new Event impact on Trigger (API module mainly)
Browse files Browse the repository at this point in the history
- Add eventType and eventText to trigger.
- Change rule engine to generate Alert or Event based on Trigger.
  note that an Alert gets a related Event automatically.
- new addEvents() service.
- Add tags to Events but note that I think we need to do work on
  Tags so they are more consistent across Triggers and Events.
  (See TODO in Event)
- Actions should probably be Event-level, not Alert-level
  • Loading branch information
jshaughn committed Sep 28, 2015
1 parent 218458d commit d3d3604
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
*/
package org.hawkular.alerts.api.model.action;

import org.hawkular.alerts.api.model.event.Alert;
import org.hawkular.alerts.api.model.event.Event;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

/**
* A base class for action representation from the perspective of the alerts engine.
* An action is the abstract concept of a consequence of an alert.
* An action is the abstract concept of a consequence of an event.
* A Trigger definition can be linked with a list of actions.
*
* Alert engine only needs to know an action id and message/payload.
* Action payload can optionally have an alert as payload.
* Action payload can optionally have an event as payload.
*
* Action plugins will be responsible to process the action according its own plugin configuration.
*
Expand All @@ -49,7 +49,7 @@ public class Action {
private String message;

@JsonInclude(Include.NON_NULL)
private Alert alert;
private Event event;

public Action() { }

Expand All @@ -60,11 +60,11 @@ public Action(String tenantId, String actionPlugin, String actionId, String mess
this.message = message;
}

public Action(String tenantId, String actionPlugin, String actionId, Alert alert) {
public Action(String tenantId, String actionPlugin, String actionId, Event event) {
this.tenantId = tenantId;
this.actionPlugin = actionPlugin;
this.actionId = actionId;
this.alert = alert;
this.event = event;
}

public String getTenantId() {
Expand Down Expand Up @@ -99,12 +99,12 @@ public void setActionPlugin(String actionPlugin) {
this.actionPlugin = actionPlugin;
}

public Alert getAlert() {
return alert;
public Event getEvent() {
return event;
}

public void setAlert(Alert alert) {
this.alert = alert;
public void setEvent(Event event) {
this.event = event;
}

@Override
Expand All @@ -117,7 +117,7 @@ public boolean equals(Object o) {
if (!tenantId.equals(action.tenantId)) return false;
if (!actionPlugin.equals(action.actionPlugin)) return false;
if (!actionId.equals(action.actionId)) return false;
return alert.equals(action.alert);
return event.equals(action.event);

}

Expand All @@ -136,7 +136,7 @@ public String toString() {
", actionPlugin='" + actionPlugin + '\'' +
", actionId='" + actionId + '\'' +
", message='" + message + '\'' +
", alert=" + alert +
", event=" + event +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Alert(String tenantId, Trigger trigger, List<Set<ConditionEval>> evalSets
}

public Alert(String tenantId, Trigger trigger, Dampening dampening, List<Set<ConditionEval>> evalSets) {
super(tenantId, trigger, dampening, evalSets);
super(tenantId, trigger, dampening, evalSets, null);

this.status = Status.OPEN;
this.severity = trigger.getSeverity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
*/
package org.hawkular.alerts.api.model.event;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hawkular.alerts.api.model.condition.ConditionEval;
import org.hawkular.alerts.api.model.dampening.Dampening;
import org.hawkular.alerts.api.model.trigger.Tag;
import org.hawkular.alerts.api.model.trigger.Trigger;

import com.fasterxml.jackson.annotation.JsonInclude;
Expand All @@ -48,13 +51,16 @@ public class Event {

// A description of the event, suitable for display
@JsonInclude
private String eventText;
private String text;

@JsonInclude(Include.NON_EMPTY)
private Map<String, String> context;

// TODO: WE NEED TO POTENTIALLY CONSOLIDATE OUR TAG APPROACH. I THINK MAYBE WE SHOULD MOVE THE CURRENT Tag TO
// A NAME-VALUE PAIR, GET RID OF HIDDEN, AND CHANGE triggerId to a more generic ID. The Tag as is is not
// going to work here, but for now leave as is/.
@JsonInclude(Include.NON_EMPTY)
private Map<String, String> tags;
private Set<Tag> tags;

// Null for API-generated Events. Otherwise the Trigger that created the event (@ctime)
@JsonInclude(Include.NON_EMPTY)
Expand All @@ -75,28 +81,29 @@ public Event() {
// for json assembly
}

public Event(String tenantId, String id, String eventText, Map<String, String> context, Map<String, String> tags) {
public Event(String tenantId, String id, String text, Map<String, String> context, Set<Tag> tags) {
this.tenantId = tenantId;
this.id = id;
this.eventText = eventText;
this.text = text;
this.context = context;
this.tags = tags;

this.ctime = System.currentTimeMillis();
}

public Event(String tenantId, Trigger trigger, Dampening dampening, List<Set<ConditionEval>> evalSets) {
public Event(String tenantId, Trigger trigger, Dampening dampening, List<Set<ConditionEval>> evalSets,
Set<Tag> tags) {
this.tenantId = tenantId;
this.trigger = trigger;
this.dampening = dampening;
this.evalSets = evalSets;
this.tags = tags;

this.ctime = System.currentTimeMillis();

this.id = trigger.getId() + "-" + this.ctime;
this.eventText = trigger.getDescription(); // is this sufficient text?
this.text = isEmpty(trigger.getDescription()) ? trigger.getName() : trigger.getDescription();
this.context = trigger.getContext();
// this.tags = ???
}

public String getTenantId() {
Expand All @@ -123,30 +130,30 @@ public void setCtime(long ctime) {
this.ctime = ctime;
}

public String getEventText() {
return eventText;
public String getText() {
return text;
}

public void setEventText(String eventText) {
this.eventText = eventText;
public void setText(String text) {
this.text = text;
}

public Map<String, String> getTags() {
public Collection<Tag> getTags() {
if (null == tags) {
tags = new HashMap<>();
tags = new HashSet<>();
}
return tags;
}

public void setTags(Map<String, String> tags) {
this.tags = context;
public void setTags(Set<Tag> tags) {
this.tags = tags;
}

public void addTag(String name, String value) {
if (null == name || null == value) {
throw new IllegalArgumentException("Propety must have non-null name and value");
public void addTag(Tag tag) {
if (null == tag) {
throw new IllegalArgumentException("Tag must be non-null");
}
getTags().put(name, value);
getTags().add(tag);
}

public Map<String, String> getContext() {
Expand Down Expand Up @@ -222,4 +229,8 @@ public boolean equals(Object obj) {
return true;
}

private static boolean isEmpty(String s) {
return null == s || s.trim().isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.alerts.api.model.event;

/**
* What the trigger produces.
*
* @author jay shaughnessy
* @author lucas ponce
*/
public enum EventType {
ALERT, EVENT
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
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;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

/**
* A trigger definition.
* A Trigger definition. A Trigger can fire an Alert or an Event.
*
* @author Jay Shaughnessy
* @author Lucas Ponce
Expand All @@ -47,6 +48,20 @@ public class Trigger {
@JsonInclude
private String name;

@JsonInclude(Include.NON_EMPTY)
private String description;

@JsonInclude
private EventType eventType;

// defaults to non-null trigger description, otherwise trigger name
@JsonInclude
private String eventText;

// Ignored for Event Triggers
@JsonInclude
private Severity severity;

@JsonInclude(Include.NON_EMPTY)
protected Map<String, String> context;

Expand Down Expand Up @@ -79,9 +94,6 @@ public class Trigger {
@JsonInclude(Include.NON_EMPTY)
private String memberOf;

@JsonInclude(Include.NON_EMPTY)
private String description;

@JsonInclude
private boolean enabled;

Expand All @@ -94,9 +106,6 @@ public class Trigger {
@JsonInclude
private boolean group;

@JsonInclude
private Severity severity;

@JsonIgnore
private Mode mode;

Expand Down Expand Up @@ -206,6 +215,22 @@ public void setDescription(String description) {
this.description = description;
}

public EventType getEventType() {
return eventType;
}

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

public String getEventText() {
return eventText;
}

public void setEventText(String eventText) {
this.eventText = eventText;
}

public Map<String, String> getContext() {
if (null == context) {
context = new HashMap<>();
Expand Down Expand Up @@ -451,11 +476,11 @@ public int hashCode() {
@Override
public String toString() {
return "Trigger [tenantId=" + tenantId + ", id=" + id + ", name=" + name + ", description=" + description
+ ", autoDisable=" + autoDisable + ", autoEnable=" + autoEnable + ", autoResolve=" + autoResolve
+ ", autoResolveAlerts=" + autoResolveAlerts + ", severity=" + severity + ", actions=" + actions
+ ", firingMatch=" + firingMatch + ", autoResolveMatch=" + autoResolveMatch + ", context=" + context
+ ", group=" + group + ", memberOf=" + memberOf + ", orphan=" + orphan + ", enabled=" + enabled
+ ", mode=" + mode + ", tags=" + tags + "]";
+ ", eventType=" + eventType + ", 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 @@ -23,6 +23,7 @@
import org.hawkular.alerts.api.model.condition.ConditionEval;
import org.hawkular.alerts.api.model.data.Data;
import org.hawkular.alerts.api.model.event.Alert;
import org.hawkular.alerts.api.model.event.Event;
import org.hawkular.alerts.api.model.paging.Page;
import org.hawkular.alerts.api.model.paging.Pager;

Expand Down Expand Up @@ -52,6 +53,13 @@ public interface AlertsService {
*/
void addAlerts(Collection<Alert> alerts) throws Exception;

/**
* Persist the provided events.
* @param events Set of unpersisted Events.
* @throws Exception any problem
*/
void addEvents(Collection<Event> events) throws Exception;

/**
* Delete the requested Alerts, as described by the provided criteria.
* @param tenantId Tenant where alerts are stored
Expand Down

0 comments on commit d3d3604

Please sign in to comment.