diff --git a/src/Repository/GithubRepository.ts b/src/Repository/GithubRepository.ts index c2a408c..1c33434 100644 --- a/src/Repository/GithubRepository.ts +++ b/src/Repository/GithubRepository.ts @@ -1,6 +1,7 @@ import { ServiceError } from "../Types/index.ts"; import { GitHubUserActivity, + GitHubUserAll, GitHubUserIssue, GitHubUserPullRequest, GitHubUserRepository, @@ -9,6 +10,9 @@ import { export abstract class GithubRepository { abstract requestUserInfo(username: string): Promise; + abstract requestUserAll( + username: string, + ): Promise; abstract requestUserActivity( username: string, ): Promise; diff --git a/src/Schemas/index.ts b/src/Schemas/index.ts index 34be792..8e09dbb 100644 --- a/src/Schemas/index.ts +++ b/src/Schemas/index.ts @@ -46,7 +46,49 @@ export const queryUserRepository = ` repositories(first: 50, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) { totalCount nodes { - languages(first: 3, orderBy: {direction:DESC, field: SIZE}) { + languages(first: 2, orderBy: {direction:DESC, field: SIZE}) { + nodes { + name + } + } + stargazers { + totalCount + } + createdAt + } + } + } + } +`; + +export const queryUserAll = ` + query userInfo($username: String!) { + user(login: $username) { + createdAt + contributionsCollection { + totalCommitContributions + restrictedContributionsCount + totalPullRequestReviewContributions + } + organizations(first: 1) { + totalCount + } + followers(first: 1) { + totalCount + } + openIssues: issues(states: OPEN) { + totalCount + } + closedIssues: issues(states: CLOSED) { + totalCount + } + pullRequests(first: 1) { + totalCount + } + repositories(first: 50, ownerAffiliations: OWNER, orderBy: {direction: DESC, field: STARGAZERS}) { + totalCount + nodes { + languages(first: 2, orderBy: {direction:DESC, field: SIZE}) { nodes { name } diff --git a/src/Services/GithubApiService.ts b/src/Services/GithubApiService.ts index 52be0f2..167525d 100644 --- a/src/Services/GithubApiService.ts +++ b/src/Services/GithubApiService.ts @@ -1,6 +1,7 @@ import { GithubRepository } from "../Repository/GithubRepository.ts"; import { GitHubUserActivity, + GitHubUserAll, GitHubUserIssue, GitHubUserPullRequest, GitHubUserRepository, @@ -8,6 +9,7 @@ import { } from "../user_info.ts"; import { queryUserActivity, + queryUserAll, queryUserIssue, queryUserPullRequest, queryUserRepository, @@ -25,6 +27,13 @@ export const TOKENS = [ ]; export class GithubApiService extends GithubRepository { + async requestUserAll( + username: string, + ): Promise { + return await this.executeQuery(queryUserAll, { + username, + }); + } async requestUserRepository( username: string, ): Promise { @@ -55,34 +64,13 @@ export class GithubApiService extends GithubRepository { ); } async requestUserInfo(username: string): Promise { - // Avoid to call others if one of them is null - - const promises = Promise.allSettled([ - this.requestUserRepository(username), - this.requestUserActivity(username), - this.requestUserIssue(username), - this.requestUserPullRequest(username), - ]); + // Use single combined query instead of 4 separate queries to reduce Function Duration try { - const [repository, activity, issue, pullRequest] = await promises; - const status = [ - repository.status, - activity.status, - issue.status, - pullRequest.status, - ]; - - if (status.includes("rejected")) { - Logger.error(`Can not find a user with username:' ${username}'`); - return new ServiceError("Not found", EServiceKindError.NOT_FOUND); + const result = await this.requestUserAll(username); + if (result instanceof ServiceError) { + return result; } - - return new UserInfo( - (activity as PromiseFulfilledResult).value, - (issue as PromiseFulfilledResult).value, - (pullRequest as PromiseFulfilledResult).value, - (repository as PromiseFulfilledResult).value, - ); + return UserInfo.fromCombined(result); } catch { Logger.error(`Error fetching user info for username: ${username}`); return new ServiceError("Not found", EServiceKindError.NOT_FOUND); diff --git a/src/Services/__tests__/githubApiService.test.ts b/src/Services/__tests__/githubApiService.test.ts index 6f0fcdc..b9a697e 100644 --- a/src/Services/__tests__/githubApiService.test.ts +++ b/src/Services/__tests__/githubApiService.test.ts @@ -28,26 +28,8 @@ stub( new Promise((resolve) => { resolve(successGithubResponseMock.default); }), - // Should throw NOT FOUND (requestUserInfo makes 4 API calls: repository, activity, issue, pullRequest) - // Each call makes 2 attempts (one per token), so 8 promises total - new Promise((resolve) => { - resolve(notFoundGithubResponseMock.default); - }), - new Promise((resolve) => { - resolve(notFoundGithubResponseMock.default); - }), - new Promise((resolve) => { - resolve(notFoundGithubResponseMock.default); - }), - new Promise((resolve) => { - resolve(notFoundGithubResponseMock.default); - }), - new Promise((resolve) => { - resolve(notFoundGithubResponseMock.default); - }), - new Promise((resolve) => { - resolve(notFoundGithubResponseMock.default); - }), + // Should throw NOT FOUND (requestUserInfo makes 1 combined API call) + // Each call makes 2 attempts (one per token), so 2 promises total new Promise((resolve) => { resolve(notFoundGithubResponseMock.default); }), diff --git a/src/user_info.ts b/src/user_info.ts index 5d1e286..c5b986e 100644 --- a/src/user_info.ts +++ b/src/user_info.ts @@ -41,6 +41,12 @@ export type GitHubUserActivity = { totalCount: number; }; }; + +export type GitHubUserAll = + & GitHubUserActivity + & GitHubUserIssue + & GitHubUserPullRequest + & GitHubUserRepository; export class UserInfo { public readonly totalCommits: number; public readonly totalFollowers: number; @@ -56,6 +62,11 @@ export class UserInfo { public readonly ancientAccount: number; public readonly joined2020: number; public readonly ogAccount: number; + + static fromCombined(data: GitHubUserAll): UserInfo { + return new UserInfo(data, data, data, data); + } + constructor( userActivity: GitHubUserActivity, userIssue: GitHubUserIssue, diff --git a/src/utils.ts b/src/utils.ts index 9d52973..4b0e6e6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -63,7 +63,7 @@ export const CONSTANTS = { DEFAULT_NO_FRAME: false, DEFAULT_GITHUB_API: "https://api.github.com/graphql", DEFAULT_GITHUB_RETRY_DELAY: 500, - REVALIDATE_TIME: HOUR_IN_MILLISECONDS, + REVALIDATE_TIME: HOUR_IN_MILLISECONDS * 6, REDIS_TTL: HOUR_IN_MILLISECONDS * 4, };