private void startCalculate()

in hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/component/status/CalculateStatus.java [82:160]


    private void startCalculate() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setUncaughtExceptionHandler((thread, throwable) -> {
                    log.error("Status calculate has uncaughtException.");
                    log.error(throwable.getMessage(), throwable);
                })
                .setDaemon(true)
                .setNameFormat("status-page-calculate-%d")
                .build();
        ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory);
        scheduledExecutor.scheduleAtFixedRate(() -> {
            log.info("start to calculate status page state");
            try {
                // calculate component state from tag bind monitors status
                List<StatusPageOrg> statusPageOrgList = statusPageOrgDao.findAll();
                for (StatusPageOrg statusPageOrg : statusPageOrgList) {
                    long orgId = statusPageOrg.getId();
                    List<StatusPageComponent> pageComponentList = statusPageComponentDao.findByOrgId(orgId);
                    Set<Byte> stateSet = new HashSet<>(8);
                    for (StatusPageComponent component : pageComponentList) {
                        byte state;
                        if (component.getMethod() == CommonConstants.STATUS_PAGE_CALCULATE_METHOD_MANUAL) {
                            state = component.getConfigState();
                        } else {
                            Map<String, String> labels = component.getLabels();
                            if (labels == null || labels.isEmpty()) {
                                continue;
                            }
                            Specification<Monitor> specification = (root, query, criteriaBuilder) -> {
                                List<Predicate> predicates = new ArrayList<>();
                                // create every label condition
                                labels.forEach((key, value) -> {
                                    String pattern = String.format("%%\"%s\":\"%s\"%%", key, value);
                                    predicates.add(criteriaBuilder.like(root.get("labels"), pattern));
                                });

                                // use or connect them
                                return criteriaBuilder.or(predicates.toArray(new Predicate[0]));
                            };
                            List<Monitor> monitorList = monitorDao.findAll(specification);
                            state = CommonConstants.STATUS_PAGE_COMPONENT_STATE_UNKNOWN;
                            for (Monitor monitor : monitorList) {
                                if (monitor.getStatus() == CommonConstants.MONITOR_DOWN_CODE) {
                                    state = CommonConstants.STATUS_PAGE_COMPONENT_STATE_ABNORMAL;
                                    break;
                                } else if (monitor.getStatus() == CommonConstants.MONITOR_UP_CODE) {
                                    state = CommonConstants.STATUS_PAGE_COMPONENT_STATE_NORMAL;
                                }
                            }
                        }
                        stateSet.add(state);
                        component.setState(state);
                        statusPageComponentDao.save(component);
                        // insert component state history
                        StatusPageHistory statusPageHistory = StatusPageHistory.builder()
                                .componentId(component.getId())
                                .state(state)
                                .timestamp(System.currentTimeMillis())
                                .build();
                        statusPageHistoryDao.save(statusPageHistory);
                    }
                    stateSet.remove(CommonConstants.STATUS_PAGE_COMPONENT_STATE_UNKNOWN);
                    if (stateSet.remove(CommonConstants.STATUS_PAGE_COMPONENT_STATE_ABNORMAL)) {
                        if (stateSet.contains(CommonConstants.STATUS_PAGE_COMPONENT_STATE_NORMAL)) {
                            statusPageOrg.setState(CommonConstants.STATUS_PAGE_ORG_STATE_SOME_ABNORMAL);
                        } else {
                            statusPageOrg.setState(CommonConstants.STATUS_PAGE_ORG_STATE_ALL_ABNORMAL);
                        }
                    } else {
                        statusPageOrg.setState(CommonConstants.STATUS_PAGE_ORG_STATE_ALL_NORMAL);
                    }
                    statusPageOrg.setGmtUpdate(LocalDateTime.now());
                    statusPageOrgDao.save(statusPageOrg);
                }
            } catch (Exception e) {
                log.error("status page calculate component state error: {}", e.getMessage(), e);
            }
        }, 5, intervals, TimeUnit.SECONDS);
    }