Skip to content

Commit

Permalink
streak now counting all activity period
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Dec 22, 2024
1 parent c16cafc commit 8d0ce15
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 19 deletions.
80 changes: 62 additions & 18 deletions src/apis/apis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,44 @@ export class APIService {
return data;
}


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

if (cache) return JSON.parse(cache);

const date = new Date();
const timezoneOffset = date.getTimezoneOffset();
const now = date.getTime() - timezoneOffset * 60000;
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 nowISO = new Date(now).toISOString();
if (response.status !== 200) return null;

const from = new Date(now);
from.setFullYear(from.getFullYear() - 1);
const fromISO = from.toISOString();
const data: UserReg = response.data;

await this.cacheManager.set(`user_reg:${username}`, JSON.stringify(data), 1000 * 60 * 60);
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: DateTime!)" +
"{ user(login: $username) { contributionsCollection(from: $from, to: $to)" +
query: `query GetUserContributions($username: String!, $from: DateTime!${!!to ? ", $to: DateTime!" : ""})` +
`{ user(login: $username) { contributionsCollection(from: $from${!!to ? ", to: $to" : ""})` +
"{ contributionCalendar { weeks { contributionDays { date contributionCount } } } } } }",
variables: {
username: username,
from: fromISO,
to: nowISO
from: from,
to: to
}
},
{
Expand All @@ -72,18 +84,49 @@ export class APIService {
}
);
if (response.status !== 200 || !response.data.data.user) return null;
const data: GithubStreak = response.data;

const data = response.data as GithubStreak;
const days = data.data.user.contributionsCollection.contributionCalendar.weeks.flatMap(week =>
return 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;
let longest_streak = 0;
let total_contributions = 0;

for (let i = 0; i < days.length; i++) {
const day = days[i];
total_contributions += day;

if (day !== 0) {
if (streak_start === -1) {
Expand All @@ -102,10 +145,11 @@ export class APIService {
const streak_days = streak_end - streak_start;
const result = {
streak: streak_days,
longest: longest_streak
longest: longest_streak,
total_contributions
}

await this.cacheManager.set(`streak:${username}`, JSON.stringify(result), 1000 * 60 * 60);
await this.cacheManager.set(`streak:${username}`, JSON.stringify(result), 1000 * 60 * 60 * 3);
return result;
}

Expand Down
9 changes: 9 additions & 0 deletions src/apis/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,12 @@ interface ActivityResponse {
start_time: Date
}[]
}


interface UserReg {
data: {
user: {
createdAt: Date
}
}
}
3 changes: 2 additions & 1 deletion src/widget/widget.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export class WidgetService {
top_repos: top_repos,
streak: streak ? {
current: streak.streak,
longest: streak.longest
longest: streak.longest,
total_contributions: streak.total_contributions
} : null,
},
wakatime: {
Expand Down

0 comments on commit 8d0ce15

Please sign in to comment.