Skip to content

Commit

Permalink
feat: support returning statistical data from interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Codennnn committed Nov 16, 2024
1 parent 97af066 commit d140190
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/app/api/contribution/[username]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 14 additions & 7 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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, ',')
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -232,6 +236,8 @@ export function getValuableStatistics(graphData: GraphData) {
totalContributions += calendar.total
})

const averageContributionsPerDay = Math.round(totalContributions / totalDays)

return {
weekendContributions,
totalContributions,
Expand All @@ -243,5 +249,6 @@ export function getValuableStatistics(graphData: GraphData) {
longestGapEndDate,
maxContributionsInADay,
maxContributionsDate,
averageContributionsPerDay,
}
}
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ export interface GitHubIssue {
export type RepoCreatedInYear = InferInput<typeof ReposCreatedInYearSchema>

export type IssuesInYear = InferInput<typeof IssuesInYearSchema>

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
}

0 comments on commit d140190

Please sign in to comment.