-
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.
Use java money library to calculate cost metrics
- Loading branch information
Showing
12 changed files
with
428 additions
and
162 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().getAsDouble(); | ||
} | ||
|
||
/** | ||
* 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().getAsDouble(); | ||
} | ||
|
||
/** | ||
* 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().getAsDouble(); | ||
} | ||
|
||
/** | ||
* 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(); | ||
} | ||
} |
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) | ||
.get(); | ||
} | ||
|
||
/** | ||
* 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) | ||
.get(); | ||
} | ||
|
||
/** | ||
* 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.