Example - Worst Health Indicator Rule

The following rule finds the worst status from all of the health indicators (HIs) of the calculated CI, based on active statuses only. Active statuses are Critical, Major, Minor, Warning, and OK.

public void calculateKPI(CI ci, KPI kpi) {
    // Get all health indicators.
    List<HI> his = ci.getHIs();
    // Create a variable to set the status of the calculated KPI,
    // only if an active status is found.
    boolean isActiveStatusFound = false;
    // Set the current worst status to OK;
    // if a worse status is found this will be updated.
    Status worstHiStatus = Status.OK;
    his.each {HI hi ->
        Status hiStatus = hi.getStatus();
        // Check if the current HI status is an active status.
        if (hiStatus.isActive()) {
            // Mark that an active status was found.
            isActiveStatusFound = true;
            // Check if the child KPI's status is worse than the current worst status.
            if (hiStatus.isWorse(worstHiStatus)) {
                // Update the worst status.
                worstHiStatus = hiStatus;
            }
        }
    }
    // Check if an active status was found in the child KPI.
    if (isActiveStatusFound) {
        // Set the calculated KPI status.
        kpi.setStatus(worstHiStatus);
    }
}


Parent topic: Examples - API Group and Sibling Rule