private Number getStatisticalValue(FieldStatsResult fieldStatsResult) {
   switch (statsFunction) {
     case "count":
       return fieldStatsResult.getCount();
     case "mean":
       return fieldStatsResult.getMean();
     case "stddev":
       return fieldStatsResult.getStdDeviation();
     case "min":
       return fieldStatsResult.getMin();
     case "max":
       return fieldStatsResult.getMax();
     case "sum":
       return fieldStatsResult.getSum();
     case "variance":
       return fieldStatsResult.getVariance();
     case "squares":
       return fieldStatsResult.getSumOfSquares();
     case "cardinality":
       return fieldStatsResult.getCardinality();
     default:
       throw new IllegalArgumentException(
           "Statistic function " + statsFunction + " is not supported");
   }
 }
  @Override
  protected ComputationResult compute() {
    try {
      final String filter;
      if (!isNullOrEmpty(streamId)) {
        filter = "streams:" + streamId;
      } else {
        filter = null;
      }
      // if we only need the cardinality, we can skip calculating the extended stats and vice versa
      boolean isCardinalityFunction = "cardinality".equals(statsFunction);
      final FieldStatsResult fieldStatsResult =
          getSearches()
              .fieldStats(
                  field, query, filter, timeRange, isCardinalityFunction, isCardinalityFunction);
      if (trend && timeRange instanceof RelativeRange) {
        DateTime toPrevious = timeRange.getFrom();
        DateTime fromPrevious =
            toPrevious.minus(Seconds.seconds(((RelativeRange) timeRange).getRange()));
        TimeRange previousTimeRange = new AbsoluteRange(fromPrevious, toPrevious);

        final FieldStatsResult previousFieldStatsResult =
            getSearches()
                .fieldStats(
                    field,
                    query,
                    filter,
                    previousTimeRange,
                    isCardinalityFunction,
                    isCardinalityFunction);
        Map<String, Object> results = Maps.newHashMap();
        results.put("now", getStatisticalValue(fieldStatsResult));
        results.put("previous", getStatisticalValue(previousFieldStatsResult));
        long tookMs = fieldStatsResult.took().millis() + previousFieldStatsResult.took().millis();

        return new ComputationResult(results, tookMs);
      } else {
        return new ComputationResult(
            getStatisticalValue(fieldStatsResult), fieldStatsResult.took().millis());
      }
    } catch (Searches.FieldTypeException e) {
      throw new RuntimeException("Invalid field provided.", e);
    }
  }
  protected FieldStatsResult getFieldStatsResult(
      FieldValueAlertCondition.CheckType type, Number retValue) {
    final Double value = (Double) retValue;
    final FieldStatsResult fieldStatsResult = mock(FieldStatsResult.class);

    when(fieldStatsResult.getCount()).thenReturn(1L);

    switch (type) {
      case MIN:
        when(fieldStatsResult.getMin()).thenReturn(value);
      case MAX:
        when(fieldStatsResult.getMax()).thenReturn(value);
      case MEAN:
        when(fieldStatsResult.getMean()).thenReturn(value);
      case STDDEV:
        when(fieldStatsResult.getStdDeviation()).thenReturn(value);
      case SUM:
        when(fieldStatsResult.getSum()).thenReturn(value);
    }
    return fieldStatsResult;
  }