AlertsDAO implements Cleanable { @RequiresSession public List<AlertHistoryEntity> findAll() { TypedQuery<AlertHistoryEntity> query = m_entityManagerProvider.get().createNamedQuery( "AlertHistoryEntity.findAll", AlertHistoryEntity.class); return m_daoUtils.selectList(query); } @Inject  AlertsDAO(Configuration configuration); @RequiresSession AlertHistoryEntity findById(long alertId); @RequiresSession List<AlertHistoryEntity> findAll(); @RequiresSession List<AlertHistoryEntity> findAll(long clusterId); @RequiresSession List<AlertHistoryEntity> findAll(long clusterId,
      List<AlertState> alertStates); @RequiresSession List<AlertHistoryEntity> findAll(long clusterId, Date startDate,
      Date endDate); @RequiresSession List<AlertHistoryEntity> findAll(AlertHistoryRequest request); @Transactional List<AlertCurrentEntity> findAll(AlertCurrentRequest request); int getCount(Predicate predicate); @RequiresSession List<AlertCurrentEntity> findCurrent(); @RequiresSession AlertCurrentEntity findCurrentById(long alertId); @RequiresSession List<AlertCurrentEntity> findCurrentByDefinitionId(long definitionId); @RequiresSession List<AlertCurrentEntity> findCurrentByCluster(long clusterId); @RequiresSession AlertSummaryDTO findCurrentCounts(long clusterId, String serviceName, String hostName); @RequiresSession Map<String, AlertSummaryDTO> findCurrentPerHostCounts(long clusterId); @RequiresSession AlertHostSummaryDTO findCurrentHostCounts(long clusterId); @RequiresSession List<AlertCurrentEntity> findCurrentByService(long clusterId,
      String serviceName); AlertCurrentEntity findCurrentByHostAndName(long clusterId, String hostName,
      String alertName); @Transactional void removeByDefinitionId(long definitionId); @Transactional int removeCurrentByHistoryId(long historyId); @Transactional int removeCurrentDisabledAlerts(); @Transactional int removeCurrentByService(long clusterId, String serviceName); @Transactional int removeCurrentByHost(String hostName); @Transactional int removeCurrentByServiceComponentHost(long clusterId, String serviceName,
      String componentName, String hostName); @Transactional void create(AlertHistoryEntity alert); @Transactional void refresh(AlertHistoryEntity alert); @Transactional AlertHistoryEntity merge(AlertHistoryEntity alert); @Transactional void remove(AlertHistoryEntity alert); @Transactional void create(AlertCurrentEntity alert); @Transactional void refresh(AlertCurrentEntity alert); @Transactional AlertCurrentEntity merge(AlertCurrentEntity alert); AlertCurrentEntity merge(AlertCurrentEntity alert, boolean updateCacheOnly); @Transactional void remove(AlertCurrentEntity alert); @RequiresSession AlertSummaryDTO findAggregateCounts(long clusterId, String alertName); AlertCurrentEntity findCurrentByNameNoHost(long clusterId, String alertName); @Transactional void flushCachedEntitiesToJPA(); @Transactional @Override long cleanup(TimeBasedCleanupPolicy policy);  }
@Test public void testFindAll() { List<AlertHistoryEntity> alerts = m_dao.findAll(m_cluster.getClusterId()); assertNotNull(alerts); assertEquals(50, alerts.size()); }
@Test public void testAlertCurrentPredicate() throws Exception { AlertDefinitionEntity definition = m_definitionDao.findByName( m_cluster.getClusterId(), "Alert Definition 0"); assertNotNull(definition); Predicate definitionIdPredicate = null; Predicate hdfsPredicate = null; Predicate yarnPredicate = null; definitionIdPredicate = new PredicateBuilder().property( AlertResourceProvider.ALERT_DEFINITION_ID).equals( definition.getDefinitionId()).toPredicate(); AlertCurrentRequest request = new AlertCurrentRequest(); request.Predicate = definitionIdPredicate; List<AlertCurrentEntity> currentAlerts = m_dao.findAll(request); assertEquals(1, currentAlerts.size()); hdfsPredicate = new PredicateBuilder().property( AlertResourceProvider.ALERT_SERVICE).equals("HDFS").toPredicate(); yarnPredicate = new PredicateBuilder().property( AlertResourceProvider.ALERT_SERVICE).equals("YARN").toPredicate(); request.Predicate = yarnPredicate; currentAlerts = m_dao.findAll(request); assertEquals(5, currentAlerts.size()); request.Predicate = hdfsPredicate; currentAlerts = m_dao.findAll(request); assertEquals(0, currentAlerts.size()); }
@Test public void testAlertCurrentSorting() throws Exception { AlertCurrentRequest request = new AlertCurrentRequest(); Predicate clusterPredicate = new PredicateBuilder().property( AlertResourceProvider.ALERT_CLUSTER_NAME).equals(m_cluster.getClusterName()).toPredicate(); request.Predicate = clusterPredicate; SortRequestProperty sortRequestProperty = new SortRequestProperty(AlertResourceProvider.ALERT_ID, Order.ASC); request.Sort = new SortRequestImpl(Collections.singletonList(sortRequestProperty)); List<AlertCurrentEntity> currentAlerts = m_dao.findAll(request); assertTrue(currentAlerts.size() >= 5); long lastId = Long.MIN_VALUE; for (AlertCurrentEntity alert : currentAlerts) { assertTrue(lastId < alert.getAlertId()); lastId = alert.getAlertId(); } sortRequestProperty = new SortRequestProperty(AlertResourceProvider.ALERT_ID, Order.DESC); request.Sort = new SortRequestImpl(Collections.singletonList(sortRequestProperty)); currentAlerts = m_dao.findAll(request); assertTrue(currentAlerts.size() >= 5); lastId = Long.MAX_VALUE; for (AlertCurrentEntity alert : currentAlerts) { assertTrue(lastId > alert.getAlertId()); lastId = alert.getAlertId(); } }
@Test public void testFindByState() { List<AlertState> allStates = new ArrayList<AlertState>(); allStates.add(AlertState.OK); allStates.add(AlertState.WARNING); allStates.add(AlertState.CRITICAL); List<AlertHistoryEntity> history = m_dao.findAll(m_cluster.getClusterId(), allStates); assertNotNull(history); assertEquals(50, history.size()); history = m_dao.findAll(m_cluster.getClusterId(), Collections.singletonList(AlertState.OK)); assertNotNull(history); assertEquals(40, history.size()); history = m_dao.findAll(m_cluster.getClusterId(), Collections.singletonList(AlertState.CRITICAL)); assertNotNull(history); assertEquals(10, history.size()); history = m_dao.findAll(m_cluster.getClusterId(), Collections.singletonList(AlertState.WARNING)); assertNotNull(history); assertEquals(0, history.size()); }
@Test public void testFindByDate() { calendar.clear(); calendar.set(2014, Calendar.JANUARY, 1); List<AlertHistoryEntity> history = m_dao.findAll(m_cluster.getClusterId(), calendar.getTime(), null); assertNotNull(history); assertEquals(50, history.size()); history = m_dao.findAll(m_cluster.getClusterId(), null, calendar.getTime()); assertNotNull(history); assertEquals(1, history.size()); calendar.set(2014, Calendar.JANUARY, 5); Date startDate = calendar.getTime(); calendar.set(2014, Calendar.JANUARY, 10); Date endDate = calendar.getTime(); history = m_dao.findAll(m_cluster.getClusterId(), startDate, endDate); assertNotNull(history); assertEquals(6, history.size()); calendar.set(2014, Calendar.MARCH, 5); history = m_dao.findAll(m_cluster.getClusterId(), calendar.getTime(), null); assertNotNull(history); assertEquals(0, history.size()); history = m_dao.findAll(m_cluster.getClusterId(), endDate, startDate); assertNotNull(history); assertEquals(0, history.size()); }
@Test public void testAlertHistoryPredicate() throws Exception { m_helper.installHdfsService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_alertHelper.populateData(m_cluster); Predicate clusterPredicate = null; Predicate hdfsPredicate = null; Predicate yarnPredicate = null; Predicate clusterAndHdfsPredicate = null; Predicate clusterAndHdfsAndCriticalPredicate = null; Predicate hdfsAndCriticalOrWarningPredicate = null; Predicate alertNamePredicate = null; Predicate historyIdPredicate = null; clusterPredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_CLUSTER_NAME).equals("c1").toPredicate(); AlertHistoryRequest request = new AlertHistoryRequest(); request.Predicate = clusterPredicate; List<AlertHistoryEntity> histories = m_dao.findAll(request); assertEquals(3, histories.size()); hdfsPredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME).equals("HDFS").toPredicate(); yarnPredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME).equals("YARN").toPredicate(); clusterAndHdfsPredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_CLUSTER_NAME).equals("c1").and().property( AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME).equals("HDFS").toPredicate(); clusterAndHdfsPredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_CLUSTER_NAME).equals("c1").and().property( AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME).equals("HDFS").toPredicate(); clusterAndHdfsAndCriticalPredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_CLUSTER_NAME).equals("c1").and().property( AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME).equals("HDFS").and().property( AlertHistoryResourceProvider.ALERT_HISTORY_STATE).equals( AlertState.CRITICAL.name()).toPredicate(); hdfsAndCriticalOrWarningPredicate = new PredicateBuilder().begin().property( AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME).equals("HDFS").and().property( AlertHistoryResourceProvider.ALERT_HISTORY_STATE).equals( AlertState.CRITICAL.name()).end().or().property( AlertHistoryResourceProvider.ALERT_HISTORY_STATE).equals( AlertState.WARNING.name()).toPredicate(); alertNamePredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_DEFINITION_NAME).equals( "NAMENODE").toPredicate(); request.Predicate = hdfsPredicate; histories = m_dao.findAll(request); assertEquals(2, histories.size()); request.Predicate = yarnPredicate; histories = m_dao.findAll(request); assertEquals(1, histories.size()); request.Predicate = clusterAndHdfsPredicate; histories = m_dao.findAll(request); assertEquals(2, histories.size()); request.Predicate = clusterAndHdfsAndCriticalPredicate; histories = m_dao.findAll(request); assertEquals(0, histories.size()); request.Predicate = hdfsAndCriticalOrWarningPredicate; histories = m_dao.findAll(request); assertEquals(1, histories.size()); request.Predicate = alertNamePredicate; histories = m_dao.findAll(request); assertEquals(1, histories.size()); historyIdPredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_ID).equals( histories.get(0).getAlertId()).toPredicate(); request.Predicate = historyIdPredicate; histories = m_dao.findAll(request); assertEquals(1, histories.size()); }
@Test public void testAlertHistoryPagination() throws Exception { m_helper.installHdfsService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_alertHelper.populateData(m_cluster); AlertHistoryRequest request = new AlertHistoryRequest(); request.Pagination = null; List<AlertHistoryEntity> histories = m_dao.findAll(request); assertEquals(3, histories.size()); request.Pagination = new PageRequestImpl(StartingPoint.Beginning, 2, 0, null, null); histories = m_dao.findAll(request); assertEquals(2, histories.size()); request.Pagination = new PageRequestImpl(StartingPoint.Beginning, 1, 2, null, null); histories = m_dao.findAll(request); assertEquals(1, histories.size()); request.Pagination = new PageRequestImpl(StartingPoint.Beginning, 1, 3, null, null); histories = m_dao.findAll(request); assertEquals(0, histories.size()); }
@Test public void testAlertHistorySorting() throws Exception { m_helper.installHdfsService(m_cluster, m_serviceFactory, m_componentFactory, m_schFactory, HOSTNAME); m_alertHelper.populateData(m_cluster); List<SortRequestProperty> sortProperties = new ArrayList<SortRequestProperty>(); SortRequest sortRequest = new SortRequestImpl(sortProperties); AlertHistoryRequest request = new AlertHistoryRequest(); request.Sort = sortRequest; Predicate clusterPredicate = new PredicateBuilder().property( AlertHistoryResourceProvider.ALERT_HISTORY_CLUSTER_NAME).equals("c1").toPredicate(); request.Predicate = clusterPredicate; sortProperties.add(new SortRequestProperty( AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME, Order.ASC)); List<AlertHistoryEntity> histories = m_dao.findAll(request); assertEquals(3, histories.size()); String lastServiceName = null; for (AlertHistoryEntity history : histories) { if (null == lastServiceName) { lastServiceName = history.getServiceName(); continue; } String currentServiceName = history.getServiceName(); assertTrue(lastServiceName.compareTo(currentServiceName) <= 0); lastServiceName = currentServiceName; } sortProperties.clear(); sortProperties.add(new SortRequestProperty( AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME, Order.DESC)); histories = m_dao.findAll(request); assertEquals(3, histories.size()); lastServiceName = null; for (AlertHistoryEntity history : histories) { if (null == lastServiceName) { lastServiceName = history.getServiceName(); continue; } String currentServiceName = history.getServiceName(); assertTrue(lastServiceName.compareTo(currentServiceName) >= 0); lastServiceName = currentServiceName; } }