Skip to content

Commit

Permalink
HWKALERTS-99 Introduce resulting events/alerts as input events
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Nov 2, 2015
1 parent 4f7c338 commit cc8f388
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,36 @@ public EventCondition() {
this("DefaultId", Mode.FIRING, 1, 1, null, null);
}

public EventCondition(String triggerId, String dataId) {
this(triggerId, Mode.FIRING, 1, 1, dataId, null);
}

public EventCondition(String triggerId, String dataId, String expression) {
this(triggerId, Mode.FIRING, 1, 1, dataId, expression);
}

public EventCondition(String triggerId, Mode triggerMode, String dataId) {
this(triggerId, triggerMode, 1, 1, dataId, null);
}

public EventCondition(String triggerId, Mode triggerMode, String dataId, String expression) {
this(triggerId, triggerMode, 1, 1, dataId, expression);
}

public EventCondition(String triggerId, int conditionSetSize, int conditionSetIndex, String dataId) {
this(triggerId, Mode.FIRING, conditionSetSize, conditionSetIndex, dataId, null);
}

public EventCondition(String triggerId, int conditionSetSize, int conditionSetIndex,
String dataId, String expression) {
this(triggerId, Mode.FIRING, conditionSetSize, conditionSetIndex, dataId, expression);
}

public EventCondition(String triggerId, Mode triggerMode, int conditionSetSize, int conditionSetIndex,
String dataId) {
this(triggerId, triggerMode, conditionSetSize, conditionSetIndex, dataId, null);
}

public EventCondition(String triggerId, Mode triggerMode, int conditionSetSize, int conditionSetIndex,
String dataId, String expression) {
super(triggerId, triggerMode, conditionSetSize, conditionSetIndex, Type.EVENT);
Expand Down Expand Up @@ -82,9 +99,12 @@ public void setExpression(String expression) {
}

public boolean match(Event value) {
if (null == expression || expression.isEmpty() || null == value) {
if (null == value) {
return false;
}
if (null == expression || expression.isEmpty()) {
return true;
}
String[] expressions = expression.split(",");
for (int i = 0; i < expressions.length; i++) {
if (!processExpression(expressions[i], value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public Event(String tenantId, Trigger trigger, Dampening dampening, List<Set<Con
this.ctime = System.currentTimeMillis();

this.id = trigger.getId() + "-" + this.ctime;
this.dataId = trigger.getId();
this.context = trigger.getContext();
if (!isEmpty(trigger.getEventCategory())) {
this.category = trigger.getEventCategory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ rule AlertOnSatisfiedDampening
events.add(newEvent);
}

// We insert the generated events on the firing cycles to allow chained conditions
insert( newEvent );

if (actions != null) {
for (String actionPlugin : $t.getActions().keySet()) {
for (String actionId : $t.getActions().get(actionPlugin)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ public void EventTest() {
}

@Test
public void MultipleEventConditions() {
public void multipleEventConditions() {
Trigger t1 = new Trigger("tenant", "trigger-1", "Events Test");
t1.setEventType(EventType.EVENT);
EventCondition t1c1 = new EventCondition("trigger-1", Mode.FIRING, 3, 1, "myapp.war",
Expand All @@ -971,6 +971,9 @@ public void MultipleEventConditions() {
EventCondition t1c3 = new EventCondition("trigger-1", Mode.FIRING, 3, 3, "datacenter2",
"text starts 'WARN'");

/*
On multiple conditions timestamps on input events are important.
*/
Event appDownEvent1 = new Event("tenant", UUID.randomUUID().toString(), 1, "myapp.war",
EventCategory.DEPLOYMENT.name(), "DOWN");
Event logErrorEvent1 = new Event("tenant", UUID.randomUUID().toString(), 2, "datacenter1",
Expand Down Expand Up @@ -1018,6 +1021,90 @@ public void MultipleEventConditions() {
assertEquals(outputEvents.toString(), 3, outputEvents.size());
}

@Test
public void chainedEventsRules() {
Trigger t1 = new Trigger("tenant", "trigger-1", "A.war");
t1.setEventType(EventType.EVENT);
EventCondition t1c1 = new EventCondition("trigger-1", Mode.FIRING, "A.war", "text == 'DOWN'");

Trigger t2 = new Trigger("tenant", "trigger-2", "B.war");
t2.setEventType(EventType.EVENT);
EventCondition t2c1 = new EventCondition("trigger-2", Mode.FIRING, "B.war", "text == 'DOWN'");

Trigger t3 = new Trigger("tenant", "trigger-3", "A.war and B.war DOWN");
EventCondition t3c1 = new EventCondition("trigger-3", Mode.FIRING, 2, 1, "trigger-1");
EventCondition t3c2 = new EventCondition("trigger-3", Mode.FIRING, 2, 2, "trigger-2");


Event appADownEvent1 = new Event("tenant", UUID.randomUUID().toString(), 1, "A.war",
EventCategory.DEPLOYMENT.name(), "DOWN");

Event appBDownEvent1 = new Event("tenant", UUID.randomUUID().toString(), 1, "B.war",
EventCategory.DEPLOYMENT.name(), "DOWN");

Event appADownEvent2 = new Event("tenant", UUID.randomUUID().toString(), 2, "A.war",
EventCategory.DEPLOYMENT.name(), "DOWN");

Event appBDownEvent2 = new Event("tenant", UUID.randomUUID().toString(), 2, "B.war",
EventCategory.DEPLOYMENT.name(), "DOWN");

Event appADownEvent3 = new Event("tenant", UUID.randomUUID().toString(), 3, "A.war",
EventCategory.DEPLOYMENT.name(), "DOWN");

Event appBDownEvent3 = new Event("tenant", UUID.randomUUID().toString(), 3, "B.war",
EventCategory.DEPLOYMENT.name(), "DOWN");

inputEvents.add(appADownEvent1);
inputEvents.add(appBDownEvent1);
inputEvents.add(appADownEvent2);
inputEvents.add(appBDownEvent2);
inputEvents.add(appADownEvent3);
inputEvents.add(appBDownEvent3);

t1.setEnabled(true);
t2.setEnabled(true);
t3.setEnabled(true);

rulesEngine.addFact(t1);
rulesEngine.addFact(t1c1);
rulesEngine.addFact(t2);
rulesEngine.addFact(t2c1);
rulesEngine.addFact(t3);
rulesEngine.addFact(t3c1);
rulesEngine.addFact(t3c2);

rulesEngine.addEvent(inputEvents);

rulesEngine.fire();

assertEquals(outputEvents.toString(), 6, outputEvents.size());

/*
Expected inference:
Alert 1:
id=trigger-1-Event-timestamp-1
id=trigger-2-Event-timestamp-1
Alert 2:
id=trigger-1-Event-timestamp-2 ==> NEW Event from trigger-1
id=trigger-2-Event-timestamp-1
Alert 3:
id=trigger-1-Event-timestamp-2
id=trigger-2-Event-timestamp-2 ==> NEW Event from trigger-2
Alert 4:
id=trigger-1-Event-timestamp-3 ==> NEW Event from trigger-1
id=trigger-2-Event-timestamp-2
Alert 5:
id=trigger-1-Event-timestamp-3
id=trigger-2-Event-timestamp-3 ==> NEW Event from trigger-2
*/
assertEquals(alerts.toString(), 5, alerts.size());
}

@Test
public void DampeningStrictTest() {
Trigger t1 = new Trigger("tenant", "trigger-1", "Avail-DOWN");
Expand Down

0 comments on commit cc8f388

Please sign in to comment.