-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
271 lines (266 loc) · 8.93 KB
/
index.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
document.getElementById('signInForm').addEventListener('submit', signIn);
document.getElementById('logOutBtn').addEventListener('click', logOut);
validateJWT();
/**
* Validates the JWT token stored in the "token" cookie
* @returns {Promise<boolean>} True if the JWT is valid, false otherwise
*/
async function validateJWT() {
const token = getToken();
if (!token) {
document.getElementById('signIn').classList.remove('d-none');
document.getElementById('logOutBtn').disabled = true;
return false;
}
var res;
try {
res = await fetch(
'https://01.gritlab.ax/api/graphql-engine/v1/graphql',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
},
body: JSON.stringify({
query: 'query { user { id } }',
}),
}
);
} catch (e) {
document.cookie = document.cookie.split(';').filter((c) => {
return !c.startsWith('token');
});
document.getElementById('errorMsg').classList.remove('d-none');
document.getElementById('logOutBtn').disabled = true;
return false;
}
const data = await res.json();
if (data.errors) {
document.cookie = document.cookie.split(';').filter((c) => {
return !c.startsWith('token');
});
document.getElementById('signIn').classList.remove('d-none');
document.getElementById('logOutBtn').disabled = true;
return false;
}
document.getElementById('signIn').classList.add('d-none');
showInfo();
document.getElementById('logOutBtn').disabled = false;
return true;
}
/**
* Logs the user out by removing the token cookie
*/
function logOut() {
document.cookie = 'token=';
document.getElementById('signIn').classList.remove('d-none');
document.getElementById('content').classList.add('d-none');
document.getElementById('logOutBtn').disabled = true;
}
/**
* Takes the username and password from the form and sends a request to the server to sign in
*
* Sets the token cookie if the request is successful
* @param {Event} e
* @returns
*/
async function signIn(e) {
e.preventDefault();
if (await validateJWT()) {
return;
}
const username = e.target.username.value;
const password = e.target.password.value;
const res = await fetch('https://01.gritlab.ax/api/auth/signin', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + window.btoa(username + ':' + password),
},
});
if (!res.ok) {
const err = await res.json();
document.getElementById('msg').innerHTML = err.error;
document.getElementById('logOutBtn').disabled = true;
return;
}
const data = await res.json();
document.cookie = `token=${data};`;
document.getElementById('signIn').classList.add('d-none');
document.getElementById('logOutBtn').disabled = false;
showInfo();
}
async function showInfo() {
const token = getToken();
document.getElementById('content').classList.remove('d-none');
const res = await fetch(
'https://01.gritlab.ax/api/graphql-engine/v1/graphql',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
},
body: JSON.stringify({
query: 'query {\
user {\
id\
login\
attrs\
auditRatio\
skills: transactions(order_by: [{type: desc}, {amount: desc}] distinct_on: [type] where: {type: {_like: "skill_%"}}) {\
type\
amount\
}\
audits {\
group {\
captainId\
captainLogin\
path\
createdAt\
updatedAt\
members {\
userId\
userLogin\
}\
}\
}\
}\
event(where: {path: {_nlike: "%checkpoint%"}}) {\
path\
id\
}\
transaction(where: {type: {_eq: "xp"}}, order_by: {createdAt: desc}) {\
amount\
createdAt\
eventId\
path\
}\
}',
}),
}
);
const data = await res.json();
if (data.errors) {
console.log(data.errors[0].message);
return;
}
console.log(data.data);
const event = Object.fromEntries(
data.data.event.map((e) => [e.id, e.path])
);
const skills = Object.fromEntries(
data.data.user[0].skills.map((s) => [s.type.slice(6), s.amount])
);
const transaction = data.data.transaction;
let total = 0;
var dates = [];
let prevDate = new Date(transaction[transaction.length - 1].createdAt)
.toLocaleString()
.split(', ')[0];
transaction.reverse().forEach((t) => {
const date = new Date(t.createdAt).toLocaleString().split(', ')[0];
if (date !== prevDate && !t.path.includes('piscine-go')) {
dates.push(prevDate);
prevDate = date;
}
});
const xp = {};
transaction.forEach((t) => {
if (t.path.includes('piscine-go')) return;
const date = new Date(t.createdAt).toLocaleString().split(', ')[0];
xp[date] = Math.floor(t.amount + total);
total += t.amount;
});
const user = data.data.user[0];
document.getElementById('contentUsername').innerHTML = user.login;
document.getElementById('contentId').innerHTML = user.id;
document.getElementById('contentEmail').innerHTML = user.attrs.email;
document.getElementById('contentFName').innerHTML = user.attrs.firstName;
document.getElementById('contentLName').innerHTML = user.attrs.lastName;
console.log(skills);
const xpGraph = new Chart('xpGraph', {
type: 'line',
data: {
labels: dates.map((d) => d.split(', ')[0]),
datasets: [
{
label: 'XP',
data: Array.from(Object.values(xp)),
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgba(255, 99, 132, 0.5)',
tension: 0.1,
},
],
},
options: {
scales: {
y: {
beginAtZero: true,
},
},
},
});
const skillNames = {
html: 'HTML',
css: 'CSS',
js: 'JS',
sql: 'SQL',
go: 'Go',
unix: 'Unix',
tcp: 'TCP/IP',
'sys-admin': 'SysAdmin',
'front-end': 'Front-End',
'back-end': 'Back-End',
docker: 'Docker',
algo: 'Algorithms',
prog: 'Elementary Programming',
game: 'Game',
};
for (const [key, value] of Object.entries(skills)) {
console.log(key, value);
const skill = document.createElement('div');
skill.classList.add('skill', 'col-2', 'm-1');
const heading = document.createElement('h3');
heading.innerHTML = skillNames[key] ? skillNames[key] : key;
const skillGraph = document.createElement('canvas');
skillGraph.id = key;
document.getElementById('skills').appendChild(skill);
skill.appendChild(heading);
skill.appendChild(skillGraph);
const graph = new Chart(key, {
type: 'doughnut',
data: {
labels: ['XP', 'Total'],
datasets: [
{
label: key,
data: [value, 100 - value],
backgroundColor: [
'rgba(255, 99, 132, 0.5)',
'rgba(255, 99, 132, 0.06)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(255, 99, 132, 0.5)',
],
borderWidth: 1,
},
],
},
});
}
}
/**
* Returns the value of the token cookie
*
* If the cookie is not found, returns undefined
* @returns {string | undefined} The value of the token cookie
*/
function getToken() {
return document.cookie
.split(';')
.find((c) => c.startsWith('token'))
?.split('=')[1];
}