Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
268 lines (227 loc) · 9.73 KB
/
Copy pathmain.js
File metadata and controls
268 lines (227 loc) · 9.73 KB
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
document.getElementById("login-button").addEventListener("click", () => {
const username = document.getElementById("username-email").value;
const password = document.getElementById("login-password").value;
login(username, password);
});
//logoutbutton ka
let isLoggedin = false;
async function login(username, password) {
username = username.trim();
password = password.trim();
const credentials = `${username}:${password}`;
const encodedCredentials = btoa(credentials);
try {
const response = await fetch("https://01.kood.tech/api/auth/signin", {
method: "POST",
headers: {
Authorization: `Basic ${encodedCredentials}`,
},
});
if (response.ok) {
const token = await response.json();
localStorage.setItem('JWTtoken', token);
isLoggedin = true
requestGLData();
document.getElementById('login-container').style.display = 'none';
} else {
const errorData = await response.json()
alert(errorData.error)
}
} catch (error) {
console.log("Error: ", error)
alert(error)
}
}
async function requestGLData() {
const userQuery = `
query {
user {
attrs
id
auditRatio
totalUp
totalDown
createdAt
updatedAt
githubId
transactions(order_by: { createdAt: asc }) {
amount
type
path
createdAt
}
}
}
`
fetch('https://01.kood.tech/api/graphql-engine/v1/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('JWTtoken')}`
},
body: JSON.stringify({ query: userQuery }),
})
.then(response => response.json())
.then(userData => {
document.getElementById("full-container").style.display = "block";
const { firstName, lastName, personalIdentificationCode, } = userData.data.user[0].attrs
const auditRatio = userData.data.user[0].auditRatio
const userID = userData.data.user[0].id
const userJoined = userData.data.user[0].createdAt
const totalUp = userData.data.user[0].totalUp
const totalDown = userData.data.user[0].totalDown
const userAuditRatio = totalUp / totalDown.toFixed(2)
const transactions = userData.data.user[0].transactions
const personalInfo = document.getElementById("personal-info")
const nameElement = document.createElement("p")
nameElement.innerHTML = firstName + " " + lastName
const userIDElement = document.createElement("p");
userIDElement.innerHTML = "Your userID is " + userID
const personalIDElement = document.createElement("p");
personalIDElement.innerHTML = "Your personal identification code is " + personalIdentificationCode
const joined = document.createElement("p");
const joinedDate = new Date(userJoined);
const formattedDate = joinedDate.toLocaleDateString(undefined, { day: 'numeric', month: 'numeric', year: 'numeric' });
joined.innerHTML = "You joined on " + formattedDate;
personalInfo.appendChild(nameElement);
personalInfo.appendChild(userIDElement);
personalInfo.appendChild(personalIDElement);
personalInfo.appendChild(joined);
let sortedArray = [];
for (let transaction of transactions) {
if (transaction.path.startsWith('/johvi/div-01/piscine-js') || transaction.path.startsWith('/johvi/piscine-go')) {
continue
} else if (transaction.type === 'xp') {
sortedArray.push(transaction)
}
}
let cumulativeXpSum = [];
let sum = 0;
let xpVals = [];
for (let transaction of sortedArray) {
let xpValue = transaction.amount;
xpVals.push(xpValue);
}
for (let xpValue of xpVals) {
sum += xpValue;
cumulativeXpSum.push(sum);
}
const dates = sortedArray.map(transaction => {
const date = new Date(transaction.createdAt);
return date.toLocaleDateString(undefined, { day: 'numeric', month: 'numeric', year: 'numeric' });
});
const xpValues = sortedArray.map(transaction => transaction.value);
new Chart("xpChart", {
type: "line",
data: {
labels: dates,
datasets: [{
label: "Cumulative XP",
fill: false,
lineTension: 0,
backgroundColor: "rgba(0, 0, 0, 1.0)",
borderColor: "rgba(255, 255, 255, 1.0)",
data: cumulativeXpSum,
}]
},
options: {
scales: {
y: {
ticks: {
color: 'white', // Change the color of the y-axis ticks to black
beginAtZero: true,
}
},
x: {
ticks: {
color: 'white', // Change the color of the x-axis ticks to black
beginAtZero: true,
}
}
},
plugins: {
title: {
display:true,
text: "Total XP by time",
color: 'white'
},
legend: {
labels: {
color: 'white' // Change the font color of the legend labels here
}
},
tooltip: {
callbacks: {
label: function (context) {
const dateLabel = dates[context.dataIndex];
const xpValue = cumulativeXpSum[context.dataIndex];
const transactionPath = sortedArray[context.dataIndex].path.split('/').pop();
// Customize tooltip content to include date, XP value, and transaction path
return `Date: ${dateLabel}\n Total XP: ${xpValue}\n Project: ${transactionPath}`;
}
}
}
}
}
})
const totalUpPercentage = (totalUp / (totalUp + totalDown)) * 100;
const totalDownPercentage = (totalDown / (totalUp + totalDown)) * 100;
// Create a data array for the auditChart
const auditChartData = [totalUpPercentage, totalDownPercentage];
new Chart("auditChart", {
type: "doughnut",
data: {
labels: ["Total Up", "Total Down"],
datasets: [{
data: auditChartData,
backgroundColor: ["rgba(255,255,255,1.0)", "rgba(0, 0, 0, 1.0)"], // Green for Audit Ratio, Red for Remaining
}]
},
options: {
plugins: {
title: {
display:true,
text: "Audit ratio",
color: 'white'
},
legend: {
display: true,
labels: {
color: 'white' // Change the font color of the legend labels here
}
},
tooltip: {
callbacks: {
label: function (context) {
const dataIndex = context.dataIndex;
const value = auditChartData[dataIndex];
const label = context.label;
// Customize tooltip content based on the side hovered
if (label === "Total Up") {
const totalUpValue = (totalUp / (totalUp + totalDown)) * 100;
return `Total Up: ${totalUpValue.toFixed(2)}% (${totalUp}) bytes`;
} else if (label === "Total Down") {
const totalDownValue = (totalDown / (totalUp + totalDown)) * 100;
return `Total Down: ${totalDownValue.toFixed(2)}% (${totalDown}) bytes`;
}
}
}
}
}
}
});
})
.catch(error => {
console.error('Error:', error);
});
}
document.getElementById("logoutbutton")
logoutbutton.addEventListener("click", () =>{
logout();
})
function logout() {
localStorage.clear();
document.getElementById("full-container").style.display = "none";
document.getElementById("login-container").style.display = "block";
document.getElementById("personal-info").innerHTML = "";
}