diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetTopNFunctionsResponse.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetTopNFunctionsResponse.java index b16ce6f43685f..a42e64546058c 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetTopNFunctionsResponse.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/GetTopNFunctionsResponse.java @@ -19,11 +19,21 @@ public class GetTopNFunctionsResponse extends ActionResponse implements ToXContentObject { private final long selfCount; private final long totalCount; + private final double annualCo2Tons; + private final double annualCostsUsd; private final List topNFunctions; - public GetTopNFunctionsResponse(long selfCount, long totalCount, List topNFunctions) { + public GetTopNFunctionsResponse( + long selfCount, + long totalCount, + double annualCo2Tons, + double annualCostsUsd, + List topNFunctions + ) { this.selfCount = selfCount; this.totalCount = totalCount; + this.annualCo2Tons = annualCo2Tons; + this.annualCostsUsd = annualCostsUsd; this.topNFunctions = topNFunctions; } @@ -40,6 +50,14 @@ public long getTotalCount() { return totalCount; } + public double getAnnualCo2Tons() { + return annualCo2Tons; + } + + public double getAnnualCostsUsd() { + return annualCostsUsd; + } + public List getTopN() { return topNFunctions; } @@ -49,6 +67,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.startObject(); builder.field("self_count", selfCount); builder.field("total_count", totalCount); + builder.field("self_annual_co2_tons", annualCo2Tons); + builder.field("self_annual_cost_usd", annualCostsUsd); builder.xContentList("topn", topNFunctions); builder.endObject(); return builder; diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TopNFunction.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TopNFunction.java index 402d2ff012839..800b006b3cc17 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TopNFunction.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TopNFunction.java @@ -123,6 +123,10 @@ public void addTotalCount(long totalCount) { this.totalCount += totalCount; } + public double getSelfAnnualCO2Tons() { + return selfAnnualCO2Tons; + } + public void addSelfAnnualCO2Tons(double co2Tons) { this.selfAnnualCO2Tons += co2Tons; } @@ -131,6 +135,10 @@ public void addTotalAnnualCO2Tons(double co2Tons) { this.totalAnnualCO2Tons += co2Tons; } + public double getSelfAnnualCostsUSD() { + return selfAnnualCostsUSD; + } + public void addSelfAnnualCostsUSD(double costs) { this.selfAnnualCostsUSD += costs; } diff --git a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TransportGetTopNFunctionsAction.java b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TransportGetTopNFunctionsAction.java index e5d67c0b005e2..f8c8d24a0864b 100644 --- a/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TransportGetTopNFunctionsAction.java +++ b/x-pack/plugin/profiling/src/main/java/org/elasticsearch/xpack/profiling/action/TransportGetTopNFunctionsAction.java @@ -134,17 +134,22 @@ public GetTopNFunctionsResponse build() { functions.sort(Collections.reverseOrder()); long sumSelfCount = 0; long sumTotalCount = 0; + double sumAnnualCo2Tons = 0.0d; + double sumAnnualCostsUsd = 0.0d; + for (int i = 0; i < functions.size(); i++) { TopNFunction topNFunction = functions.get(i); topNFunction.setRank(i + 1); sumSelfCount += topNFunction.getSelfCount(); sumTotalCount += topNFunction.getTotalCount(); + sumAnnualCo2Tons += topNFunction.getSelfAnnualCO2Tons(); + sumAnnualCostsUsd += topNFunction.getSelfAnnualCostsUSD(); } // limit at the end so global stats are independent of the limit if (limit != null && limit > 0 && limit < functions.size()) { functions = functions.subList(0, limit); } - return new GetTopNFunctionsResponse(sumSelfCount, sumTotalCount, functions); + return new GetTopNFunctionsResponse(sumSelfCount, sumTotalCount, sumAnnualCo2Tons, sumAnnualCostsUsd, functions); } public boolean isExists(String frameGroupID) { diff --git a/x-pack/plugin/profiling/src/test/java/org/elasticsearch/xpack/profiling/action/TopNFunctionsBuilderTests.java b/x-pack/plugin/profiling/src/test/java/org/elasticsearch/xpack/profiling/action/TopNFunctionsBuilderTests.java index 26c0f066dd092..a2b6974fb2e25 100644 --- a/x-pack/plugin/profiling/src/test/java/org/elasticsearch/xpack/profiling/action/TopNFunctionsBuilderTests.java +++ b/x-pack/plugin/profiling/src/test/java/org/elasticsearch/xpack/profiling/action/TopNFunctionsBuilderTests.java @@ -21,6 +21,9 @@ public void testBuildFunctions() { assertEquals(7L, response.getSelfCount()); assertEquals(14L, response.getTotalCount()); + assertEquals(1.5d, response.getAnnualCo2Tons(), 0.001d); + assertEquals(48.2d, response.getAnnualCostsUsd(), 0.001d); + assertEquals(2, response.getTopN().size()); assertEquals(foo, response.getTopN().get(0)); assertEquals(bar, response.getTopN().get(1)); @@ -35,9 +38,12 @@ public void testBuildFunctionsWithLimitSmallerThanAvailableFunctionCount() { GetTopNFunctionsResponse response = builder.build(); - // total counts are independent of the limit + // total values are independent of the limit assertEquals(7L, response.getSelfCount()); assertEquals(14L, response.getTotalCount()); + assertEquals(1.5d, response.getAnnualCo2Tons(), 0.001d); + assertEquals(48.2d, response.getAnnualCostsUsd(), 0.001d); + assertEquals(1, response.getTopN().size()); assertEquals(foo, response.getTopN().get(0)); } @@ -53,6 +59,8 @@ public void testBuildFunctionsWithLimitHigherThanAvailableFunctionCount() { assertEquals(7L, response.getSelfCount()); assertEquals(14L, response.getTotalCount()); + assertEquals(1.5d, response.getAnnualCo2Tons(), 0.001d); + assertEquals(48.2d, response.getAnnualCostsUsd(), 0.001d); // still limited to the available two functions assertEquals(2, response.getTopN().size()); assertEquals(foo, response.getTopN().get(0));