Skip to content

Commit

Permalink
chore: more updates to charts data
Browse files Browse the repository at this point in the history
  • Loading branch information
OgDev-01 committed Jan 3, 2024
1 parent 10f6961 commit 1c5e78e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 79 deletions.
62 changes: 27 additions & 35 deletions hooks/useContributorData.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
import useSWR from "swr";
import { useCallback } from "react";
interface PaginatedDataResponse {
readonly data: DBContributors[];
readonly data: DBContributorsPR[];
readonly meta: Meta;
}

type Response =
| { type: "loading" }
| { type: "error"; error: Error }
// Todo: figure out meta
| { type: "result"; data: DBContributors[]; meta: { itemCount: number } };

// We're not currently using this, we're just using useSWR directly inside ChildWithSWR
// this needs useCallback wrap if we want to use it in the other component
const useContributorData = () => {
useCallback(async (owner: string, repo: string): Promise<Response> => {
// useSWR is probably going to be a sticking point
// eslint-disable-next-line react-hooks/rules-of-hooks
const { data, error, mutate } = useSWR<PaginatedDataResponse, Error>(
`repos/${owner}/${repo}/contributions`
);

if (!error && !data) {
return { type: "loading" };
}

if (error) {
return {
type: "error",
error: error
};
}

return {
type: "result",
data: data?.data ?? [],
meta: data?.meta ?? { itemCount: 0 }
// commenting for now to appease build
// mutate
};
}, []);
const useContributorData = (repo: string, startDate?: number, status?: "closed" | "open") => {
const query = new URLSearchParams();

if (startDate) {
query.set("prev_days_start_date", `${startDate}`);
}
if (status) {
query.set("status", `${status}`);
}
query.set("repo", `${repo}`);

query.set("limit", "100");

const baseEndpoint = "prs/search";

const endpointString = `${baseEndpoint}?${query.toString()}`;

const { data, error, mutate } = useSWR<PaginatedDataResponse, Error>(repo ? endpointString : null);

return {
data: data?.data ?? [],
isLoading: !data && !error,
isError: Object.keys(error ?? {}).length > 0,
meta: data?.meta ?? { itemCount: 0 },
mutate,
};
};
// good catch []

export { useContributorData };
40 changes: 24 additions & 16 deletions pages/[owner]/[repo]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { FaArrowTrendUp, FaArrowTrendDown } from "react-icons/fa6";

const CIResponsiveLine = dynamic(() => import("./CIResponsiveLine"), { ssr: false });

import prCounts from "./prCounts";
import prPerDay from "./prCounts";
import { useContributorData } from "hooks/useContributorData";

export interface PaginatedDataResponse {
readonly data: DBContributorsPR[];
Expand All @@ -24,19 +25,26 @@ interface StatsCardProps {
}
const ChildWithSWR = (props: { owner: string; repo: string }) => {
const { owner, repo } = props;
const { data, error, mutate } = useSWR<PaginatedDataResponse, Error>(`prs/search?repo=${owner}%2F${repo}`);

const { data: openedPrs } = useContributorData(`${owner}/${repo}`, 0, "open");
const { data: prevMonthOpenedPrs, meta: prevMonthOpenedPrsMeta } = useContributorData(`${owner}/${repo}`, 30, "open");

const { data: closedPrs } = useContributorData(`${owner}/${repo}`, 0, "closed");
const { data: prevMonthClosedPrs, meta: prevMonthClosedPrsMeta } = useContributorData(
`${owner}/${repo}`,
30,
"closed"
);

const { data: currentData, isError, isLoading, meta } = useContributorData(`${owner}/${repo}`);

// fetch previous months data seperately to compare
const {
data: prevMonthData,
error: prevMonthError,
mutate: prevMonthMutate,
} = useSWR<PaginatedDataResponse, Error>(`prs/search?repo=${owner}%2F${repo}&prev_days_start_date=30`);

if (!data && !error) {
if (isLoading) {
return <>Loading...</>;
}

const chartData = prCounts(data!.data);
const chartData = prPerDay(openedPrs, closedPrs);

const getPercentageChange = (prevCount: number, currentCount: number) => {
const percentageChange = ((currentCount - prevCount) / prevCount) * 100;
Expand Down Expand Up @@ -93,24 +101,24 @@ const ChildWithSWR = (props: { owner: string; repo: string }) => {
<StatsCard
type="pr"
status="open"
count={chartData.meta.totalCount}
prevMonthCount={prevMonthData ? prCounts(prevMonthData!.data).meta.totalCount : undefined}
count={meta.itemCount}
prevMonthCount={prevMonthOpenedPrs ? prevMonthOpenedPrsMeta.itemCount : undefined}
/>
<StatsCard
type="pr"
status="merged"
count={chartData.meta.mergedCount}
prevMonthCount={prevMonthData ? prCounts(prevMonthData!.data).meta.mergedCount : undefined}
count={closedPrs.filter((pr) => pr.pr_is_merged === true).length}
prevMonthCount={prevMonthClosedPrs ? prevMonthClosedPrsMeta.itemCount : undefined}
/>
<StatsCard
type="pr"
status="closed"
count={chartData.meta.closedCount}
prevMonthCount={prevMonthData ? prCounts(prevMonthData!.data).meta.closedCount : undefined}
count={closedPrs.filter((item) => item.pr_state === "close" && !item.pr_is_merged).length}
prevMonthCount={prevMonthClosedPrsMeta ? prevMonthClosedPrsMeta.itemCount : undefined}
/>
</div>

<CIResponsiveLine data={chartData.prsPerDay} />
<CIResponsiveLine data={chartData} />

<div className="flex justify-center gap-4 flex-wrap"></div>
</div>
Expand Down
47 changes: 19 additions & 28 deletions pages/[owner]/[repo]/prCounts.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import { PaginatedDataResponse } from ".";

const prCounts = (prData: DBContributorsPR[]) => {
const meta = count(prData);
const prsPerDay = prPerDay(prData);

return {
meta,
prsPerDay,
};
};

const count = (prData: DBContributorsPR[]): { mergedCount: number; closedCount: number; totalCount: number } => {
const mergedCount = prData.filter((item) => item.pr_is_merged).length; // Merged PRs
const closedCount = prData.filter((item) => item.pr_state === "closed").length; // Closed PRs
Expand All @@ -23,17 +11,24 @@ const count = (prData: DBContributorsPR[]): { mergedCount: number; closedCount:
};
};

const prPerDay = (prData: DBContributorsPR[]) => {
const sortedPRs = prData.sort((a, b) => {
const prPerDay = (open: DBContributorsPR[], closed: DBContributorsPR[]) => {
const sortedMergedPRs = closed.sort((a, b) => {
const aDate = new Date(a.pr_created_at);
const bDate = new Date(b.pr_created_at);

return aDate.getTime() - bDate.getTime();
});

const sortedOpenedPRs = open.sort((a, b) => {
const aDate = new Date(a.pr_created_at);
const bDate = new Date(b.pr_created_at);

return aDate.getTime() - bDate.getTime();
});
const mergedPRsPerDay = sortedPRs.reduce<Record<string, number>>((acc, item) => {
const mergedPRsPerDay = sortedMergedPRs.reduce<Record<string, number>>((acc, item) => {
const mergedDate = new Date(item.pr_merged_at).toLocaleDateString();

if (item.pr_is_merged && item.pr_merged_at !== "0001-01-01T00:00:00.000Z") {
if (item.pr_is_merged) {
if (!acc[mergedDate]) {
acc[mergedDate] = 0;
}
Expand All @@ -43,10 +38,10 @@ const prPerDay = (prData: DBContributorsPR[]) => {
return acc;
}, {});

const closedPRsPerDay = sortedPRs.reduce<Record<string, number>>((acc, item) => {
const closedPRsPerDay = sortedMergedPRs.reduce<Record<string, number>>((acc, item) => {
const closedDate = new Date(item.pr_updated_at).toLocaleDateString();

if (item.pr_state === "closed") {
if (item.pr_is_merged === false) {
if (!acc[closedDate]) {
acc[closedDate] = 0;
}
Expand All @@ -56,15 +51,13 @@ const prPerDay = (prData: DBContributorsPR[]) => {
return acc;
}, {});

const openedPRsPerDay = sortedPRs.reduce<Record<string, number>>((acc, item) => {
const openedPRsPerDay = sortedOpenedPRs.reduce<Record<string, number>>((acc, item) => {
const openedDate = new Date(item.pr_created_at).toLocaleDateString();

if (item.pr_state === "open") {
if (!acc[openedDate]) {
acc[openedDate] = 0;
}
acc[openedDate]++;
if (!acc[openedDate]) {
acc[openedDate] = 0;
}
acc[openedDate]++;

return acc;
}, {});
Expand All @@ -74,16 +67,14 @@ const prPerDay = (prData: DBContributorsPR[]) => {

const mergedPrs = Object.entries(mergedPRsPerDay).map(([x, y]) => ({ x, y }));

console.log("Merged PRs per day:", mergedPrs);

return [
{
id: "Opened PRs",
color: "#10b981",
data: openedPRs,
},
{
id: "Pull Requests",
id: "Merged PRs",
color: "#f59e0b",
data: mergedPrs,
},
Expand All @@ -95,4 +86,4 @@ const prPerDay = (prData: DBContributorsPR[]) => {
];
};

export default prCounts;
export default prPerDay;

0 comments on commit 1c5e78e

Please sign in to comment.