diff --git a/apps/frontend/components/dataprovider/metrics-data-provider.tsx b/apps/frontend/components/dataprovider/metrics-data-provider.tsx index d262dd847..c469798a6 100644 --- a/apps/frontend/components/dataprovider/metrics-data-provider.tsx +++ b/apps/frontend/components/dataprovider/metrics-data-provider.tsx @@ -10,7 +10,8 @@ import { } from "@opensource-observer/utils"; import { GET_TIMESERIES_METRICS_BY_ARTIFACT, - //GET_TIMESERIES_METRICS_BY_PROJECT, + GET_TIMESERIES_METRICS_BY_PROJECT, + GET_TIMESERIES_METRICS_BY_COLLECTION, } from "../../lib/graphql/queries"; import { eventTimeToLabel } from "../../lib/parsing"; import { RegistrationProps } from "../../lib/types/plasmic"; @@ -25,7 +26,7 @@ import { useEnsureAuth } from "./apollo-wrapper"; // Types used in the Plasmic registration type ChartType = "areaChart" | "barList"; type XAxis = "date" | "entity" | "metric"; -type EntityType = "artifact" | "project"; +type EntityType = "artifact" | "project" | "collection"; // Ideal minimum number of data points in an area chart type BucketWidth = "day" | "week" | "month"; @@ -67,7 +68,7 @@ const MetricsDataProviderRegistration: RegistrationProps(["artifact", "project"]), + options: safeCast(["artifact", "project", "collection"]), }, entityIds: { type: "array", @@ -319,8 +320,9 @@ function MetricsDataProvider(props: MetricsDataProviderProps) { return props.entityType === "artifact" ? ( ) : props.entityType === "project" ? ( - // - <>{props.children} + + ) : props.entityType === "collection" ? ( + ) : ( assertNever(props.entityType) ); @@ -387,7 +389,6 @@ function ArtifactMetricsDataProvider(props: MetricsDataProviderProps) { ); } -/** function ProjectMetricsDataProvider(props: MetricsDataProviderProps) { useEnsureAuth(); const bucketWidth = getBucketWidth(props); @@ -448,7 +449,67 @@ function ProjectMetricsDataProvider(props: MetricsDataProviderProps) { /> ); } -*/ + +function CollectionMetricsDataProvider(props: MetricsDataProviderProps) { + useEnsureAuth(); + const bucketWidth = getBucketWidth(props); + const { + data: rawData, + error: dataError, + loading: dataLoading, + } = useQuery(GET_TIMESERIES_METRICS_BY_COLLECTION, { + variables: { + collectionIds: props.entityIds ?? [], + metricIds: props.metricIds ?? [], + startDate: eventTimeToLabel(props.startDate ?? DEFAULT_START_DATE), + endDate: eventTimeToLabel(props.endDate), + }, + }); + + const metricIdToName: Record = _.fromPairs( + (rawData?.oso_metricsV0 ?? []).map((x: any) => [ + ensure(x.metricId, "Missing metricId"), + ensure(x.metricName, "Missing metricName"), + ]), + ); + const entityIdToName: Record = _.fromPairs( + (rawData?.oso_collectionsV1 ?? []).map((x: any) => [ + ensure(x.collectionId, "Missing collectionId"), + ensure(x.collectionName, "Missing collectionName"), + ]), + ); + const normalizedData: EventData[] = ( + rawData?.oso_timeseriesMetricsByCollectionV0 ?? [] + ).map((x: any) => ({ + metricId: ensure(x.metricId, "Data missing 'metricId'"), + metricName: ensure( + metricIdToName[x.metricId], + "Data missing 'metricName'", + ), + entityId: ensure(x.projectId, "Data missing 'collectionId'"), + entityName: ensure( + entityIdToName[x.collectionId], + "Data missing 'collectionName'", + ), + date: ensure(x.sampleDate, "Data missing 'sampleDate'"), + amount: ensure(x.amount, "Data missing 'amount'"), + })); + const getEntityName = (id: string) => entityIdToName[id]; + const getMetricName = (id: string) => metricIdToName[id]; + const categories = createCategories(props, getEntityName, getMetricName); + const formattedData = formatData(props, normalizedData, categories, { + gapFill: bucketWidth === "day", + }); + !dataLoading && console.log(props, rawData, dataError, formattedData); + return ( + + ); +} export { MetricsDataProviderRegistration, MetricsDataProvider }; export type { MetricsDataProviderProps }; diff --git a/apps/frontend/lib/graphql/queries.ts b/apps/frontend/lib/graphql/queries.ts index 71bbaf3ef..81d37a95e 100644 --- a/apps/frontend/lib/graphql/queries.ts +++ b/apps/frontend/lib/graphql/queries.ts @@ -43,13 +43,12 @@ const GET_TIMESERIES_METRICS_BY_ARTIFACT = gql(` } `); -/** const GET_TIMESERIES_METRICS_BY_PROJECT = gql(` query TimeseriesMetricsByProject( $projectIds: [String!], $metricIds: [String!], - $startDate: Oso_DateTime!, - $endDate: Oso_DateTime!, + $startDate: Oso_Date!, + $endDate: Oso_Date!, ) { oso_timeseriesMetricsByProjectV0(where: { projectId: {_in: $projectIds}, @@ -80,9 +79,46 @@ const GET_TIMESERIES_METRICS_BY_PROJECT = gql(` } } `); - */ + +const GET_TIMESERIES_METRICS_BY_COLLECTION = gql(` + query TimeseriesMetricsByCollection( + $collectionIds: [String!], + $metricIds: [String!], + $startDate: Oso_Date!, + $endDate: Oso_Date!, + ) { + oso_timeseriesMetricsByCollectionV0(where: { + collectionId: {_in: $collectionIds}, + metricId: {_in: $metricIds}, + sampleDate: { _gte: $startDate, _lte: $endDate } + }) { + amount + metricId + collectionId + sampleDate + unit + } + oso_collectionsV1(where: { collectionId: { _in: $collectionIds }}) { + collectionId + collectionSource + collectionNamespace + collectionName + displayName + description + } + oso_metricsV0(where: {metricId: {_in: $metricIds}}) { + metricId + metricSource + metricNamespace + metricName + displayName + description + } + } +`); export { GET_TIMESERIES_METRICS_BY_ARTIFACT, - //GET_TIMESERIES_METRICS_BY_PROJECT, + GET_TIMESERIES_METRICS_BY_PROJECT, + GET_TIMESERIES_METRICS_BY_COLLECTION, };