diff --git a/plugins/cost-insights/src/components/CostGrowth/CostGrowth.tsx b/plugins/cost-insights/src/components/CostGrowth/CostGrowth.tsx index a43cae9720..3dad0e780a 100644 --- a/plugins/cost-insights/src/components/CostGrowth/CostGrowth.tsx +++ b/plugins/cost-insights/src/components/CostGrowth/CostGrowth.tsx @@ -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); diff --git a/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx b/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx index 528cb1663d..7d55026be8 100644 --- a/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx +++ b/plugins/cost-insights/src/components/CostOverviewCard/CostOverviewCard.tsx @@ -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; diff --git a/plugins/cost-insights/src/types/ChangeStatistic.ts b/plugins/cost-insights/src/types/ChangeStatistic.ts index cdd2e02da6..e60d47a0e6 100644 --- a/plugins/cost-insights/src/types/ChangeStatistic.ts +++ b/plugins/cost-insights/src/types/ChangeStatistic.ts @@ -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 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 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, - }; -} diff --git a/plugins/cost-insights/src/utils/change.ts b/plugins/cost-insights/src/utils/change.ts new file mode 100644 index 0000000000..f5c6570b36 --- /dev/null +++ b/plugins/cost-insights/src/utils/change.ts @@ -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 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 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, + }; +}