Skip to content

Commit

Permalink
Add search cappabilities for actions history
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasponce committed Oct 2, 2015
1 parent ee123f2 commit 037f7d1
Show file tree
Hide file tree
Showing 6 changed files with 747 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* 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.paging;

import java.util.Comparator;

import org.hawkular.alerts.api.model.action.Action;

/**
*
* @author Lucas Ponce
*/
public class ActionComparator implements Comparator<Action> {

public enum Field {
ACTION_PLUGIN("actionPlugin"),
ACTION_ID("actionId"),
ALERT_ID("alertId"),
CTIME("ctime"),
RESULT("result");

private String text;

Field(String text) {
this.text = text;
}

public String getText() {
return this.text;
}

public static Field getField(String text) {
if (text == null || text.isEmpty()) {
return ALERT_ID;
}
for (Field f : values()) {
if (f.getText().compareToIgnoreCase(text) == 0) {
return f;
}
}
return ALERT_ID;
}
};

private Field field;
private Order.Direction direction;

public ActionComparator() {
this(Field.ALERT_ID, Order.Direction.ASCENDING);
}

public ActionComparator(Field field, Order.Direction direction) {
this.field = field;
this.direction = direction;
}
@Override
public int compare(Action o1, Action o2) {
if (o1 == null && o2 == null) {
return 0;
}
if (o1 == null && o2 != null) {
return 1;
}
if (o1 != null && o2 == null) {
return -1;
}
int iOrder = direction == Order.Direction.ASCENDING ? 1 : -1;
switch (field) {
case ALERT_ID:
if (o1.getAlert() == null && o2.getAlert() == null) {
return 0;
}
if (o1.getAlert() == null && o2.getAlert() != null) {
return 1;
}
if (o1.getAlert() != null && o2.getAlert() == null) {
return -1;
}
if (o1.getAlert().getAlertId() == null && o2.getAlert().getAlertId() == null) {
return 0;
}
if (o1.getAlert().getAlertId() == null && o2.getAlert().getAlertId() != null) {
return 1;
}
if (o1.getAlert().getAlertId() != null && o2.getAlert().getAlertId() == null) {
return -1;
}
return o1.getAlert().getAlertId().compareTo(o2.getAlert().getAlertId()) * iOrder;
case ACTION_PLUGIN:
if (o1.getActionPlugin() == null && o2.getActionPlugin() == null) {
return 0;
}
if (o1.getActionPlugin() == null && o2.getActionPlugin() != null) {
return 1;
}
if (o1.getActionPlugin() != null && o2.getActionPlugin() == null) {
return -1;
}
return o1.getActionPlugin().compareTo(o2.getActionPlugin()) * iOrder;
case CTIME:
return (int)((o1.getCtime() - o2.getCtime()) * iOrder);
case ACTION_ID:
if (o1.getActionId() == null && o2.getActionId() == null) {
return 0;
}
if (o1.getActionId() == null && o2.getActionId() != null) {
return 1;
}
if (o1.getActionId() != null && o2.getActionId() == null) {
return -1;
}
return o1.getActionId().compareTo(o2.getActionId()) * iOrder;
case RESULT:
if (o1.getResult() == null && o2.getResult() == null) {
return 0;
}
if (o1.getResult() == null && o2.getResult() != null) {
return 1;
}
if (o1.getResult() != null && o2.getResult() == null) {
return -1;
}
return o1.getResult().compareTo(o2.getResult()) * iOrder;
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.services;

import java.util.Collection;

/**
* Query criteria for fetching Actions from history backend.
*
* @author Jay Shaughnessy
* @author Lucas Ponce
*/
public class ActionsCriteria {
Long startTime = null;
Long endTime = null;
String actionPlugin = null;
Collection<String> actionPlugins = null;
String actionId = null;
Collection<String> actionIds = null;
String alertId = null;
Collection<String> alertIds = null;
String result = null;
Collection<String> results = null;

public Long getStartTime() {
return startTime;
}

public void setStartTime(Long startTime) {
this.startTime = startTime;
}

public Long getEndTime() {
return endTime;
}

public void setEndTime(Long endTime) {
this.endTime = endTime;
}

public String getActionPlugin() {
return actionPlugin;
}

public void setActionPlugin(String actionPlugin) {
this.actionPlugin = actionPlugin;
}

public Collection<String> getActionPlugins() {
return actionPlugins;
}

public void setActionPlugins(Collection<String> actionPlugins) {
this.actionPlugins = actionPlugins;
}

public String getActionId() {
return actionId;
}

public void setActionId(String actionId) {
this.actionId = actionId;
}

public Collection<String> getActionIds() {
return actionIds;
}

public void setActionIds(Collection<String> actionIds) {
this.actionIds = actionIds;
}

public String getAlertId() {
return alertId;
}

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

public Collection<String> getAlertIds() {
return alertIds;
}

public void setAlertIds(Collection<String> alertIds) {
this.alertIds = alertIds;
}

public String getResult() {
return result;
}

public void setResult(String result) {
this.result = result;
}

public Collection<String> getResults() {
return results;
}

public void setResults(Collection<String> results) {
this.results = results;
}

public boolean hasCriteria() {
return null != startTime
|| null != endTime
|| null != actionPlugin
|| null != actionId
|| null != alertId
|| null != result
|| (null != actionPlugins && !actionPlugins.isEmpty())
|| (null != actionIds && !actionIds.isEmpty())
|| (null != alertIds && !alertIds.isEmpty())
|| (null != results && !results.isEmpty());
}

@Override
public String toString() {
return "ActionsCriteria{" +
"startTime=" + startTime +
", endTime=" + endTime +
", actionPlugin='" + actionPlugin + '\'' +
", actionPlugins=" + actionPlugins +
", actionId='" + actionId + '\'' +
", actionIds=" + actionIds +
", alertId='" + alertId + '\'' +
", alertIds=" + alertIds +
", result='" + result + '\'' +
", results=" + results +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.hawkular.alerts.api.services;

import org.hawkular.alerts.api.model.action.Action;
import org.hawkular.alerts.api.model.paging.Page;
import org.hawkular.alerts.api.model.paging.Pager;

/**
* A interface used to send actions.
Expand Down Expand Up @@ -48,4 +50,14 @@ public interface ActionsService {
* @param listener the listener
*/
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;
}

0 comments on commit 037f7d1

Please sign in to comment.