Use reference instead of plain entities in the CostInsightsApi

Signed-off-by: Kévin Gomez <contact@kevingomez.fr>
This commit is contained in:
Kévin Gomez
2022-11-08 23:22:13 +01:00
parent 86ae4061d4
commit bc4353ca19
6 changed files with 39 additions and 13 deletions
+12
View File
@@ -267,6 +267,10 @@ export type CostInsightsApi = {
getLastCompleteBillingDate(): Promise<string>;
getUserGroups(userId: string): Promise<Group[]>;
getGroupProjects(group: string): Promise<Project[]>;
getCatalogEntityDailyCost?(
catalogEntityRef: string,
intervals: string,
): Promise<Cost>;
getGroupDailyCost(group: string, intervals: string): Promise<Cost>;
getProjectDailyCost(project: string, intervals: string): Promise<Cost>;
getDailyMetricData(metric: string, intervals: string): Promise<MetricData>;
@@ -404,11 +408,19 @@ export const EngineerThreshold = 0.5;
// @public @deprecated (undocumented)
export type Entity = common.Entity;
// @public
export const EntityCostInsightsContent: () => JSX.Element;
// @public (undocumented)
export class ExampleCostInsightsClient implements CostInsightsApi {
// (undocumented)
getAlerts(group: string): Promise<Alert[]>;
// (undocumented)
getCatalogEntityDailyCost(
entityRef: string,
intervals: string,
): Promise<Cost>;
// (undocumented)
getDailyMetricData(metric: string, intervals: string): Promise<MetricData>;
// (undocumented)
getGroupDailyCost(group: string, intervals: string): Promise<Cost>;
@@ -24,7 +24,6 @@ import {
MetricData,
} from '../types';
import { createApiRef } from '@backstage/core-plugin-api';
import { Entity as CatalogEntity } from '@backstage/catalog-model';
/** @public */
export type ProductInsightsOptions = {
@@ -79,7 +78,7 @@ export type CostInsightsApi = {
getGroupProjects(group: string): Promise<Project[]>;
/**
* Get daily cost aggregations for a given entity and interval time frame.
* Get daily cost aggregations for a given catalog entity and interval time frame.
*
* The return type includes an array of daily cost aggregations as well as statistics about the
* change in cost over the intervals. Calculating these statistics requires us to bucket costs
@@ -91,11 +90,15 @@ export type CostInsightsApi = {
*
* Note: implementing this is only required when using the `EntityCostInsightsContent` extension.
*
* @param entity - The catalog entity
* @param catalogEntityRef - A reference to the catalog entity, as described in
* https://backstage.io/docs/features/software-catalog/references
* @param intervals - An ISO 8601 repeating interval string, such as R2/P30D/2020-09-01
* https://en.wikipedia.org/wiki/ISO_8601#Repeating_intervals
*/
getEntityDailyCost?(entity: CatalogEntity, intervals: string): Promise<Cost>;
getCatalogEntityDailyCost?(
catalogEntityRef: string,
intervals: string,
): Promise<Cost>;
/**
* Get daily cost aggregations for a given group and interval time frame.
@@ -48,7 +48,7 @@ function renderInContext(children: JSX.Element) {
getLastCompleteBillingDate: jest.fn().mockResolvedValue('2022-10-30'),
getUserGroups: jest.fn().mockResolvedValue(['team-a']),
getGroupProjects: jest.fn().mockResolvedValue(['project-a', 'project-b']),
getEntityDailyCost: jest.fn().mockResolvedValue(mockGroupDailyCost),
getCatalogEntityDailyCost: jest.fn().mockResolvedValue(mockGroupDailyCost),
getGroupDailyCost: jest.fn().mockResolvedValue({}),
getProjectDailyCost: jest.fn().mockResolvedValue({}),
getDailyMetricData: jest.fn().mockResolvedValue({}),
@@ -37,6 +37,7 @@ import { costInsightsApiRef } from '../../api';
import { useApi } from '@backstage/core-plugin-api';
import { useEntity } from '@backstage/plugin-catalog-react';
import { Cost, Maybe } from '@backstage/plugin-cost-insights-common';
import { stringifyEntityRef } from '@backstage/catalog-model';
export const EntityCostsCard = () => {
const client = useApi(costInsightsApiRef);
@@ -79,8 +80,8 @@ export const EntityCostsCard = () => {
lastCompleteBillingDate,
);
const fetchedDailyCost = await client.getEntityDailyCost!(
entity,
const fetchedDailyCost = await client.getCatalogEntityDailyCost!(
stringifyEntityRef(entity),
intervals,
);
setDailyCost(fetchedDailyCost);
@@ -129,11 +130,11 @@ export const EntityCostsCard = () => {
export const EntityCosts = () => {
const client = useApi(costInsightsApiRef);
if (!client.getEntityDailyCost) {
if (!client.getCatalogEntityDailyCost) {
return (
<WarningPanel
title="Could display costs for entity"
message="The getEntityDailyCost() method is not implemented by the costInsightsApi."
message="The getCatalogEntityDailyCost() method is not implemented by the costInsightsApi."
/>
);
}
+3 -4
View File
@@ -38,7 +38,6 @@ import {
getGroupedProjects,
trendlineOf,
} from '../testUtils';
import { Entity as CatalogEntity } from '@backstage/catalog-model';
/** @public */
export class ExampleCostInsightsClient implements CostInsightsApi {
@@ -93,13 +92,13 @@ export class ExampleCostInsightsClient implements CostInsightsApi {
return cost;
}
async getEntityDailyCost(
entity: CatalogEntity,
async getCatalogEntityDailyCost(
entityRef: string,
intervals: string,
): Promise<Cost> {
const aggregation = aggregationFor(intervals, 8_000);
const groupDailyCost: Cost = await this.request(
{ entity, intervals },
{ entityRef, intervals },
{
aggregation: aggregation,
change: changeOf(aggregation),
@@ -87,6 +87,17 @@ export class CostInsightsClient implements CostInsightsApi {
}
}
async getCatalogEntityDailyCost(catalogEntityRef: string, intervals: string): Promise<Cost> {
return {
id: 'remove-me',
aggregation: [],
change: {
ratio: 0,
amount: 0
}
}
}
async getProductInsights(options: ProductInsightsOptions): Promise<Entity> {
return {
id: 'remove-me',