-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionScript.js
252 lines (194 loc) · 7.45 KB
/
functionScript.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
async function fetchData(){
var userName = document.getElementById("userNameBox").value;
var token = document.getElementById("tokenBox").value;
getUserDetails(userName, token);
generatePieGraph(userName,token);
getLanguageGraph(userName, token);
popularRepos(userName, token)
}
async function getRequest(url, token) {
const headers = {
'Authorization': `Token ${token}`
}
const response = (token == undefined) ? await fetch(url) : await fetch(url, {
"method": "GET",
"headers": headers
});
let data = await response.json();
return data;
}
async function getUserDetails(userName, token){
const url = `https://api.github.com/users/${userName}`;
const user_info = await getRequest(url,token);
let img = document.getElementById('img');
img.src = user_info.avatar_url
let name = document.getElementById('name');
name.innerHTML = `<b>Name: </b>${user_info.name}`;
let login = document.getElementById('login');
login.innerHTML = `<b>Username: </b>${user_info.login}`;
let profileURL = document.getElementById('profileURL');
profileURL.innerHTML = `<b>URL: </b>${user_info.html_url }`;
let followers = document.getElementById('followers');
followers.innerHTML = `<b>Followers: </b>${user_info.followers}`;
let following = document.getElementById('following');
following.innerHTML = `<b>Following: </b>${user_info.following}`;
let location = document.getElementById('location');
location.innerHTML = `<b>Location: </b>${user_info.location}`;
let public_repos = document.getElementById('public_repos');
public_repos.innerHTML = `<b>Number of Public Repos: </b>${user_info.public_repos}`;
}
// Number of commits in a repo
async function generatePieGraph(userName, token){
//Getting Repos
const url = `https://api.github.com/users/${userName}/repos`;
const listOfRepos = await getRequest(url,token);
let data = [];
for (let i = 0; i < listOfRepos.length; i++)
{
const repoName = listOfRepos[i].name;
const commitsList = await getRequest(`https://api.github.com/repos/${userName}/${repoName}/commits`,token).catch((error) => console.error(error));
let b = { key: repoName, value: commitsList.length };
data.push(b);
}
var keyTable = [];
data.forEach((element) => {
keyTable.push(element.key);
});
var valueTable = [];
data.forEach((element) => {
valueTable.push(element.value);
});
//Make a Pie Chart
createPieGraph(keyTable, valueTable);
}
// Takes list of starred repo from the user and plot them along their stargzer's count
async function popularRepos(userName, token){
let labelArray = [];
let dataArray = [];
// Getting Repos that are Starred by the User
var url = `https://api.github.com/users/${userName}/starred`;
const repoData = await getRequest(url, token)
// Getting the number of stargazzers of each repos from all starred
for(i in repoData){
var owner = repoData[i].owner.login;
var repoName = repoData[i].name;
var url2 = `https://api.github.com/repos/${owner}/${repoName}`;
const starredRepo = await getRequest(url, token);
labelArray.push(repoName);
dataArray.push(starredRepo[i].stargazers_count);
}
//Plot the bar graph where the user's starred repositories are plotted against the number of contributors to that contributors
var ctx = document.getElementById('myPopularStarredRepoChart').getContext('2d');
createBarGraph(labelArray, dataArray, ctx, 'Number of Stargazers');
}
// Bar graph of languages used by the user in all repositories.
async function getLanguageGraph(userName, token) {
// Getting Repos of the User
const url = `https://api.github.com/users/${userName}/repos`;
const repoData = await getRequest(url, token)
let labelArray = [];
let dataArray = [];
// Getting Languages of repos
for (i in repoData) {
let url = `https://api.github.com/repos/${userName}/${repoData[i].name}/languages`;
let repoLanguages = await getRequest(url, token).catch(error => console.error(error));
for (l in repoLanguages) {
var index = labelArray.indexOf(l);
if(index != -1){
dataArray[index] = dataArray[index] + repoLanguages[l]; // Add number of bytes to the associated index of the language.
} else {
labelArray.push(l); // Create new language entry
dataArray.push(repoLanguages[l]);
}
}
}
var ctx = document.getElementById('myLanguagesChart').getContext('2d');
createBarGraph(labelArray, dataArray, ctx, 'Bytes of Code');
}
async function createBarGraph(labelArray, dataArray, ctx, inputLabel){
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labelArray,
datasets: [{
label: inputLabel,
data: dataArray,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(200, 100, 64, 0.2)',
'rgba(150, 100, 100, 0.2)',
'rgba(200, 200, 64, 0.2)',
'rgba(175, 200, 64, 0.2)',
'rgba(125, 55, 164, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
async function createPieGraph(dataTable, dataName){
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: dataTable,
datasets: [{
label: 'Total Number of Commits',
data: dataName,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)',
'rgba(200, 100, 64, 0.2)',
'rgba(150, 100, 100, 0.2)',
'rgba(200, 200, 64, 0.2)',
'rgba(175, 200, 64, 0.2)',
'rgba(125, 55, 164, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}