-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
607 lines (549 loc) · 21.6 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// Function for displaying the data as table
function showData(columnNames, dataArray) {
let table = document.createElement('table');
document.body.appendChild(table);
// Row for Headings
let tableHeadRow = document.createElement('tr');
table.appendChild(tableHeadRow);
for (let i = 0; i < columnNames.length; i++) {
let th = document.createElement('th'),
thText = document.createTextNode(columnNames[i]);
th.appendChild(thText);
tableHeadRow.appendChild(th);
}
// Rows for all entries
for (let i = 0; i < dataArray.length; i++) {
let row = dataArray[i];
let tableRow = document.createElement('tr');
for (let j = 0; j < row.length; j++) {
table.appendChild(tableRow);
let td = document.createElement('td');
let tdText = document.createTextNode(row[j]);
td.appendChild(tdText);
tableRow.appendChild(td);
}
}
}
// Function for converting Registration Time Stamp and Birthdate strings to Date object
function stringToDate(d) {
// 17/05/2021 16:30:50
// 17/05/2021
let date = d.substr(0, 2);
let month = d.substr(3, 2);
let year = d.substr(6, 4);
// let result = year + "-" + month + "-" + date;
let result = `${year}-${month}-${date}`;
if (d.length == 19) { // for Registration Time Stamp
let hour = d.substr(11, 2);
let minute = d.substr(14, 2);
let second = d.substr(17, 2);
// result += "T" + hour + ":" + minute + ":" + second;
result += `T${hour}:${minute}:${second}`;
}
return new Date(result);
}
// Function for updating/correcting category column
function updateCatetgory(data, dividingDate) {
let date = new Date(dividingDate);
data.forEach((element, index) => {
if (element[4] == 4 || element[4] == 5) {
if (stringToDate(element[3]).getTime() < date.getTime()) {
data[index][4] = 4;
}
else {
data[index][4] = 5;
}
}
});
}
// Function for updating birthdate and registration time to Date Object
function updateDateTime(dataset) {
dataset.forEach((element, index) => {
dataset[index][3] = stringToDate(element[3]);
dataset[index][5] = stringToDate(element[5]);
});
}
// Function to find Days Span of dataset
function findDaysSpan(d1, d2) {
let date1 = new Date(d1.getTime());
let date2 = new Date(d2.getTime());
date1.setHours(0, 0, 0);
date2.setHours(0, 0, 0);
return (date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24) + 1;
}
// Function to calculate schedule
function calculateSchedule(dataset, vaccinesPerDay, algorithm = 'fcfs') {
// Sorting according to Registration Time
// dataset.sort((a, b) => a[5] - b[5]);
let currentDate = new Date(dataset[0][5].getTime());
currentDate.setHours(0, 0, 0);
currentDate.setDate(currentDate.getDate() + 1);
let buffer = [];
let dayOfVaccine = [];
let vaccinetedPerDay = [];
let vaccineSequence = [];
let daysFromRegistration = [];
let dayCount = 0;
function priorityCompare(a, b) {
if (dataset[a][4] < dataset[b][4]) {
return -1;
}
else if (dataset[a][4] > dataset[b][4]) {
return 1;
}
else {
return dataset[a][5] - dataset[b][5];
}
}
// Adding elements to buffer
dataset.forEach((element, index) => {
if (element[5].getTime() >= currentDate.getTime()) {
dayCount++;
if (algorithm == "priority") {
buffer.sort(priorityCompare);
}
let filteredBuffer = buffer.filter(bufferElement =>
dataset[bufferElement][5].getTime() < currentDate.getTime());
let today = filteredBuffer.splice(0, Math.min(vaccinesPerDay, filteredBuffer.length));
buffer = buffer.filter(x => !today.includes(x));
vaccineSequence = vaccineSequence.concat(today);
dayOfVaccine = dayOfVaccine.concat(Array(today.length).fill(dayCount));
vaccinetedPerDay.push(today.length);
today.forEach(element => {
daysFromRegistration.push(findDaysSpan(dataset[element][5], currentDate));
daysFromRegistration[daysFromRegistration.length - 1]--;
});
currentDate.setDate(currentDate.getDate() + 1);
}
buffer.push(index);
});
// If the buffer has some elements
while (buffer.length) {
dayCount++;
if (algorithm == "priority") {
buffer.sort(priorityCompare);
}
let filteredBuffer = buffer.filter(bufferElement =>
dataset[bufferElement][5].getTime() < currentDate.getTime());
let today = filteredBuffer.splice(0, Math.min(vaccinesPerDay, filteredBuffer.length));
buffer = buffer.filter(x => !today.includes(x));
vaccineSequence = vaccineSequence.concat(today);
dayOfVaccine = dayOfVaccine.concat(Array(today.length).fill(dayCount));
vaccinetedPerDay.push(today.length);
today.forEach(element => {
daysFromRegistration.push(findDaysSpan(dataset[element][5], currentDate));
daysFromRegistration[daysFromRegistration.length - 1]--;
});
currentDate.setDate(currentDate.getDate() + 1);
}
return {
dayOfVaccine,
vaccineSequence,
vaccinetedPerDay,
daysFromRegistration
};
}
//Function to get Vaccines Per Day Data
function getVaccineSchedulerData(dataset, datasetDaysSpan) {
let vaccineSchedulerData = [[], []];
let algorithms = ['fcfs', 'priority'];
for (let vaccinesPerDay = 1; ; vaccinesPerDay += 1) {
algorithms.forEach((algorithm, index) => {
let scheduleResult = calculateSchedule(dataset, vaccinesPerDay, algorithm);
vaccineSchedulerData[index].push(scheduleResult);
});
if (vaccineSchedulerData[0][vaccinesPerDay - 1].vaccinetedPerDay.length == datasetDaysSpan) {
break;
}
}
return vaccineSchedulerData;
}
// Vaccine Day Chart
function vaccineDayChart(vaccinesPerDayData, vaccinesPerDayLabelData) {
let vaccinesPerDayChartCanvas = document.createElement('canvas');
vaccinesPerDayChartCanvas.id = "vaccines-day-chart";
let vaccinesPerDayChartDiv = document.createElement('div');
vaccinesPerDayChartDiv.id = "vaccines-day-chart-div";
vaccinesPerDayChartDiv.appendChild(vaccinesPerDayChartCanvas);
document.body.appendChild(vaccinesPerDayChartDiv);
new Chart(document.getElementById('vaccines-day-chart'), {
type: 'line',
data: {
labels: vaccinesPerDayLabelData,
datasets: [
{
label: "Max Number Of Days",
borderColor: '#DC3912',
data: vaccinesPerDayData
}
]
},
options: {
plugins: {
title: {
display: true,
text: ['Vaccines Per Day', 'Maximum Number of Days of Vaccination']
}
},
responsive: true,
scales: {
x: {
display: true,
title: {
display: true,
text: 'Vaccines Per Day'
}
},
y: {
display: true,
title: {
display: true,
text: ['Maximum Number of Days of Vaccination', 'Logairthmic Scale']
},
type: 'logarithmic'
}
}
}
});
}
// Score Chart
function scoreChart(scoreChartData, scoreLabelData) {
let scoreChartCanvas = document.createElement('canvas');
scoreChartCanvas.id = "score-chart";
let scoreChartDiv = document.createElement('div');
scoreChartDiv.id = "score-chart-div";
scoreChartDiv.appendChild(scoreChartCanvas);
document.body.appendChild(scoreChartDiv);
new Chart(document.getElementById('score-chart'), {
type: 'line',
data: {
labels: scoreLabelData,
datasets: [
{
label: "FCFS",
borderColor: '#109618',
data: scoreChartData[0],
yAxisID: 'y'
},
{
label: "Priority",
borderColor: '#990099',
data: scoreChartData[1],
yAxisID: 'y'
},
{
label: "Difference (Priority - FCFS)",
borderColor: '#FF9900',
data: scoreChartData[1].map((x, i) => (x - scoreChartData[0][i])),
yAxisID: 'y1'
}
]
},
options: {
plugins: {
title: {
display: true,
text: ['Scoring Algorithms', 'FCFS vs Priority']
}
},
stacked: true,
responsive: true,
scales: {
x: {
display: true,
title: {
display: true,
text: 'Vaccines Per Day'
}
},
y: {
display: true,
title: {
display: true,
text: ['Score']
},
position: 'left'
// type: 'logarithmic'
},
y1: {
type: 'linear',
display: true,
position: 'right',
display: true,
title: {
display: true,
text: ['Score Difference']
},
grid: {
drawOnChartArea: false
},
}
}
}
});
}
//Category Chart
function categoryChart(categoryChartData, categoryLabelData, numberOfVaccinesPerDay) {
let categoryChartCanvas = document.createElement('canvas');
categoryChartCanvas.id = "category-chart";
let categoryChartDiv = document.createElement('div');
categoryChartDiv.id = "category-chart-div";
categoryChartDiv.appendChild(categoryChartCanvas);
document.body.appendChild(categoryChartDiv);
new Chart(document.getElementById('category-chart'), {
type: 'bar',
data: {
labels: categoryLabelData,
datasets: [
{
label: "FCFS - Category 1",
backgroundColor: '#0C0636',
data: categoryChartData[0][0],
stack: 'FCFS'
},
{
label: "FCFS - Category 2",
backgroundColor: '#095169',
data: categoryChartData[0][1],
stack: 'FCFS'
},
{
label: "FCFS - Category 3",
backgroundColor: '#059B9A',
data: categoryChartData[0][2],
stack: 'FCFS'
},
{
label: "FCFS - Category 4",
backgroundColor: '#53BA83',
data: categoryChartData[0][3],
stack: 'FCFS'
},
{
label: "FCFS - Category 5",
backgroundColor: '#9FD86B',
data: categoryChartData[0][4],
stack: 'FCFS'
},
{
label: "Priority - Category 1",
backgroundColor: '#402929',
data: categoryChartData[1][0],
stack: 'Priority'
},
{
label: "Priority - Category 2",
backgroundColor: '#B80000',
data: categoryChartData[1][1],
stack: 'Priority'
},
{
label: "Priority - Category 3",
backgroundColor: '#FF4000',
data: categoryChartData[1][2],
stack: 'Priority'
},
{
label: "Priority - Category 4",
backgroundColor: '#FFA700',
data: categoryChartData[1][3],
stack: 'Priority'
},
{
label: "Priority - Category 5",
backgroundColor: '#F8F9AC',
data: categoryChartData[1][4],
stack: 'Priority'
},
]
},
options: {
plugins: {
title: {
display: true,
text: ['Vaccines To a Category', 'Comparision of algorithms to which category vaccine given', `Vaccines Per Day : ${numberOfVaccinesPerDay}`]
}
},
stacked: true,
responsive: true,
scales: {
x: {
display: true,
title: {
display: true,
text: ['Algorithm', 'Day Number']
},
stacked: true
},
y: {
display: true,
title: {
display: true,
text: ['Number of Vaccines to a particular category']
},
stacked: true,
ticks: {
stepSize: 1
}
}
}
}
});
}
// Doughtnut Category Chart
function doughnutCategoryChart(doughnutCategoryChartData) {
let doughnutCategoryChartCanvas = document.createElement('canvas');
doughnutCategoryChartCanvas.id = "doughnut-category-chart";
let doughnutCategoryChartDiv = document.createElement('div');
doughnutCategoryChartDiv.id = "doughnut-category-chart-div";
doughnutCategoryChartDiv.appendChild(doughnutCategoryChartCanvas);
document.body.appendChild(doughnutCategoryChartDiv);
new Chart(document.getElementById('doughnut-category-chart'), {
type: 'doughnut',
data: {
labels: ['Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5'],
datasets: [
{
label: "Category Data",
data: doughnutCategoryChartData,
backgroundColor: ['#370617', '#9d0208', '#dc2f02', '#f48c06', '#ffba08']
}
]
},
options: {
plugins: {
title: {
display: true,
text: ['Category Distribution']
}
},
responsive: true,
}
});
}
// Get Schedule Date
function getSpecificSchedule(aadharNumber, currentAlgorithm, numberOfVaccinesPerDay, dataset, vaccineSchedulerData) {
algorithmMap = {
'fcfs': 0,
'priority': 1
}
console.log(dataset);
let currentElementIndex = dataset.findIndex(x => x[1] == aadharNumber);
let aadharResult = document.createElement('p');
if (currentElementIndex == -1) {
aadharResult.innerText = "Enter Valid Aadhar Number.";
}
else {
let dayAfterStart = (vaccineSchedulerData[algorithmMap[currentAlgorithm]][numberOfVaccinesPerDay].dayOfVaccine[
vaccineSchedulerData[algorithmMap[currentAlgorithm]][numberOfVaccinesPerDay].vaccineSequence.findIndex(x => x == currentElementIndex)]);
console.log(dayAfterStart);
let resultDate = new Date(dataset[0][5]);
resultDate.setHours(0, 0, 0);
resultDate.setDate(resultDate.getDate() + dayAfterStart);
aadharResult.innerHTML = `${dataset[currentElementIndex][0]} :
Your Vaccination Date is : ${resultDate.getDate()}/${resultDate.getMonth()}/${resultDate.getFullYear()}`;
}
document.body.appendChild(aadharResult);
}
// Event Listener for Calculate
document.getElementById("calculate").onclick = () => {
// When calculate is pressed, Show Data button appears
let showDataButton = document.getElementById("show-data-button");
showDataButton.classList.remove("visible");
// Accessing the file uploaded in input
let file = document.getElementById("file");
readXlsxFile(file.files[0]).then(function (data) {
// Removing the features row from main dataset
dataColumns = data.shift();
// data = data.slice(0, 20);
//Updating Category column for values 4 and 5 accoording to birthdate
updateCatetgory(data, "1976-05-10T00:00:00");
// Event Listener for Show Data button
showDataButton.onclick = () => {
showData(dataColumns, data.slice(0, 20));
};
// Copying the data to run operations on it
let dataset = JSON.parse(JSON.stringify(data));
// Updating Birthdate and Registration Time columns to Date object
updateDateTime(dataset);
dataset.sort((a, b) => a[5] - b[5]);
console.log(dataset);
let datasetDaysSpan = findDaysSpan(dataset[0][5], dataset[dataset.length - 1][5]);
let vaccineSchedulerData = getVaccineSchedulerData(dataset, datasetDaysSpan);
console.log(vaccineSchedulerData);
// Vaccine Per Day Chart
let vaccinesPerDayData = [];
vaccineSchedulerData[0].forEach(element => {
vaccinesPerDayData.push(element.vaccinetedPerDay.length);
});
let vaccinesPerDayLabelData = Array.from(Array(vaccineSchedulerData[0].length + 1).keys()).slice(1,);
vaccineDayChart(vaccinesPerDayData, vaccinesPerDayLabelData);
//Score Chart
let scoreChartData = [[], []];
vaccineSchedulerData.forEach((algorithmData, algorithmIndex) => {
algorithmData.forEach(datapoint => {
let currentScore = 0;
datapoint.vaccineSequence.forEach((element, index) => {
currentScore += (5 * (dataset.length - datapoint.daysFromRegistration[index]) + (6 - dataset[element][4])) ** 2;
});
scoreChartData[algorithmIndex].push(currentScore ** 0.5);
});
});
console.log(scoreChartData);
let scoreLabelData = Array.from(Array(vaccineSchedulerData[0].length + 1).keys()).slice(1,);
scoreChart(scoreChartData, scoreLabelData);
// Category Chart
let numberOfVaccinesPerDay = Number(document.getElementById('vaccines-per-day').value);
if (numberOfVaccinesPerDay > vaccineSchedulerData[0].length) {
numberOfVaccinesPerDay = vaccineSchedulerData[0].length;
}
let categoryChartData = [];
let algorithms = ['fcfs', 'priority'];
algorithms.forEach((algorithm, algorithmIndex) => {
let currentElement = vaccineSchedulerData[algorithmIndex][numberOfVaccinesPerDay - 1];
let algorithmCategoryChartData = [];
for (let i = 0; i < 5; i++) {
algorithmCategoryChartData.push(new Array(currentElement.vaccinetedPerDay.length).fill(0));
}
for (let i = 1; i <= currentElement.vaccinetedPerDay.length; i++) {
let currentDayIndexes = currentElement.vaccineSequence.filter((x, ind) => currentElement.dayOfVaccine[ind] == i);
currentDayIndexes.forEach(element => {
algorithmCategoryChartData[dataset[element][4] - 1][i - 1]++;
});
}
categoryChartData.push(algorithmCategoryChartData);
});
console.log(categoryChartData);
let categoryLabelData = Array.from(Array(categoryChartData[0][0].length + 1).keys()).slice(1,);
categoryChart(categoryChartData, categoryLabelData, numberOfVaccinesPerDay);
// Doughnut Chart
let doughnutCategoryChartData = new Array(5).fill(0);
dataset.forEach(element => {
doughnutCategoryChartData[element[4] - 1]++;
});
console.log(doughnutCategoryChartData);
doughnutCategoryChart(doughnutCategoryChartData);
// Aadhar Number Result
let aadharNumberInputLabel = document.createElement('label');
aadharNumberInputLabel.for = 'aadhar-number';
aadharNumberInputLabel.innerHTML = '<strong>Enter Your Aadhar Number : </strong>';
document.body.appendChild(aadharNumberInputLabel);
let aadharNumberInput = document.createElement("input");
aadharNumberInput.id = 'aadhar-number';
aadharNumberInput.name = 'aadhar-number';
aadharNumberInput.type = 'number';
aadharNumberInput.min = 200000000000;
aadharNumberInput.max = 999999999999;
aadharNumberInput.value = dataset[0][1];
document.body.appendChild(aadharNumberInput);
let aadharNumberSubmit = document.createElement('button');
aadharNumberSubmit.innerText = "Get Date";
aadharNumberSubmit.id = "aadhar-number-button";
document.body.appendChild(aadharNumberSubmit);
document.getElementById('aadhar-number-button').onclick = () => {
let aadharNumber = Number(document.getElementById('aadhar-number').value);
let currentAlgorithm = document.querySelector('input[name="algorithm"]:checked').value;
getSpecificSchedule(aadharNumber, currentAlgorithm, numberOfVaccinesPerDay, dataset, vaccineSchedulerData);
};
});
};