@Override
  public Result finalizeResult(Result queryResult) {
    TreeNode<Resource> resultTree = queryResult.getResultTree();
    AlertStateSummary alertSummary = new AlertStateSummary();

    // iterate over all returned flattened alerts and build the summary info
    for (TreeNode<Resource> node : resultTree.getChildren()) {
      Resource resource = node.getObject();
      AlertState state = (AlertState) resource.getPropertyValue(AlertResourceProvider.ALERT_STATE);
      Long originalTimestampObject = (Long) resource.getPropertyValue(AlertResourceProvider.ALERT_ORIGINAL_TIMESTAMP);
      MaintenanceState maintenanceState = (MaintenanceState) resource.getPropertyValue(AlertResourceProvider.ALERT_MAINTENANCE_STATE);

      // NPE sanity
      if (null == state) {
        state = AlertState.UNKNOWN;
      }

      // NPE sanity
      long originalTimestamp = 0;
      if (null != originalTimestampObject) {
        originalTimestamp = originalTimestampObject.longValue();
      }

      // NPE sanity
      boolean isMaintenanceModeEnabled = false;
      if (null != maintenanceState && maintenanceState != MaintenanceState.OFF) {
        isMaintenanceModeEnabled = true;
      }

      final AlertStateValues alertStateValues;
      switch (state) {
        case CRITICAL: {
          alertStateValues = alertSummary.Critical;
          break;
        }
        case OK: {
          alertStateValues = alertSummary.Ok;
          break;
        }
        case WARNING: {
          alertStateValues = alertSummary.Warning;
          break;
        }
        default:
        case UNKNOWN: {
          alertStateValues = alertSummary.Unknown;
          break;
        }
      }

      if (isMaintenanceModeEnabled) {
        alertStateValues.MaintenanceCount++;
      } else {
        alertStateValues.Count++;
      }

      if (originalTimestamp > alertStateValues.Timestamp) {
        alertStateValues.Timestamp = originalTimestamp;
      }
    }

    Result summary = new ResultImpl(true);
    Resource resource = new ResourceImpl(Resource.Type.Alert);
    TreeNode<Resource> summaryTree = summary.getResultTree();
    summaryTree.addChild(resource, "alerts_summary");
    resource.setProperty("alerts_summary", alertSummary);

    return summary;
  }
{@inheritDoc}
<p/>
This will iterate over all of the nodes in the result tree and combine
their {@link AlertResourceProvider#ALERT_STATE} into a single summary
structure.
ambari-server/src/main/java/org/apache/ambari/server/api/query/render/AlertSummaryRenderer.java