-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aggregate cost metric #471
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
379117d
Aggregate cost
kathy-t 9f6ad83
Use java money library to calculate cost metrics
kathy-t 5496e3a
Make constructors protected
kathy-t c4113ad
Add dateExecuted to executions in tests
kathy-t fae9e4c
Use 1.15.0-alpha.5 tag
kathy-t 62c9e7c
Use default value
kathy-t File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
metricsaggregator/src/main/java/io/dockstore/metricsaggregator/DoubleStatistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright 2023 OICR and UCSC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package io.dockstore.metricsaggregator; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Record that contains statistical information obtained from a list of Doubles. | ||
*/ | ||
public class DoubleStatistics extends Statistics<Double> { | ||
public DoubleStatistics() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Constructor that calculates statistical information from the provided list of data points. | ||
* @param dataPoints List of Doubles | ||
*/ | ||
public DoubleStatistics(List<Double> dataPoints) { | ||
super(dataPoints); | ||
} | ||
|
||
public DoubleStatistics(Double minimum, Double maximum, Double average, int numberOfDataPoints) { | ||
super(minimum, maximum, average, numberOfDataPoints); | ||
} | ||
|
||
/** | ||
* Constructor used to create a Statistics object that can be used to calculate weighted averages for non-Statistics objects. | ||
* A placeholder value is set for the min and maximum fields | ||
* @param average | ||
* @param numberOfDataPoints | ||
*/ | ||
public DoubleStatistics(double average, int numberOfDataPoints) { | ||
super(0d, 0d, average, numberOfDataPoints); | ||
} | ||
|
||
/** | ||
* Create a new Statistics object from a list of statistics by aggregating the list of statistics | ||
* @param statistics | ||
* @return | ||
*/ | ||
public static DoubleStatistics createFromStatistics(List<DoubleStatistics> statistics) { | ||
if (statistics.size() == 1) { | ||
return statistics.get(0); | ||
} | ||
|
||
DoubleStatistics newStatistics = new DoubleStatistics(); | ||
newStatistics.setAverage(statistics); | ||
newStatistics.setMinimum(statistics); | ||
newStatistics.setMaximum(statistics); | ||
newStatistics.setNumberOfDataPoints(statistics); | ||
return newStatistics; | ||
} | ||
|
||
/** | ||
* Get the lowest value from the list of data points. | ||
* @param dataPoints | ||
* @return | ||
*/ | ||
@Override | ||
public Double calculateMinimum(List<Double> dataPoints) { | ||
return dataPoints.stream().mapToDouble(d -> d).min().orElse(0); | ||
} | ||
|
||
/** | ||
* Get the highest value from the list of data points. | ||
* @param dataPoints | ||
* @return | ||
*/ | ||
@Override | ||
public Double calculateMaximum(List<Double> dataPoints) { | ||
return dataPoints.stream().mapToDouble(d -> d).max().orElse(0); | ||
} | ||
|
||
/** | ||
* Calculate the average from the list of data points. | ||
* @param dataPoints | ||
* @return | ||
*/ | ||
@Override | ||
public Double calculateAverage(List<Double> dataPoints) { | ||
return dataPoints.stream().mapToDouble(d -> d).average().orElse(0); | ||
} | ||
|
||
/** | ||
* Calculate a weighted average | ||
*/ | ||
@Override | ||
public Double calculateWeightedAverage(List<? extends Statistics<Double>> statistics) { | ||
int totalNumberOfDataPoints = getTotalNumberOfDataPoints(statistics); | ||
return statistics.stream() | ||
.map(stat -> { | ||
double weight = (double)stat.getNumberOfDataPoints() / (double)totalNumberOfDataPoints; | ||
denis-yuen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return stat.getAverage() * weight; | ||
}) | ||
.mapToDouble(Double::doubleValue) | ||
.sum(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
metricsaggregator/src/main/java/io/dockstore/metricsaggregator/MoneyStatistics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.dockstore.metricsaggregator; | ||
|
||
import java.util.List; | ||
import org.javamoney.moneta.Money; | ||
|
||
/** | ||
* Calculates money statistics in USD using the Java Money library to preserve accuracy. | ||
*/ | ||
public class MoneyStatistics extends Statistics<Money> { | ||
private static String currency = "USD"; | ||
|
||
private MoneyStatistics() { | ||
super(); | ||
} | ||
|
||
public MoneyStatistics(List<Money> dataPoints) { | ||
super(dataPoints); | ||
} | ||
|
||
public MoneyStatistics(Money minimum, Money maximum, Money average, int numberOfDataPoints) { | ||
super(minimum, maximum, average, numberOfDataPoints); | ||
} | ||
|
||
/** | ||
* Create a new Statistics object from a list of statistics by aggregating the list of statistics | ||
* @param statistics | ||
* @return | ||
*/ | ||
public static MoneyStatistics createFromStatistics(List<MoneyStatistics> statistics) { | ||
if (statistics.size() == 1) { | ||
return statistics.get(0); | ||
} | ||
|
||
MoneyStatistics newStatistics = new MoneyStatistics(); | ||
newStatistics.setAverage(statistics); | ||
newStatistics.setMinimum(statistics); | ||
newStatistics.setMaximum(statistics); | ||
newStatistics.setNumberOfDataPoints(statistics); | ||
return newStatistics; | ||
} | ||
|
||
/** | ||
* Get the lowest value from the list of data points. | ||
* @param dataPoints | ||
* @return | ||
*/ | ||
@Override | ||
public Money calculateMinimum(List<Money> dataPoints) { | ||
return dataPoints.stream() | ||
.min(Money::compareTo) | ||
.orElse(Money.of(0, currency)); | ||
} | ||
|
||
/** | ||
* Get the highest value from the list of data points. | ||
* @param dataPoints | ||
* @return | ||
*/ | ||
@Override | ||
public Money calculateMaximum(List<Money> dataPoints) { | ||
return dataPoints.stream() | ||
.max(Money::compareTo) | ||
.orElse(Money.of(0, currency)); | ||
} | ||
|
||
/** | ||
* Calculate the average from the list of data points. | ||
* @param dataPoints | ||
* @return | ||
*/ | ||
@Override | ||
public Money calculateAverage(List<Money> dataPoints) { | ||
Money sum = dataPoints.stream().reduce(Money.of(0, currency), Money::add); | ||
return sum.divide(dataPoints.size()); | ||
} | ||
|
||
/** | ||
* Calculate a weighted average | ||
*/ | ||
@Override | ||
public Money calculateWeightedAverage(List<? extends Statistics<Money>> statistics) { | ||
int totalNumberOfDataPoints = getTotalNumberOfDataPoints(statistics); | ||
return statistics.stream() | ||
.map(stat -> { | ||
double weight = (double)stat.getNumberOfDataPoints() / (double)totalNumberOfDataPoints; | ||
denis-yuen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return stat.getAverage().multiply(weight); | ||
}) | ||
.reduce(Money.of(0, currency), Money::add); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not this existed. :)