Search in sources :

Example 46 with CriteriaBuilder

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());
    }
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) QueryParameters(org.opennms.web.utils.QueryParameters)

Example 47 with CriteriaBuilder

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());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsNode(org.opennms.netmgt.model.OnmsNode) Criteria(org.opennms.core.criteria.Criteria) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 48 with CriteriaBuilder

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());
}
Also used : Order(org.opennms.core.criteria.Order) CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Alias(org.opennms.core.criteria.Alias) Criteria(org.opennms.core.criteria.Criteria) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 49 with CriteriaBuilder

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;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) ArrayList(java.util.ArrayList) OnmsApplication(org.opennms.netmgt.model.OnmsApplication) OnmsMonitoredService(org.opennms.netmgt.model.OnmsMonitoredService) ApplicationStatus(org.opennms.netmgt.dao.api.ApplicationStatus) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 50 with CriteriaBuilder

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());
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)155 Test (org.junit.Test)60 Criteria (org.opennms.core.criteria.Criteria)31 OnmsNode (org.opennms.netmgt.model.OnmsNode)25 Transactional (org.springframework.transaction.annotation.Transactional)23 Date (java.util.Date)20 GET (javax.ws.rs.GET)13 ArrayList (java.util.ArrayList)12 Produces (javax.ws.rs.Produces)12 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)11 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)11 InetSocketAddress (java.net.InetSocketAddress)9 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)7 HibernateDaoFactory (org.opennms.smoketest.utils.HibernateDaoFactory)7 OnmsMinion (org.opennms.netmgt.model.minion.OnmsMinion)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)4 JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)4 EventDao (org.opennms.netmgt.dao.api.EventDao)4 OnmsSeverity (org.opennms.netmgt.model.OnmsSeverity)4 OnmsSnmpInterface (org.opennms.netmgt.model.OnmsSnmpInterface)4