Example - Worst Child Rule

The following rule finds the worst status from all of the KPIs of the calculated CI's child CIs, which are of the same type as the calculated KPI, based on active statuses only. Active statuses are Critical, Major, Minor, Warning, and OK.

public void calculateKPI(CI ci, KPI kpi) {
    // Get the calculated KPI's type ID (as defined in the Service Health KPI Repository).
    int kpiId = kpi.getType();
    // Get a list of all of the KPIs of the calculated CI's child CIs, which are of the same
    // type as the calculated KPI.
    List<KPI> childKpiList = ci.getChildrenKPIsByID(kpiId);
    // 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 worstStatus = Status.OK;
    // Go over the list of child KPIs.
    childKpiList.each{KPI childKPI->
        // Get the child KPI's status. 
        Status childKpiStatus = childKPI.status;
        // Check if the child KPI's status is an active status.
        if(childKpiStatus.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(childKpiStatus.isWorse(worstStatus)){
                // Update the worst status.
                worstStatus = childKpiStatus;
            }
        }
    }
    // Check if an active status was found in the child KPI.
    if(isActiveStatusFound){
        // Set the calculated KPI status.
        kpi.setStatus(worstStatus);
    }
}


Parent topic: Examples - API Group and Sibling Rule