Skip to content

Commit

Permalink
Add context map at Condition level
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Sep 3, 2015
1 parent 55044e3 commit 9bbc282
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.hawkular.alerts.api.model.condition;

import static com.fasterxml.jackson.annotation.JsonInclude.*;

import java.util.Map;

import org.hawkular.alerts.api.model.trigger.Mode;

import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down Expand Up @@ -70,6 +74,9 @@ public enum Type {
@JsonInclude
protected String conditionId;

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

public Condition() {
// for json assembly
}
Expand Down Expand Up @@ -131,6 +138,14 @@ public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}

public Map<String, String> getContext() {
return context;
}

public void setContext(Map<String, String> context) {
this.context = context;
}

private void updateId() {
StringBuilder sb = new StringBuilder(triggerId);
sb.append("-").append(triggerMode.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ private void initConditions(File initFolder) throws Exception {
Mode triggerMode = Mode.valueOf((String) c.get("triggerMode"));
int conditionSetSize = (Integer) c.get("conditionSetSize");
int conditionSetIndex = (Integer) c.get("conditionSetIndex");
String description = (String) c.get("description");
String type = (String) c.get("type");
Map<String, String> context = (Map<String, String>) c.get("context");
if (type != null && !type.isEmpty() && type.equals("threshold")) {
String dataId = (String) c.get("dataId");
String operator = (String) c.get("operator");
Expand All @@ -244,6 +246,7 @@ private void initConditions(File initFolder) throws Exception {
newCondition.setOperator(ThresholdCondition.Operator.valueOf(operator));
newCondition.setThreshold(threshold);
newCondition.setTenantId(tenantId);
newCondition.setContext(context);

initCondition(newCondition);
log.debugf("Init registration - Inserting [%s]", newCondition);
Expand All @@ -268,6 +271,7 @@ private void initConditions(File initFolder) throws Exception {
newCondition.setThresholdHigh(thresholdHigh);
newCondition.setInRange(inRange);
newCondition.setTenantId(tenantId);
newCondition.setContext(context);

initCondition(newCondition);
log.debugf("Init registration - Inserting [%s]", newCondition);
Expand All @@ -288,6 +292,7 @@ private void initConditions(File initFolder) throws Exception {
newCondition.setData2Multiplier(data2Multiplier);
newCondition.setData2Id(data2Id);
newCondition.setTenantId(tenantId);
newCondition.setContext(context);

initCondition(newCondition);
log.debugf("Init registration - Inserting [%s]", newCondition);
Expand All @@ -308,6 +313,7 @@ private void initConditions(File initFolder) throws Exception {
newCondition.setPattern(pattern);
newCondition.setIgnoreCase(ignoreCase);
newCondition.setTenantId(tenantId);
newCondition.setContext(context);

initCondition(newCondition);
log.debugf("Init registration - Inserting [%s]", newCondition);
Expand All @@ -324,6 +330,7 @@ private void initConditions(File initFolder) throws Exception {
newCondition.setDataId(dataId);
newCondition.setOperator(AvailabilityCondition.Operator.valueOf(operator));
newCondition.setTenantId(tenantId);
newCondition.setContext(context);

initCondition(newCondition);
log.debugf("Init registration - Inserting [%s]", newCondition);
Expand Down Expand Up @@ -1851,51 +1858,53 @@ public Collection<Condition> setConditions(String tenantId, String triggerId, Mo
if (cond instanceof AvailabilityCondition) {

AvailabilityCondition aCond = (AvailabilityCondition) cond;
futures.add(session.executeAsync(insertConditionAvailability.bind(aCond.getTenantId(), aCond
.getTriggerId(),
aCond.getTriggerMode().name(), aCond.getConditionSetSize(), aCond.getConditionSetIndex(),
futures.add(session.executeAsync(insertConditionAvailability.bind(aCond.getTenantId(),
aCond.getTriggerId(), aCond.getTriggerMode().name(), aCond.getContext(),
aCond.getConditionSetSize(), aCond.getConditionSetIndex(),
aCond.getConditionId(), aCond.getDataId(), aCond.getOperator().name())));

} else if (cond instanceof CompareCondition) {

CompareCondition cCond = (CompareCondition) cond;
dataIds.add(cCond.getData2Id());
futures.add(session.executeAsync(insertConditionCompare.bind(cCond.getTenantId(),
cCond.getTriggerId(), cCond.getTriggerMode().name(), cCond.getConditionSetSize(),
cCond.getConditionSetIndex(), cCond.getConditionId(), cCond.getDataId(),
cCond.getOperator().name(), cCond.getData2Id(), cCond.getData2Multiplier())));
cCond.getTriggerId(), cCond.getTriggerMode().name(), cCond.getContext(),
cCond.getConditionSetSize(), cCond.getConditionSetIndex(),
cCond.getConditionId(), cCond.getDataId(), cCond.getOperator().name(), cCond.getData2Id(),
cCond.getData2Multiplier())));

} else if (cond instanceof ExternalCondition) {

ExternalCondition eCond = (ExternalCondition) cond;
futures.add(session.executeAsync(insertConditionExternal.bind(eCond.getTenantId(), eCond
.getTriggerId(), eCond.getTriggerMode().name(), eCond.getConditionSetSize(),
eCond.getConditionSetIndex(), eCond.getConditionId(), eCond.getDataId(),
eCond.getSystemId(), eCond.getExpression())));
futures.add(session.executeAsync(insertConditionExternal.bind(eCond.getTenantId(),
eCond.getTriggerId(), eCond.getTriggerMode().name(), eCond.getContext(),
eCond.getConditionSetSize(), eCond.getConditionSetIndex(), eCond.getConditionId(),
eCond.getDataId(), eCond.getSystemId(), eCond.getExpression())));

} else if (cond instanceof StringCondition) {

StringCondition sCond = (StringCondition) cond;
futures.add(session.executeAsync(insertConditionString.bind(sCond.getTenantId(), sCond
.getTriggerId(), sCond.getTriggerMode().name(), sCond.getConditionSetSize(),
sCond.getConditionSetIndex(), sCond.getConditionId(), sCond.getDataId(),
sCond.getOperator().name(), sCond.getPattern(), sCond.isIgnoreCase())));
futures.add(session.executeAsync(insertConditionString.bind(sCond.getTenantId(),
sCond.getTriggerId(), sCond.getTriggerMode().name(), sCond.getContext(),
sCond.getConditionSetSize(), sCond.getConditionSetIndex(), sCond.getConditionId(),
sCond.getDataId(), sCond.getOperator().name(), sCond.getPattern(), sCond.isIgnoreCase())));

} else if (cond instanceof ThresholdCondition) {
ThresholdCondition tCond = (ThresholdCondition) cond;
futures.add(session.executeAsync(insertConditionThreshold.bind(tCond.getTenantId(),
tCond.getTriggerId(), tCond.getTriggerMode().name(), tCond.getConditionSetSize(),
tCond.getConditionSetIndex(), tCond.getConditionId(), tCond.getDataId(),
tCond.getOperator().name(), tCond.getThreshold())));
tCond.getTriggerId(), tCond.getTriggerMode().name(), tCond.getContext(),
tCond.getConditionSetSize(), tCond.getConditionSetIndex(),
tCond.getConditionId(), tCond.getDataId(), tCond.getOperator().name(),
tCond.getThreshold())));

} else if (cond instanceof ThresholdRangeCondition) {

ThresholdRangeCondition rCond = (ThresholdRangeCondition) cond;
futures.add(session.executeAsync(insertConditionThresholdRange.bind(rCond.getTenantId(),
rCond.getTriggerId(), rCond.getTriggerMode().name(), rCond.getConditionSetSize(),
rCond.getConditionSetIndex(), rCond.getConditionId(), rCond.getDataId(),
rCond.getOperatorLow().name(), rCond.getOperatorHigh().name(), rCond.getThresholdLow(),
rCond.getThresholdHigh(), rCond.isInRange())));
rCond.getTriggerId(), rCond.getTriggerMode().name(), rCond.getContext(),
rCond.getConditionSetSize(), rCond.getConditionSetIndex(), rCond.getConditionId(),
rCond.getDataId(), rCond.getOperatorLow().name(), rCond.getOperatorHigh().name(),
rCond.getThresholdLow(), rCond.getThresholdHigh(), rCond.isInRange())));

} else {
throw new IllegalArgumentException("Unexpected ConditionType: " + cond);
Expand Down Expand Up @@ -2213,6 +2222,7 @@ private Condition mapCondition(Row row) throws Exception {
aCondition.setConditionSetIndex(row.getInt("conditionSetIndex"));
aCondition.setDataId(row.getString("dataId"));
aCondition.setOperator(AvailabilityCondition.Operator.valueOf(row.getString("operator")));
aCondition.setContext(row.getMap("context", String.class, String.class));
condition = aCondition;
break;
case COMPARE:
Expand All @@ -2226,6 +2236,7 @@ private Condition mapCondition(Row row) throws Exception {
cCondition.setOperator(CompareCondition.Operator.valueOf(row.getString("operator")));
cCondition.setData2Id(row.getString("data2Id"));
cCondition.setData2Multiplier(row.getDouble("data2Multiplier"));
cCondition.setContext(row.getMap("context", String.class, String.class));
condition = cCondition;
break;
case EXTERNAL:
Expand All @@ -2238,6 +2249,7 @@ private Condition mapCondition(Row row) throws Exception {
eCondition.setDataId(row.getString("dataId"));
eCondition.setSystemId(row.getString("operator"));
eCondition.setExpression(row.getString("pattern"));
eCondition.setContext(row.getMap("context", String.class, String.class));
condition = eCondition;
break;
case RANGE:
Expand All @@ -2255,6 +2267,7 @@ private Condition mapCondition(Row row) throws Exception {
rCondition.setThresholdLow(row.getDouble("thresholdLow"));
rCondition.setThresholdHigh(row.getDouble("thresholdHigh"));
rCondition.setInRange(row.getBool("inRange"));
rCondition.setContext(row.getMap("context", String.class, String.class));
condition = rCondition;
break;
case STRING:
Expand All @@ -2268,6 +2281,7 @@ private Condition mapCondition(Row row) throws Exception {
sCondition.setOperator(StringCondition.Operator.valueOf(row.getString("operator")));
sCondition.setPattern(row.getString("pattern"));
sCondition.setIgnoreCase(row.getBool("ignoreCase"));
sCondition.setContext(row.getMap("context", String.class, String.class));
condition = sCondition;
break;
case THRESHOLD:
Expand All @@ -2280,6 +2294,7 @@ private Condition mapCondition(Row row) throws Exception {
tCondition.setDataId(row.getString("dataId"));
tCondition.setOperator(ThresholdCondition.Operator.valueOf(row.getString("operator")));
tCondition.setThreshold(row.getDouble("threshold"));
tCondition.setContext(row.getMap("context", String.class, String.class));
condition = tCondition;
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,29 +185,31 @@ public class CassStatement {
+ "(tenantId, alertId, status) VALUES (?, ?, ?) ";

INSERT_CONDITION_AVAILABILITY = "INSERT INTO " + keyspace + ".conditions "
+ "(tenantId, triggerId, triggerMode, type, conditionSetSize, conditionSetIndex, conditionId, "
+ "dataId, operator) VALUES (?, ?, ?, 'AVAILABILITY', ?, ?, ?, ?, ?) ";
+ "(tenantId, triggerId, triggerMode, type, context, conditionSetSize, conditionSetIndex, " +
"conditionId, dataId, operator) VALUES (?, ?, ?, 'AVAILABILITY', ?, ?, ?, ?, ?, ?) ";

INSERT_CONDITION_COMPARE = "INSERT INTO " + keyspace + ".conditions "
+ "(tenantId, triggerId, triggerMode, type, conditionSetSize, conditionSetIndex, conditionId, "
+ "dataId, operator, data2Id, data2Multiplier) VALUES (?, ?, ?, 'COMPARE', ?, ?, ?, ?, ?, ?, ?) ";
+ "(tenantId, triggerId, triggerMode, type, context, conditionSetSize, conditionSetIndex, " +
"conditionId, dataId, operator, data2Id, data2Multiplier) " +
"VALUES (?, ?, ?, 'COMPARE', ?, ?, ?, ?, ?, ?, ?, ?) ";

INSERT_CONDITION_EXTERNAL = "INSERT INTO " + keyspace + ".conditions "
+ "(tenantId, triggerId, triggerMode, type, conditionSetSize, conditionSetIndex, conditionId, "
+ "dataId, operator, pattern) VALUES (?, ?, ?, 'EXTERNAL', ?, ?, ?, ?, ?, ?) ";
+ "(tenantId, triggerId, triggerMode, type, context, conditionSetSize, conditionSetIndex, " +
"conditionId, dataId, operator, pattern) VALUES (?, ?, ?, 'EXTERNAL', ?, ?, ?, ?, ?, ?, ?) ";

INSERT_CONDITION_STRING = "INSERT INTO " + keyspace + ".conditions "
+ "(tenantId, triggerId, triggerMode, type, conditionSetSize, conditionSetIndex, conditionId, "
+ "dataId, operator, pattern, ignoreCase) VALUES (?, ?, ?, 'STRING', ?, ?, ?, ?, ?, ?, ?) ";
+ "(tenantId, triggerId, triggerMode, type, context, conditionSetSize, conditionSetIndex, " +
"conditionId, dataId, operator, pattern, ignoreCase) " +
"VALUES (?, ?, ?, 'STRING', ?, ?, ?, ?, ?, ?, ?, ?) ";

INSERT_CONDITION_THRESHOLD = "INSERT INTO " + keyspace + ".conditions "
+ "(tenantId, triggerId, triggerMode, type, conditionSetSize, conditionSetIndex, conditionId, "
+ "dataId, operator, threshold) VALUES (?, ?, ?, 'THRESHOLD', ?, ?, ?, ?, ?, ?) ";
+ "(tenantId, triggerId, triggerMode, type, context, conditionSetSize, conditionSetIndex, " +
"conditionId, dataId, operator, threshold) VALUES (?, ?, ?, 'THRESHOLD', ?, ?, ?, ?, ?, ?, ?) ";

INSERT_CONDITION_THRESHOLD_RANGE = "INSERT INTO " + keyspace + ".conditions "
+ "(tenantId, triggerId, triggerMode, type, conditionSetSize, conditionSetIndex, conditionId, "
+ "dataId, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange) "
+ "VALUES (?, ?, ?, 'RANGE', ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
+ "(tenantId, triggerId, triggerMode, type, context, conditionSetSize, conditionSetIndex, " +
"conditionId, dataId, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange) "
+ "VALUES (?, ?, ?, 'RANGE', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";

INSERT_DAMPENING = "INSERT INTO " + keyspace + ".dampenings "
+ "(triggerId, triggerMode, type, evalTrueSetting, evalTotalSetting, evalTimeSetting, "
Expand Down Expand Up @@ -273,18 +275,21 @@ public class CassStatement {

SELECT_CONDITION_ID = "SELECT triggerId, triggerMode, type, conditionSetSize, "
+ "conditionSetIndex, conditionId, dataId, operator, data2Id, data2Multiplier, pattern, "
+ "ignoreCase, threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId "
+ "ignoreCase, threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId, "
+ "context "
+ "FROM " + keyspace + ".conditions "
+ "WHERE tenantId = ? AND conditionId = ? ";

SELECT_CONDITIONS_ALL = "SELECT triggerId, triggerMode, type, conditionSetSize, "
+ "conditionSetIndex, conditionId, dataId, operator, data2Id, data2Multiplier, pattern, "
+ "ignoreCase, threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId "
+ "ignoreCase, threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId, "
+ "context "
+ "FROM " + keyspace + ".conditions ";

SELECT_CONDITIONS_BY_TENANT = "SELECT triggerId, triggerMode, type, conditionSetSize, "
+ "conditionSetIndex, conditionId, dataId, operator, data2Id, data2Multiplier, pattern, "
+ "ignoreCase, threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId "
+ "ignoreCase, threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId, "
+ "context "
+ "FROM " + keyspace + ".conditions "
+ "WHERE tenantId = ? ";

Expand Down Expand Up @@ -344,13 +349,15 @@ public class CassStatement {

SELECT_TRIGGER_CONDITIONS = "SELECT triggerId, triggerMode, type, conditionSetSize, "
+ "conditionSetIndex, conditionId, dataId, operator, data2Id, data2Multiplier, pattern, "
+ "ignoreCase, threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId "
+ "ignoreCase, threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId, "
+ "context "
+ "FROM " + keyspace + ".conditions "
+ "WHERE tenantId = ? AND triggerId = ?";

SELECT_TRIGGER_CONDITIONS_TRIGGER_MODE = "SELECT triggerId, triggerMode, type, conditionSetSize, "
+ "conditionSetIndex, conditionId, dataId, operator, data2Id, data2Multiplier, pattern, ignoreCase, "
+ "threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId "
+ "threshold, operatorLow, operatorHigh, thresholdLow, thresholdHigh, inRange, tenantId, "
+ "context "
+ "FROM " + keyspace + ".conditions "
+ "WHERE tenantId = ? AND triggerId = ? AND triggerMode = ? ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ CREATE TABLE ${keyspace}.conditions (
thresholdLow double,
thresholdHigh double,
inRange boolean,
context map<text,text>,
PRIMARY KEY (tenantId, triggerId, triggerMode, conditionId)
);

Expand Down

0 comments on commit 9bbc282

Please sign in to comment.