-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlook.js
More file actions
571 lines (515 loc) · 23.4 KB
/
look.js
File metadata and controls
571 lines (515 loc) · 23.4 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
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
var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
var popoverList = popoverTriggerList.map(function (popoverTriggerEl) {
return new bootstrap.Popover(popoverTriggerEl)
});
var alertList = document.querySelectorAll('.alert')
alertList.forEach(function (alert) {
new bootstrap.Alert(alert)
});
// GETTING ALL VARIABLES AND INPUTS
document.getElementById("chart-image").classList.toggle("show-element");
var ctx = document.getElementById("chart").getContext("2d");
var requests = document.getElementById("requests");
var maxTrack = document.getElementById("max-track");
var headPosition = document.getElementById("head");
var tracks = document.getElementById("tracks");
var run = document.getElementById("run");
run.classList.toggle("disabled");
var dir = document.getElementById("direction");
var yrange = 0, head = 0, xrange = 0;
var xlabel = [], ylabel=[];
var fisrtTime = true;
var algoChart = new Chart(ctx, {});
var trackRequests;
//FUNCITON THAT RETURNS THE SCAN ARRAY
function look(trequests, headpos, direction, max){
let tr = trequests;
var requestorder = [];
let i = 0;
if(tr.indexOf(headpos)!==-1){
tr.splice(tr.indexOf(headpos), 1);
yrange -=1;
}
tr.sort(function(b,c) {
return b - c
});
if(direction==="right"){
let startindex;
for(i=0; i<yrange; i++){
if(tr[i]>headpos){
startindex = i;
break;
}
}
for(i=startindex; i<yrange; i++){
requestorder.push(tr[i]);
}
for(i=startindex-1; i>-1; i--){
requestorder.push(tr[i]);
}
}
else if(direction==="left"){
let startindex;
for(i=0; i<yrange; i++){
if(tr[i]>headpos){
startindex = i-1;
break;
}
}
for(i=startindex; i>-1; i--){
requestorder.push(tr[i]);
}
for(i=startindex+1; i<yrange; i++){
requestorder.push(tr[i]);
}
}
return requestorder;
}
// FUCNTION TO CALCULATE SEEK OPERATIONS
function seekOperations(requestorder, headpos){
var seektime = 0 ;
seektime += Math.abs(headpos - requestorder[0]);
for(var i=0;i<requestorder.length-1;i++){
seektime += Math.abs(requestorder[i+1] - requestorder[i]);
}
return seektime;
}
function seekOperationsCalculations(requestorder, headpos){
var calc = '';
for(let i=0; i<requestorder.length; i++){
if(i===0){
calc += '|'+headpos+'-'+requestorder[i]+'|';
}
else{
calc += ' + '+'|'+requestorder[i-1]+'-'+requestorder[i]+'|';
}
}
return calc;
}
function execute() {
document.getElementById("alert-wrapper").innerHTML = ``;
document.getElementById("chart-image").style.display = "flex";
document.getElementById("chart-container").style.display = "none";
let allOk = true;
var str = '';
if (requests.value === '') {
str = 'Number of requests cannot be left blank!';
document.getElementById("alert-wrapper").innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin: 15px;">
<strong>Warning!</strong> ${str}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
allOk = false;
}
if (Number(requests.value) <= 0 && allOk) {
str = 'The number of request should be greater than 0!';
document.getElementById("alert-wrapper").innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin: 15px;">
<strong>Warning!</strong> ${str}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
allOk = false;
}
if (maxTrack.value === '' && allOk) {
str = 'Maximum number of tracks cannot be left blank!';
document.getElementById("alert-wrapper").innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin: 15px;">
<strong>Warning!</strong> ${str}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
allOk = false;
}
if (Number(maxTrack.value) <= 0 && allOk) {
str = 'Maximum number of tracks should be greater than 0!';
document.getElementById("alert-wrapper").innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin: 15px;">
<strong>Warning!</strong> ${str}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
allOk = false;
}
if (headPosition.value === '' && allOk) {
str = 'The starting head position needs to be mentioned! It cannot be left blank.'
document.getElementById("alert-wrapper").innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin: 15px;">
<strong>Warning!</strong> ${str}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
allOk = false;
}
if ((Number(headPosition.value) < 0 || Number(headPosition.value) > Number(maxTrack.value)) && allOk) {
str = 'The starting head position of must lie between 0 and maximum track number.';
document.getElementById("alert-wrapper").innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin: 15px;">
<strong>Warning!</strong> ${str}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
allOk = false;
}
trackRequests = [];
if ((tracks.value.split('')).indexOf(',') === -1) {
trackRequests = (tracks.value.split(' ')).map(Number);
}
else {
trackRequests = (tracks.value.split(',')).map(Number);
}
trackRequests.forEach((x) => {
if (x < 0 || x > Number(maxTrack.value) && allOk) {
str = 'All the track requests must lie between 0 and maximum track number.';
document.getElementById("alert-wrapper").innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin: 15px;">
<strong>Warning!</strong> ${str}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
allOk = false;
}
});
if(trackRequests.length != Number(requests.value) && allOk){
str = 'Please make sure that the number of track requests in the array match the total number of requests.';
document.getElementById("alert-wrapper").innerHTML = `
<div class="alert alert-danger alert-dismissible fade show" role="alert" style="margin: 15px;">
<strong>Warning!</strong> ${str}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>`;
allOk = false;
}
if (allOk) {
run.classList.toggle("disabled");
head = Number(headPosition.value);
xrange = Number(maxTrack.value);
// CALLING THE REQUIRED FUNCTION FOR GETTING THE FINAL ARRAY
trackRequests = [...new Set(trackRequests)];
yrange = Number(trackRequests.length);
trackRequests = look(trackRequests, head, String(dir.value), xrange);
yrange = Number(trackRequests.length);
for (var i = 0; i <= xrange; i++) {
xlabel[i] = i;
}
for (i = 0; i <= yrange; i++) {
ylabel[i] = i;
}
// FOR HIDING THE IMAGE AND DISPLAYING THE IMAGE
document.getElementById("chart-image").style.display = "none";
document.getElementById("chart-container").style.display = "block";
// FOR PROGRESS BAR
document.getElementById('seek').style.width = '100%';
var progressWrapper = document.getElementById("seek");
progressWrapper.innerHTML =
`<div id="progressBarContainer" class="progress animate__animated animate__backInUp">
<div id="progressBar" class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" style="width: 0%" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100"></div>
</div>`;
document.getElementById("dImageIcon").innerHTML = `<a id="url"></a>`;
document.getElementById("dPDFIcon").innerHTML = `<a id="genPDF"></a>`;
document.getElementById("download-buttons").style.display = 'none';
var progressBar = document.getElementById("progressBar");
var progressBarContainer = document.getElementById("progressBarContainer");
// FOR DOWNLOAD BUTTON
function done() {
let dbut = document.getElementById("download-buttons");
dbut.style.display = 'flex';
dbut.style.flexDirection = 'row';
dbut.style.width = 'fit-content';
var ImageURL;
var url = algoChart.toBase64Image();
document.getElementById("url").href = url;
// CONVERTING BASE64 URL TO DATAURL FOR PDF
var imgw, imgh;
var imgurl = new Image();
imgurl.onload = function(){
imgw = imgurl.width;
imgh = imgurl.height;
var canvas = document.createElement("canvas");
canvas.width = imgw;
canvas.height = imgh;
var ctx2 = canvas.getContext("2d");
ctx2.fillStyle="#FFFFFF";
ctx2.fillRect(0, 0, canvas.width, canvas.height);
ctx2.drawImage(this, 0, 0, imgw, imgh);
ImageURL = canvas.toDataURL("image/png", 1);
}
imgurl.src = url;
// NEW CODE TO BE ADDED IN OTHER JS FILES
var genPDF = document.getElementById("genPDF");
genPDF.addEventListener('click', () => {
var doc = new jsPDF('p', 'mm', 'a4');
doc.setFontSize(25);
doc.setFont('times', 'bold', '100');
doc.text("LOOK Algorithm", (doc.internal.pageSize.width/2), 18, 'center');
doc.line(0,30,doc.internal.pageSize.width,30,'S');
// GIVEN INFORMATION
doc.setFontSize(14);
doc.text('Given Information', 10, 40);
doc.setFontSize(12);
doc.setFont('times', 'normal', 'normal');
doc.text('Number of track requests: '+ requests.value, 20, 50);
doc.text('Total number of tracks: '+ maxTrack.value, 20, 58);
doc.text('Initial head position: '+ headPosition.value, 20, 66);
var tReq;
if ((tracks.value.split('')).indexOf(',') === -1) {
tReq = (tracks.value.split(' ')).map(Number);
}
else {
tReq = (tracks.value.split(',')).map(Number);
}
doc.text('Track requests: '+ tReq.join(', '), 20, 74);
tReq='';
for(let i=0; i<trackRequests.length; i++){
if(i===0){
tReq += String(trackRequests[i]);
}
else{
tReq += ', '+String(trackRequests[i]);
}
}
doc.setFont('times', 'bold', '100');
doc.text('Order in which tracks are serviced: '+ tReq, 20, 82);
// SEEK OPERATIONS
doc.setFontSize(14);
doc.text('Calculation of seek operations', 10, 97);
doc.setFontSize(12);
doc.setFont("Times", "Roman");
var seekCalc = 'Total Seek Operations = '+seekOperationsCalculations(trackRequests, head);
var calc = doc.splitTextToSize(seekCalc, 180);
doc.text(calc,20,107);
doc.setFont("Times","bold");
doc.text('Thus, total seek time = ' + String(seekOperations(trackRequests, head))+'ms (Considering successive track seek time as 1ms)', 20, 121);
var seekTime = 'Average Seek Time = '+String(Math.round((xrange/3)*100)/100)+'ms (Time taken by the header to move across one third of total tracks)';
seekTime = doc.splitTextToSize(seekTime, 180);
doc.text(seekTime, 20, 131);
var note = 'If your disk takes "x" amount of time (in milliseconds) to seek across successive tracks, then multiply the above results by "x" to get the correct results';
note = doc.splitTextToSize(note, 180);
doc.text(note, 20, 144);
// TRACK SERVICING CHART
let factorw = imgw / doc.internal.pageSize.width;
let factorh = imgh / ((doc.internal.pageSize.height / 2) - 15);
let xshift;
if (factorw > factorh && factorw > 1) {
xshift = (doc.internal.pageSize.width - ((imgw / factorw) - 10)) / 2;
doc.addImage(ImageURL, 'PNG', xshift, 158, (imgw / factorw) - 10, (imgh / factorw));
}
else if (factorh > factorw && factorh > 1) {
xshift = (doc.internal.pageSize.width - ((imgw / factorh) - 10)) / 2;
doc.addImage(ImageURL, 'PNG', xshift, 158, (imgw / factorh) - 10, (imgh / factorh));
}
else {
doc.addImage(ImageURL, 'PNG', 7, 158, (imgw) - 10, (imgh));
}
// FINALLY SAVING THE PDF
doc.save('LOOK.pdf');
});
}
// THE CHART ITSELF
algoChart.destroy();
algoChart = new Chart(ctx, {
type: 'line',
data: {
labels: xlabel,
datasets: [
{
label: "Showing",
yAxisID: "first",
xAxisID: "scale",
fill: false,
borderColor: 'black',
pointBackgroundColor: 'rgba(168, 255, 120,1)',
pointHoverBackgroundColor: 'red',
pointBorderWidth: 2,
pointRadius: 4,
pointHoverRadius: 6,
lineTension: 0.2,
data: []
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
fontFamily: 'Roboto Slab',
fontSize: 30,
fontColor: 'black',
text: 'LOOK Graph'
},
hover:{
mode: 'index',
axis: 'x',
},
legend: {
display: false,
},
tooltips: {
callbacks: {
title: function(tooltipItem, data){
return 'LOOK';
},
label: function(tooltipItem, data) {
return 'Properties';
},
afterLabel: function(tooltipItem, data) {
let rnumber = 'Request: ' + tooltipItem.yLabel;
let tnumber = 'Track: ' + data.datasets[tooltipItem.datasetIndex].data[Number(tooltipItem.yLabel)].x;
return (
[
rnumber,
tnumber
]
);
}
}
},
animation: {
easing: 'easeInQuad',
},
scales: {
yAxes: [
{
scaleLabel: {
display: true,
fontFamily: 'Roboto Slab',
fontSize: 12,
fontStyle: 'bold',
fontColor: 'black',
labelString: 'REQUEST NUMBER'
},
id: 'first',
position: 'top',
ticks: {
reverse: true,
max: yrange+1,
min: 0,
stepSize: 1,
},
}
],
xAxes: [
{
scaleLabel: {
display: true,
fontFamily: 'Roboto Slab',
fontSize: 12,
fontStyle: 'bold',
fontColor: 'black',
labelString: 'TRACK NUMBER'
},
id: 'scale',
ticks: {
max: xrange,
min: 0,
stepSize: 1,
},
display: true,
position: 'top',
}
]
}
}
});
function displaySeekOp(){
let temp = document.getElementById('temp');
let temp1 = document.getElementById('temp1');
temp.remove(); temp1.remove();
let url = document.getElementById("url");
url.remove();
let genPDF = document.getElementById("genPDF");
genPDF.remove();
document.getElementById('seek').style.width = 'fit-content'; // to be added to other js files
// document.getElementById('seek').style.flexDirection = 'column';
var seekOp1 = document.createElement('h4');
seekOp1.id = 'temp';
seekOp1.style.fontWeight = "700"; seekOp1.style.margin = "5px";
str = 'Total Seek Time: ' + seekOperations(trackRequests, head) + ' ms';
seekOp1.append(document.createTextNode(str));
seekOp1.classList.add("animate__animated"); seekOp1.classList.add("animate__backInUp");
document.getElementById('seek').append(seekOp1);
var seekOp2 = document.createElement('h4');
seekOp2.id = 'temp1';
seekOp2.style.fontWeight = "700"; seekOp2.style.margin = "5px";
str = 'Average Seek Time: ' + Math.round((xrange/3)*100)/100 + ' ms';
seekOp2.append(document.createTextNode(str));
seekOp2.classList.add("animate__animated"); seekOp2.classList.add("animate__backInUp");
document.getElementById('seek').append(seekOp2);
var dIcon = document.createElement("a");
dIcon.id = 'url'; dIcon.download = "LOOK.jpeg";
var dButton = document.createElement("button");
dButton.type = 'button'; dButton.className = 'dButton btn btn-outline-dark animate__animated animate__backInUp';
dButton.style.padding = '0';
dButton.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" fill="black" class="bi bi-download" viewBox="0 0 16 16" style="margin: 6px;">
<path d="M.5 9.9a.5.5 0 0 1 .5.5v2.5a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-2.5a.5.5 0 0 1 1 0v2.5a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2v-2.5a.5.5 0 0 1 .5-.5z"/>
<path d="M7.646 11.854a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V1.5a.5.5 0 0 0-1 0v8.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3z"/>
</svg>`;
dIcon.append(dButton);
document.getElementById('dImageIcon').append(dIcon);
// NEW CODE TO BE ADDED IN OTHER JS FILES
var pdfButton = document.createElement("button");
pdfButton.type = 'button'; pdfButton.className = 'pdfButton btn btn-outline-dark animate__animated animate__backInUp';
pdfButton.style.padding = '0'; pdfButton.id="genPDF"
pdfButton.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" fill="black" class="bi bi-file-earmark-arrow-down" viewBox="0 0 16 16" style="margin: 6px;">
<path d="M8.5 6.5a.5.5 0 0 0-1 0v3.793L6.354 9.146a.5.5 0 1 0-.708.708l2 2a.5.5 0 0 0 .708 0l2-2a.5.5 0 0 0-.708-.708L8.5 10.293V6.5z"/>
<path d="M14 14V4.5L9.5 0H4a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2zM9.5 3A1.5 1.5 0 0 0 11 4.5h2V14a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h5.5v2z"/>
</svg>`;
document.getElementById('dPDFIcon').append(pdfButton);
setTimeout(() => {
window.scrollTo(0,document.body.scrollHeight);
}, 700);
}
// UPDATING THE CHART
var start = {
x: head,
y: 0,
};
algoChart.data.datasets[0].data.push(start);
algoChart.update();
var a = 0;
var incrementValue = 100/yrange, counter=0;
var updatingData = setInterval(pushData, 700);
function pushData(){
if(a<yrange){
var obj = {
x: trackRequests[a],
y: a + 1
};
algoChart.data.datasets[0].data.push(obj);
algoChart.update();
a = a+1;
counter+=incrementValue;
progressBar.style.width = counter+"%"
}
else{
clearInterval(updatingData);
progressBarContainer.classList.toggle("animate__backOutDown");
setTimeout(function () {
progressBarContainer.style.display = "none";
progressWrapper.innerHTML = `<h4 id="temp"> </h4> <h4 id="temp1"> </h4>`;
run.classList.toggle("disabled");
displaySeekOp();
done();
}, 1000
);
}
}
}
}
run.addEventListener("click", execute);
window.addEventListener('wheel', (e) => {
if (e.deltaY > 0) {
document.getElementsByClassName('navbar')[0].classList.add('animate__slideOutUp');
setTimeout(() => {
document.getElementsByClassName('navbar')[0].style.display = 'none';
document.getElementsByClassName('navbar')[0].classList.remove('animate__slideOutUp');
}, 100);
}
else {
if (document.getElementsByClassName('navbar')[0].style.display === 'none') {
document.getElementsByClassName('navbar')[0].classList.add('animate__slideInDown');
setTimeout(() => {
document.getElementsByClassName('navbar')[0].style.display = 'block';
}, 50);
setTimeout(() => {
document.getElementsByClassName('navbar')[0].classList.remove('animate__slideInDown');
}, 500);
}
}
});