Skip to content

Commit

Permalink
update OSO endpoint and queries (#3672)
Browse files Browse the repository at this point in the history
* update OSO endpoint and queries

* fix fetch conditions

* rm unused var

* improve data typing
  • Loading branch information
MasterHW authored Oct 8, 2024
1 parent 923c5d5 commit 7c95b97
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 134 deletions.
153 changes: 38 additions & 115 deletions packages/grant-explorer/src/features/api/oso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOSOStats | null>(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<IOSOId>(queryId);
const items: IOSOStats = await graphQLClient.request<IOSOStats>(
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<IOSOStats>(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);
Expand All @@ -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
Expand Down
26 changes: 7 additions & 19 deletions packages/grant-explorer/src/features/round/OSO/ImpactStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? (
<React.Fragment>
<h4 className="text-3xl mt-5 ml-4">Impact stats</h4>
<Flex gap={2} flexDir={{ base: "column", md: "row" }} py={6} px={3}>
Expand All @@ -19,7 +19,7 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => {
{" "}
<Stat
isLoading={false}
value={`${formatTimeAgo(stats.code_metrics_by_project_v1.first_commit_date)}`}
value={`${formatTimeAgo(stats.oso_codeMetricsByProjectV1[0].firstCommitDate)}`}
>
Project age
</Stat>
Expand All @@ -32,7 +32,7 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => {
>
<Stat
isLoading={false}
value={`${stats.code_metrics_by_project_v1.contributor_count}`}
value={`${stats.oso_codeMetricsByProjectV1[0].contributorCount}`}
>
Unique code contributors
</Stat>
Expand All @@ -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"
}
>
<Stat isLoading={false} value={`${projectVelocity(stats)}`}>
Velocity
<Stat isLoading={false} value={`${projectDevs(stats)}`}>
Active devs
</Stat>
</div>
</Flex>
Expand All @@ -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;
}

0 comments on commit 7c95b97

Please sign in to comment.