Skip to content

Commit

Permalink
Add delete support for actions history
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Oct 3, 2015
1 parent a75ff95 commit 0a37e0e
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,21 @@ public interface ActionsService {
void addListener(ActionListener listener);

/**
*
* @param tenantId Tenant where actions are stored
* @param criteria If null returns all actions (not recommended)
* @param pager Paging requirement for fetching actions. Optional. Return all if null.
* @return NotNull, can be empty.
* @throws Exception
*/
Page<Action> getActions(String tenantId, ActionsCriteria criteria, Pager pager) throws Exception;

/**
* Delete the requested Actions from the history, as described by the provided criteria.
*
* @param tenantId
* @param criteria
* @return
* @throws Exception
*/
int deleteActions(String tenantId, ActionsCriteria criteria) throws Exception;
}
4 changes: 2 additions & 2 deletions hawkular-alerts-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
</systemProperties>
<excludes>
<exclude>**/*/PerfRulesEngineTest.java</exclude>
<exclude>**/*/CassDefinitionsTest.java</exclude>
<exclude>**/*/CassPersistenceTest.java</exclude>
</excludes>
</configuration>
</plugin>
Expand Down Expand Up @@ -234,7 +234,7 @@
</property>
</systemProperties>
<includes>
<include>**/*/CassDefinitionsTest.java</include>
<include>**/*/CassPersistenceTest.java</include>
</includes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,50 @@ private Page<Action> preparePage(List<Action> actions, Pager pager) {
}
}

@Override
public int deleteActions(String tenantId, ActionsCriteria criteria) throws Exception {
if (isEmpty(tenantId)) {
throw new IllegalArgumentException("TenantId must be not null");
}
if (null == criteria) {
throw new IllegalArgumentException("Criteria must be not null");
}

List<Action> actionsToDelete = getActions(tenantId, criteria, null);
if (actionsToDelete == null || actionsToDelete.isEmpty()) {
return 0;
}

PreparedStatement deleteActionHistory = CassStatement.get(session,
CassStatement.DELETE_ACTION_HISTORY);
PreparedStatement deleteActionHistoryAction = CassStatement.get(session,
CassStatement.DELETE_ACTION_HISTORY_ACTION);
PreparedStatement deleteActionHistoryAlert = CassStatement.get(session,
CassStatement.DELETE_ACTION_HISTORY_ALERT);
PreparedStatement deleteActionHistoryCtime = CassStatement.get(session,
CassStatement.DELETE_ACTION_HISTORY_CTIME);
PreparedStatement deleteActionHistoryResult = CassStatement.get(session,
CassStatement.DELETE_ACTION_HISTORY_RESULT);

for (Action action : actionsToDelete) {
List<ResultSetFuture> futures = new ArrayList<>();
futures.add(session.executeAsync(deleteActionHistory.bind(action.getTenantId(), action.getActionPlugin(),
action.getActionId(), action.getAlert().getAlertId(), action.getCtime())));
futures.add(session.executeAsync(deleteActionHistoryAction.bind(action.getTenantId(), action.getActionId(),
action.getActionPlugin(), action.getAlert().getAlertId(), action.getCtime())));
futures.add(session.executeAsync(deleteActionHistoryAlert.bind(action.getTenantId(),
action.getAlert().getAlertId(), action.getActionPlugin(), action.getActionId(),
action.getCtime())));
futures.add(session.executeAsync(deleteActionHistoryCtime.bind(action.getTenantId(), action.getCtime(),
action.getActionPlugin(), action.getActionId(), action.getAlert().getAlertId())));
futures.add(session.executeAsync(deleteActionHistoryResult.bind(action.getTenantId(),
action.getResult(), action.getActionPlugin(), action.getActionId(), action.getAlert().getAlertId(),
action.getCtime())));
Futures.allAsList(futures).get();
}

return actionsToDelete.size();
}

private boolean isEmpty(String s) {
return null == s || s.trim().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class CassStatement {
private static final Map<String, PreparedStatement> statementMap = new HashMap<>();

public static final String DELETE_ACTION;
public static final String DELETE_ACTION_HISTORY;
public static final String DELETE_ACTION_HISTORY_ACTION;
public static final String DELETE_ACTION_HISTORY_ALERT;
public static final String DELETE_ACTION_HISTORY_CTIME;
Expand Down Expand Up @@ -136,6 +137,9 @@ public class CassStatement {
DELETE_ACTION = "DELETE FROM " + keyspace + ".actions "
+ "WHERE tenantId = ? AND actionPlugin = ? AND actionId = ? ";

DELETE_ACTION_HISTORY = "DELETE FROM " + keyspace + ".actions_history " +
"WHERE tenantId = ? AND actionPlugin = ? AND actionId = ? AND alertId = ? AND ctime = ?";

DELETE_ACTION_HISTORY_ACTION = "DELETE FROM " + keyspace + ".actions_history_actions " +
"WHERE tenantId = ? AND actionId = ? AND actionPlugin = ? AND alertId = ? AND ctime = ?";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.hawkular.alerts.engine;

import org.hawkular.alerts.api.services.ActionsCriteria;
import org.hawkular.alerts.api.services.AlertsCriteria;
import org.hawkular.alerts.engine.cassandra.EmbeddedCassandra;
import org.hawkular.alerts.engine.impl.AlertProperties;
Expand All @@ -34,7 +35,7 @@
* @author Lucas Ponce
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class CassDefinitionsTest extends DefinitionsTest {
public class CassPersistenceTest extends PersistenceTest {

private static final String JBOSS_DATA_DIR = "jboss.server.data.dir";
private static final String EXTERNAL_CASSANDRA = "external_cassandra";
Expand All @@ -46,7 +47,7 @@ public class CassDefinitionsTest extends DefinitionsTest {
@BeforeClass
public static void initSessionAndResetTestSchema() throws Exception {

String testFolder = CassDefinitionsTest.class.getResource("/").getPath();
String testFolder = CassPersistenceTest.class.getResource("/").getPath();
System.setProperty(JBOSS_DATA_DIR, testFolder);

externalCassandra = (null != System.getProperty(EXTERNAL_CASSANDRA));
Expand Down Expand Up @@ -96,4 +97,10 @@ public void cleanAlerts() throws Exception {
System.out.printf("Deleted [%s] Alerts before test.\n", alertsService.deleteAlerts(TEST_TENANT, criteria));
}

@Before
public void cleanActions() throws Exception {
ActionsCriteria criteria = new ActionsCriteria();
System.out.printf("Deleted [%s] Actions before test.\n", actionsService.deleteActions(TEST_TENANT, criteria));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
*
* @author Lucas Ponce
*/
public abstract class DefinitionsTest {
public abstract class PersistenceTest {

/*
TenantId = 28026b36-8fe4-4332-84c8-524e173a68bf
Expand Down Expand Up @@ -1078,21 +1078,21 @@ public void test0050PagingAlerts() throws Exception {
@Test
public void test0060BasicActionsHistory() throws Exception {
for (int i = 0; i < 107; i++) {
Alert testAlert = new Alert("my-organization", "test-trigger", Severity.CRITICAL, null);
Alert testAlert = new Alert(TEST_TENANT, "test-trigger", Severity.CRITICAL, null);
Action action = new Action(testAlert.getTenantId(), "testplugin", "send-to-this-groups", testAlert);
Thread.sleep(2);
actionsService.send(action);
}

List<Action> actions = actionsService.getActions("my-organization", null, null);
List<Action> actions = actionsService.getActions(TEST_TENANT, null, null);
assertEquals(107, actions.size());
}

@Test
public void test0070SearchActionsHistory() throws Exception {
for (int i = 0; i < 10; i++) {
Alert testAlert = new Alert();
testAlert.setTenantId("my-organization");
testAlert.setTenantId(TEST_TENANT);
testAlert.setTriggerId("test-trigger");
testAlert.setSeverity(Severity.CRITICAL);
testAlert.setCtime(i);
Expand All @@ -1115,76 +1115,76 @@ public void test0070SearchActionsHistory() throws Exception {
actionsService.send(action4);
}

List<Action> actions = actionsService.getActions("my-organization", null, null);
List<Action> actions = actionsService.getActions(TEST_TENANT, null, null);
assertEquals(10 * 4, actions.size());

ActionsCriteria criteria = new ActionsCriteria();
criteria.setStartTime(2L);

actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(8 * 4, actions.size());

criteria.setStartTime(2L);
criteria.setEndTime(3L);

actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(2 * 4, actions.size());

criteria = new ActionsCriteria();
criteria.setActionPlugin("plugin1");

actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(10 * 2, actions.size());

criteria = new ActionsCriteria();
criteria.setActionPlugins(Arrays.asList("plugin1", "plugin2"));

actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(10 * 4, actions.size());

criteria = new ActionsCriteria();
criteria.setActionId("action1");
actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(10 * 2, actions.size());

criteria = new ActionsCriteria();
criteria.setActionIds(Arrays.asList("action1", "action2"));
actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(10 * 4, actions.size());

criteria = new ActionsCriteria();
criteria.setAlertId("test-alert1");
actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(1 * 4, actions.size());

criteria = new ActionsCriteria();
criteria.setAlertIds(Arrays.asList("test-alert1", "test-alert2", "test-alert3"));
actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(3 * 4, actions.size());

criteria = new ActionsCriteria();
criteria.setResult("result1");
actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(10 * 1, actions.size());

criteria = new ActionsCriteria();
criteria.setResults(Arrays.asList("result1", "result2"));
actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(10 * 2, actions.size());

criteria = new ActionsCriteria();
criteria.setStartTime(2L);
criteria.setActionPlugin("plugin1");
criteria.setActionId("action1");
actions = actionsService.getActions("my-organization", criteria, null);
actions = actionsService.getActions(TEST_TENANT, criteria, null);
assertEquals(8 * 1, actions.size());
}

@Test
public void test0080PaginationActionsHistory() throws Exception {
for (int i = 0; i < 103; i++) {
Alert testAlert = new Alert();
testAlert.setTenantId("my-organization");
testAlert.setTenantId(TEST_TENANT);
testAlert.setTriggerId("test-trigger");
testAlert.setSeverity(Severity.CRITICAL);
testAlert.setCtime(i);
Expand All @@ -1207,14 +1207,14 @@ public void test0080PaginationActionsHistory() throws Exception {
actionsService.send(action4);
}

List<Action> actions = actionsService.getActions("my-organization", null, null);
List<Action> actions = actionsService.getActions(TEST_TENANT, null, null);
assertEquals(103 * 4, actions.size());

Pager pager = Pager.builder().withPageSize(10).withStartPage(0)
.orderByAscending(ActionComparator.Field.ALERT_ID.getText()).build();

System.out.println("1st Pager: " + pager + " pager.getEnd(): " + pager.getEnd());
Page<Action> page = actionsService.getActions("my-organization", null, pager);
Page<Action> page = actionsService.getActions(TEST_TENANT, null, pager);
System.out.println("1st Page size: " + page.size() + " totalSize: " + page.getTotalSize());

Action firstAction = page.get(0);
Expand All @@ -1225,7 +1225,7 @@ public void test0080PaginationActionsHistory() throws Exception {
while (pager.getEnd() < page.getTotalSize()) {
pager = pager.nextPage();
System.out.println("Pager: " + pager + " pager.getEnd(): " + pager.getEnd());
page = actionsService.getActions("my-organization", null, pager);
page = actionsService.getActions(TEST_TENANT, null, pager);
System.out.println("Page size: " + page.size() + " totalSize: " + page.getTotalSize());
}

Expand All @@ -1239,7 +1239,7 @@ public void test0080PaginationActionsHistory() throws Exception {
.orderByDescending(ActionComparator.Field.RESULT.getText()).build();

System.out.println("1st Pager: " + pager + " pager.getEnd(): " + pager.getEnd());
page = actionsService.getActions("my-organization", null, pager);
page = actionsService.getActions(TEST_TENANT, null, pager);
System.out.println("1st Page size: " + page.size() + " totalSize: " + page.getTotalSize());

firstAction = page.get(0);
Expand All @@ -1250,7 +1250,7 @@ public void test0080PaginationActionsHistory() throws Exception {
while (pager.getEnd() < page.getTotalSize()) {
pager = pager.nextPage();
System.out.println("Pager: " + pager + " pager.getEnd(): " + pager.getEnd());
page = actionsService.getActions("my-organization", null, pager);
page = actionsService.getActions(TEST_TENANT, null, pager);
System.out.println("Page size: " + page.size() + " totalSize: " + page.getTotalSize());
}

Expand All @@ -1260,5 +1260,4 @@ public void test0080PaginationActionsHistory() throws Exception {

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

}

0 comments on commit 0a37e0e

Please sign in to comment.