Example - Group Average Value by CI Type

The following rule calculates the average status of the KPIs of child CIs, which are of the same CI type as the calculated KPI.

Only child CIs of type "unix" are used in the calculation. If there are no child CIs of this type, or no child CI KPIs have value, no value is set for the KPI.

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 the KPIs of the child CIs, which are of the same CI type as the calculated 
    // KPI, whose CI type is  "unix".
    List<KPI> unixChildKpiList = ci.getChildrenKPIsByIDAndCiType(kpiId, "unix")
    // Create a variable to sum the total values from child KPIs. 
    // If no child exists or no child has value the variable will remain null.
    Double totalChildValue = null;
    // Write information to the log file.
    logger.debug("DashboardGroupAvgValueByCiTypeRule : number of child CIs with type unix: " + unixChildKpiList.size())
    // Go over the list of child KPIs.
    unixChildKpiList.each {KPI childKPI ->
        // Sum values of the child KPIs using the Utils class, which handles null values.
        totalChildValue = Utils.sum(totalChildValue, childKPI.value);
    }
    // Set the calculated KPI's value to the average value, using the Utils class. 
    // If totalChildValue is null, null value will be set.
    kpi.value = Utils.divide(totalChildValue, unixChildKpiList.size());
}


Parent topic: Examples - API Group and Sibling Rule