-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from HackAtUCI/feature/applicant-summary
Add Applicant Status Summary to Admin Dashboard
- Loading branch information
Showing
8 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from collections import Counter | ||
|
||
from pydantic import BaseModel, TypeAdapter | ||
|
||
from services import mongodb_handler | ||
from services.mongodb_handler import Collection | ||
from utils.user_record import ApplicantStatus, Role | ||
|
||
|
||
class ApplicantSummaryRecord(BaseModel): | ||
status: ApplicantStatus | ||
|
||
|
||
async def applicant_summary() -> Counter[ApplicantStatus]: | ||
"""Get summary of applicants by status.""" | ||
records = await mongodb_handler.retrieve( | ||
Collection.USERS, | ||
{"role": Role.APPLICANT}, | ||
["status"], | ||
) | ||
applicants = TypeAdapter(list[ApplicantSummaryRecord]).validate_python(records) | ||
|
||
return Counter(applicant.status for applicant in applicants) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from unittest.mock import AsyncMock, patch | ||
|
||
from admin.summary_handler import applicant_summary | ||
|
||
|
||
@patch("services.mongodb_handler.retrieve", autospec=True) | ||
async def test_applicant_summary(mock_mongodb_handler_retrieve: AsyncMock) -> None: | ||
"""Test applicant summary counts by status.""" | ||
mock_mongodb_handler_retrieve.return_value = ( | ||
[{"status": "ACCEPTED"}, {"status": "REJECTED"}] * 20 | ||
+ [{"status": "CONFIRMED"}] * 24 | ||
+ [{"status": "WAITLISTED"}, {"status": "WAIVER_SIGNED"}] * 3 | ||
) | ||
|
||
summary = await applicant_summary() | ||
mock_mongodb_handler_retrieve.assert_awaited_once() | ||
assert dict(summary) == { | ||
"REJECTED": 20, | ||
"WAITLISTED": 3, | ||
"ACCEPTED": 20, | ||
"WAIVER_SIGNED": 3, | ||
"CONFIRMED": 24, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
apps/site/src/app/admin/dashboard/components/ApplicantSummary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Box from "@cloudscape-design/components/box"; | ||
import Container from "@cloudscape-design/components/container"; | ||
import PieChart from "@cloudscape-design/components/pie-chart"; | ||
|
||
import { Status } from "@/lib/admin/useApplicant"; | ||
|
||
import useApplicantSummary from "./useApplicantSummary"; | ||
|
||
function ApplicantSummary() { | ||
const { summary, loading, error } = useApplicantSummary(); | ||
const totalApplicants = Object.values(summary).reduce((s, v) => s + v, 0); | ||
|
||
const orderedData = [ | ||
Status.rejected, | ||
Status.waitlisted, | ||
Status.accepted, | ||
Status.signed, | ||
Status.confirmed, | ||
Status.attending, | ||
Status.void, | ||
].map((status) => ({ | ||
title: status, | ||
value: summary[status] ?? 0, | ||
})); | ||
|
||
return ( | ||
<Container header={<Box variant="h2">Applicant Summary</Box>}> | ||
<PieChart | ||
data={orderedData} | ||
statusType={(loading && "loading") || (error && "error")} | ||
loadingText="Loading chart" | ||
hideFilter={true} | ||
segmentDescription={(datum, sum) => | ||
`${datum.value} applicants (${percentage(datum.value / sum)}%)` | ||
} | ||
ariaDescription="Donut chart showing summary of applicant statuses." | ||
ariaLabel="Donut chart" | ||
innerMetricDescription="total applicants" | ||
innerMetricValue={`${totalApplicants}`} | ||
size="large" | ||
variant="donut" | ||
empty={ | ||
<Box textAlign="center" color="inherit"> | ||
<b>No data available</b> | ||
<Box variant="p" color="inherit"> | ||
There is no data available | ||
</Box> | ||
</Box> | ||
} | ||
/> | ||
</Container> | ||
); | ||
} | ||
|
||
const percentage = (value: number): string => (value * 100).toFixed(0); | ||
|
||
export default ApplicantSummary; |
26 changes: 26 additions & 0 deletions
26
apps/site/src/app/admin/dashboard/components/useApplicantSummary.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import axios from "axios"; | ||
import useSWR from "swr"; | ||
|
||
import { Status } from "@/lib/admin/useApplicant"; | ||
|
||
type ApplicantSummary = Partial<Record<Status, number>>; | ||
|
||
const fetcher = async (url: string) => { | ||
const res = await axios.get<ApplicantSummary>(url); | ||
return res.data; | ||
}; | ||
|
||
function useApplicantSummary() { | ||
const { data, error, isLoading } = useSWR<ApplicantSummary>( | ||
"/api/admin/summary/applicants", | ||
fetcher, | ||
); | ||
|
||
return { | ||
summary: data ?? ({} as ApplicantSummary), | ||
loading: isLoading, | ||
error, | ||
}; | ||
} | ||
|
||
export default useApplicantSummary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters