diff --git a/src/app/api/contribution/[username]/route.ts b/src/app/api/contribution/[username]/route.ts index 83a80fa..8b83924 100644 --- a/src/app/api/contribution/[username]/route.ts +++ b/src/app/api/contribution/[username]/route.ts @@ -4,7 +4,7 @@ import { ErrorType } from '~/enums' import { getValuableStatistics } from '~/helpers' import { mockGraphData } from '~/mock-data' import { fetchContributionsCollection, fetchGitHubUser } from '~/services' -import type { GraphData, ResponseData } from '~/types' +import type { GraphData, ResponseData, ValuableStatistics } from '~/types' interface GetContributionRequestParams { username: string @@ -46,9 +46,13 @@ export async function GET( contributionCalendars, } - const valuableStatistics = getValuableStatistics(graphData) + let valuableStatistics: ValuableStatistics | undefined - const data = statistics ? { ...graphData, statistics: valuableStatistics } : graphData + if (statistics) { + valuableStatistics = getValuableStatistics(graphData) + } + + const data = valuableStatistics ? { ...graphData, statistics: valuableStatistics } : graphData return NextResponse.json({ data }, { status: 200 }) } catch (err) { diff --git a/src/helpers.ts b/src/helpers.ts index aec765d..8e590e4 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,6 +1,6 @@ import splitbee from '@splitbee/web' -import type { GraphData } from '~/types' +import type { GraphData, ValuableStatistics } from '~/types' export function numberWithCommas(num: number): string { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') @@ -167,26 +167,30 @@ export function getWeekendActivity(graphData: GraphData): { total: number; ratio } } -export function getValuableStatistics(graphData: GraphData) { +export function getValuableStatistics(graphData: GraphData): ValuableStatistics { let weekendContributions = 0 let totalContributions = 0 let longestStreak = 0 let currentStreak = 0 - let longestStreakStartDate: string | null = null - let longestStreakEndDate: string | null = null + let longestStreakStartDate: string | undefined = undefined + let longestStreakEndDate: string | undefined = undefined let longestGap = 0 let currentGap = 0 - let longestGapStartDate: string | null = null - let longestGapEndDate: string | null = null + let longestGapStartDate: string | undefined = undefined + let longestGapEndDate: string | undefined = undefined let maxContributionsInADay = 0 - let maxContributionsDate: string | null = null + let maxContributionsDate: string | undefined = undefined + + let totalDays = 0 graphData.contributionCalendars.forEach((calendar) => { calendar.weeks.forEach((week) => { week.days.forEach((day) => { + totalDays++ + const isWeekend = day.weekday === 0 || day.weekday === 6 if (isWeekend) { @@ -232,6 +236,8 @@ export function getValuableStatistics(graphData: GraphData) { totalContributions += calendar.total }) + const averageContributionsPerDay = Math.round(totalContributions / totalDays) + return { weekendContributions, totalContributions, @@ -243,5 +249,6 @@ export function getValuableStatistics(graphData: GraphData) { longestGapEndDate, maxContributionsInADay, maxContributionsDate, + averageContributionsPerDay, } } diff --git a/src/types.ts b/src/types.ts index a1fc2cc..bc6cedb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -147,3 +147,17 @@ export interface GitHubIssue { export type RepoCreatedInYear = InferInput export type IssuesInYear = InferInput + +export interface ValuableStatistics { + weekendContributions: number + totalContributions: number + longestStreak: number + longestStreakStartDate?: string + longestStreakEndDate?: string + longestGap: number + longestGapStartDate?: string + longestGapEndDate?: string + maxContributionsInADay: number + maxContributionsDate?: string + averageContributionsPerDay: number +}