Skip to content

Commit

Permalink
Merge pull request #100 from jshaughn/jay-events
Browse files Browse the repository at this point in the history
Playing with new Event impact on Trigger (API module mainly)
  • Loading branch information
jshaughn committed Sep 28, 2015
2 parents 73846ce + 510980e commit 249d999
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@

import org.hawkular.alerts.actions.api.PluginMessage;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.event.Alert;
import org.hawkular.alerts.api.model.condition.AvailabilityCondition;
import org.hawkular.alerts.api.model.condition.AvailabilityConditionEval;
import org.hawkular.alerts.api.model.condition.ConditionEval;
import org.hawkular.alerts.api.model.condition.ThresholdCondition;
import org.hawkular.alerts.api.model.condition.ThresholdConditionEval;
import org.hawkular.alerts.api.model.data.AvailabilityType;
import org.hawkular.alerts.api.model.data.Data;
import org.hawkular.alerts.api.model.event.Alert;
import org.hawkular.alerts.api.model.trigger.Trigger;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.hawkular.alerts.api.model.trigger.Trigger;

/**
* Provide test data for Availability Alerts on Url resources
* Provide test data for Data Alerts on Url resources
*
* @author Jay Shaughnessy
* @author Lucas Ponce
Expand All @@ -53,7 +53,7 @@ public class UrlAvailabilityData extends CommonData {
context.put("resourceName", "http://www.jboss.org");

String triggerId = "jboss-url-availability-trigger";
String triggerDescription = "Availability for http://www.jboss.org";
String triggerDescription = "Data for http://www.jboss.org";
String dataId = "jboss-url-availability-data-id";

trigger = new Trigger(TEST_TENANT,
Expand Down
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 @@ -173,7 +173,6 @@ public void addProperty(String name, String value) {
context.put(name, value);
}


@Override
public int hashCode() {
final int prime = 31;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ 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;

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

// Null for API-generated Events. Otherwise the Trigger that created the event (@ctime)
@JsonInclude(Include.NON_EMPTY)
Expand All @@ -75,10 +75,18 @@ 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) {
this(tenantId, id, text, null, null);
}

public Event(String tenantId, String id, String text, Map<String, String> context) {
this(tenantId, id, text, context, null);
}

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

Expand All @@ -94,9 +102,9 @@ public Event(String tenantId, Trigger trigger, Dampening dampening, List<Set<Con
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 = ???
this.tags = trigger.getTags();
}

public String getTenantId() {
Expand All @@ -123,12 +131,12 @@ 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() {
Expand All @@ -139,12 +147,12 @@ public Map<String, String> getTags() {
}

public void setTags(Map<String, String> tags) {
this.tags = context;
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");
throw new IllegalArgumentException("Tag must have non-null name and value");
}
getTags().put(name, value);
}
Expand Down Expand Up @@ -222,4 +230,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
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void jsonToAlertTest() throws Exception {
"}," +
"\"ctime\":1436964192878," +
"\"context\":{\"n1\":\"v1\",\"n2\":\"v2\"}," +
"\"eventText\":\"trigger-test\"," +
"\"text\":\"trigger-test\"," +
"\"evalSets\":[" +
"[{\"evalTimestamp\":1436964294055," +
"\"dataTimestamp\":2," +
Expand Down Expand Up @@ -189,7 +189,7 @@ public void jsonToAlertTest() throws Exception {
assertTrue(alert.getContext().size() == 2);
assertTrue(alert.getContext().get("n1").equals("v1"));
assertTrue(alert.getContext().get("n2").equals("v2"));
assertEquals("trigger-test", alert.getEventText());
assertEquals("trigger-test", alert.getText());

/*
Testing thin deserializer
Expand Down

0 comments on commit 249d999

Please sign in to comment.