mirror of
https://github.com/reiyua/github-profile-trophy-reiyua-mirror.git
synced 2026-11-17 06:02:09 +11:00
Consolidate 4 GraphQL queries into 1 and extend cache TTL to reduce Function Duration
Merged separate GitHub API queries (repository, activity, issue, pullRequest) into a single combined GraphQL query to reduce HTTP roundtrips from 4 to 1 per cache miss. Also extended file cache revalidation time from 1 hour to 6 hours to decrease function invocation frequency. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
9a3ae0f512
commit
5bbd8f6863
6 changed files with 73 additions and 47 deletions
|
|
@ -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<UserInfo | ServiceError>;
|
||||
abstract requestUserAll(
|
||||
username: string,
|
||||
): Promise<GitHubUserAll | ServiceError>;
|
||||
abstract requestUserActivity(
|
||||
username: string,
|
||||
): Promise<GitHubUserActivity | ServiceError>;
|
||||
|
|
|
|||
|
|
@ -60,3 +60,45 @@ export const queryUserRepository = `
|
|||
}
|
||||
}
|
||||
`;
|
||||
|
||||
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: 3, orderBy: {direction:DESC, field: SIZE}) {
|
||||
nodes {
|
||||
name
|
||||
}
|
||||
}
|
||||
stargazers {
|
||||
totalCount
|
||||
}
|
||||
createdAt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
|
|
|||
|
|
@ -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<GitHubUserAll | ServiceError> {
|
||||
return await this.executeQuery<GitHubUserAll>(queryUserAll, {
|
||||
username,
|
||||
});
|
||||
}
|
||||
async requestUserRepository(
|
||||
username: string,
|
||||
): Promise<GitHubUserRepository | ServiceError> {
|
||||
|
|
@ -55,34 +64,13 @@ export class GithubApiService extends GithubRepository {
|
|||
);
|
||||
}
|
||||
async requestUserInfo(username: string): Promise<UserInfo | ServiceError> {
|
||||
// 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<GitHubUserActivity>).value,
|
||||
(issue as PromiseFulfilledResult<GitHubUserIssue>).value,
|
||||
(pullRequest as PromiseFulfilledResult<GitHubUserPullRequest>).value,
|
||||
(repository as PromiseFulfilledResult<GitHubUserRepository>).value,
|
||||
);
|
||||
return UserInfo.fromCombined(result);
|
||||
} catch {
|
||||
Logger.error(`Error fetching user info for username: ${username}`);
|
||||
return new ServiceError("Not found", EServiceKindError.NOT_FOUND);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -41,6 +41,11 @@ 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 +61,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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue