use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class AbstractStatusService method count.
public int count(Q query) {
final QueryParameters queryParameters = query.getParameters();
final SeverityFilter filter = query.getSeverityFilter();
final CriteriaBuilder builder = getCriteriaBuilder(queryParameters);
// Remove limit, offset and ordering to fetch count
builder.limit(null);
builder.offset(null);
builder.clearOrder();
// If a severity is given, we must count manually!
if (filter != null && filter.getSeverities() != null && !filter.getSeverities().isEmpty()) {
List<StatusEntity<T>> collect = findMatching(query, builder);
collect = apply(collect, filter);
return collect.size();
} else {
return countMatching(builder.toCriteria());
}
}
use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class NodeDaoIT method testCriteriaBuilderOrderBy.
@Test
@Transactional
public void testCriteriaBuilderOrderBy() {
CriteriaBuilder cb = new CriteriaBuilder(OnmsNode.class);
cb.alias("ipInterfaces", "ipInterface").distinct();
// TODO: Make this work but we need to put the fields into
// an aggregator function since node->ipInterfaces is a 1->M
// relationship.
//
// cb.orderBy("ipInterfaces.ipAddress").distinct();
Criteria criteria = cb.toCriteria();
System.out.println("Criteria: " + criteria.toString());
List<OnmsNode> nodes = m_nodeDao.findMatching(criteria);
nodes.stream().forEach(System.out::println);
assertEquals(6, nodes.size());
}
use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class NotificationDaoIT method testCriteria.
/**
* Test to make sure that orderBy across an alias works.
*/
@Test
@Transactional
public void testCriteria() {
Criteria criteria = new Criteria(OnmsNotification.class);
criteria.setAliases(Arrays.asList(new Alias[] { new Alias("node", "node", JoinType.LEFT_JOIN), new Alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN), new Alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN), new Alias("event", "event", JoinType.LEFT_JOIN), new Alias("usersNotified", "usersNotified", JoinType.LEFT_JOIN), new Alias("serviceType", "serviceType", JoinType.LEFT_JOIN) }));
criteria.setOrders(Arrays.asList(new Order[] { new Order("event.id", false), new Order("event.eventSeverity", false), new Order("node.label", false), new Order("serviceType.name", false) }));
m_notificationDao.findMatching(criteria);
CriteriaBuilder builder = new CriteriaBuilder(OnmsNotification.class);
builder.alias("node", "node", JoinType.LEFT_JOIN);
builder.alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
builder.alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
builder.alias("event", "event", JoinType.LEFT_JOIN);
builder.alias("usersNotified", "usersNotified", JoinType.LEFT_JOIN);
builder.alias("serviceType", "serviceType", JoinType.LEFT_JOIN);
builder.orderBy("event.id", false);
builder.orderBy("event.eventSeverity", false);
builder.orderBy("node.label", false);
builder.orderBy("serviceType.name", false);
m_notificationDao.findMatching(builder.toCriteria());
}
use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class ApplicationDaoHibernate method getApplicationStatus.
@Override
public List<ApplicationStatus> getApplicationStatus(List<OnmsApplication> applications) {
// Applications do not have a alarm mapping, so we grab all nodeDown, interfaceDown and serviceLost alarms
// for all monitored services of each application to calculate the maximum severity (-> status)
final List<ApplicationStatus> statusList = new ArrayList<>();
for (OnmsApplication application : applications) {
final Set<String> reductionKeys = new HashSet<>();
for (OnmsMonitoredService eachService : application.getMonitoredServices()) {
reductionKeys.addAll(ReductionKeyHelper.getReductionKeys(eachService));
}
if (!reductionKeys.isEmpty()) {
final CriteriaBuilder builder = new CriteriaBuilder(OnmsAlarm.class);
builder.in("reductionKey", reductionKeys);
// findMatching would exepct OnmsApplications, but we need OnmsAlarms, so hack it
HibernateCallback<List<OnmsAlarm>> callback = buildHibernateCallback(builder.toCriteria());
List<OnmsAlarm> alarms = getHibernateTemplate().execute(callback);
// All alarms for the current application have been determined, now get the max severity
final Optional<OnmsAlarm> maxSeverity = alarms.stream().reduce((leftAlarm, rightAlarm) -> {
if (leftAlarm.getSeverity().isGreaterThan(rightAlarm.getSeverity())) {
return leftAlarm;
}
return rightAlarm;
});
if (maxSeverity.isPresent()) {
statusList.add(new ApplicationStatus(application, maxSeverity.get().getSeverity()));
} else {
// ensure that each application has a status
statusList.add(new ApplicationStatus(application, OnmsSeverity.NORMAL));
}
} else {
// ensure that each application has a status
statusList.add(new ApplicationStatus(application, OnmsSeverity.NORMAL));
}
}
return statusList;
}
use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.
the class AlarmDetailsDashlet method getAlarms.
/**
* Returns the alarms defined by this dashlet.
*
* @return the list of alarms
*/
private List<OnmsAlarm> getAlarms() {
final CriteriaBuilder alarmCb = new CriteriaBuilder(OnmsAlarm.class);
alarmCb.alias("node", "node");
alarmCb.alias("node.categories", "category");
alarmCb.alias("lastEvent", "event");
String criteria = getDashletSpec().getParameters().get("criteria");
m_criteriaBuilderHelper.parseConfiguration(alarmCb, criteria);
alarmCb.fetch("firstEvent", Fetch.FetchType.EAGER);
alarmCb.fetch("lastEvent", Fetch.FetchType.EAGER);
/**
* due to restrictions in the criteria api it's quite hard
* to use distinct and orderBy together, so I apply a workaround
* to avoid alarmCb.distinct();
*/
List<OnmsAlarm> onmsAlarmList = m_alarmDao.findMatching(alarmCb.toCriteria());
Map<Integer, OnmsAlarm> onmsAlarmMap = new LinkedHashMap<>();
for (OnmsAlarm onmsAlarm : onmsAlarmList) {
if (!onmsAlarmMap.containsKey(onmsAlarm.getId())) {
onmsAlarmMap.put(onmsAlarm.getId(), onmsAlarm);
}
}
return Lists.newArrayList(onmsAlarmMap.values());
}
Aggregations