package com.emc.mongoose.metrics.util;

import java.util.ArrayList;
import java.util.List;

/**
 @author veronika K. on 12.10.18 */
public class TimingMetricSnapshotImpl
	implements TimingMetricSnapshot {

	private final long sum;
	private final long count;
	private final long min;
	private final long max;
	private final double mean;
	private final HistogramSnapshot histogramSnapshot;
	private final String metricName;

	public TimingMetricSnapshotImpl(
		final long sum, final long count, final long min, final long max, final double mean,
		final HistogramSnapshot histogramSnapshot,
		final String metricName
	) {
		this.sum = sum;
		this.count = count;
		this.min = min;
		this.max = max;
		this.mean = mean;
		this.histogramSnapshot = histogramSnapshot;
		this.metricName = metricName;
	}

	public TimingMetricSnapshotImpl(final List<TimingMetricSnapshot> snapshots) {
		long newCount = 0;
		long newSum = 0;
		long newMax = Long.MIN_VALUE;
		long newMin = Long.MAX_VALUE;
		final int snapshotsCount = snapshots.size();
		final List<HistogramSnapshot> histogramSnapshots = new ArrayList<>();
		for(final TimingMetricSnapshot snapshot : snapshots) {
			newCount += snapshot.count();
			newSum += snapshot.sum();
			newMax = Math.max(newMax, snapshot.max());
			newMin = Math.min(newMin, snapshot.min());
			histogramSnapshots.add(snapshot.histogramSnapshot());
		}
		this.count = newCount;
		this.sum = newSum;
		this.mean = ((double) newSum) / snapshotsCount;
		this.max = newMax;
		this.min = newMin;
		this.histogramSnapshot = new HistogramSnapshotImpl(histogramSnapshots);
		this.metricName = snapshots.get(0).name();
	}

	@Override
	public long sum() {
		return sum;
	}

	@Override
	public long min() {
		return min;
	}

	@Override
	public long max() {
		return max;
	}

	@Override
	public long quantile(final double value) {
		return histogramSnapshot.quantile(value);
	}

	@Override
	public HistogramSnapshot histogramSnapshot() {
		return histogramSnapshot;
	}

	@Override
	public String name() {
		return metricName;
	}

	@Override
	public double mean() {
		return mean;
	}

	@Override
	public long count() {
		return count;
	}
}

@@ -1577,16 +1577,47 @@
 shots);%0A
+%09%09if(snapshots.size() != 0) %7B%0A%09
 %09%09this.m
@@ -1653,16 +1653,56 @@
 name();%0A
+%09%09%7D else %7B%0A%09%09%09this.metricName = %22%22;%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Ov

save [skip ci]