Skip to content

Commit

Permalink
Merge branch 'Events' into copy-master
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Nov 4, 2015
2 parents 46aa5bb + e4c5075 commit 992c94a
Show file tree
Hide file tree
Showing 120 changed files with 4,927 additions and 1,993 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
import java.util.Set;

import org.hawkular.alerts.actions.api.ActionMessage;
import org.hawkular.alerts.api.model.Severity;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.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.Availability;
import org.hawkular.alerts.api.model.data.NumericData;
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 All @@ -53,17 +53,18 @@ public class JsonTest {

@BeforeClass
public static void initTest() {
Alert alert = new Alert(TEST_TENANT, "trigger-test", Severity.MEDIUM, null);
Trigger trigger = new Trigger(TEST_TENANT, "trigger-test", "trigger-test");
Alert alert = new Alert(TEST_TENANT, trigger, null);

AvailabilityCondition aCond = new AvailabilityCondition("trigger-test", "Default",
AvailabilityCondition.Operator.UP);
Availability aData = new Availability("Metric-test", 1, Availability.AvailabilityType.UP);
Data aData = Data.forAvailability("Metric-test", 1, AvailabilityType.UP);
AvailabilityConditionEval aEval = new AvailabilityConditionEval(aCond, aData);

ThresholdCondition tCond = new ThresholdCondition("trigger-test", "Default",
ThresholdCondition.Operator.LTE,
50.0);
NumericData tData = new NumericData("Metric-test2", 2, 25.5);
Data tData = Data.forNumeric("Metric-test2", 2, 25.5);
ThresholdConditionEval tEval = new ThresholdConditionEval(tCond, tData);

Set<ConditionEval> evals = new HashSet<>();
Expand All @@ -82,7 +83,6 @@ public static void initTest() {
incomingAction.setProperties(props);
}


@Test
public void jsonPluginMessage() throws Exception {

Expand All @@ -95,7 +95,7 @@ public void jsonPluginMessage() throws Exception {
ActionMessage newMsg = objectMapper.readValue(json, TestActionMessage.class);

assertEquals("v2", newMsg.getAction().getProperties().get("k2"));
assertEquals("trigger-test", newMsg.getAction().getAlert().getTriggerId());
assertEquals("trigger-test", newMsg.getAction().getEvent().getTrigger().getId());
}

public static class TestActionMessage implements ActionMessage {
Expand Down
1 change: 1 addition & 0 deletions hawkular-alerts-actions-plugins/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
import org.hawkular.alerts.actions.api.Sender;
import org.hawkular.alerts.api.json.JsonUtil;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.event.Alert;
import org.hawkular.alerts.api.model.event.Event;
import org.jboss.aerogear.unifiedpush.DefaultPushSender;
import org.jboss.aerogear.unifiedpush.PushSender;
import org.jboss.aerogear.unifiedpush.message.UnifiedMessage;
Expand Down Expand Up @@ -127,15 +128,19 @@ private boolean isBlank(String value) {

private String prepareMessage(ActionMessage msg) {
String preparedMsg = null;
if (msg.getAction() != null && msg.getAction().getAlert() != null) {
Alert alert = msg.getAction().getAlert();
if (alert != null) {
Event event = msg.getAction() != null ? msg.getAction().getEvent() : null;

if (event != null) {
if (event instanceof Alert) {
Alert alert = (Alert) event;
preparedMsg = "Alert : " + alert.getTriggerId() + " at " + alert.getCtime() + " -- Severity: " +
alert.getSeverity().toString();
} else {
preparedMsg = "Message received without data at " + System.currentTimeMillis();
msgLog.warnMessageReceivedWithoutPayload("aerogear");
preparedMsg = "Event [" + event.getCategory() + "] " + event.getText() + " at " + event.getCtime();
}
} else {
preparedMsg = "Message received without data at " + System.currentTimeMillis();
msgLog.warnMessageReceivedWithoutPayload("aerogear");
}
return preparedMsg;
}
Expand All @@ -155,5 +160,4 @@ private void sendResult(Action action) {
msgLog.error("Error sending ActionResponseMessage", e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@
import org.hawkular.alerts.actions.api.ActionMessage;
import org.hawkular.alerts.actions.api.ActionPluginSender;
import org.hawkular.alerts.actions.api.ActionResponseMessage;
import org.hawkular.alerts.api.model.Severity;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.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.Availability;
import org.hawkular.alerts.api.model.data.NumericData;
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.jboss.aerogear.unifiedpush.PushSender;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -74,26 +74,26 @@ public Action getAction() {
}
}


@BeforeClass
public static void configureListener() {
System.setProperty(AerogearPlugin.ROOT_SERVER_URL_PROPERTY, "http://localhost:9191/ag-push");
System.setProperty(AerogearPlugin.APPLICATION_ID_PROPERTY, "4d564d56qs4056-d0sq564065");
System.setProperty(AerogearPlugin.MASTER_SECRET_PROPERTY, "sddqs--sqd-qs--d-qs000dsq0d");

Alert alert = new Alert(TEST_TENANT, "trigger-test", Severity.MEDIUM, null);
Trigger trigger = new Trigger( TEST_TENANT, "trigger-test", "trigger-test");
Alert alert = new Alert(TEST_TENANT, trigger, null);

AvailabilityCondition aCond = new AvailabilityCondition("trigger-test",
"Default",
AvailabilityCondition.Operator.UP);
Availability aData = new Availability("Metric-test", 1, Availability.AvailabilityType.UP);
Data aData = Data.forAvailability("Metric-test", 1, AvailabilityType.UP);
AvailabilityConditionEval aEval = new AvailabilityConditionEval(aCond, aData);

ThresholdCondition tCond = new ThresholdCondition("trigger-test",
"Default",
ThresholdCondition.Operator.LTE,
50.0);
NumericData tData = new NumericData("Metric-test2", 2, 25.5);
Data tData = Data.forNumeric("Metric-test2", 2, 25.5);
ThresholdConditionEval tEval = new ThresholdConditionEval(tCond, tData);

Set<ConditionEval> evals = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
import org.hawkular.alerts.actions.api.Sender;
import org.hawkular.alerts.api.json.JsonUtil;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.condition.Alert.Status;
import org.hawkular.alerts.api.model.event.Alert;
import org.hawkular.alerts.api.model.event.Alert.Status;
import org.hawkular.alerts.api.model.event.Event;
import org.jboss.logging.Logger;

/**
Expand Down Expand Up @@ -278,7 +279,8 @@ protected Message createMimeMessage(ActionMessage msg) throws Exception {
if (null == props || props.isEmpty()) {
msgLog.warn("Properties empty on plugin " + PLUGIN_NAME);
}
Alert alert = msg.getAction() != null ? msg.getAction().getAlert() : null;
Event event = msg.getAction() != null ? (Alert) msg.getAction().getEvent() : null;
Alert alert = null != event && (event instanceof Alert) ? (Alert) event : null;
Status status = alert != null && alert.getStatus() != null ? alert.getStatus() : Status.OPEN;
String statusStr = status.name().toLowerCase();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Set;

import org.hawkular.alerts.actions.api.ActionMessage;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.condition.AvailabilityCondition;
import org.hawkular.alerts.api.model.condition.AvailabilityConditionEval;
import org.hawkular.alerts.api.model.condition.CompareCondition;
Expand All @@ -41,6 +40,8 @@
import org.hawkular.alerts.api.model.condition.ThresholdRangeCondition;
import org.hawkular.alerts.api.model.condition.ThresholdRangeConditionEval;
import org.hawkular.alerts.api.model.dampening.Dampening;
import org.hawkular.alerts.api.model.event.Alert;
import org.hawkular.alerts.api.model.event.Event;
import org.hawkular.alerts.api.model.trigger.Trigger;

/**
Expand Down Expand Up @@ -69,6 +70,9 @@ public class PluginMessageDescription {
/** Context property "description". Supported at Condition.getContext() level with CompareCondition classes */
public static final String CONTEXT_PROPERTY_DESCRIPTION2 = "description2";

/** Shortcut for PluginMessage.getAction().event */
private Event event;

/** Shortcut for PluginMessage.getAction().alert */
private Alert alert;

Expand Down Expand Up @@ -203,16 +207,24 @@ public PluginMessageDescription(ActionMessage pm) {
if (pm.getAction().getProperties() == null) {
throw new IllegalArgumentException("Properties cannot be null on PluginMessage");
}
alert = pm.getAction().getAlert();
event = pm.getAction().getEvent();
if (event instanceof Alert) {
alert = (Alert)event;
}
props = pm.getAction().getProperties();
if (alert != null && alert.getStatus() != null) {
status = alert.getStatus().name().toLowerCase();
emailSubject = "Alert [" + status + "] message";
if (event != null && event instanceof Alert) {
Alert alert = (Alert) event;
if (alert.getStatus() != null) {
status = alert.getStatus().name().toLowerCase();
emailSubject = "Alert [" + status + "] message";
} else {
emailSubject = "Alert message";
}
} else {
emailSubject = "Alert message";
emailSubject = "Event message";
}
if (alert != null && alert.getTrigger() != null) {
trigger = alert.getTrigger();
if (event != null && event.getTrigger() != null) {
trigger = event.getTrigger();
if (trigger.getContext() != null &&
!trigger.getContext().isEmpty() &&
trigger.getContext().containsKey(CONTEXT_PROPERTY_RESOURCE_TYPE) &&
Expand All @@ -223,11 +235,11 @@ public PluginMessageDescription(ActionMessage pm) {
triggerDescription = trigger.getName();
}
}
if (alert != null && alert.getDampening() != null) {
dampening = alert.getDampening();
if (event != null && event.getDampening() != null) {
dampening = event.getDampening();
dampeningDescription = dampeningDescription(dampening);
}
initConditions(alert);
initConditions(event);

if (numConditions == 1) {
emailSubject += ": " + conditions[0].description + " ";
Expand All @@ -245,7 +257,8 @@ public PluginMessageDescription(ActionMessage pm) {
}

private String dampeningDescription(Dampening d) {
if (d == null) return null;
if (d == null)
return null;
String description = "Alert triggered ";
switch (d.getType()) {
case STRICT:
Expand All @@ -255,29 +268,32 @@ private String dampeningDescription(Dampening d) {
description += "after " + d.getEvalTrueSetting() + " of " + d.getEvalTotalSetting() + " evaluations";
break;
case RELAXED_TIME:
description += "after" + d.getEvalTrueSetting() + " evaluations in " + (d.getEvalTimeSetting()/1000) +
description += "after" + d.getEvalTrueSetting() + " evaluations in " + (d.getEvalTimeSetting() / 1000)
+
" s";
break;
case STRICT_TIME:
case STRICT_TIMEOUT:
description += "after " + (d.getEvalTimeSetting()/1000) + " s";
description += "after " + (d.getEvalTimeSetting() / 1000) + " s";
break;
default:
throw new IllegalArgumentException(d.getType().name());
}
return description;
}

private void initConditions(Alert alert) {
private void initConditions(Event event) {
numConditions = 0;
if (alert == null) return;
if (alert.getEvalSets() == null || alert.getEvalSets().isEmpty()) return;
if (event == null)
return;
if (event.getEvalSets() == null || event.getEvalSets().isEmpty())
return;

Map<String, ConditionDescription> mapConditions = new HashMap<>();

int listEvals = alert.getEvalSets().size();
int listEvals = event.getEvalSets().size();
for (int i = 0; i < listEvals; i++) {
Set<ConditionEval> iEvalSet = alert.getEvalSets().get(i);
Set<ConditionEval> iEvalSet = event.getEvalSets().get(i);
for (ConditionEval condEval : iEvalSet) {
Condition condition = extractCondition(condEval);
if (!mapConditions.containsKey(condition.getConditionId())) {
Expand All @@ -297,7 +313,7 @@ private void initConditions(Alert alert) {

for (int i = 0; i < numConditions; i++) {
ConditionDescription condDesc = conditions[i];
condDesc.average = ( condDesc.data.stream().reduce(0.0, (j,k) -> j+k ) ) / condDesc.data.size();
condDesc.average = (condDesc.data.stream().reduce(0.0, (j, k) -> j + k)) / condDesc.data.size();
if (condDesc.condition.getContext().containsKey(CONTEXT_PROPERTY_UNIT)) {
condDesc.averageDescription = decimalFormat.format(condDesc.average) + " " +
condDesc.condition.getContext().get(CONTEXT_PROPERTY_UNIT);
Expand All @@ -306,7 +322,8 @@ private void initConditions(Alert alert) {
}

private Condition extractCondition(ConditionEval conditionEval) {
if (conditionEval == null) return null;
if (conditionEval == null)
return null;
switch (conditionEval.getType()) {
case AVAILABILITY:
return ((AvailabilityConditionEval) conditionEval).getCondition();
Expand All @@ -326,7 +343,8 @@ private Condition extractCondition(ConditionEval conditionEval) {
}

private Double extractValue(ConditionEval conditionEval) {
if (conditionEval == null) return 0d;
if (conditionEval == null)
return 0d;
switch (conditionEval.getType()) {
case THRESHOLD:
return ((ThresholdConditionEval) conditionEval).getValue();
Expand All @@ -338,7 +356,8 @@ private Double extractValue(ConditionEval conditionEval) {
}

private String description(Condition condition) {
if (condition == null) return null;
if (condition == null)
return null;
switch (condition.getType()) {
case AVAILABILITY:
return availability((AvailabilityCondition) condition);
Expand Down Expand Up @@ -604,6 +623,14 @@ public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}

public Event getEvent() {
return event;
}

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

public Alert getAlert() {
return alert;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.hawkular.alerts.actions.tests.JvmGarbageCollectionData;
import org.hawkular.alerts.actions.tests.TestActionMessage;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.event.Alert;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.hawkular.alerts.actions.tests.JvmGarbageCollectionData;
import org.hawkular.alerts.actions.tests.TestActionMessage;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.event.Alert;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.hawkular.alerts.actions.tests.JvmHeapUsageData;
import org.hawkular.alerts.actions.tests.TestActionMessage;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.event.Alert;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.hawkular.alerts.actions.tests.JvmNonHeapUsageData;
import org.hawkular.alerts.actions.tests.TestActionMessage;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.event.Alert;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.hawkular.alerts.actions.tests.MultipleAllJvmData;
import org.hawkular.alerts.actions.tests.TestActionMessage;
import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.event.Alert;
import org.junit.BeforeClass;
import org.junit.Test;

Expand Down

0 comments on commit 992c94a

Please sign in to comment.