migrate change utils

This commit is contained in:
Ryan Vazquez
2020-10-28 11:15:42 -04:00
parent 86baf03274
commit 78ffb124dc
4 changed files with 64 additions and 60 deletions
@@ -16,15 +16,9 @@
import React from 'react';
import classnames from 'classnames';
import {
ChangeStatistic,
CurrencyType,
Duration,
EngineerThreshold,
Growth,
growthOf,
rateOf,
} from '../../types';
import { ChangeStatistic, Duration } from '../../types';
import { rateOf, CurrencyType } from '../../utils/currency';
import { growthOf, GrowthType, EngineerThreshold } from '../../utils/change';
import { useCostGrowthStyles as useStyles } from '../../utils/styles';
import { formatPercent, formatCurrency } from '../../utils/formatters';
import { indefiniteArticleOf } from '../../utils/grammar';
@@ -51,8 +45,8 @@ export const CostGrowth = ({ change, duration }: CostGrowthProps) => {
// Determine if growth is significant enough to highlight
const growth = growthOf(engineers, change.ratio);
const classes = classnames({
[styles.excess]: growth === Growth.Excess,
[styles.savings]: growth === Growth.Savings,
[styles.excess]: growth === GrowthType.Excess,
[styles.savings]: growth === GrowthType.Savings,
});
const percent = formatPercent(ratio);
@@ -27,12 +27,8 @@ import { mapFiltersToProps } from './selector';
import { DefaultNavigation } from '../../utils/navigation';
import { formatPercent } from '../../utils/formatters';
import { findAlways } from '../../utils/assert';
import {
Cost,
CostInsightsTheme,
MetricData,
getComparedChange,
} from '../../types';
import { getComparedChange } from '../../utils/change';
import { Cost, CostInsightsTheme, MetricData } from '../../types';
export type CostOverviewCardProps = {
dailyCostData: Cost;
@@ -14,52 +14,9 @@
* limitations under the License.
*/
import { Cost } from './Cost';
import { MetricData } from './MetricData';
import { aggregationSort } from '../utils/sort';
export interface ChangeStatistic {
// The ratio of change from one duration to another, expressed as: (newSum - oldSum) / oldSum
ratio: number;
// The actual USD change between time periods (can be negative if costs decreased)
amount: number;
}
export const EngineerThreshold = 0.5;
export enum ChangeThreshold {
upper = 0.05,
lower = -0.05,
}
export enum Growth {
Negligible,
Savings,
Excess,
}
// Used by <CostGrowth /> for displaying status colors
export function growthOf(amount: number, ratio: number) {
if (amount >= EngineerThreshold && ratio >= ChangeThreshold.upper) {
return Growth.Excess;
}
if (amount >= EngineerThreshold && ratio <= ChangeThreshold.lower) {
return Growth.Savings;
}
return Growth.Negligible;
}
// Used by <CostOverviewCard /> for displaying engineer totals
export function getComparedChange(
dailyCost: Cost,
metricData: MetricData,
): ChangeStatistic {
const ratio = dailyCost.change.ratio - metricData.change.ratio;
const amount = dailyCost.aggregation.slice().sort(aggregationSort)[0].amount;
return {
ratio: ratio,
amount: amount * ratio,
};
}
+57
View File
@@ -0,0 +1,57 @@
/*
* 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 { Cost, ChangeStatistic, MetricData } from '../types';
import { aggregationSort } from '../utils/sort';
export const EngineerThreshold = 0.5;
export enum ChangeThreshold {
upper = 0.05,
lower = -0.05,
}
export enum GrowthType {
Negligible,
Savings,
Excess,
}
// Used by <CostGrowth /> for displaying status colors
export function growthOf(amount: number, ratio: number) {
if (amount >= EngineerThreshold && ratio >= ChangeThreshold.upper) {
return GrowthType.Excess;
}
if (amount >= EngineerThreshold && ratio <= ChangeThreshold.lower) {
return GrowthType.Savings;
}
return GrowthType.Negligible;
}
// Used by <CostOverviewCard /> for displaying engineer totals
export function getComparedChange(
dailyCost: Cost,
metricData: MetricData,
): ChangeStatistic {
const ratio = dailyCost.change.ratio - metricData.change.ratio;
const amount = dailyCost.aggregation.slice().sort(aggregationSort)[0].amount;
return {
ratio: ratio,
amount: amount * ratio,
};
}