Add getLastCompleteBillingDate to CostInsightsApi

Add useLastCompleteBillingDate hook
More usage of PropsWithChildren
This commit is contained in:
Tim Hansen
2020-10-22 19:22:29 -06:00
parent 628f782948
commit 169ed042ae
36 changed files with 542 additions and 266 deletions
@@ -23,6 +23,7 @@ import {
Cost,
CostInsightsApi,
DateAggregation,
DEFAULT_DATE_FORMAT,
Duration,
exclusiveEndDateOf,
Group,
@@ -38,18 +39,32 @@ import {
UnlabeledDataflowData,
} from '@backstage/plugin-cost-insights';
function durationOf(intervals: string): Duration {
const match = intervals.match(/\/(?<duration>P\d+[DM])\//);
const { duration } = match!.groups!;
return duration as Duration;
type IntervalFields = {
duration: Duration;
endDate: string;
};
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,
};
}
function aggregationFor(
duration: Duration,
intervals: string,
baseline: number,
): DateAggregation[] {
const days = dayjs(exclusiveEndDateOf(duration)).diff(
inclusiveStartDateOf(duration),
const { duration, endDate } = parseIntervals(intervals);
const days = dayjs(exclusiveEndDateOf(duration, endDate)).diff(
inclusiveStartDateOf(duration, endDate),
'day',
);
@@ -57,10 +72,10 @@ function aggregationFor(
(values: DateAggregation[], i: number): DateAggregation[] => {
const last = values.length ? values[values.length - 1].amount : baseline;
values.push({
date: dayjs(inclusiveStartDateOf(duration))
date: dayjs(inclusiveStartDateOf(duration, endDate))
.add(i, 'day')
.format('YYYY-MM-DD'),
amount: last + (baseline / 20) * (Math.random() * 2 - 1),
.format(DEFAULT_DATE_FORMAT),
amount: Math.max(0, last + (baseline / 20) * (Math.random() * 2 - 1)),
});
return values;
},
@@ -99,6 +114,12 @@ export class ExampleCostInsightsClient implements CostInsightsApi {
return new Promise(resolve => setTimeout(resolve, 0, res));
}
getLastCompleteBillingDate(): Promise<string> {
return Promise.resolve(
dayjs().subtract(1, 'day').format(DEFAULT_DATE_FORMAT),
);
}
async getUserGroups(userId: string): Promise<Group[]> {
const groups: Group[] = await this.request({ userId }, [
{ id: 'pied-piper' },
@@ -121,10 +142,10 @@ export class ExampleCostInsightsClient implements CostInsightsApi {
metric: string,
intervals: string,
): Promise<MetricData> {
const aggregation = aggregationFor(
durationOf(intervals),
100_000,
).map(entry => ({ ...entry, amount: Math.round(entry.amount) }));
const aggregation = aggregationFor(intervals, 100_000).map(entry => ({
...entry,
amount: Math.round(entry.amount),
}));
const cost: MetricData = await this.request(
{ metric, intervals },
@@ -140,7 +161,7 @@ export class ExampleCostInsightsClient implements CostInsightsApi {
}
async getGroupDailyCost(group: string, intervals: string): Promise<Cost> {
const aggregation = aggregationFor(durationOf(intervals), 8_000);
const aggregation = aggregationFor(intervals, 8_000);
const groupDailyCost: Cost = await this.request(
{ group, intervals },
{
@@ -154,7 +175,7 @@ export class ExampleCostInsightsClient implements CostInsightsApi {
}
async getProjectDailyCost(project: string, intervals: string): Promise<Cost> {
const aggregation = aggregationFor(durationOf(intervals), 1_500);
const aggregation = aggregationFor(intervals, 1_500);
const projectDailyCost: Cost = await this.request(
{ project, intervals },
{