-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It is still raw, next PRs add styling and date filtering for only single year. ![image](https://github.com/user-attachments/assets/8cd4e74f-3ed4-4179-a193-a45a191c4933) --------- Co-authored-by: kwasniew <[email protected]>
- Loading branch information
Showing
6 changed files
with
157 additions
and
1 deletion.
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
90 changes: 90 additions & 0 deletions
90
frontend/src/component/project/Project/ProjectStatus/ProjectActivity.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,90 @@ | ||
import { useRequiredPathParam } from 'hooks/useRequiredPathParam'; | ||
import { useProjectStatus } from 'hooks/api/getters/useProjectStatus/useProjectStatus'; | ||
import ActivityCalendar, { type ThemeInput } from 'react-activity-calendar'; | ||
import type { ProjectActivitySchema } from '../../../../openapi'; | ||
import { styled, Tooltip } from '@mui/material'; | ||
|
||
const StyledContainer = styled('div')(({ theme }) => ({ | ||
gap: theme.spacing(1), | ||
})); | ||
|
||
type Output = { date: string; count: number; level: number }; | ||
|
||
export function transformData(inputData: ProjectActivitySchema): Output[] { | ||
const resultMap: Record<string, number> = {}; | ||
|
||
// Step 1: Count the occurrences of each date | ||
inputData.forEach((item) => { | ||
const formattedDate = new Date(item.date).toISOString().split('T')[0]; | ||
resultMap[formattedDate] = (resultMap[formattedDate] || 0) + 1; | ||
}); | ||
|
||
// Step 2: Get all counts, sort them, and find the cut-off values for percentiles | ||
const counts = Object.values(resultMap).sort((a, b) => a - b); | ||
|
||
const percentile = (percent: number) => { | ||
const index = Math.floor((percent / 100) * counts.length); | ||
return counts[index] || counts[counts.length - 1]; | ||
}; | ||
|
||
const thresholds = [ | ||
percentile(25), // 25th percentile | ||
percentile(50), // 50th percentile | ||
percentile(75), // 75th percentile | ||
percentile(100), // 100th percentile | ||
]; | ||
|
||
// Step 3: Assign a level based on the percentile thresholds | ||
const calculateLevel = (count: number): number => { | ||
if (count <= thresholds[0]) return 1; // 1-25% | ||
if (count <= thresholds[1]) return 2; // 26-50% | ||
if (count <= thresholds[2]) return 3; // 51-75% | ||
return 4; // 76-100% | ||
}; | ||
|
||
// Step 4: Convert the map back to an array and assign levels | ||
return Object.entries(resultMap) | ||
.map(([date, count]) => ({ | ||
date, | ||
count, | ||
level: calculateLevel(count), | ||
})) | ||
.reverse(); // Optional: reverse the order if needed | ||
} | ||
|
||
export const ProjectActivity = () => { | ||
const projectId = useRequiredPathParam('projectId'); | ||
const { data } = useProjectStatus(projectId); | ||
|
||
const explicitTheme: ThemeInput = { | ||
light: ['#f1f0fc', '#ceccfd', '#8982ff', '#6c65e5', '#615bc2'], | ||
dark: ['#f1f0fc', '#ceccfd', '#8982ff', '#6c65e5', '#615bc2'], | ||
}; | ||
|
||
const levelledData = transformData(data.activityCountByDate); | ||
|
||
return ( | ||
<StyledContainer> | ||
{data.activityCountByDate.length > 0 ? ( | ||
<> | ||
<span>Activity in project</span> | ||
<ActivityCalendar | ||
theme={explicitTheme} | ||
data={levelledData} | ||
maxLevel={4} | ||
showWeekdayLabels={true} | ||
renderBlock={(block, activity) => ( | ||
<Tooltip | ||
title={`${activity.count} activities on ${activity.date}`} | ||
> | ||
{block} | ||
</Tooltip> | ||
)} | ||
/> | ||
</> | ||
) : ( | ||
<span>No activity</span> | ||
)} | ||
</StyledContainer> | ||
); | ||
}; |
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
19 changes: 19 additions & 0 deletions
19
frontend/src/hooks/api/getters/useProjectStatus/useProjectStatus.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,19 @@ | ||
import { fetcher, useApiGetter } from '../useApiGetter/useApiGetter'; | ||
import type { ProjectStatusSchema } from '../../../../openapi'; | ||
import { formatApiPath } from 'utils/formatPath'; | ||
|
||
const path = (projectId: string) => `api/admin/projects/${projectId}/status`; | ||
|
||
const placeholderData: ProjectStatusSchema = { | ||
activityCountByDate: [], | ||
}; | ||
|
||
export const useProjectStatus = (projectId: string) => { | ||
const projectPath = formatApiPath(path(projectId)); | ||
const { data, refetch, loading, error } = useApiGetter<ProjectStatusSchema>( | ||
projectPath, | ||
() => fetcher(projectPath, 'Project Status'), | ||
); | ||
|
||
return { data: data || placeholderData, refetch, loading, error }; | ||
}; |
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