Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 54 additions & 34 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,12 @@ async function fetchRepositoriesREST(username: string): Promise<RepositoryData>
}

const totalRepoCount = Array.from(languageRepoCount.values()).reduce((a, b) => a + b, 0);
const languages: LanguageStats[] = Array.from(languageRepoCount.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([name, repoCount]) => ({
name,
bytes: repoCount,
percentage: totalRepoCount > 0 ? Math.round((repoCount / totalRepoCount) * 1000) / 10 : 0,
color: getLanguageColor(name),
}));
const languages: LanguageStats[] = getTopK(languageRepoCount, 10).map(({ name, count }) => ({
name,
bytes: count,
percentage: totalRepoCount > 0 ? Math.round((count / totalRepoCount) * 1000) / 10 : 0,
color: getLanguageColor(name),
}));

const topRepos: TopRepo[] = nonFork.slice(0, 5).map((r) => ({
name: r.name,
Expand All @@ -339,10 +336,7 @@ async function fetchRepositoriesREST(username: string): Promise<RepositoryData>
: null,
}));

const topics = Array.from(topicCountMap.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([name, count]) => ({ name, count }));
const topics = getTopK(topicCountMap, 10);

return { languages, topics, topRepos, totalCount: nonFork.length };
}
Expand Down Expand Up @@ -371,15 +365,27 @@ function processRepoData(repos: RepoNode[]): RepositoryData {
}

const totalBytes = Array.from(languageMap.values()).reduce((a, b) => a + b.bytes, 0);
const languages: LanguageStats[] = Array.from(languageMap.entries())
.sort((a, b) => b[1].bytes - a[1].bytes)
.slice(0, 10)
.map(([name, { bytes, color }]) => ({
name,
bytes,
percentage: totalBytes > 0 ? Math.round((bytes / totalBytes) * 1000) / 10 : 0,
color,
}));
const topLanguages: { name: string; bytes: number; color: string }[] = [];
for (const [name, data] of languageMap.entries()) {
if (topLanguages.length < 10) {
topLanguages.push({ name, ...data });
topLanguages.sort((a, b) => b.bytes - a.bytes);
} else if (data.bytes > topLanguages[9].bytes) {
let i = 8;
while (i >= 0 && topLanguages[i].bytes < data.bytes) {
topLanguages[i + 1] = topLanguages[i];
i--;
}
topLanguages[i + 1] = { name, ...data };
}
}

const languages: LanguageStats[] = topLanguages.map(({ name, bytes, color }) => ({
name,
bytes,
percentage: totalBytes > 0 ? Math.round((bytes / totalBytes) * 1000) / 10 : 0,
color,
}));

const topRepos: TopRepo[] = repos.slice(0, 5).map((r) => ({
name: r.name,
Expand All @@ -390,10 +396,7 @@ function processRepoData(repos: RepoNode[]): RepositoryData {
primaryLanguage: r.primaryLanguage,
}));

const topics = Array.from(topicCountMap.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([name, count]) => ({ name, count }));
const topics = getTopK(topicCountMap, 10);

return { languages, topics, topRepos, totalCount: repos.length };
}
Expand Down Expand Up @@ -592,15 +595,9 @@ export async function fetchStarredRepos(
}
}

const topTopics = Array.from(topicCounts.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([name, count]) => ({ name, count }));
const topTopics = getTopK(topicCounts, 10);

const topLanguages = Array.from(languageCounts.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([name, count]) => ({ name, count }));
const topLanguages = getTopK(languageCounts, 10);

return {
topTopics,
Expand Down Expand Up @@ -685,6 +682,29 @@ export async function fetchActivity(

// ===== 6. fetchUserSummary =====


/**
* 効率的に Map から上位 K 件を抽出するヘルパー関数
* 配列の作成とソートを最小限に抑えることでパフォーマンスを向上させます
*/
function getTopK(map: Map<string, number>, k: number = 10): { name: string; count: number }[] {
const top: { name: string; count: number }[] = [];
for (const [name, count] of map.entries()) {
if (top.length < k) {
top.push({ name, count });
top.sort((a, b) => b.count - a.count);
} else if (count > top[k - 1].count) {
let i = k - 2;
while (i >= 0 && top[i].count < count) {
top[i + 1] = top[i];
i--;
}
top[i + 1] = { name, count };
}
}
return top;
}

/**
* 結果を処理し、エラーがあれば記録するヘルパー関数
*/
Expand Down
Loading