All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.indexer.results.FieldStatsResult Maven / Gradle / Ivy

There is a newer version: 5.2.7
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.indexer.results;

import io.searchbox.core.search.aggregation.CardinalityAggregation;
import io.searchbox.core.search.aggregation.ExtendedStatsAggregation;
import io.searchbox.core.search.aggregation.ValueCountAggregation;

import java.util.List;

public class FieldStatsResult extends IndexQueryResult {
    private List searchHits;

    private final long count;
    private final double sum;
    private final double sumOfSquares;
    private final double mean;
    private final double min;
    private final double max;
    private final double variance;
    private final double stdDeviation;
    private final long cardinality;

    public FieldStatsResult(ValueCountAggregation valueCountAggregation,
                            ExtendedStatsAggregation extendedStatsAggregation,
                            CardinalityAggregation cardinalityAggregation,
                            List hits,
                            String query,
                            String source,
                            long tookMs) {
        super(query, source, tookMs);
        this.count = getValueCount(valueCountAggregation, extendedStatsAggregation);
        this.cardinality = cardinalityAggregation == null || cardinalityAggregation.getCardinality() == null ? Long.MIN_VALUE : cardinalityAggregation.getCardinality();

        if (extendedStatsAggregation != null) {
            sum = extendedStatsAggregation.getSum() != null ? extendedStatsAggregation.getSum() : Double.NaN;
            sumOfSquares = extendedStatsAggregation.getSumOfSquares() != null ? extendedStatsAggregation.getSumOfSquares() : Double.NaN;
            mean = extendedStatsAggregation.getAvg() != null ? extendedStatsAggregation.getAvg() : Double.NaN;
            min = extendedStatsAggregation.getMin() != null ? extendedStatsAggregation.getMin() : Double.NaN;
            max = extendedStatsAggregation.getMax() != null ? extendedStatsAggregation.getMax() : Double.NaN;
            variance = extendedStatsAggregation.getVariance() != null ? extendedStatsAggregation.getVariance() : Double.NaN;
            stdDeviation = extendedStatsAggregation.getStdDeviation() != null ? extendedStatsAggregation.getStdDeviation() : Double.NaN;
        } else {
            sum = Double.NaN;
            sumOfSquares = Double.NaN;
            mean = Double.NaN;
            min = Double.NaN;
            max = Double.NaN;
            variance = Double.NaN;
            stdDeviation = Double.NaN;
        }

        this.searchHits = hits;
    }

    private FieldStatsResult(String query, String bytesReference) {
        super(query, bytesReference, 0);

        this.count = Long.MIN_VALUE;
        this.cardinality = Long.MIN_VALUE;
        sum = Double.NaN;
        sumOfSquares = Double.NaN;
        mean = Double.NaN;
        min = Double.NaN;
        max = Double.NaN;
        variance = Double.NaN;
        stdDeviation = Double.NaN;
    }

    private long getValueCount(ValueCountAggregation valueCountAggregation, ExtendedStatsAggregation extendedStatsAggregation) {
        if (valueCountAggregation != null && valueCountAggregation.getValueCount() != null) {
            return valueCountAggregation.getValueCount();
        } else if (extendedStatsAggregation != null && extendedStatsAggregation.getCount() != null) {
            return extendedStatsAggregation.getCount();
        }
        return Long.MIN_VALUE;
    }

    public long getCount() {
        return count;
    }

    public double getSum() {
        return sum;
    }

    public double getSumOfSquares() {
        return sumOfSquares;
    }

    public double getMean() {
        return mean;
    }

    public double getMin() {
        return min;
    }

    public double getMax() {
        return max;
    }

    public double getVariance() {
        return variance;
    }

    public double getStdDeviation() {
        return stdDeviation;
    }

    public long getCardinality() {
        return cardinality;
    }

    public List getSearchHits() {
        return searchHits;
    }

    public static FieldStatsResult empty(String query, String bytesReference) {
        return new FieldStatsResult(query, bytesReference);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy