Skip to content

Commit

Permalink
feat: MetricsDataProvider supports projects and collections (#2227)
Browse files Browse the repository at this point in the history
* Copying logic from the metrics provider for artifacts
  • Loading branch information
ryscheng authored Sep 26, 2024
1 parent ad07e07 commit 689b4aa
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 12 deletions.
75 changes: 68 additions & 7 deletions apps/frontend/components/dataprovider/metrics-data-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -67,7 +68,7 @@ const MetricsDataProviderRegistration: RegistrationProps<MetricsDataProviderProp
entityType: {
type: "choice",
helpText: "What kind of entity?",
options: safeCast<EntityType[]>(["artifact", "project"]),
options: safeCast<EntityType[]>(["artifact", "project", "collection"]),
},
entityIds: {
type: "array",
Expand Down Expand Up @@ -319,8 +320,9 @@ function MetricsDataProvider(props: MetricsDataProviderProps) {
return props.entityType === "artifact" ? (
<ArtifactMetricsDataProvider {...props} />
) : props.entityType === "project" ? (
//<ProjectMetricsDataProvider {...props} />
<>{props.children}</>
<ProjectMetricsDataProvider {...props} />
) : props.entityType === "collection" ? (
<CollectionMetricsDataProvider {...props} />
) : (
assertNever(props.entityType)
);
Expand Down Expand Up @@ -387,7 +389,6 @@ function ArtifactMetricsDataProvider(props: MetricsDataProviderProps) {
);
}

/**
function ProjectMetricsDataProvider(props: MetricsDataProviderProps) {
useEnsureAuth();
const bucketWidth = getBucketWidth(props);
Expand Down Expand Up @@ -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<string, string> = _.fromPairs(
(rawData?.oso_metricsV0 ?? []).map((x: any) => [
ensure<string>(x.metricId, "Missing metricId"),
ensure<string>(x.metricName, "Missing metricName"),
]),
);
const entityIdToName: Record<string, string> = _.fromPairs(
(rawData?.oso_collectionsV1 ?? []).map((x: any) => [
ensure<string>(x.collectionId, "Missing collectionId"),
ensure<string>(x.collectionName, "Missing collectionName"),
]),
);
const normalizedData: EventData[] = (
rawData?.oso_timeseriesMetricsByCollectionV0 ?? []
).map((x: any) => ({
metricId: ensure<string>(x.metricId, "Data missing 'metricId'"),
metricName: ensure<string>(
metricIdToName[x.metricId],
"Data missing 'metricName'",
),
entityId: ensure<string>(x.projectId, "Data missing 'collectionId'"),
entityName: ensure<string>(
entityIdToName[x.collectionId],
"Data missing 'collectionName'",
),
date: ensure<string>(x.sampleDate, "Data missing 'sampleDate'"),
amount: ensure<number>(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 (
<DataProviderView
{...props}
formattedData={formattedData}
loading={dataLoading}
error={dataError}
/>
);
}

export { MetricsDataProviderRegistration, MetricsDataProvider };
export type { MetricsDataProviderProps };
46 changes: 41 additions & 5 deletions apps/frontend/lib/graphql/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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,
};

0 comments on commit 689b4aa

Please sign in to comment.