Skip to content

Commit

Permalink
[Profiling] Add CO2/costs for TopN on top-level
Browse files Browse the repository at this point in the history
Wit this commit we add CO2 emission and cost information on the
top-level of the TopN functions API response. This is needed by the UI
to show summary info.
  • Loading branch information
danielmitterdorfer committed Apr 10, 2024
1 parent d6f9d1e commit 60505a2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TopNFunction> topNFunctions;

public GetTopNFunctionsResponse(long selfCount, long totalCount, List<TopNFunction> topNFunctions) {
public GetTopNFunctionsResponse(
long selfCount,
long totalCount,
double annualCo2Tons,
double annualCostsUsd,
List<TopNFunction> topNFunctions
) {
this.selfCount = selfCount;
this.totalCount = totalCount;
this.annualCo2Tons = annualCo2Tons;
this.annualCostsUsd = annualCostsUsd;
this.topNFunctions = topNFunctions;
}

Expand All @@ -40,6 +50,14 @@ public long getTotalCount() {
return totalCount;
}

public double getAnnualCo2Tons() {
return annualCo2Tons;
}

public double getAnnualCostsUsd() {
return annualCostsUsd;
}

public List<TopNFunction> getTopN() {
return topNFunctions;
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
}
Expand All @@ -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));
Expand Down

0 comments on commit 60505a2

Please sign in to comment.