diff --git a/plugins/cost-insights/src/utils/mockData.ts b/plugins/cost-insights/src/utils/mockData.ts index 4432753c32..cf9f13daa7 100644 --- a/plugins/cost-insights/src/utils/mockData.ts +++ b/plugins/cost-insights/src/utils/mockData.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import dayjs from 'dayjs'; import regression, { DataPoint } from 'regression'; import { Config } from '@backstage/config'; import { ConfigApi } from '@backstage/core'; @@ -28,17 +29,23 @@ import { UnlabeledDataflowAlertProject, UnlabeledDataflowData, DateAggregation, + DEFAULT_DATE_FORMAT, } from '../types'; import { DefaultLoadingAction, getDefaultState as getDefaultLoadingState, } from '../utils/loading'; import { findAlways } from '../utils/assert'; -import { aggregationFor } from '../client'; +import { inclusiveStartDateOf } from './duration'; type mockAlertRenderer = (alert: T) => T; type mockEntityRenderer = (entity: T) => T; +type IntervalFields = { + duration: Duration; + endDate: string; +}; + export const createMockEntity = ( callback?: mockEntityRenderer, ): Entity => { @@ -217,6 +224,45 @@ export function changeOf(aggregation: DateAggregation[]): ChangeStatistic { }; } +export function aggregationFor( + intervals: string, + baseline: number, +): DateAggregation[] { + const { duration, endDate } = parseIntervals(intervals); + const days = dayjs(endDate).diff( + inclusiveStartDateOf(duration, endDate), + 'day', + ); + + return [...Array(days).keys()].reduce( + (values: DateAggregation[], i: number): DateAggregation[] => { + const last = values.length ? values[values.length - 1].amount : baseline; + values.push({ + date: dayjs(inclusiveStartDateOf(duration, endDate)) + .add(i, 'day') + .format(DEFAULT_DATE_FORMAT), + amount: Math.max(0, last + (baseline / 20) * (Math.random() * 2 - 1)), + }); + return values; + }, + [], + ); +} + +function parseIntervals(intervals: string): IntervalFields { + const match = intervals.match( + /\/(?P\d+[DM])\/(?\d{4}-\d{2}-\d{2})/, + ); + if (Object.keys(match?.groups || {}).length !== 2) { + throw new Error(`Invalid intervals: ${intervals}`); + } + const { duration, date } = match!.groups!; + return { + duration: duration as Duration, + endDate: date, + }; +} + export const MockAggregatedDailyCosts: DateAggregation[] = [ { date: '2020-08-07', @@ -467,15 +513,15 @@ export const MockAggregatedDailyCosts: DateAggregation[] = [ export const getGroupedProducts = (intervals: string) => [ { id: 'Cloud Dataflow', - aggregation: aggregationFor(intervals, 1_000), + aggregation: aggregationFor(intervals, 1_700), }, { id: 'Compute Engine', - aggregation: aggregationFor(intervals, 100), + aggregation: aggregationFor(intervals, 350), }, { id: 'Cloud Storage', - aggregation: aggregationFor(intervals, 500), + aggregation: aggregationFor(intervals, 1_300), }, { id: 'BigQuery', @@ -483,7 +529,7 @@ export const getGroupedProducts = (intervals: string) => [ }, { id: 'Cloud SQL', - aggregation: aggregationFor(intervals, 10), + aggregation: aggregationFor(intervals, 750), }, { id: 'Cloud Spanner', @@ -491,7 +537,7 @@ export const getGroupedProducts = (intervals: string) => [ }, { id: 'Cloud Pub/Sub', - aggregation: aggregationFor(intervals, 30), + aggregation: aggregationFor(intervals, 1_000), }, { id: 'Cloud Bigtable',