diff --git a/src/apis/apis.service.ts b/src/apis/apis.service.ts index 2309b8a..ed62dbe 100644 --- a/src/apis/apis.service.ts +++ b/src/apis/apis.service.ts @@ -38,44 +38,32 @@ export class APIService { return data; } - async getGithubUserRegistration(username: string): Promise { - const cache = await this.cacheManager.get(`user_reg:${username}`); + + async getGithubStreak(username: string): Promise<{ streak: number; longest: number; total_contributions: number; } | null> { + const cache = await this.cacheManager.get(`streak:${username}`); if (cache) return JSON.parse(cache); - const response = await axios.post( - 'https://api.github.com/graphql', - { - query: "query GetUserCreatedAt($username: String!) { user(login: $username) { createdAt } }", - variables: { - username: username - } - }, - { - headers: { Authorization: `Bearer ${process.env.GITHUB}` }, - validateStatus: () => true - } - ); + const date = new Date(); + const timezoneOffset = date.getTimezoneOffset(); + const now = date.getTime() - timezoneOffset * 60000; - if (response.status !== 200) return null; + const nowISO = new Date(now).toISOString(); - const data: UserReg = response.data; + const from = new Date(now); + from.setFullYear(from.getFullYear() - 1); + const fromISO = from.toISOString(); - await this.cacheManager.set(`user_reg:${username}`, JSON.stringify(data), 1000 * 60 * 60 * 1000); - return data; - } - - async getGithubStreakYear(username: string, from: string, to: string) { const response = await axios.post( 'https://api.github.com/graphql', { - query: `query GetUserContributions($username: String!, $from: DateTime!${!!to ? ", $to: DateTime!" : ""})` + - `{ user(login: $username) { contributionsCollection(from: $from${!!to ? ", to: $to" : ""})` + + query: "query GetUserContributions($username: String!, $from: DateTime!, $to: DateTime!)" + + "{ user(login: $username) { contributionsCollection(from: $from, to: $to)" + "{ contributionCalendar { weeks { contributionDays { date contributionCount } } } } } }", variables: { username: username, - from: from, - to: to + from: fromISO, + to: nowISO } }, { @@ -84,40 +72,11 @@ export class APIService { } ); if (response.status !== 200 || !response.data.data.user) return null; - const data: GithubStreak = response.data; - return data.data.user.contributionsCollection.contributionCalendar.weeks.flatMap(week => + const data = response.data as GithubStreak; + const days = data.data.user.contributionsCollection.contributionCalendar.weeks.flatMap(week => week.contributionDays.map(val => val.contributionCount) ); - } - - - async getGithubStreak(username: string): Promise<{ streak: number; longest: number; total_contributions: number; } | null> { - const cache = await this.cacheManager.get(`streak:${username}`); - - if (cache) return JSON.parse(cache); - - const date = new Date(); - const timezoneOffset = date.getTimezoneOffset(); - const now = date.getTime() - timezoneOffset * 60000; - const nowISO = new Date(now).toISOString(); - - const currentYearStart = `${date.getFullYear()}-01-01T00:00:00Z`; - - let days = await this.getGithubStreakYear(username, currentYearStart, nowISO); - if (!days) return null; - - const regTime = new Date((await this.getGithubUserRegistration(username)).data.user.createdAt); - - for (let year = date.getFullYear() - 1; year >= regTime.getFullYear(); year--) { - try { - const days_last = await this.getGithubStreakYear(username, `${year}-01-01T00:00:00Z`, undefined); - days = [...days_last, ...days]; - } catch (e) { - console.error(e); - break; - } - } let streak_start = -1; let streak_end = -1; @@ -149,7 +108,7 @@ export class APIService { total_contributions } - await this.cacheManager.set(`streak:${username}`, JSON.stringify(result), 1000 * 60 * 60 * 3); + await this.cacheManager.set(`streak:${username}`, JSON.stringify(result), 1000 * 60 * 60); return result; } diff --git a/src/widget/widget.service.ts b/src/widget/widget.service.ts index d0e44a0..f35d46d 100644 --- a/src/widget/widget.service.ts +++ b/src/widget/widget.service.ts @@ -74,7 +74,7 @@ export class WidgetService { followers: github_data?.data?.user?.followers?.totalCount, total_stars: github_data?.total_stars, top_repos: top_repos, - streak: streak ? { + streak_last_year: streak ? { current: streak.streak, longest: streak.longest, total_contributions: streak.total_contributions