public List getReceiverFilterRule()

in hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/NoticeConfigServiceImpl.java [206:253]


    public List<NoticeRule> getReceiverFilterRule(GroupAlert alert) {
        // use cache
        List<NoticeRule> rules = CacheFactory.getNoticeCache();
        if (rules == null) {
            rules = noticeRuleDao.findNoticeRulesByEnableTrue();
            CacheFactory.setNoticeCache(rules);
        }

        // The temporary rule is to forward all, and then implement more matching rules: alarm status selection, monitoring type selection, etc.
        return rules.stream()
                .filter(rule -> {
                    if (!rule.isFilterAll()) {
                        // filter labels
                        if (rule.getLabels() != null && !rule.getLabels().isEmpty()) {
                            boolean labelMatch = rule.getLabels().entrySet().stream().allMatch(labelItem -> {
                                if (!alert.getCommonLabels().containsKey(labelItem.getKey())) {
                                    return false;
                                }
                                String alertLabelValue = alert.getCommonLabels().get(labelItem.getKey());
                                return Objects.equals(labelItem.getValue(), alertLabelValue);
                            });
                            if (!labelMatch) {
                                return false;
                            }
                        }
                    }
                    
                    LocalDateTime nowDate = LocalDateTime.now();
                    // filter day
                    int currentDayOfWeek = nowDate.toLocalDate().getDayOfWeek().getValue();
                    if (rule.getDays() != null && !rule.getDays().isEmpty()) {
                        boolean dayMatch = rule.getDays().stream().anyMatch(item -> item == currentDayOfWeek);
                        if (!dayMatch) {
                            return false;
                        }
                    }
                    // filter time
                    LocalTime nowTime = nowDate.toLocalTime();
                    boolean startMatch = rule.getPeriodStart() == null
                            || nowTime.isAfter(rule.getPeriodStart().toLocalTime())
                            || (rule.getPeriodEnd() != null && rule.getPeriodStart().isAfter(rule.getPeriodEnd())
                                    && nowTime.isBefore(rule.getPeriodStart().toLocalTime()));
                    boolean endMatch = rule.getPeriodEnd() == null
                            || nowTime.isBefore(rule.getPeriodEnd().toLocalTime());
                    return startMatch && endMatch;
                })
                .collect(Collectors.toList());
    }