Enable filter by severity in ORM
Filtering by severity was not possible with ORM in Alarm and AlarmDefinition repositories Change-Id: I0e1636b8bc63470a559209f063bd8f7ad1a1e552
This commit is contained in:
parent
de084928d6
commit
4df2cca40f
|
@ -235,10 +235,6 @@ public class AlarmDefinitionSqlRepoImpl
|
||||||
throw Exceptions.unprocessableEntity(
|
throw Exceptions.unprocessableEntity(
|
||||||
"Sort_by is not implemented for the hibernate database type");
|
"Sort_by is not implemented for the hibernate database type");
|
||||||
}
|
}
|
||||||
if (severities != null && !severities.isEmpty()) {
|
|
||||||
throw Exceptions.unprocessableEntity(
|
|
||||||
"Severity is not implemented for the hibernate database type");
|
|
||||||
}
|
|
||||||
|
|
||||||
Session session = null;
|
Session session = null;
|
||||||
List<AlarmDefinition> resultSet = Lists.newArrayList();
|
List<AlarmDefinition> resultSet = Lists.newArrayList();
|
||||||
|
@ -260,6 +256,21 @@ public class AlarmDefinitionSqlRepoImpl
|
||||||
sbWhere.append(" and ad.name = :name");
|
sbWhere.append(" and ad.name = :name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (CollectionUtils.isNotEmpty(severities)) {
|
||||||
|
if (severities.size() == 1) {
|
||||||
|
sbWhere.append(" and ad.severity = :severity");
|
||||||
|
} else {
|
||||||
|
sbWhere.append(" and (");
|
||||||
|
for (int i = 0; i < severities.size(); i++) {
|
||||||
|
sbWhere.append("ad.severity = :severity_").append(i);
|
||||||
|
if (i < severities.size() - 1) {
|
||||||
|
sbWhere.append(" or ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbWhere.append(")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (offset != null && !offset.equals("0")) {
|
if (offset != null && !offset.equals("0")) {
|
||||||
sbWhere.append(" and ad.id > :offset");
|
sbWhere.append(" and ad.id > :offset");
|
||||||
}
|
}
|
||||||
|
@ -283,6 +294,16 @@ public class AlarmDefinitionSqlRepoImpl
|
||||||
qAlarmDefinition.setString("name", name);
|
qAlarmDefinition.setString("name", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (CollectionUtils.isNotEmpty(severities)) {
|
||||||
|
if (severities.size() == 1) {
|
||||||
|
qAlarmDefinition.setString("severity", severities.get(0).name());
|
||||||
|
} else {
|
||||||
|
for (int it = 0; it < severities.size(); it++) {
|
||||||
|
qAlarmDefinition.setString(String.format("severity_%d", it), severities.get(it).name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (offset != null && !offset.equals("0")) {
|
if (offset != null && !offset.equals("0")) {
|
||||||
qAlarmDefinition.setString("offset", offset);
|
qAlarmDefinition.setString("offset", offset);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ import com.google.common.base.Preconditions;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.hibernate.Query;
|
import org.hibernate.Query;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
@ -174,10 +175,6 @@ public class AlarmSqlRepoImpl
|
||||||
throw Exceptions.unprocessableEntity(
|
throw Exceptions.unprocessableEntity(
|
||||||
"Sort_by is not implemented for the hibernate database type");
|
"Sort_by is not implemented for the hibernate database type");
|
||||||
}
|
}
|
||||||
if (severities != null && !severities.isEmpty()) {
|
|
||||||
throw Exceptions.unprocessableEntity(
|
|
||||||
"Severity filter is not implemented for the hibernate database type");
|
|
||||||
}
|
|
||||||
|
|
||||||
Preconditions.checkNotNull(tenantId, "TenantId is required");
|
Preconditions.checkNotNull(tenantId, "TenantId is required");
|
||||||
|
|
||||||
|
@ -192,6 +189,7 @@ public class AlarmSqlRepoImpl
|
||||||
metricName,
|
metricName,
|
||||||
metricDimensions,
|
metricDimensions,
|
||||||
state,
|
state,
|
||||||
|
severities,
|
||||||
lifecycleState,
|
lifecycleState,
|
||||||
link,
|
link,
|
||||||
stateUpdatedStart,
|
stateUpdatedStart,
|
||||||
|
@ -223,6 +221,16 @@ public class AlarmSqlRepoImpl
|
||||||
query.setString("state", state.name());
|
query.setString("state", state.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (CollectionUtils.isNotEmpty(severities)) {
|
||||||
|
if (severities.size() == 1) {
|
||||||
|
query.setString("severity", severities.get(0).name());
|
||||||
|
} else {
|
||||||
|
for (int it = 0; it < severities.size(); it++) {
|
||||||
|
query.setString(String.format("severity_%d", it), severities.get(it).name());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (link != null) {
|
if (link != null) {
|
||||||
query.setString("link", link);
|
query.setString("link", link);
|
||||||
}
|
}
|
||||||
|
@ -271,6 +279,7 @@ public class AlarmSqlRepoImpl
|
||||||
final String metricName,
|
final String metricName,
|
||||||
final Map<String, String> metricDimensions,
|
final Map<String, String> metricDimensions,
|
||||||
final AlarmState state,
|
final AlarmState state,
|
||||||
|
final List<AlarmSeverity> severities,
|
||||||
final String lifecycleState,
|
final String lifecycleState,
|
||||||
final String link,
|
final String link,
|
||||||
final DateTime stateUpdatedStart,
|
final DateTime stateUpdatedStart,
|
||||||
|
@ -320,6 +329,21 @@ public class AlarmSqlRepoImpl
|
||||||
sbWhere.append(" and a.state = :state");
|
sbWhere.append(" and a.state = :state");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (CollectionUtils.isNotEmpty(severities)) {
|
||||||
|
if (severities.size() == 1) {
|
||||||
|
sbWhere.append(" and ad.severity = :severity");
|
||||||
|
} else {
|
||||||
|
sbWhere.append(" and (");
|
||||||
|
for (int i = 0; i < severities.size(); i++) {
|
||||||
|
sbWhere.append("ad.severity = :severity_").append(i);
|
||||||
|
if (i < severities.size() - 1) {
|
||||||
|
sbWhere.append(" or ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sbWhere.append(")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (lifecycleState != null) {
|
if (lifecycleState != null) {
|
||||||
sbWhere.append(" and a.lifecycle_state = :lifecycleState");
|
sbWhere.append(" and a.lifecycle_state = :lifecycleState");
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import monasca.api.domain.exception.EntityNotFoundException;
|
import monasca.api.domain.exception.EntityNotFoundException;
|
||||||
import monasca.api.domain.model.alarmdefinition.AlarmDefinition;
|
import monasca.api.domain.model.alarmdefinition.AlarmDefinition;
|
||||||
import monasca.api.domain.model.alarmdefinition.AlarmDefinitionRepo;
|
import monasca.api.domain.model.alarmdefinition.AlarmDefinitionRepo;
|
||||||
|
@ -92,7 +93,7 @@ public class AlarmDefinitionSqlRepositoryImplTest {
|
||||||
final AlarmDefinitionDb alarmDefinition123 = new AlarmDefinitionDb()
|
final AlarmDefinitionDb alarmDefinition123 = new AlarmDefinitionDb()
|
||||||
.setTenantId("bob")
|
.setTenantId("bob")
|
||||||
.setName("90% CPU")
|
.setName("90% CPU")
|
||||||
.setSeverity(AlarmSeverity.LOW)
|
.setSeverity(AlarmSeverity.HIGH)
|
||||||
.setExpression("avg(hpcs.compute{flavor_id=777, image_id=888, metric_name=cpu, device=1}) > 10")
|
.setExpression("avg(hpcs.compute{flavor_id=777, image_id=888, metric_name=cpu, device=1}) > 10")
|
||||||
.setMatchBy("flavor_id,image_id")
|
.setMatchBy("flavor_id,image_id")
|
||||||
.setActionsEnabled(true);
|
.setActionsEnabled(true);
|
||||||
|
@ -188,7 +189,7 @@ public class AlarmDefinitionSqlRepositoryImplTest {
|
||||||
session.close();
|
session.close();
|
||||||
|
|
||||||
alarmDef_123 =
|
alarmDef_123 =
|
||||||
new AlarmDefinition("123", "90% CPU", null, "LOW", "avg(hpcs.compute{flavor_id=777, image_id=888, metric_name=cpu, device=1}) > 10",
|
new AlarmDefinition("123", "90% CPU", null, "HIGH", "avg(hpcs.compute{flavor_id=777, image_id=888, metric_name=cpu, device=1}) > 10",
|
||||||
Arrays.asList("flavor_id", "image_id"), true, Arrays.asList("29387234", "77778687"), Collections.<String>emptyList(),
|
Arrays.asList("flavor_id", "image_id"), true, Arrays.asList("29387234", "77778687"), Collections.<String>emptyList(),
|
||||||
Collections.<String>emptyList());
|
Collections.<String>emptyList());
|
||||||
alarmDef_234 =
|
alarmDef_234 =
|
||||||
|
@ -377,4 +378,22 @@ public class AlarmDefinitionSqlRepositoryImplTest {
|
||||||
public void shouldFindThrowException() {
|
public void shouldFindThrowException() {
|
||||||
repo.find("bob", null, null, null, Arrays.asList("severity", "state"), null, 1);
|
repo.find("bob", null, null, null, Arrays.asList("severity", "state"), null, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void shouldFilterBySeverity() {
|
||||||
|
List<AlarmDefinition> alarmDefinitions = repo.find("bob", null, null, Lists.newArrayList(AlarmSeverity.HIGH), null, null, 1);
|
||||||
|
AlarmDefinition alarmDefinition;
|
||||||
|
|
||||||
|
assertEquals(1, alarmDefinitions.size());
|
||||||
|
alarmDefinition = alarmDefinitions.get(0);
|
||||||
|
|
||||||
|
assertEquals(this.alarmDef_123, alarmDefinition);
|
||||||
|
|
||||||
|
alarmDefinitions = repo.find("bob", null, null, Lists.newArrayList(AlarmSeverity.LOW), null, null, 1);
|
||||||
|
|
||||||
|
assertEquals(1, alarmDefinitions.size());
|
||||||
|
alarmDefinition = alarmDefinitions.get(0);
|
||||||
|
|
||||||
|
assertEquals(this.alarmDef_234, alarmDefinition);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,7 +120,7 @@ public class AlarmSqlRepositoryImplTest {
|
||||||
TENANT_ID,
|
TENANT_ID,
|
||||||
"50% CPU",
|
"50% CPU",
|
||||||
"avg(cpu.sys_mem{service=monitoring}) > 20 and avg(cpu.idle_perc{service=monitoring}) < 10",
|
"avg(cpu.sys_mem{service=monitoring}) > 20 and avg(cpu.idle_perc{service=monitoring}) < 10",
|
||||||
AlarmSeverity.LOW,
|
AlarmSeverity.HIGH,
|
||||||
"hostname,region",
|
"hostname,region",
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
@ -236,7 +236,7 @@ public class AlarmSqlRepositoryImplTest {
|
||||||
session.getTransaction().commit();
|
session.getTransaction().commit();
|
||||||
|
|
||||||
compoundAlarm =
|
compoundAlarm =
|
||||||
new Alarm("234111", "234", "50% CPU", "LOW", buildAlarmMetrics(
|
new Alarm("234111", "234", "50% CPU", "HIGH", buildAlarmMetrics(
|
||||||
buildMetricDefinition("cpu.sys_mem", "hostname", "roland", "region", "colorado", "service", "monitoring"),
|
buildMetricDefinition("cpu.sys_mem", "hostname", "roland", "region", "colorado", "service", "monitoring"),
|
||||||
buildMetricDefinition("cpu.idle_perc", "extra", "vivi", "hostname", "roland", "region", "colorado", "service", "monitoring")),
|
buildMetricDefinition("cpu.idle_perc", "extra", "vivi", "hostname", "roland", "region", "colorado", "service", "monitoring")),
|
||||||
AlarmState.UNDETERMINED, null, null, timestamp4, timestamp4, timestamp4);
|
AlarmState.UNDETERMINED, null, null, timestamp4, timestamp4, timestamp4);
|
||||||
|
@ -465,6 +465,23 @@ public class AlarmSqlRepositoryImplTest {
|
||||||
repo.update(TENANT_ID, "Not a valid alarm id", AlarmState.UNDETERMINED, null, null);
|
repo.update(TENANT_ID, "Not a valid alarm id", AlarmState.UNDETERMINED, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void shouldFilterBySeverity() {
|
||||||
|
|
||||||
|
checkList(repo.find(TENANT_ID, null, null, null, null, Lists.newArrayList(AlarmSeverity.LOW), null, null, null, null, null, 1, false),
|
||||||
|
alarm1, alarm2, alarm3);
|
||||||
|
|
||||||
|
checkList(repo.find(TENANT_ID, null, null, null, null, Lists.newArrayList(AlarmSeverity.HIGH), null, null, null, null, null, 1, false),
|
||||||
|
compoundAlarm);
|
||||||
|
|
||||||
|
checkList(repo.find(TENANT_ID, null, null, null, null, Lists.newArrayList(AlarmSeverity.LOW, AlarmSeverity.HIGH), null, null, null, null, null, 1, false),
|
||||||
|
alarm1, alarm2, compoundAlarm, alarm3);
|
||||||
|
|
||||||
|
// no alarms for those severities
|
||||||
|
checkList(repo.find(TENANT_ID, null, null, null, null, Lists.newArrayList(AlarmSeverity.CRITICAL), null, null, null, null, null, 1, false));
|
||||||
|
checkList(repo.find(TENANT_ID, null, null, null, null, Lists.newArrayList(AlarmSeverity.MEDIUM), null, null, null, null, null, 1, false));
|
||||||
|
checkList(repo.find(TENANT_ID, null, null, null, null, Lists.newArrayList(AlarmSeverity.CRITICAL, AlarmSeverity.MEDIUM), null, null, null, null, null, 1, false));
|
||||||
|
}
|
||||||
|
|
||||||
private void checkList(List<Alarm> found, Alarm... expected) {
|
private void checkList(List<Alarm> found, Alarm... expected) {
|
||||||
assertEquals(found.size(), expected.length);
|
assertEquals(found.size(), expected.length);
|
||||||
Alarm actual;
|
Alarm actual;
|
||||||
|
|
Loading…
Reference in New Issue