From bc4353ca19ef075f0439ee311b48bf43ae9cf1ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Tue, 8 Nov 2022 23:22:13 +0100 Subject: [PATCH] Use reference instead of plain entities in the CostInsightsApi MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kévin Gomez --- plugins/cost-insights/api-report.md | 12 ++++++++++++ plugins/cost-insights/src/api/CostInsightsApi.ts | 11 +++++++---- .../src/components/EntityCosts/EntityCost.test.tsx | 2 +- .../src/components/EntityCosts/EntityCosts.tsx | 9 +++++---- plugins/cost-insights/src/example/client.ts | 7 +++---- .../src/example/templates/CostInsightsClient.ts | 11 +++++++++++ 6 files changed, 39 insertions(+), 13 deletions(-) diff --git a/plugins/cost-insights/api-report.md b/plugins/cost-insights/api-report.md index f50f0b0c81..dd4e404450 100644 --- a/plugins/cost-insights/api-report.md +++ b/plugins/cost-insights/api-report.md @@ -267,6 +267,10 @@ export type CostInsightsApi = { getLastCompleteBillingDate(): Promise; getUserGroups(userId: string): Promise; getGroupProjects(group: string): Promise; + getCatalogEntityDailyCost?( + catalogEntityRef: string, + intervals: string, + ): Promise; getGroupDailyCost(group: string, intervals: string): Promise; getProjectDailyCost(project: string, intervals: string): Promise; getDailyMetricData(metric: string, intervals: string): Promise; @@ -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; // (undocumented) + getCatalogEntityDailyCost( + entityRef: string, + intervals: string, + ): Promise; + // (undocumented) getDailyMetricData(metric: string, intervals: string): Promise; // (undocumented) getGroupDailyCost(group: string, intervals: string): Promise; diff --git a/plugins/cost-insights/src/api/CostInsightsApi.ts b/plugins/cost-insights/src/api/CostInsightsApi.ts index ffc2314435..a7065a04aa 100644 --- a/plugins/cost-insights/src/api/CostInsightsApi.ts +++ b/plugins/cost-insights/src/api/CostInsightsApi.ts @@ -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; /** - * 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; + getCatalogEntityDailyCost?( + catalogEntityRef: string, + intervals: string, + ): Promise; /** * Get daily cost aggregations for a given group and interval time frame. diff --git a/plugins/cost-insights/src/components/EntityCosts/EntityCost.test.tsx b/plugins/cost-insights/src/components/EntityCosts/EntityCost.test.tsx index 395480c6df..53e1a13ebf 100644 --- a/plugins/cost-insights/src/components/EntityCosts/EntityCost.test.tsx +++ b/plugins/cost-insights/src/components/EntityCosts/EntityCost.test.tsx @@ -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({}), diff --git a/plugins/cost-insights/src/components/EntityCosts/EntityCosts.tsx b/plugins/cost-insights/src/components/EntityCosts/EntityCosts.tsx index 9fdcb9c98b..9dda1c65e3 100644 --- a/plugins/cost-insights/src/components/EntityCosts/EntityCosts.tsx +++ b/plugins/cost-insights/src/components/EntityCosts/EntityCosts.tsx @@ -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 ( ); } diff --git a/plugins/cost-insights/src/example/client.ts b/plugins/cost-insights/src/example/client.ts index a1401a06a2..e2a183b682 100644 --- a/plugins/cost-insights/src/example/client.ts +++ b/plugins/cost-insights/src/example/client.ts @@ -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 { const aggregation = aggregationFor(intervals, 8_000); const groupDailyCost: Cost = await this.request( - { entity, intervals }, + { entityRef, intervals }, { aggregation: aggregation, change: changeOf(aggregation), diff --git a/plugins/cost-insights/src/example/templates/CostInsightsClient.ts b/plugins/cost-insights/src/example/templates/CostInsightsClient.ts index fc68eb62c7..50c3bb1470 100644 --- a/plugins/cost-insights/src/example/templates/CostInsightsClient.ts +++ b/plugins/cost-insights/src/example/templates/CostInsightsClient.ts @@ -87,6 +87,17 @@ export class CostInsightsClient implements CostInsightsApi { } } + async getCatalogEntityDailyCost(catalogEntityRef: string, intervals: string): Promise { + return { + id: 'remove-me', + aggregation: [], + change: { + ratio: 0, + amount: 0 + } + } + } + async getProductInsights(options: ProductInsightsOptions): Promise { return { id: 'remove-me',