Skip to content

Commit

Permalink
revert global streak
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Dec 22, 2024
1 parent de1e41a commit 8863b32
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 59 deletions.
75 changes: 17 additions & 58 deletions src/apis/apis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,32 @@ export class APIService {
return data;
}

async getGithubUserRegistration(username: string): Promise<UserReg | null> {
const cache = await this.cacheManager.get<string>(`user_reg:${username}`);

async getGithubStreak(username: string): Promise<{ streak: number; longest: number; total_contributions: number; } | null> {
const cache = await this.cacheManager.get<string>(`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
}
},
{
Expand All @@ -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<string>(`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;
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/widget/widget.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8863b32

Please sign in to comment.