Skip to content

Commit

Permalink
feat: set the cache if repo not found or has no star. This reduces AP…
Browse files Browse the repository at this point in the history
…I call
  • Loading branch information
tianzhou committed Aug 27, 2023
1 parent 3b66c57 commit e1f9f39
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
21 changes: 21 additions & 0 deletions common/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import utils from "./utils";

export const DEFAULT_MAX_REQUEST_AMOUNT = 15;

const STAR_HISTORY_LOGO_URL =
"https://avatars.githubusercontent.com/u/124480067";

export const getReposStarData = async (
repos: string[],
token = "",
Expand Down Expand Up @@ -112,6 +115,24 @@ export const getRepoData = async (
message = "Some unexpected error happened, try again later";
}

console.error("Failed to request data:", status, message);

// If encountering not found or no star error, we will return an empty image so that cache can be set.
if (status === 404 || status === 501) {
return [
{
repo,
starRecords: [
{
date: utils.getDateString(Date.now(), "yyyy/MM/dd"),
count: 0,
},
],
logoUrl: STAR_HISTORY_LOGO_URL,
},
];
}

return Promise.reject({
message,
status,
Expand Down
2 changes: 1 addition & 1 deletion packages/xy-chart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const XYChart = (
initialOptions: Partial<XYChartOptions>
) => {
const options: XYChartOptions = {
...(theme === 'dark' ? getDarkThemeDefaultOptions() : getDefaultOptions()),
...(theme === "dark" ? getDarkThemeDefaultOptions() : getDefaultOptions()),
...initialOptions,
};

Expand Down
6 changes: 0 additions & 6 deletions server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ const startServer = async () => {
const message =
error.message || "Some unexpected error happened, try again later";

if (status === 404) {
// do nth, repo from user not found.
} else {
logger.error("Failed to request data", error);
}

ctx.status = status;
ctx.message = `${http.STATUS_CODES[status]}: ${message}`;
return;
Expand Down
6 changes: 5 additions & 1 deletion server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions src/components/StarChartViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@ const fetchReposData = async (repos: string[]) => {
const repoData: RepoData[] = [];
for (const repo of repos) {
const cachedRepo = state.repoCacheMap.get(repo);
repoData.push({
repo,
starRecords: cachedRepo!.starData,
logoUrl: cachedRepo!.logoUrl,
});
if (cachedRepo) {
repoData.push({
repo,
starRecords: cachedRepo.starData,
logoUrl: cachedRepo.logoUrl,
});
}
}
if (repoData.length === 0) {
Expand Down

0 comments on commit e1f9f39

Please sign in to comment.