-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://ucsc-cgl.atlassian.net/browse/SEAB-5625 * Aggregate cost * Use java money library to calculate cost metrics * Use 1.15.0-alpha.5 tag * Use default value
- Loading branch information
Showing
15 changed files
with
589 additions
and
252 deletions.
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; | ||
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; | ||
return stat.getAverage().multiply(weight); | ||
}) | ||
.reduce(Money.of(0, currency), Money::add); | ||
} | ||
} |
Oops, something went wrong.