From 7c95b97f78d9e9a8b731534f88feee4bd7ce33dd Mon Sep 17 00:00:00 2001 From: MasterHW <43460021+MasterHW@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:34:01 -0400 Subject: [PATCH] update OSO endpoint and queries (#3672) * update OSO endpoint and queries * fix fetch conditions * rm unused var * improve data typing --- .../grant-explorer/src/features/api/oso.ts | 153 +++++------------- .../src/features/round/OSO/ImpactStats.tsx | 26 +-- 2 files changed, 45 insertions(+), 134 deletions(-) diff --git a/packages/grant-explorer/src/features/api/oso.ts b/packages/grant-explorer/src/features/api/oso.ts index b11a961d7..ee849f282 100644 --- a/packages/grant-explorer/src/features/api/oso.ts +++ b/packages/grant-explorer/src/features/api/oso.ts @@ -4,148 +4,70 @@ import { Hex } from "viem"; import { gql, GraphQLClient } from "graphql-request"; const osoApiKey = process.env.REACT_APP_OSO_API_KEY as string; -const osoUrl = "https://opensource-observer.hasura.app/v1/graphql"; +const osoUrl = "https://www.opensource.observer/api/v1/graphql"; const graphQLClient = new GraphQLClient(osoUrl, { headers: { authorization: `Bearer ${osoApiKey}`, }, }); -let hasFetched = false; - -interface IOSOId { - projects_v1: { - project_id: Hex; - }; -} +let fetchedProject = ""; export interface IOSOStats { - code_metrics_by_project_v1: { - contributor_count: number; - first_commit_date: number; - }; - events_monthly_to_project: [ - { - bucket_month: number; - amount: number; - }, - { - bucket_month: number; - amount: number; - }, - { - bucket_month: number; - amount: number; - }, - { - bucket_month: number; - amount: number; - }, - { - bucket_month: number; - amount: number; - }, + oso_codeMetricsByProjectV1: [ { - bucket_month: number; - amount: number; + contributorCount: number; + firstCommitDate: number; + activeDeveloperCount6Months: number; }, ]; } export function useOSO(projectGithub?: string) { const emptyReturn: IOSOStats = { - code_metrics_by_project_v1: { - contributor_count: 0, - first_commit_date: 0, - }, - events_monthly_to_project: [ - { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, - }, + oso_codeMetricsByProjectV1: [ { - bucket_month: 0, - amount: 0, - }, - { - bucket_month: 0, - amount: 0, + contributorCount: 0, + firstCommitDate: 0, + activeDeveloperCount6Months: 0, }, ], }; const [stats, setStats] = useState(null); const getStatsFor = async (projectRegistryGithub: string) => { + fetchedProject = projectRegistryGithub; if (osoApiKey === "") throw new Error("OpenSourceObserver API key not set."); - const queryId = gql`{ - projects_v1(where: {display_name: {_ilike: "${projectRegistryGithub}"}} - distinct_on: project_id - ) { - project_id + const queryVars = { + where: { + displayName: { + _ilike: `${projectRegistryGithub}`, + }, + }, + }; + const queryStats = gql` + query myQuery($where: Oso_CodeMetricsByProjectV1BoolExp) { + oso_codeMetricsByProjectV1(where: $where) { + contributorCount + firstCommitDate + activeDeveloperCount6Months + } } - }`; + `; try { - hasFetched = true; - const idData: IOSOId = await graphQLClient.request(queryId); + const items: IOSOStats = await graphQLClient.request( + queryStats, + queryVars + ); - if (!Array.isArray(idData.projects_v1)) { - setStats(emptyReturn); - return; + if (!items.oso_codeMetricsByProjectV1?.length) { + throw new Error("no stats returned"); } - - const parsedId: IOSOId = { - projects_v1: idData.projects_v1[0], + const parsedItems: IOSOStats = { + oso_codeMetricsByProjectV1: items.oso_codeMetricsByProjectV1, }; - - const queryStats = gql`{ - code_metrics_by_project_v1(where: {project_id: {_eq: "${parsedId.projects_v1.project_id}"}}) { - contributor_count - first_commit_date - } - events_monthly_to_project( - where: {project_id: {_eq: "${parsedId.projects_v1.project_id}"}, event_type: {_eq: "COMMIT_CODE"}} - limit: 6 - order_by: {bucket_month: desc} - ) { - bucket_month - amount - } - }`; - - const items: IOSOStats = - await graphQLClient.request(queryStats); - - if (!Array.isArray(items.code_metrics_by_project_v1)) { - setStats(emptyReturn); - return; - } - - if (items.events_monthly_to_project.length === 6) { - const parsedItems: IOSOStats = { - code_metrics_by_project_v1: items.code_metrics_by_project_v1[0], - events_monthly_to_project: items.events_monthly_to_project, - }; - setStats(parsedItems); - } else { - const parsedItems: IOSOStats = { - code_metrics_by_project_v1: items.code_metrics_by_project_v1[0], - events_monthly_to_project: emptyReturn.events_monthly_to_project, - }; - setStats(parsedItems); - } + setStats(parsedItems); } catch (e) { console.error(`No stats found for project: ${projectGithub}`); console.error(e); @@ -158,8 +80,9 @@ export function useOSO(projectGithub?: string) { revalidateOnMount: true, }); - if (stats === null && !hasFetched) - projectGithub && getStatsFor(projectGithub); + if (fetchedProject !== projectGithub) + // check if currently loaded stats are for viewed project + projectGithub && getStatsFor(projectGithub); // fetch if not return { /** * Fetch OSO for stats on a project diff --git a/packages/grant-explorer/src/features/round/OSO/ImpactStats.tsx b/packages/grant-explorer/src/features/round/OSO/ImpactStats.tsx index 4bd8e20e6..f4d932209 100644 --- a/packages/grant-explorer/src/features/round/OSO/ImpactStats.tsx +++ b/packages/grant-explorer/src/features/round/OSO/ImpactStats.tsx @@ -6,7 +6,7 @@ import { formatTimeAgo } from "../../common/utils/utils"; export const StatList = ({ stats }: { stats: IOSOStats | null }) => { if (stats === null) return; - return stats.code_metrics_by_project_v1.contributor_count > 0 ? ( + return stats.oso_codeMetricsByProjectV1[0].contributorCount > 0 ? (

Impact stats

@@ -19,7 +19,7 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => { {" "} Project age @@ -32,7 +32,7 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => { > Unique code contributors @@ -42,8 +42,8 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => { "rounded-2xl bg-gray-50 flex-auto p-3 md:p-6 gap-4 flex flex-col" } > - - Velocity + + Active devs @@ -66,18 +66,6 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => { ); }; -function projectVelocity(stats: IOSOStats) { - const recentCommits = - stats.events_monthly_to_project[0].amount + - stats.events_monthly_to_project[1].amount + - stats.events_monthly_to_project[2].amount; - const olderCommits = - stats.events_monthly_to_project[3].amount + - stats.events_monthly_to_project[4].amount + - stats.events_monthly_to_project[5].amount; - - if (recentCommits === 0 && olderCommits === 0) return "unknown"; - if (recentCommits >= 1.5 * olderCommits) return "increasing"; - if (recentCommits <= 0.5 * olderCommits) return "decreasing"; - return "steady"; +function projectDevs(stats: IOSOStats) { + return stats.oso_codeMetricsByProjectV1[0].activeDeveloperCount6Months; }