Skip to content

Commit

Permalink
HWKALERTS-92 Add thin filter into Action History API
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Oct 6, 2015
1 parent 4b0914c commit 7f73572
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;

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;
Expand Down Expand Up @@ -355,12 +356,17 @@ public AlertThinDeserializer() {
ignorables.add(field.getName());
}
}
for (Field field : Action.class.getDeclaredFields()) {
if (field.isAnnotationPresent(Alert.Thin.class)) {
ignorables.add(field.getName());
}
}
}

@Override
public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc,
BeanDeserializerBuilder builder) {
if (!beanDesc.getBeanClass().equals(Alert.class)) {
if (!beanDesc.getBeanClass().equals(Alert.class) && !beanDesc.getBeanClass().equals(Action.class)) {
return builder;
}
for (String ignore : ignorables) {
Expand All @@ -372,7 +378,7 @@ public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanD
@Override
public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config,
BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) {
if (!beanDesc.getBeanClass().equals(Alert.class)) {
if (!beanDesc.getBeanClass().equals(Alert.class) && !beanDesc.getBeanClass().equals(Action.class)) {
return propDefs;
}
List<BeanPropertyDefinition> newPropDefs = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Map;

import org.hawkular.alerts.api.model.condition.Alert;
import org.hawkular.alerts.api.model.condition.Alert.Thin;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
Expand Down Expand Up @@ -47,12 +48,16 @@ public class Action {
@JsonInclude
private String actionId;

@JsonInclude(Include.NON_NULL)
private Alert alert;
@JsonInclude
private String alertId;

@JsonInclude
private long ctime;

@JsonInclude(Include.NON_NULL)
@Thin
private Alert alert;

@JsonInclude(Include.NON_EMPTY)
private Map<String, String> properties;

Expand All @@ -66,6 +71,9 @@ public Action(String tenantId, String actionPlugin, String actionId, Alert alert
this.actionPlugin = actionPlugin;
this.actionId = actionId;
this.alert = alert;
if (alert != null) {
this.alertId = alert.getAlertId();
}
this.ctime = System.currentTimeMillis();
}

Expand Down Expand Up @@ -93,6 +101,14 @@ public void setActionPlugin(String actionPlugin) {
this.actionPlugin = actionPlugin;
}

public String getAlertId() {
return alertId;
}

public void setAlertId(String alertId) {
this.alertId = alertId;
}

public Alert getAlert() {
return alert;
}
Expand Down Expand Up @@ -137,17 +153,22 @@ public boolean equals(Object o) {
if (actionPlugin != null ? !actionPlugin.equals(action.actionPlugin) : action.actionPlugin != null)
return false;
if (actionId != null ? !actionId.equals(action.actionId) : action.actionId != null) return false;
return !(alert != null ? !alert.equals(action.alert) : action.alert != null);
if (alertId != null ? !alertId.equals(action.alertId) : action.alertId != null) return false;
if (properties != null ? !properties.equals(action.properties) : action.properties != null) return false;
return !(result != null ? !result.equals(action.result) : action.result != null);

}

@Override
public int hashCode() {
int result = tenantId != null ? tenantId.hashCode() : 0;
result = 31 * result + (actionPlugin != null ? actionPlugin.hashCode() : 0);
result = 31 * result + (actionId != null ? actionId.hashCode() : 0);
result = 31 * result + (alert != null ? alert.hashCode() : 0);
result = 31 * result + (int) (ctime ^ (ctime >>> 32));
return result;
int result1 = tenantId != null ? tenantId.hashCode() : 0;
result1 = 31 * result1 + (actionPlugin != null ? actionPlugin.hashCode() : 0);
result1 = 31 * result1 + (actionId != null ? actionId.hashCode() : 0);
result1 = 31 * result1 + (alertId != null ? alertId.hashCode() : 0);
result1 = 31 * result1 + (int) (ctime ^ (ctime >>> 32));
result1 = 31 * result1 + (properties != null ? properties.hashCode() : 0);
result1 = 31 * result1 + (result != null ? result.hashCode() : 0);
return result1;
}

@Override
Expand All @@ -156,8 +177,9 @@ public String toString() {
"tenantId='" + tenantId + '\'' +
", actionPlugin='" + actionPlugin + '\'' +
", actionId='" + actionId + '\'' +
", alert=" + alert +
", alertId='" + alertId + '\'' +
", ctime=" + ctime +
", alert=" + alert +
", properties=" + properties +
", result='" + result + '\'' +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ActionsCriteria {
Collection<String> alertIds = null;
String result = null;
Collection<String> results = null;
boolean thin = false;

public Long getStartTime() {
return startTime;
Expand Down Expand Up @@ -116,6 +117,14 @@ public void setResults(Collection<String> results) {
this.results = results;
}

public boolean isThin() {
return thin;
}

public void setThin(boolean thin) {
this.thin = thin;
}

public boolean hasCriteria() {
return null != startTime
|| null != endTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ public void before() {
@Test
public void jsonActionTest() throws Exception {
String str = "{\"tenantId\":\"tenantTest\",\"actionPlugin\":\"plugin\"," +
"\"actionId\":\"test\",\"ctime\":123}";
"\"actionId\":\"test\",\"alertId\":\"testAlert\",\"ctime\":123}";
Action action = objectMapper.readValue(str, Action.class);

assertEquals("tenantTest", action.getTenantId());
assertEquals("plugin", action.getActionPlugin());
assertEquals("test", action.getActionId());
assertEquals("testAlert", action.getAlertId());
assertEquals(123, action.getCtime());

String output = objectMapper.writeValueAsString(action);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public Page<Action> getActions(String tenantId, ActionsCriteria criteria, Pager
throw new IllegalArgumentException("TenantId must be not null");
}
session = CassCluster.getSession();
boolean thin = (null != criteria && criteria.isThin());
boolean filter = (null != criteria && criteria.hasCriteria());

List<Action> actions = new ArrayList<>();
Expand Down Expand Up @@ -313,7 +314,7 @@ public Page<Action> getActions(String tenantId, ActionsCriteria criteria, Pager
Iterator<Row> itActionHistoryByTenant = rsActionHistoryByTenant.iterator();
while (itActionHistoryByTenant.hasNext()) {
Row row = itActionHistoryByTenant.next();
Action actionHistory = JsonUtil.fromJson(row.getString("payload"), Action.class);
Action actionHistory = JsonUtil.fromJson(row.getString("payload"), Action.class, thin);
actions.add(actionHistory);
}
} else {
Expand All @@ -325,7 +326,7 @@ public Page<Action> getActions(String tenantId, ActionsCriteria criteria, Pager
List<ResultSet> rsActionHistory = Futures.allAsList(futures).get();
rsActionHistory.stream().forEach(r -> {
for (Row row : r) {
Action actionHistory = JsonUtil.fromJson(row.getString("payload"), Action.class);
Action actionHistory = JsonUtil.fromJson(row.getString("payload"), Action.class, thin);
actions.add(actionHistory);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1260,4 +1260,44 @@ public void test0080PaginationActionsHistory() throws Exception {

assertTrue(firstAction.getResult().compareTo(lastAction.getResult()) > 0);
}

@Test
public void test0090ThinActionsHistory() throws Exception {
for (int i = 0; i < 103; i++) {
Alert testAlert = new Alert();
testAlert.setTenantId(TEST_TENANT);
testAlert.setTriggerId("test-trigger");
testAlert.setSeverity(Severity.CRITICAL);
testAlert.setCtime(i);
testAlert.setAlertId("test-alert" + i);
Action action1 = new Action(testAlert.getTenantId(), "plugin1", "action1", testAlert);
Action action2 = new Action(testAlert.getTenantId(), "plugin1", "action2", testAlert);
Action action3 = new Action(testAlert.getTenantId(), "plugin2", "action1", testAlert);
Action action4 = new Action(testAlert.getTenantId(), "plugin2", "action2", testAlert);
action1.setCtime(i);
action2.setCtime(i);
action3.setCtime(i);
action4.setCtime(i);
action1.setResult("result1");
action2.setResult("result2");
action3.setResult("result3");
action4.setResult("result4");
actionsService.send(action1);
actionsService.send(action2);
actionsService.send(action3);
actionsService.send(action4);
}

ActionsCriteria criteria = new ActionsCriteria();
criteria.setThin(true);
List<Action> actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(103 * 4, actions.size());

for (Action action : actions) {
System.out.println(action);
assertNull(action.getAlert());
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,15 @@ public Response findActionsHistory(
"comma separated list of action results")
@QueryParam("results")
final String results,
@ApiParam(required = false, value = "return only thin actions, do not include full alert, only alertId")
@QueryParam("thin")
final Boolean thin,
@Context
final UriInfo uri) {
Pager pager = RequestUtil.extractPaging(uri);
try {
ActionsCriteria criteria = buildCriteria(startTime, endTime, actionPlugins, actionIds, alertIds, results);
ActionsCriteria criteria = buildCriteria(startTime, endTime, actionPlugins, actionIds, alertIds, results,
thin);
Page<Action> actionPage = actions.getActions(tenantId, criteria, pager);
log.debugf("Actions: %s ", actionPage);
if (isEmpty(actionPage)) {
Expand Down Expand Up @@ -329,7 +333,8 @@ public Response deleteActionsHistory(
@QueryParam("results")
final String results) {
try {
ActionsCriteria criteria = buildCriteria(startTime, endTime, actionPlugins, actionIds, alertIds, results);
ActionsCriteria criteria = buildCriteria(startTime, endTime, actionPlugins, actionIds, alertIds, results,
false);
int numDeleted = actions.deleteActions(tenantId, criteria);
log.debugf("Actions deleted: %s ", numDeleted);
return ResponseUtil.ok(numDeleted);
Expand All @@ -340,7 +345,7 @@ public Response deleteActionsHistory(
}

private ActionsCriteria buildCriteria(Long startTime, Long endTime, String actionPlugins, String actionIds,
String alertIds, String results) {
String alertIds, String results, boolean thin) {
ActionsCriteria criteria = new ActionsCriteria();
criteria.setStartTime(startTime);
criteria.setEndTime(endTime);
Expand All @@ -356,6 +361,7 @@ private ActionsCriteria buildCriteria(Long startTime, Long endTime, String actio
if (!isEmpty(results)) {
criteria.setResults(Arrays.asList(results.split(",")));
}
criteria.setThin(thin);
return criteria;
}

Expand Down

0 comments on commit 7f73572

Please sign in to comment.