Skip to content

Commit 7c95b97

Browse files
authored
update OSO endpoint and queries (#3672)
* update OSO endpoint and queries * fix fetch conditions * rm unused var * improve data typing
1 parent 923c5d5 commit 7c95b97

File tree

2 files changed

+45
-134
lines changed

2 files changed

+45
-134
lines changed

packages/grant-explorer/src/features/api/oso.ts

Lines changed: 38 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -4,148 +4,70 @@ import { Hex } from "viem";
44
import { gql, GraphQLClient } from "graphql-request";
55

66
const osoApiKey = process.env.REACT_APP_OSO_API_KEY as string;
7-
const osoUrl = "https://opensource-observer.hasura.app/v1/graphql";
7+
const osoUrl = "https://www.opensource.observer/api/v1/graphql";
88
const graphQLClient = new GraphQLClient(osoUrl, {
99
headers: {
1010
authorization: `Bearer ${osoApiKey}`,
1111
},
1212
});
13-
let hasFetched = false;
14-
15-
interface IOSOId {
16-
projects_v1: {
17-
project_id: Hex;
18-
};
19-
}
13+
let fetchedProject = "";
2014

2115
export interface IOSOStats {
22-
code_metrics_by_project_v1: {
23-
contributor_count: number;
24-
first_commit_date: number;
25-
};
26-
events_monthly_to_project: [
27-
{
28-
bucket_month: number;
29-
amount: number;
30-
},
31-
{
32-
bucket_month: number;
33-
amount: number;
34-
},
35-
{
36-
bucket_month: number;
37-
amount: number;
38-
},
39-
{
40-
bucket_month: number;
41-
amount: number;
42-
},
43-
{
44-
bucket_month: number;
45-
amount: number;
46-
},
16+
oso_codeMetricsByProjectV1: [
4717
{
48-
bucket_month: number;
49-
amount: number;
18+
contributorCount: number;
19+
firstCommitDate: number;
20+
activeDeveloperCount6Months: number;
5021
},
5122
];
5223
}
5324

5425
export function useOSO(projectGithub?: string) {
5526
const emptyReturn: IOSOStats = {
56-
code_metrics_by_project_v1: {
57-
contributor_count: 0,
58-
first_commit_date: 0,
59-
},
60-
events_monthly_to_project: [
61-
{
62-
bucket_month: 0,
63-
amount: 0,
64-
},
65-
{
66-
bucket_month: 0,
67-
amount: 0,
68-
},
69-
{
70-
bucket_month: 0,
71-
amount: 0,
72-
},
73-
{
74-
bucket_month: 0,
75-
amount: 0,
76-
},
27+
oso_codeMetricsByProjectV1: [
7728
{
78-
bucket_month: 0,
79-
amount: 0,
80-
},
81-
{
82-
bucket_month: 0,
83-
amount: 0,
29+
contributorCount: 0,
30+
firstCommitDate: 0,
31+
activeDeveloperCount6Months: 0,
8432
},
8533
],
8634
};
8735
const [stats, setStats] = useState<IOSOStats | null>(null);
8836

8937
const getStatsFor = async (projectRegistryGithub: string) => {
38+
fetchedProject = projectRegistryGithub;
9039
if (osoApiKey === "")
9140
throw new Error("OpenSourceObserver API key not set.");
92-
const queryId = gql`{
93-
projects_v1(where: {display_name: {_ilike: "${projectRegistryGithub}"}}
94-
distinct_on: project_id
95-
) {
96-
project_id
41+
const queryVars = {
42+
where: {
43+
displayName: {
44+
_ilike: `${projectRegistryGithub}`,
45+
},
46+
},
47+
};
48+
const queryStats = gql`
49+
query myQuery($where: Oso_CodeMetricsByProjectV1BoolExp) {
50+
oso_codeMetricsByProjectV1(where: $where) {
51+
contributorCount
52+
firstCommitDate
53+
activeDeveloperCount6Months
54+
}
9755
}
98-
}`;
56+
`;
9957

10058
try {
101-
hasFetched = true;
102-
const idData: IOSOId = await graphQLClient.request<IOSOId>(queryId);
59+
const items: IOSOStats = await graphQLClient.request<IOSOStats>(
60+
queryStats,
61+
queryVars
62+
);
10363

104-
if (!Array.isArray(idData.projects_v1)) {
105-
setStats(emptyReturn);
106-
return;
64+
if (!items.oso_codeMetricsByProjectV1?.length) {
65+
throw new Error("no stats returned");
10766
}
108-
109-
const parsedId: IOSOId = {
110-
projects_v1: idData.projects_v1[0],
67+
const parsedItems: IOSOStats = {
68+
oso_codeMetricsByProjectV1: items.oso_codeMetricsByProjectV1,
11169
};
112-
113-
const queryStats = gql`{
114-
code_metrics_by_project_v1(where: {project_id: {_eq: "${parsedId.projects_v1.project_id}"}}) {
115-
contributor_count
116-
first_commit_date
117-
}
118-
events_monthly_to_project(
119-
where: {project_id: {_eq: "${parsedId.projects_v1.project_id}"}, event_type: {_eq: "COMMIT_CODE"}}
120-
limit: 6
121-
order_by: {bucket_month: desc}
122-
) {
123-
bucket_month
124-
amount
125-
}
126-
}`;
127-
128-
const items: IOSOStats =
129-
await graphQLClient.request<IOSOStats>(queryStats);
130-
131-
if (!Array.isArray(items.code_metrics_by_project_v1)) {
132-
setStats(emptyReturn);
133-
return;
134-
}
135-
136-
if (items.events_monthly_to_project.length === 6) {
137-
const parsedItems: IOSOStats = {
138-
code_metrics_by_project_v1: items.code_metrics_by_project_v1[0],
139-
events_monthly_to_project: items.events_monthly_to_project,
140-
};
141-
setStats(parsedItems);
142-
} else {
143-
const parsedItems: IOSOStats = {
144-
code_metrics_by_project_v1: items.code_metrics_by_project_v1[0],
145-
events_monthly_to_project: emptyReturn.events_monthly_to_project,
146-
};
147-
setStats(parsedItems);
148-
}
70+
setStats(parsedItems);
14971
} catch (e) {
15072
console.error(`No stats found for project: ${projectGithub}`);
15173
console.error(e);
@@ -158,8 +80,9 @@ export function useOSO(projectGithub?: string) {
15880
revalidateOnMount: true,
15981
});
16082

161-
if (stats === null && !hasFetched)
162-
projectGithub && getStatsFor(projectGithub);
83+
if (fetchedProject !== projectGithub)
84+
// check if currently loaded stats are for viewed project
85+
projectGithub && getStatsFor(projectGithub); // fetch if not
16386
return {
16487
/**
16588
* Fetch OSO for stats on a project

packages/grant-explorer/src/features/round/OSO/ImpactStats.tsx

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { formatTimeAgo } from "../../common/utils/utils";
66

77
export const StatList = ({ stats }: { stats: IOSOStats | null }) => {
88
if (stats === null) return;
9-
return stats.code_metrics_by_project_v1.contributor_count > 0 ? (
9+
return stats.oso_codeMetricsByProjectV1[0].contributorCount > 0 ? (
1010
<React.Fragment>
1111
<h4 className="text-3xl mt-5 ml-4">Impact stats</h4>
1212
<Flex gap={2} flexDir={{ base: "column", md: "row" }} py={6} px={3}>
@@ -19,7 +19,7 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => {
1919
{" "}
2020
<Stat
2121
isLoading={false}
22-
value={`${formatTimeAgo(stats.code_metrics_by_project_v1.first_commit_date)}`}
22+
value={`${formatTimeAgo(stats.oso_codeMetricsByProjectV1[0].firstCommitDate)}`}
2323
>
2424
Project age
2525
</Stat>
@@ -32,7 +32,7 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => {
3232
>
3333
<Stat
3434
isLoading={false}
35-
value={`${stats.code_metrics_by_project_v1.contributor_count}`}
35+
value={`${stats.oso_codeMetricsByProjectV1[0].contributorCount}`}
3636
>
3737
Unique code contributors
3838
</Stat>
@@ -42,8 +42,8 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => {
4242
"rounded-2xl bg-gray-50 flex-auto p-3 md:p-6 gap-4 flex flex-col"
4343
}
4444
>
45-
<Stat isLoading={false} value={`${projectVelocity(stats)}`}>
46-
Velocity
45+
<Stat isLoading={false} value={`${projectDevs(stats)}`}>
46+
Active devs
4747
</Stat>
4848
</div>
4949
</Flex>
@@ -66,18 +66,6 @@ export const StatList = ({ stats }: { stats: IOSOStats | null }) => {
6666
);
6767
};
6868

69-
function projectVelocity(stats: IOSOStats) {
70-
const recentCommits =
71-
stats.events_monthly_to_project[0].amount +
72-
stats.events_monthly_to_project[1].amount +
73-
stats.events_monthly_to_project[2].amount;
74-
const olderCommits =
75-
stats.events_monthly_to_project[3].amount +
76-
stats.events_monthly_to_project[4].amount +
77-
stats.events_monthly_to_project[5].amount;
78-
79-
if (recentCommits === 0 && olderCommits === 0) return "unknown";
80-
if (recentCommits >= 1.5 * olderCommits) return "increasing";
81-
if (recentCommits <= 0.5 * olderCommits) return "decreasing";
82-
return "steady";
69+
function projectDevs(stats: IOSOStats) {
70+
return stats.oso_codeMetricsByProjectV1[0].activeDeveloperCount6Months;
8371
}

0 commit comments

Comments
 (0)