RunningRule { public void in(MetaInAlarm meta, Metrics metrics) { if (!meta.getMetricsName().equals(metricsName)) { if (log.isTraceEnabled()) { log.trace("Metric names are inconsistent, {}-{}", meta.getMetricsName(), metricsName); } return; } final String metaName = meta.getName(); if (!validate(metaName, includeNames, excludeNames, includeNamesRegex, excludeNamesRegex)) { return; } if (valueType == null) { if (metrics instanceof LongValueHolder) { valueType = MetricsValueType.LONG; threshold.setType(MetricsValueType.LONG); } else if (metrics instanceof IntValueHolder) { valueType = MetricsValueType.INT; threshold.setType(MetricsValueType.INT); } else if (metrics instanceof DoubleValueHolder) { valueType = MetricsValueType.DOUBLE; threshold.setType(MetricsValueType.DOUBLE); } else if (metrics instanceof MultiIntValuesHolder) { valueType = MetricsValueType.MULTI_INTS; threshold.setType(MetricsValueType.MULTI_INTS); } else if (metrics instanceof LabeledValueHolder) { if (((LabeledValueHolder) metrics).getValue().keys().stream() .noneMatch(label -> validate( label, includeLabels, excludeLabels, includeLabelsRegex, excludeLabelsRegex))) { return; } valueType = MetricsValueType.LABELED_LONG; threshold.setType(MetricsValueType.LONG); } else { log.warn("Unsupported value type {}", valueType); return; } } if (valueType != null) { Window window = windows.computeIfAbsent(meta, ignored -> new Window(period)); window.add(metrics); } } RunningRule(AlarmRule alarmRule); void in(MetaInAlarm meta, Metrics metrics); void moveTo(LocalDateTime targetTime); List<AlarmMessage> check();  }
@Test public void testInitAndStart() { AlarmRule alarmRule = new AlarmRule(); alarmRule.setAlarmRuleName("endpoint_percent_rule"); alarmRule.setMetricsName("endpoint_percent"); alarmRule.setOp("<"); alarmRule.setThreshold("75"); alarmRule.setCount(3); alarmRule.setPeriod(15); RunningRule runningRule = new RunningRule(alarmRule); LocalDateTime startTime = TIME_BUCKET_FORMATTER.parseLocalDateTime("201808301434"); long timeInPeriod1 = 201808301434L; runningRule.in(getMetaInAlarm(123), getMetrics(timeInPeriod1, 70)); Map<MetaInAlarm, RunningRule.Window> windows = Whitebox.getInternalState(runningRule, "windows"); RunningRule.Window window = windows.get(getMetaInAlarm(123)); LocalDateTime endTime = Whitebox.getInternalState(window, "endTime"); int period = Whitebox.getInternalState(window, "period"); LinkedList<Metrics> metricsBuffer = Whitebox.getInternalState(window, "values"); Assert.assertTrue(startTime.equals(endTime)); Assert.assertEquals(15, period); Assert.assertEquals(15, metricsBuffer.size()); }