Search in sources :

Example 16 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class AlarmStatsRestService method getStats.

protected AlarmStatistics getStats(final UriInfo uriInfo, final OnmsSeverity severity) {
    final AlarmStatistics stats = new AlarmStatistics();
    final CriteriaBuilder builder = getCriteriaBuilder(uriInfo.getQueryParameters(), false);
    // note: this is just the *total count* criteria, so no ordering, and count everything
    builder.count();
    if (severity != null) {
        builder.eq("severity", severity);
    }
    final Criteria criteria = builder.toCriteria();
    LOG.debug("criteria = {}", criteria);
    final int count = m_statisticsService.getTotalCount(criteria);
    stats.setTotalCount(count);
    stats.setAcknowledgedCount(m_statisticsService.getAcknowledgedCount(criteria));
    stats.setNewestAcknowledged(getNewestAcknowledged(severity));
    stats.setNewestUnacknowledged(getNewestUnacknowledged(severity));
    stats.setOldestAcknowledged(getOldestAcknowledged(severity));
    stats.setOldestUnacknowledged(getOldestUnacknowledged(severity));
    return stats;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) Criteria(org.opennms.core.criteria.Criteria)

Example 17 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class AlarmStatsRestService method getCriteriaBuilder.

protected static CriteriaBuilder getCriteriaBuilder(final OnmsSeverity severity) {
    final CriteriaBuilder builder = new CriteriaBuilder(OnmsAlarm.class);
    if (severity != null) {
        builder.eq("severity", severity);
    }
    builder.fetch("firstEvent", FetchType.EAGER);
    builder.fetch("lastEvent", FetchType.EAGER);
    builder.alias("node", "node", JoinType.LEFT_JOIN);
    builder.alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
    builder.alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
    return builder;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder)

Example 18 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class AlarmRestService method getAlarms.

/**
     * <p>
     * getAlarms
     * </p>
     * 
     * @return a {@link org.opennms.netmgt.model.OnmsAlarmCollection} object.
     */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Transactional
public OnmsAlarmCollection getAlarms(@Context final SecurityContext securityContext, @Context final UriInfo uriInfo) {
    assertUserReadCredentials(securityContext);
    final CriteriaBuilder builder = getCriteriaBuilder(uriInfo.getQueryParameters(), false);
    builder.distinct();
    final OnmsAlarmCollection coll = new OnmsAlarmCollection(m_alarmDao.findMatching(builder.toCriteria()));
    // For getting totalCount
    coll.setTotalCount(m_alarmDao.countMatching(builder.count().toCriteria()));
    return coll;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) OnmsAlarmCollection(org.opennms.netmgt.model.OnmsAlarmCollection) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with CriteriaBuilder

use of org.opennms.core.criteria.CriteriaBuilder in project opennms by OpenNMS.

the class AlarmRestServiceBase method getCriteriaBuilder.

protected CriteriaBuilder getCriteriaBuilder(final MultivaluedMap<String, String> params, final boolean stripOrdering) {
    translateParameters(params);
    final CriteriaBuilder cb = new CriteriaBuilder(OnmsAlarm.class);
    cb.fetch("firstEvent", FetchType.EAGER);
    cb.fetch("lastEvent", FetchType.EAGER);
    cb.alias("node", "node", JoinType.LEFT_JOIN);
    cb.alias("node.snmpInterfaces", "snmpInterface", JoinType.LEFT_JOIN);
    cb.alias("node.ipInterfaces", "ipInterface", JoinType.LEFT_JOIN);
    cb.alias("node.location", "location", JoinType.LEFT_JOIN);
    if (params.containsKey("alarmId")) {
        if (params.containsKey("id")) {
            throw getException(Status.BAD_REQUEST, "Form parameters contain both the 'alarmId' and 'id' properties!  Pick a side!");
        }
        params.put("id", params.remove("alarmId"));
    }
    applyQueryFilters(params, cb);
    if (stripOrdering) {
        cb.clearOrder();
        cb.limit(DEFAULT_LIMIT);
        cb.offset(0);
    } else {
        cb.orderBy("lastEventTime").desc();
    }
    cb.distinct();
    return cb;
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder)

Example 20 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)

Aggregations

CriteriaBuilder (org.opennms.core.criteria.CriteriaBuilder)128 Test (org.junit.Test)55 Criteria (org.opennms.core.criteria.Criteria)28 OnmsNode (org.opennms.netmgt.model.OnmsNode)21 Transactional (org.springframework.transaction.annotation.Transactional)20 Date (java.util.Date)18 GET (javax.ws.rs.GET)13 Produces (javax.ws.rs.Produces)12 InetSocketAddress (java.net.InetSocketAddress)8 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)8 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)8 ArrayList (java.util.ArrayList)7 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)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 OnmsSnmpInterface (org.opennms.netmgt.model.OnmsSnmpInterface)4 ScanReport (org.opennms.netmgt.model.ScanReport)4 ScanReportRestService (org.opennms.web.rest.v2.ScanReportRestService)4 PUT (javax.ws.rs.PUT)3