private void decorateResource(DecoratorContext context) { Metric.Level globalLevel = Metric.Level.OK; List<String> labels = Lists.newArrayList(); for (final Alert alert : profile.getAlerts()) { Measure measure = context.getMeasure(alert.getMetric()); if (measure != null) { Metric.Level level = AlertUtils.getLevel(alert, measure); measure.setAlertStatus(level); String text = getText(alert, level); if (!StringUtils.isBlank(text)) { measure.setAlertText(text); labels.add(text); } context.saveMeasure(measure); if (Metric.Level.WARN == level && globalLevel != Metric.Level.ERROR) { globalLevel = Metric.Level.WARN; } else if (Metric.Level.ERROR == level) { globalLevel = Metric.Level.ERROR; } } } Measure globalMeasure = new Measure(CoreMetrics.ALERT_STATUS, globalLevel); globalMeasure.setAlertStatus(globalLevel); globalMeasure.setAlertText(StringUtils.join(labels, ", ")); context.saveMeasure(globalMeasure); }
@DependsUpon public List<Metric> dependsUponMetrics() { List<Metric> metrics = Lists.newLinkedList(); for (Alert alert : profile.getAlerts()) { metrics.add(alert.getMetric()); } return metrics; }
private static boolean evaluateAlert(Alert alert, Measure measure, Metric.Level alertLevel) { String valueToEval; if (alertLevel.equals(Metric.Level.ERROR)) { valueToEval = alert.getValueError(); } else if (alertLevel.equals(Metric.Level.WARN)) { valueToEval = alert.getValueWarning(); } else { throw new IllegalStateException(alertLevel.toString()); } if (StringUtils.isEmpty(valueToEval)) { return false; } Comparable criteriaValue = getValueForComparison(alert.getMetric(), valueToEval); Comparable metricValue = getMeasureValue(alert.getMetric(), measure); int comparison = metricValue.compareTo(criteriaValue); if (alert.isNotEqualsOperator() && comparison == 0 || alert.isGreaterOperator() && comparison != 1 || alert.isSmallerOperator() && comparison != -1 || alert.isEqualsOperator() && comparison != 0) { return false; } return true; }
private void importAlerts(RulesProfile profile) { if (profile.getAlerts() != null) { for (Iterator<Alert> ia = profile.getAlerts().iterator(); ia.hasNext(); ) { Alert alert = ia.next(); Metric unMarshalledMetric = alert.getMetric(); String validKey = unMarshalledMetric.getKey(); Metric matchingMetricInDb = session.getSingleResult(Metric.class, "key", validKey); if (matchingMetricInDb == null) { LoggerFactory.getLogger(getClass()).error("Unable to find metric " + validKey); ia.remove(); continue; } alert.setMetric(matchingMetricInDb); alert.setRulesProfile(profile); } } }
private String getText(Alert alert, Metric.Level level) { if (level == Metric.Level.OK) { return null; } return alert.getAlertLabel(level); }