-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
168 lines (148 loc) · 5.45 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const usernameInput = document.getElementById('username');
const themeSelect = document.getElementById('theme-select');
let currentTheme = 'light';
let languagesChart;
let statsChart;
let commitsChart;
async function fetchData() {
const username = usernameInput.value;
if (!username) {
alert('Please enter a GitHub username.');
return;
}
try {
const reposResponse = await fetch(`https://api.github.com/users/${username}/repos`);
const reposRateLimit = parseInt(reposResponse.headers.get('X-RateLimit-Remaining'));
if (reposRateLimit === 0) {
alert('GitHub API rate limit exceeded. Please try again later.');
return;
}
if (!reposResponse.ok) {
throw new Error(`Error fetching repositories: ${reposResponse.statusText}`);
}
const reposData = await reposResponse.json();
const languages = {};
let forks = 0, stars = 0, pullRequests = 0, issues = 0;
const commitsPerRepo = [];
for (const repo of reposData) {
forks += repo.forks_count;
stars += repo.stargazers_count;
if (repo.language) {
languages[repo.language] = (languages[repo.language] || 0) + 1;
}
const commitsResponse = await fetch(`https://api.github.com/repos/${username}/${repo.name}/commits`);
if (!commitsResponse.ok) {
throw new Error(`Error fetching commits for ${repo.name}: ${commitsResponse.statusText}`);
}
const commitsData = await commitsResponse.json();
commitsPerRepo.push({ repo: repo.name, commits: commitsData.length });
}
const stats = { forks, stars, pullRequests, issues };
renderCharts(languages, stats, commitsPerRepo);
} catch (error) {
console.error('Error fetching data:', error);
alert(error.message);
}
}
function renderCharts(languages, stats, commitsPerRepo) {
if (languagesChart) {
languagesChart.destroy();
}
if (statsChart) {
statsChart.destroy();
}
if (commitsChart) {
commitsChart.destroy();
}
const languagesCtx = document.getElementById('languagesChart').getContext('2d');
languagesChart = new Chart(languagesCtx, {
type: 'pie',
data: {
labels: Object.keys(languages),
datasets: [{
data: Object.values(languages),
backgroundColor: Object.keys(languages).map(() => `rgba(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, 0.2)`),
borderColor: Object.keys(languages).map(() => `rgba(0, 0, 0, 1)`),
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false
}
});
const statsCtx = document.getElementById('statsChart').getContext('2d');
statsChart = new Chart(statsCtx, {
type: 'bar',
data: {
labels: ['Forks', 'Stars'],
datasets: [{
data: [stats.forks, stats.stars],
backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)'],
borderColor: ['rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)'],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false
}
});
const commitsCtx = document.getElementById('commitsChart').getContext('2d');
commitsChart = new Chart(commitsCtx, {
type: 'bar',
data: {
labels: commitsPerRepo.map(repo => repo.repo),
datasets: [{
label: 'Commits',
data: commitsPerRepo.map(repo => repo.commits),
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Repositories'
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Commits'
}
}]
}
}
});
}
function downloadData() {
const username = usernameInput.value;
if (!username) {
alert('Please enter a GitHub username.');
return;
}
const data = {
username,
languages: languagesChart.data.datasets[0].data,
stats: statsChart.data.datasets[0].data,
commitsPerRepo: commitsChart.data.datasets[0].data
};
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${username}_data.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
themeSelect.addEventListener('change', function () {
currentTheme = themeSelect.value;
document.body.className = currentTheme === 'dark' ? 'dark-theme' : '';
});