Skip to content

Commit

Permalink
A few possible additions to Wintermute
Browse files Browse the repository at this point in the history
- provide a default UUID triggerId if a triggerId is not provided. In most
  prod cases it seems a generated ID will be desirable. users/apps will likely
  not be able to provide a unique ID on their own.
- pull getDataId() down into the Condition base class as it is common to
  almost all conditions and makes coding easier.
- Add a copyTrigger to the definitions service. This is experimental, the
  idea being to enable using a Tokenized Trigger and generating a bunch
  of explicit triggers by provising a map of token dataIds to real dataids.
  • Loading branch information
jshaughn committed Mar 6, 2015
1 parent 5e975a0 commit 853f263
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public CompareCondition(String triggerId, Mode triggerMode, int conditionSetSize
this.data2Multiplier = data2Multiplier;
}

/** Returns null. Use {@link #getDataId1} and {@link #getDataId2}
* @see org.hawkular.alerts.api.model.condition.Condition#getDataId()
*/
public String getDataId() {
return null;
}

public String getData1Id() {
return data1Id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@ public String toString() {
+ conditionSetSize + ", conditionSetIndex=" + conditionSetIndex + "]";
}

/**
* @return The dataId, can be null if the Condition has no relevant dataId.
*/
public abstract String getDataId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.hawkular.alerts.api.model.trigger;

import java.util.UUID;

/**
* A trigger definition.
*
Expand All @@ -39,7 +41,11 @@ public Trigger() {
/*
Default constructor is needed for JSON libraries in JAX-RS context.
*/
this("DefaultId", null);
this("defaultName");
}

public Trigger(String name) {
this(UUID.randomUUID().toString(), name);
}

public Trigger(String id, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public interface DefinitionsService {

Collection<Trigger> getAllTriggers() throws Exception;

/**
* Used to generate an explicit Trigger from a Tokenized Trigger. The dataIdMap replaces the tokens in the
* Conditions with actual dataIds.
* @param triggerId
* @param dataIdMap
* @return
* @throws Exception
*/
Trigger copyTrigger(String triggerId, Map<String, String> dataIdMap) throws Exception;

/*
CRUD interface for Dampening
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,95 @@ public void addTrigger(Trigger trigger) throws Exception {
}
}

@Override
public Trigger copyTrigger(String triggerId, Map<String, String> dataIdMap) throws Exception {
if (triggerId == null || triggerId.isEmpty()) {
throw new IllegalArgumentException("TriggerId must be not null");
}
if (dataIdMap == null || dataIdMap.isEmpty()) {
throw new IllegalArgumentException("DataIdMap must be not null");
}

Trigger trigger = getTrigger(triggerId);
if (trigger == null) {
throw new IllegalArgumentException("Trigger not found for triggerId [" + triggerId + "]");
}
// ensure we have a 1-1 mapping for the dataId substitution
Set<String> dataIdTokens = new HashSet<>();
Collection<Condition> conditions = getTriggerConditions(triggerId, null);
for (Condition c : conditions) {
if (c instanceof CompareCondition) {
dataIdTokens.add(((CompareCondition) c).getData1Id());
dataIdTokens.add(((CompareCondition) c).getData2Id());
} else {
dataIdTokens.add(c.getDataId());
}
}
if (!dataIdTokens.equals(dataIdMap.keySet())) {
throw new IllegalArgumentException(
"DataIdMap must contain the exact dataIds (keyset) expected by the condition set. Expected: "
+ dataIdMap.keySet() + ", dataIdMap: " + dataIdMap.keySet());
}
Collection<Dampening> dampenings = getTriggerDampenings(triggerId, null);

Trigger newTrigger = new Trigger(trigger.getName());
newTrigger.setName(trigger.getName());
newTrigger.setDescription(trigger.getDescription());
newTrigger.setFiringMatch(trigger.getFiringMatch());
newTrigger.setSafetyMatch(trigger.getSafetyMatch());
newTrigger.setNotifiers(trigger.getNotifiers());

addTrigger(newTrigger);

for (Condition c : conditions) {
Condition newCondition = null;
if (c instanceof ThresholdCondition) {
newCondition = new ThresholdCondition(newTrigger.getId(), c.getTriggerMode(),
c.getConditionSetSize(), c.getConditionSetIndex(), dataIdMap.get(c.getDataId()),
((ThresholdCondition) c).getOperator(), ((ThresholdCondition) c).getThreshold());

} else if (c instanceof ThresholdRangeCondition) {
newCondition = new ThresholdRangeCondition(newTrigger.getId(), c.getTriggerMode(),
c.getConditionSetSize(), c.getConditionSetIndex(), dataIdMap.get(c.getDataId()),
((ThresholdRangeCondition) c).getOperatorLow(),
((ThresholdRangeCondition) c).getOperatorHigh(),
((ThresholdRangeCondition) c).getThresholdLow(),
((ThresholdRangeCondition) c).getThresholdHigh(),
((ThresholdRangeCondition) c).isInRange());

} else if (c instanceof AvailabilityCondition) {
newCondition = new AvailabilityCondition(newTrigger.getId(), c.getTriggerMode(),
c.getConditionSetSize(), c.getConditionSetIndex(), dataIdMap.get(c.getDataId()),
((AvailabilityCondition) c).getOperator());

} else if (c instanceof CompareCondition) {
newCondition = new CompareCondition(newTrigger.getId(), c.getTriggerMode(),
c.getConditionSetSize(), c.getConditionSetIndex(), dataIdMap.get(((CompareCondition) c)
.getData1Id()),
((CompareCondition) c).getOperator(),
((CompareCondition) c).getData2Multiplier(),
dataIdMap.get(((CompareCondition) c).getData2Id()));

} else if (c instanceof StringCondition) {
newCondition = new StringCondition(newTrigger.getId(), c.getTriggerMode(),
c.getConditionSetSize(), c.getConditionSetIndex(), dataIdMap.get(c.getDataId()),
((StringCondition) c).getOperator(), ((StringCondition) c).getPattern(),
((StringCondition) c).isIgnoreCase());
}

addCondition(newTrigger.getId(), newCondition.getTriggerMode(), newCondition);
}

for (Dampening d : dampenings) {
Dampening newDampening = new Dampening(newTrigger.getId(), d.getTriggerMode(), d.getType(),
d.getEvalTrueSetting(), d.getEvalTotalSetting(), d.getEvalTimeSetting());

addDampening(newDampening);
}

return newTrigger;
}

@Override
public Map<String, String> getNotifier(String notifierId) throws Exception {
if (notifierId == null || notifierId.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.h2.jdbcx.JdbcDataSource;
import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.condition.Condition;
import org.hawkular.alerts.api.model.dampening.Dampening;
import org.hawkular.alerts.api.model.data.Data;
import org.hawkular.alerts.api.model.trigger.Trigger;
import org.hawkular.alerts.api.services.AlertsService;
import org.hawkular.alerts.engine.impl.DbDefinitionsServiceImpl;
import org.junit.Before;
Expand Down Expand Up @@ -63,6 +68,54 @@ public void checkInitTest() throws Exception {
assertTrue(db.getAllNotifiers().size() > 0);
}

@Test
public void copyTriggerTest() throws Exception {

DbDefinitionsServiceImpl db = new DbDefinitionsServiceImpl(new TestAlertsService(), ds);
db.init();

Trigger t = db.getTrigger("trigger-1");
assert t != null;

Collection<Condition> cs = db.getTriggerConditions(t.getId(), null);
assert cs.size() == 1 : cs;
Condition c = cs.iterator().next();

Collection<Dampening> ds = db.getTriggerDampenings(t.getId(), null);
assert ds.size() == 1 : cs;
Dampening d = ds.iterator().next();

Map<String, String> dataIdMap = new HashMap<>(1);
dataIdMap.put(c.getDataId(), "NewDataId");

Trigger nt = db.copyTrigger(t.getId(), dataIdMap);
assert nt != null;
assert !nt.getId().equals(t.getId()) : nt;
assert nt.getName().equals(t.getName()) : nt;
assert nt.getDescription().equals(t.getDescription()) : nt;
assert nt.getFiringMatch().equals(t.getFiringMatch()) : nt;
assert nt.getSafetyMatch().equals(t.getSafetyMatch()) : nt;

Collection<Condition> ncs = db.getTriggerConditions(nt.getId(), null);
assert ncs.size() == 1 : ncs;
Condition nc = ncs.iterator().next();
assert nc.getClass().equals(c.getClass()) : nc;
assert nc.getTriggerId().equals(nt.getId()) : nc;
assert nc.getTriggerMode().equals(c.getTriggerMode()) : nc;
assert nc.getDataId().equals("NewDataId") : nc;
assert nc.getConditionSetIndex() == c.getConditionSetIndex() : nc;
assert nc.getConditionSetSize() == c.getConditionSetSize() : nc;

Collection<Dampening> nds = db.getTriggerDampenings(nt.getId(), null);
assert nds.size() == 1 : nds;
Dampening nd = nds.iterator().next();
assert nd.getTriggerId().equals(nt.getId()) : nd;
assert nd.getTriggerMode().equals(d.getTriggerMode()) : nd;
assert nd.getEvalTrueSetting() == d.getEvalTrueSetting() : nd;
assert nd.getEvalTotalSetting() == d.getEvalTotalSetting() : nd;
assert nd.getEvalTimeSetting() == d.getEvalTimeSetting() : nd;
}

private static class TestAlertsService implements AlertsService {

@Override
Expand Down

0 comments on commit 853f263

Please sign in to comment.