Skip to content

Commit

Permalink
- Add support for Alert persistence
Browse files Browse the repository at this point in the history
  - Add support for criteria-based Alert query
  - Remove memory-based Alert caching
  - change Alert.time to Alert.ctime for clarity and possibility of more associated times
  - let CompareCondition properly use the inherited Condition.dataId
- Add preliminary tag support

TODO: Rest service support
  • Loading branch information
jshaughn committed Mar 13, 2015
1 parent 60361f3 commit 38e6ae6
Show file tree
Hide file tree
Showing 19 changed files with 730 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonInclude;

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

/**
* A status of an alert thrown by several matched conditions.
Expand All @@ -38,12 +37,16 @@ public class Alert {
private List<Set<ConditionEval>> evalSets;

@JsonInclude
private long time;
private long ctime;

public Alert() {
// for json assembly
}

public Alert(String triggerId, List<Set<ConditionEval>> evalSets) {
this.triggerId = triggerId;
this.evalSets = evalSets;
this.time = System.currentTimeMillis();
this.ctime = System.currentTimeMillis();
}

public List<Set<ConditionEval>> getEvalSets() {
Expand All @@ -54,12 +57,12 @@ public void setEvalSets(List<Set<ConditionEval>> evalSets) {
this.evalSets = evalSets;
}

public long getTime() {
return time;
public long getCTime() {
return ctime;
}

public void setTime(long time) {
this.time = time;
public void setCTime(long ctime) {
this.ctime = ctime;
}

public String getTriggerId() {
Expand All @@ -77,7 +80,8 @@ public boolean equals(Object o) {

Alert alert = (Alert) o;

if (time != alert.time) return false;
if (ctime != alert.ctime)
return false;
if (evalSets != null ? !evalSets.equals(alert.evalSets) : alert.evalSets != null) return false;
if (triggerId != null ? !triggerId.equals(alert.triggerId) : alert.triggerId != null) return false;

Expand All @@ -88,15 +92,15 @@ public boolean equals(Object o) {
public int hashCode() {
int result = triggerId != null ? triggerId.hashCode() : 0;
result = 31 * result + (evalSets != null ? evalSets.hashCode() : 0);
result = 31 * result + (int) (time ^ (time >>> 32));
result = 31 * result + (int) (ctime ^ (ctime >>> 32));
return result;
}

@Override
public String toString() {
return "Alert [triggerId=" + triggerId + ", " +
"evals=" + evalSets + ", " +
"time=" + time + "]";
"ctime=" + ctime + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import org.hawkular.alerts.api.log.MsgLogger;
import org.hawkular.alerts.api.model.trigger.Trigger.Mode;

Expand All @@ -38,7 +39,7 @@ public enum Operator {
}

@JsonInclude(Include.NON_NULL)
private String data1Id;
private String dataId;

@JsonInclude(Include.NON_NULL)
private Operator operator;
Expand All @@ -57,42 +58,35 @@ public CompareCondition() {
}

public CompareCondition(String triggerId,
String data1Id, Operator operator, Double data2Multiplier, String data2Id) {
this(triggerId, FIRE, 1, 1, data1Id, operator, data2Multiplier, data2Id);
String dataId, Operator operator, Double data2Multiplier, String data2Id) {
this(triggerId, FIRE, 1, 1, dataId, operator, data2Multiplier, data2Id);
}

public CompareCondition(String triggerId, Mode triggerMode,
String data1Id, Operator operator, Double data2Multiplier, String data2Id) {
this(triggerId, triggerMode, 1, 1, data1Id, operator, data2Multiplier, data2Id);
String dataId, Operator operator, Double data2Multiplier, String data2Id) {
this(triggerId, triggerMode, 1, 1, dataId, operator, data2Multiplier, data2Id);
}

public CompareCondition(String triggerId, int conditionSetSize, int conditionSetIndex,
String data1Id, Operator operator, Double data2Multiplier, String data2Id) {
this(triggerId, FIRE, conditionSetSize, conditionSetIndex, data1Id, operator, data2Multiplier, data2Id);
String dataId, Operator operator, Double data2Multiplier, String data2Id) {
this(triggerId, FIRE, conditionSetSize, conditionSetIndex, dataId, operator, data2Multiplier, data2Id);
}

public CompareCondition(String triggerId, Mode triggerMode, int conditionSetSize, int conditionSetIndex,
String data1Id, Operator operator, Double data2Multiplier, String data2Id) {
String dataId, Operator operator, Double data2Multiplier, String data2Id) {
super(triggerId, triggerMode, conditionSetSize, conditionSetIndex, Type.COMPARE);
this.data1Id = data1Id;
this.dataId = dataId;
this.operator = operator;
this.data2Id = data2Id;
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;
return dataId;
}

public void setData1Id(String data1Id) {
this.data1Id = data1Id;
public void setDataId(String dataId) {
this.dataId = dataId;
}

public String getData2Id() {
Expand All @@ -119,23 +113,23 @@ public void setOperator(Operator operator) {
this.operator = operator;
}

public String getLog(double data1Value, double data2Value) {
public String getLog(double dataValue, double data2Value) {
Double val = data2Multiplier * data2Value;
return triggerId + " : " + data1Value + " " + operator.name() + " " +
return triggerId + " : " + dataValue + " " + operator.name() + " " +
val + " (" + data2Multiplier + "*" + data2Value + ")";
}

public boolean match(double data1Value, double data2Value) {
public boolean match(double dataValue, double data2Value) {
double threshold = (data2Multiplier * data2Value);
switch (operator) {
case LT:
return data1Value < threshold;
return dataValue < threshold;
case GT:
return data1Value > threshold;
return dataValue > threshold;
case LTE:
return data1Value <= threshold;
return dataValue <= threshold;
case GTE:
return data1Value >= threshold;
return dataValue >= threshold;
default:
msgLog.warnUnknowOperatorOnCondition(operator.name(), this.getClass().getName());
return false;
Expand All @@ -153,7 +147,7 @@ public boolean equals(Object o) {

CompareCondition that = (CompareCondition) o;

if (data1Id != null ? !data1Id.equals(that.data1Id) : that.data1Id != null)
if (dataId != null ? !dataId.equals(that.dataId) : that.dataId != null)
return false;
if (data2Id != null ? !data2Id.equals(that.data2Id) : that.data2Id != null)
return false;
Expand All @@ -168,7 +162,7 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (data1Id != null ? data1Id.hashCode() : 0);
result = 31 * result + (dataId != null ? dataId.hashCode() : 0);
result = 31 * result + (operator != null ? operator.hashCode() : 0);
result = 31 * result + (data2Id != null ? data2Id.hashCode() : 0);
result = 31 * result + (data2Multiplier != null ? data2Multiplier.hashCode() : 0);
Expand All @@ -179,7 +173,7 @@ public int hashCode() {
public String toString() {
return "CompareCondition [triggerId='" + triggerId + "', " +
"triggerMode=" + triggerMode + ", " +
"data1Id=" + (data1Id == null ? null : '\'' + data1Id + '\'') + ", " +
"dataId=" + (dataId == null ? null : '\'' + dataId + '\'') + ", " +
"operator=" + (operator == null ? null : '\'' + operator.toString() + '\'') + ", " +
"data2Id=" + (data2Id == null ? null : '\'' + data2Id + '\'') + ", " +
"data2Multiplier=" + data2Multiplier + "]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public enum Type {
@JsonIgnore
protected String conditionId;

public Condition() {
// for json assembly
}

public Condition(String triggerId, Mode triggerMode, int conditionSetSize, int conditionSetIndex, Type type) {
this.triggerId = triggerId;
this.triggerMode = triggerMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public abstract class ConditionEval {
@JsonIgnore
protected boolean used;

public ConditionEval() {
// for json assembly
}

public ConditionEval(boolean match, long dataTimestamp) {
this.match = match;
this.dataTimestamp = dataTimestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package org.hawkular.alerts.api.model.condition;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.hawkular.alerts.api.model.data.NumericData;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import static com.fasterxml.jackson.annotation.JsonInclude.*;
import org.hawkular.alerts.api.model.data.NumericData;

/**
* An evaluation state for threshold condition.
Expand All @@ -36,9 +36,7 @@ public class ThresholdConditionEval extends ConditionEval {
private Double value;

public ThresholdConditionEval() {
super(false, 0);
this.condition = null;
this.value = null;
super();
}

public ThresholdConditionEval(ThresholdCondition condition, NumericData data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.trigger;

/**
* An immutable class. Each instance is a tag
*
* @author jay shaughnessy
* @author lucas ponce
*/
public class Tag {

private String triggerId;
private String category;
private String name;
private boolean visible;

/**
* Create an invisible Tag.
*
* @param triggerId @Nullable Note, required for storage but not search.
* @param category @Nullable
* @param tag @NotEmpty
*/
public Tag(String triggerId, String category, String tag) {
this(triggerId, category, tag, false);
}

/**
* @param triggerId @Nullable Note, required for storage but not search.
* @param category @Nullable
* @param name @NotEmpty
* @param visible flag indicating whether this tag is available for display
*/
public Tag(String triggerId, String category, String name, boolean visible) {
super();
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Tag can not be null or empty.");
}

this.triggerId = triggerId;
this.category = category;
this.name = name;
this.visible = visible;
}

public String getTriggerId() {
return triggerId;
}

public String getCategory() {
return category;
}

public String getName() {
return name;
}

public boolean isVisible() {
return visible;
}

@Override
public String toString() {
return "Tag [triggerId=" + triggerId + ", category=" + category + ", name=" + name + ", visible=" + visible
+ "]";
}

}

0 comments on commit 38e6ae6

Please sign in to comment.