Move mock data utils
This commit is contained in:
@@ -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<T> = (alert: T) => T;
|
||||
type mockEntityRenderer<T> = (entity: T) => T;
|
||||
|
||||
type IntervalFields = {
|
||||
duration: Duration;
|
||||
endDate: string;
|
||||
};
|
||||
|
||||
export const createMockEntity = (
|
||||
callback?: mockEntityRenderer<Entity>,
|
||||
): 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(
|
||||
/\/(?<duration>P\d+[DM])\/(?<date>\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',
|
||||
|
||||
Reference in New Issue
Block a user