Skip to content

Instantly share code, notes, and snippets.

@ebernhardson
Created August 22, 2017 20:33
Show Gist options
  • Save ebernhardson/9e8119727ea6d6e053c0877773a678d3 to your computer and use it in GitHub Desktop.
Save ebernhardson/9e8119727ea6d6e053c0877773a678d3 to your computer and use it in GitHub Desktop.
// The LatencyStat object is a POJO with three fields: bucket, percentile and latency
// The goal is to get a new List<LatencyStat> with one object per (bucket, percentile) combination
// averaging the latency value between them.
Map<String, Map<Double, List<SearchLatencyProbe.LatencyStat>>> foo = nodes.stream()
.flatMap(n -> n.statDetails.latencies.stream())
.collect(Collectors.groupingBy(LatencyStat::getBucket,
Collectors.groupingBy(LatencyStat::getPercentile)));
List<LatencyStat> allNodes = new ArrayList<>();
for (Map.Entry<String, Map<Double, List<LatencyStat>>> bucketEntry : foo.entrySet()) {
for (Map.Entry<Double, List<LatencyStat>> percentileEntry : bucketEntry.getValue().entrySet()) {
double latency = percentileEntry.getValue().stream().mapToDouble(LatencyStat::getLatencyMs).average().orElse(0D);
allNodes.add(new LatencyStat(bucketEntry.getKey(), percentileEntry.getKey(), latency));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment