DefaultAlertService extends DefaultJPAService implements AlertService { public void sendClearNotification(Trigger trigger, Metric metric, History history, Notification notification, Alert alert, Long alertEnqueueTime, String action) { Pair<String, byte[]> evaluatedMetricSnapshotDetails = null; String evaluatedMetricSnapshotURL = null; if (isImagesInNotificationsEnabled(action)) { ImageDataRetrievalContext imageDataRetrievalContext = new ImageDataRetrievalContext(alert, trigger, metric, Notifier.NotificationStatus.CLEARED); evaluatedMetricSnapshotDetails = getEvaluatedMetricSnapshotDetails(imageDataRetrievalContext); if (evaluatedMetricSnapshotDetails != null) { evaluatedMetricSnapshotURL = _imageDataRetriever.getImageURL(evaluatedMetricSnapshotDetails); } } Long timestamp = (alertEnqueueTime != null) ? alertEnqueueTime : System.currentTimeMillis(); String alertEvaluationTrackingID = getAlertEvaluationTrackingID(alert, timestamp); NotificationContext context = new NotificationContext(alert, trigger, notification, System.currentTimeMillis(), 0.0, metric, history, evaluatedMetricSnapshotDetails, evaluatedMetricSnapshotURL, alertEvaluationTrackingID); context.setAlertEnqueueTimestamp(alertEnqueueTime); Notifier notifier = getNotifier(SupportedNotifier.fromClassName(notification.getNotifierName())); String alertId = (trigger != null) ? trigger.getAlert().getId().toString() : alert.getId().toString(); String notificationTarget = SupportedNotifier.fromClassName(notification.getNotifierName()).name(); Map<String, String> tags = new HashMap<>(); String logMessage; boolean rc; try { rc = notifier.clearNotification(context); } catch (Exception e) { _logger.error("clearNotification() hit exception", e); rc = false; } if (rc) { tags.put(STATUSTAG, STATUS_SUCCESS); logMessage = MessageFormat.format("The notification {0} was cleared.", notification.getName()); } else { tags.put(STATUSTAG, STATUS_FAILURE); logMessage = MessageFormat.format("Failed to send clear notification to {0}", notificationTarget); } tags.put(USERTAG, alert.getOwner().getUserName()); tags.put(ACTIONTAG, action); tags.put(NOTIFYTARGETTAG, notificationTarget); _monitorService.modifyCounter(Counter.NOTIFICATIONS_SENT, 1, tags); tags.put(HOSTTAG, HOSTNAME); tags.put(ALERTIDTAG, alertId); publishAlertTrackingMetric(Counter.NOTIFICATIONS_SENT.getMetric(), 1.0, tags); _logger.info(logMessage); history.appendMessageNUpdateHistory(logMessage, null, 0); } @Inject  DefaultAlertService(SystemConfiguration configuration, MQService mqService, MetricService metricService,
							   AuditService auditService, TSDBService tsdbService, MailService mailService, HistoryService historyService,
							   MonitorService monitorService, ImageDataRetriever imageDataRetriever, NotifierFactory notifierFactory, Provider<EntityManager> emProvider); @Override @Transactional Alert updateAlert(Alert alert); @Override @Transactional void deleteAlert(String name, PrincipalUser owner); @Override @Transactional void deleteAlert(Alert alert); @Override @Transactional void markAlertForDeletion(String name, PrincipalUser owner); @Override @Transactional void markAlertForDeletion(Alert alert); @Override List<Alert> findAlertsMarkedForDeletion(); @Override List<Alert> findAlertsMarkedForDeletion(final int limit); @Override List<Alert> findAlertsByOwner(PrincipalUser owner, boolean metadataOnly); @Override List<Alert> findAlertsByOwnerPaged(PrincipalUser owner, Integer limit, Integer offset, String searchText, String sortField, String sortOrder); @Override Alert findAlertByPrimaryKey(BigInteger id); @Override List<Alert> findAlertsByPrimaryKeys(List<BigInteger> ids); @Override void updateNotificationsActiveStatusAndCooldown(List<Notification> notifications); @Override @Transactional Integer executeScheduledAlerts(int alertCount, int timeout); void _processNotification(INotificationHandler notificationHandler,
									 Alert alert,
									 History history,
									 List<Metric> metrics,
									 Map<BigInteger, Map<Metric, Long>> triggerFiredTimesAndMetricsByTrigger,
									 Notification notification,
									 Long alertEnqueueTimestamp); void _processTriggerlessNotification(INotificationHandler notificationHandler,
												Alert alert,
												History history,
												List<Metric> metrics,
												Notification notification,
												Long alertEnqueueTimestamp); void _processMissingDataNotification(INotificationHandler notificationHandler,
												Alert alert,
												History history,
												Set<Trigger> triggers,
												Notification notification,
												boolean isDataMissing,
												Long alertEnqueueTimestamp); void sendNotification(Trigger trigger, Metric metric, History history, Notification notification, Alert alert,
			Long triggerFiredTime, Long alertEnqueueTime, String action); void sendClearNotification(Trigger trigger, Metric metric, History history, Notification notification, Alert alert, Long alertEnqueueTime, String action); @Override @Transactional Alert findAlertByNameAndOwner(String name, PrincipalUser owner); @Override void enqueueAlerts(List<Alert> alerts); @Override // IMPORTANT - @Transactional ?? - should not be as it shouldn't talk to the DB void testEvaluateAlert(String serializedAlert, Long when, String testUuid); @VisibleForTesting boolean testEvaluateAlert(Alert alert, Long alertEvaluationTime, AlertTestResults testResults); @Override List<Alert> findAllAlerts(boolean metadataOnly); @Override List<Alert> findAlertsByStatus(boolean enabled); @Override List<BigInteger> findAlertIdsByStatus(boolean enabled); @Override List<Alert> findAlertsByRangeAndStatus(BigInteger fromId, BigInteger toId, boolean enabled); @Override List<Alert> findAlertsModifiedAfterDate(Date modifiedDate); @Override int alertCountByStatus(boolean enabled); @Override List<Alert> findAlertsByLimitOffsetStatus(int limit, int offset,
			boolean enabled); @Override List<Alert> findAlertsByNameWithPrefix(String prefix); @Override List<String> getSupportedNotifiers(); @Override List<Alert> findSharedAlerts(boolean metadataOnly, PrincipalUser owner, Integer limit); @Override List<Alert> findSharedAlertsPaged(Integer limit, Integer offset, String searchText, String sortField, String sortOrder); @Override List<Alert> findPrivateAlertsForPrivilegedUserPaged(PrincipalUser owner, Integer limit, Integer offset, String searchText,
		String sortField, String sortOrder); @Override int countAlerts(AlertsCountContext context); @Override Notifier getNotifier(SupportedNotifier notifier); @Override void dispose(); Long getTriggerFiredDatapointTime(Trigger trigger, Metric metric, String queryExpression, Long alertEnqueueTimestamp); Long getLatestDatapointTime(Metric metric, String queryExpression, Long alertEnqueueTimestamp); @Override @Transactional void deleteTrigger(Trigger trigger); @Override @Transactional void deleteNotification(Notification notification); @Override void exportMetric(Metric metric, Double value); @Override void updateCounter(Counter counter, Double value);  }
@Test public void testClearNotificationWhenImageSendingIsEnabled() { UserService userService = system.getServiceFactory().getUserService(); Alert alert = generateAlert("testAlert", userService.findAdminUser(), "-1h:"+METRIC_NAME); alert.setEnabled(true); _setAlertId(alert, "1"); Trigger trigger = getTrigger(alert, TriggerType.GREATER_THAN, "testTrigger", "2.0", "1"); List<Trigger> triggerList = ImmutableList.of(trigger); alert.setTriggers(triggerList); Notification notification = getNotification("EmailNotifier", EmailNotifier.class.getName(), alert, ImmutableList.of("test@salesforce.com")); alert.addNotification(notification); Metric metric = createMetric(); History history = getHistory(); Pair<String, byte[]> evaluatedMetricSnapshotDetails = Pair.of("img1", "Test String".getBytes()); when(_imageDataRetrieverMock.getAnnotatedImage(any(ImageDataRetrievalContext.class))).thenReturn(evaluatedMetricSnapshotDetails); when(_imageDataRetrieverMock.getImageURL(evaluatedMetricSnapshotDetails)).thenReturn("https: alertService.sendClearNotification(trigger, metric, history, notification, alert, 2L, "cleared"); verify(_imageDataRetrieverMock, times(1)).getAnnotatedImage(any(ImageDataRetrievalContext.class)); verify(_imageDataRetrieverMock, times(1)).getImageURL(evaluatedMetricSnapshotDetails); }
@Test public void testClearNotificationWhenImageSendingIsDisabled() { UserService userService = system.getServiceFactory().getUserService(); Alert alert = generateAlert("testAlert", userService.findAdminUser(), "-1h:"+METRIC_NAME); alert.setEnabled(true); _setAlertId(alert, "1"); Trigger trigger = getTrigger(alert, TriggerType.GREATER_THAN, "testTrigger", "2.0", "1"); List<Trigger> triggerList = ImmutableList.of(trigger); alert.setTriggers(triggerList); Notification notification = getNotification("EmailNotifier", EmailNotifier.class.getName(), alert, ImmutableList.of("test@salesforce.com")); alert.addNotification(notification); Metric metric = createMetric(); History history = getHistory(); Pair<String, byte[]> evaluatedMetricSnapshotDetails = Pair.of("img1", "Test String".getBytes()); alertService.sendClearNotification(trigger, metric, history, notification, alert, 2L, "missingdata"); verify(_imageDataRetrieverMock, never()).getAnnotatedImage(any(ImageDataRetrievalContext.class)); verify(_imageDataRetrieverMock, never()).getImageURL(evaluatedMetricSnapshotDetails); }