-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
459 lines (387 loc) · 15.4 KB
/
script.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// script.js
// Use environment variables or a configuration file for endpoints
const API_BASE_URL = 'https://learn.01founders.co/api';
const graphqlEndpoint = `${API_BASE_URL}/graphql-engine/v1/graphql`;
const signinEndpoint = `${API_BASE_URL}/auth/signin`;
// Wait for the DOM to be fully loaded before attaching event listeners
document.addEventListener('DOMContentLoaded', function () {
// Get the login form element by its ID
const loginForm = document.getElementById('loginForm');
// Check if the login form exists
if (loginForm) {
// Add a 'submit' event listener to the login form
loginForm.addEventListener('submit', function (event) {
event.preventDefault(); // Prevent the default form submission behavior
login(); // Call the login function when the form is submitted
});
}
// Get the logout button element by its ID
const logoutButton = document.getElementById('logoutButton');
// Check if the logout button exists
if (logoutButton) {
// Add a 'click' event listener to the logout button
logoutButton.addEventListener('click', logout);
//logout(); // Call the logout function when the button is clicked
}
});
// Logout function
function logout() {
localStorage.removeItem('token'); // Remove the token from local storage
// Redirect to the login page or perform any other necessary cleanup
window.location.href = 'index.html'; // Adjust the URL based on your project structure
}
// Asynchronous function to handle user login
async function login() {
// Get the values of the email or username and password fields
const identifier = document.getElementById('identifier').value;
const password = document.getElementById('password').value;
// Create a Basic Authentication header using Base64 encoding
const authheader = "Basic " + btoa(`${identifier}:${password}`);
//console.log('Attempting login with:', identifier, password);
try {
// Send a POST request to the authentication endpoint
const res = await fetch(signinEndpoint, {
method: 'POST',
headers: {
'Authorization': authheader,
'Content-Type': 'application/json',
},
});
// Check if the authentication was successful
if (!res.ok) {
// Display an alert if authentication fails
alert('Incorrect email or password. Please try again');
throw new Error('Authentication failed');
}
// Parse the response JSON to get the authentication token
const tokenData = await res.json();
console.log('Received token data:', tokenData);
const authToken = tokenData;
localStorage.setItem("token", authToken);
document.getElementById("loginForm").style.display = "none";
document.getElementById('profilePage').style.display = 'block';
console.log('Stored auth token:', tokenData);
// Fetch user data and XP data
const userData = await getData(userLoginQuery, authToken);
const xpData = await getData(actualXPQuery, authToken);
const skillProg = await getData(skillProgQuery, authToken);
const technoProg = await getData(technoProgQuery, authToken);
console.log('Fetched user data:', userData);
console.log('Fetched XP data:', xpData.data.transaction_aggregate.aggregate.sum.amount);
//console.log('Fetched best skills:', skillProg);
// Extract user information
const user = userData.data.user[0];
const score = userData.data.transaction[0]; // Adjust the index based on your response structure
const userSkillProg = userData.data.user[0].skills;
console.log("User data:", user);
console.log("XP score:", score);
//console.log("Best skills:", userSkillProg);
// Update the DOM with user information
document.getElementById('userId').innerHTML = `Username: ${user.login}<br>ID: ${user.id}<br>Total XP: ${xpData.data.transaction_aggregate.aggregate.sum.amount}`;
//data.transaction_aggregate.aggregate.sum.amount
// Generate and display a bar chart
generateBarChart(skillProg.data.transaction);
generateSecondBarChart(technoProg.data.transaction);
//console.log(skillProgQuery);
} catch (error) {
// Handle errors during login and log to the console
console.error('Error during login:', error);
}
}
// Function to fetch data from GraphQL endpoint
async function getData(query, authToken) {
try {
const res = await fetch(graphqlEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${authToken}`,
},
body: JSON.stringify({ query }),
});
if (!res.ok) {
throw new Error('Failed to fetch data');
}
return await res.json();
} catch (error) {
console.error('Error fetching data:', error);
}
}
//AUDIT RATIO SECTION BEGINS HERE
//basic query to find audit ratio data
const queryAuditRatio = `
query {
transaction {
type
amount
}
}
`;
//used to get user audit ratio from query
async function auditRatio() {
let responseData = await getData(queryAuditRatio)
const transactions = responseData.data.transaction;
let totalUp = 0;
let totalDown = 0;
//chooses particular transactions labeled up and down
transactions.forEach((transaction) => {
if (transaction.type === "up") {
totalUp += transaction.amount;
} else if (transaction.type === "down") {
totalDown += transaction.amount;
}
});
//does the calculation to find audit ratio and then rounds it
const auditRatio = totalUp / totalDown;
const roundedAuditRatio = Math.round(auditRatio * 10) / 10;
// Log or display the calculated audit ratio
console.log("Audit Ratio:", roundedAuditRatio);
return roundedAuditRatio
}
// GraphQL query to fetch user data
const userLoginQuery = `
query {
user {
id
login
}
transaction {
id
type
amount
userId
path
}
}
`;
// GraphQL query to fetch XP data
const actualXPQuery = `
query {
transaction_aggregate(where: {type: {_eq: "xp"}, path: {_regex: "/london/div-01"}, eventId: {_lte: 134}}) {
aggregate {
sum {
amount
}
}
}
}
`;
// GraphQL query to fetch User Best skills
const skillProgQuery = `
query {
transaction(where: { type: { _in: ["skill_prog", "skill_algo", "skill_sys-admin", "skill_front-end", "skill_back-end", "skill_stats", "skill_game" ]}}) {
type
amount
}
}
`;
// GraphQL query to fetch User Technological skills
const technoProgQuery = `
query {
transaction(where: { type: { _in: ["skill_go", "skill_js", "skill_html", "skill_css", "skill_docker", "skill_sql", "skill_graphql"]}}) {
type
amount
}
}
`;
// Function to generate a bar chart using D3.js
function generateBarChart(skillProgData) {
console.log(skillProgData)
try {
// Extract skill IDs and amounts from the fetched data
const data = skillProgData.map(item => ({
skill_id: item.type,
amount: item.amount
}));
console.log("THIS IS SKILLPROGQUERY DATA", data)
// Initialize total amounts for each skill category
let totalSkillprog = 0;
let totalSkillalgo = 0;
let totalSkillsysadmin = 0;
let totalSkillfrontend = 0;
let totalSkillbackend = 0;
let totalSkillstats = 0;
let totalSkillgame = 0;
// Accumulate the amounts based on skill categories
data.forEach((transaction) => {
if (transaction.skill_id === "skill_prog") {
totalSkillprog += transaction.amount;
} else if (transaction.skill_id === "skill_algo") {
totalSkillalgo += transaction.amount;
} else if (transaction.skill_id === "skill_sys-admin") {
totalSkillsysadmin += transaction.amount;
} else if (transaction.skill_id === "skill_front-end") {
totalSkillfrontend += transaction.amount;
} else if (transaction.skill_id === "skill_back-end") {
totalSkillbackend += transaction.amount;
} else if (transaction.skill_id === "skill_stats") {
totalSkillstats += transaction.amount;
} else if (transaction.skill_id === "skill_game") {
totalSkillgame += transaction.amount;
}
});
console.log("this is prog amount", totalSkillprog);
console.log("this is algo amount", totalSkillalgo);
console.log("this is sys admin amount", totalSkillsysadmin);
console.log("this is frontend amount", totalSkillfrontend);
console.log("this is backend amount", totalSkillbackend);
console.log("this is stats amount", totalSkillstats);
console.log("this is game amount", totalSkillgame);
// Data for the bar chart
const chartData = [
{ skill: "Prog", amount: totalSkillprog },
{ skill: "Algo", amount: totalSkillalgo },
{ skill: "Sys Admin", amount: totalSkillsysadmin },
{ skill: "Frontend", amount: totalSkillfrontend },
{ skill: "Backend", amount: totalSkillbackend },
{ skill: "Stats", amount: totalSkillstats },
{ skill: "Game", amount: totalSkillgame },
];
// Set up SVG dimensions
const width = 800;
const height = 400;
const margin = { top: 20, right: 20, bottom: 50, left: 50 };
const barWidth = (width - margin.left - margin.right) / chartData.length;
// Select the container
const container = document.getElementById('firstBarChart');
container.innerHTML = ''; // Clear any previous SVG content
// Create SVG element
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("width", width);
svg.setAttribute("height", height);
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
// Find the maximum amount for scaling
const maxAmount = Math.max(...chartData.map(d => d.amount));
// Create bars and labels
chartData.forEach((d, i) => {
// Calculate bar height
const barHeight = (d.amount / maxAmount) * (height - margin.top - margin.bottom);
// Create bar
const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect.setAttribute("x", margin.left + i * barWidth);
rect.setAttribute("y", height - margin.bottom - barHeight);
rect.setAttribute("width", barWidth - 10); // 10 is for spacing between bars
rect.setAttribute("height", barHeight);
rect.setAttribute("fill", "steelblue");
svg.appendChild(rect);
// Create label for value
const text = document.createElementNS("http://www.w3.org/2000/svg", "text");
text.setAttribute("x", margin.left + i * barWidth + (barWidth - 10) / 2);
text.setAttribute("y", height - margin.bottom - barHeight - 5);
text.setAttribute("text-anchor", "middle");
text.textContent = d.amount;
svg.appendChild(text);
// Create label for skill name
const skillText = document.createElementNS("http://www.w3.org/2000/svg", "text");
skillText.setAttribute("x", margin.left + i * barWidth + (barWidth - 10) / 2);
skillText.setAttribute("y", height - margin.bottom + 20);
skillText.setAttribute("text-anchor", "middle");
skillText.textContent = d.skill;
svg.appendChild(skillText);
});
// Append SVG to the container
container.appendChild(svg);
} catch (error) {
console.error('Error generating bar chart:', error);
}
}
// Function to generate a bar chart using D3.js
function generateSecondBarChart(technoProgData) {
console.log(technoProgData)
try {
// Extract skill IDs and amounts from the fetched data
const data = technoProgData.map(item => ({
skill_id: item.type,
amount: item.amount
}));
console.log("THIS IS TECHNOPROGQUERY DATA", data)
// Initialize total amounts for each skill category
let totalSkillgo = 0;
let totalSkilljs = 0;
let totalSkillhtml = 0;
let totalSkillcss = 0;
let totalSkilldocker = 0;
let totalSkillsql = 0;
let totalSkillgraphql = 0;
// Accumulate the amounts based on skill categories
data.forEach((transaction) => {
if (transaction.skill_id === "skill_go") {
totalSkillgo += transaction.amount;
} else if (transaction.skill_id === "skill_js") {
totalSkilljs += transaction.amount;
} else if (transaction.skill_id === "skill_html") {
totalSkillhtml += transaction.amount;
} else if (transaction.skill_id === "skill_css") {
totalSkillcss += transaction.amount;
} else if (transaction.skill_id === "skill_docker") {
totalSkilldocker += transaction.amount;
} else if (transaction.skill_id === "skill_sql") {
totalSkillsql += transaction.amount;
} else if (transaction.skill_id === "skill_graphql") {
totalSkillgraphql += transaction.amount;
}
});
console.log("this is go amount", totalSkillgo);
console.log("this is js amount", totalSkilljs);
console.log("this is html amount", totalSkillhtml);
console.log("this is css amount", totalSkillcss);
console.log("this is docker amount", totalSkilldocker);
console.log("this is sql amount", totalSkillsql);
console.log("this is graphql amount", totalSkillgraphql);
// Data for the bar chart
const chartData = [
{ skill: "Go", amount: totalSkillgo },
{ skill: "JavaScript", amount: totalSkilljs },
{ skill: "HTML", amount: totalSkillhtml },
{ skill: "CSS", amount: totalSkillcss },
{ skill: "Docker", amount: totalSkilldocker },
{ skill: "SQL", amount: totalSkillsql },
{ skill: "GraphQL", amount: totalSkillgraphql },
];
// Set up SVG dimensions
const width = 800;
const height = 400;
const margin = { top: 20, right: 20, bottom: 50, left: 50 };
const barWidth = (width - margin.left - margin.right) / chartData.length;
// Select the container
const container = document.getElementById('secondBarChart');
container.innerHTML = ''; // Clear any previous SVG content
// Create SVG element
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("width", width);
svg.setAttribute("height", height);
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
// Find the maximum amount for scaling
const maxAmount = Math.max(...chartData.map(d => d.amount));
// Create bars and labels
chartData.forEach((d, i) => {
// Calculate bar height
const barHeight = (d.amount / maxAmount) * (height - margin.top - margin.bottom);
// Create bar
const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect.setAttribute("x", margin.left + i * barWidth);
rect.setAttribute("y", height - margin.bottom - barHeight);
rect.setAttribute("width", barWidth - 10); // 10 is for spacing between bars
rect.setAttribute("height", barHeight);
rect.setAttribute("fill", "steelblue");
svg.appendChild(rect);
// Create label for value
const text = document.createElementNS("http://www.w3.org/2000/svg", "text");
text.setAttribute("x", margin.left + i * barWidth + (barWidth - 10) / 2);
text.setAttribute("y", height - margin.bottom - barHeight - 5);
text.setAttribute("text-anchor", "middle");
text.textContent = d.amount;
svg.appendChild(text);
// Create label for skill name
const skillText = document.createElementNS("http://www.w3.org/2000/svg", "text");
skillText.setAttribute("x", margin.left + i * barWidth + (barWidth - 10) / 2);
skillText.setAttribute("y", height - margin.bottom + 20);
skillText.setAttribute("text-anchor", "middle");
skillText.textContent = d.skill;
svg.appendChild(skillText);
});
container.appendChild(svg);
} catch (error) {
console.error('Error generating bar chart:', error);
}
}