Add aggregation sum util

This commit is contained in:
Brenda Sukh
2020-11-23 18:45:40 -05:00
parent 8072bc85c3
commit f127264419
2 changed files with 25 additions and 4 deletions
+5 -4
View File
@@ -23,6 +23,7 @@ import {
MetricData,
Duration,
DEFAULT_DATE_FORMAT,
DateAggregation,
} from '../types';
import dayjs, { OpUnitType } from 'dayjs';
import duration from 'dayjs/plugin/duration';
@@ -54,9 +55,9 @@ export function getComparedChange(
duration: Duration,
lastCompleteBillingDate: string, // YYYY-MM-DD,
): ChangeStatistic {
const ratio = dailyCost.change.ratio - metricData.change.ratio;
const ratio = dailyCost.change!.ratio - metricData.change.ratio;
const previousPeriodTotal = getPreviousPeriodTotalCost(
dailyCost,
dailyCost.aggregation,
duration,
lastCompleteBillingDate,
);
@@ -67,7 +68,7 @@ export function getComparedChange(
}
export function getPreviousPeriodTotalCost(
dailyCost: Cost,
aggregation: DateAggregation[],
duration: Duration,
inclusiveEndDate: string,
): number {
@@ -83,7 +84,7 @@ export function getPreviousPeriodTotalCost(
const nextPeriodStart = dayjs(startDate).add(amount, type);
// Add up costs that incurred before the start of the next period.
return dailyCost.aggregation.reduce((acc, costByDate) => {
return aggregation.reduce((acc, costByDate) => {
return dayjs(costByDate.date).isBefore(nextPeriodStart)
? acc + costByDate.amount
: acc;
+20
View File
@@ -0,0 +1,20 @@
/*
* Copyright 2020 Spotify AB
*
* 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.
*/
import { DateAggregation } from '../types';
export const aggregationSum = (aggregation: DateAggregation[]) =>
aggregation.reduce((total, curAgg) => total + curAgg.amount, 0);