-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompModel.js
360 lines (322 loc) · 11.2 KB
/
compModel.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
// Load data from json files
var pagesLog;
var getPagesLog = function() {
d3.json("pagesLog.json", function(error, json) {
if (error) return console.warn(error);
pagesLog = json;
// Make the animation
makeComp();
// Bind to window resize
//$(window).resize(function() {
// makeComp();
//});
});
}
getPagesLog();
// Set some globals
var pageWidth = 50 + 20*pageSize; // NOTE: this is hard-coding the font-width!!!
var pageHeight = 30;
var pagePadding = 5;
var frameWidth = pageWidth + 2*pagePadding;
var frameHeight = pageHeight + 2*pagePadding;
var framePadding = 2*pagePadding;
var bufferFrameFill = '#3366FF';
var diskFrameFill = '#B685FF';
var frameStroke = 'white'; // '#C1C0C0'
var diskBackground = '#ABACB0';
var pageColor = 'blue';
var pageHighlightColor = 'red';
var maxDiskSize = 4; // MAX # of files that can fit in disk
var fileSize;
var height = framePadding + (maxDiskSize + 2) * (framePadding + frameHeight);
var width;
// Create a row of frames
var appendFrameRowTo = function(e, x, y, len, name, label, fill, duration, callback) {
var frameRow = e.append('g')
.attr({
'transform': 'translate(' + x + ',' + y + ')',
'id': name + '-g'})
.style('opacity', 0);
var frameRange = new Array(len);
frameRow.append('text')
.text(label)
.attr({
'text-anchor': 'middle',
'dominant-baseline': 'middle', // NOTE: doesn't work in IE?
'dx': 0.5*frameWidth,
'dy': 0.5*frameHeight});
frameRow.selectAll('rect.' + name + '-frame')
.data(frameRange)
.enter()
.append('rect')
.attr({
'id': function(d,i) { return name + '-frame-' + i; },
'class': name + '-frame',
'width': frameWidth,
'height': frameHeight,
'x': function(d,i) { return (i+1)*frameWidth; },
'y': pagePadding,
'fill': fill,
'stroke': frameStroke});
if (duration != null) {
frameRow.transition()
.style('opacity', 1)
.duration(DURATION)
.each("end", function() { if (callback != null) { callback(); } });
} else {
frameRow.style('opacity', 1);
if (callback != null) { callback(); }
}
return frameRow;
}
var diskHeight = function(nFiles) {
return 2*framePadding + nFiles * (framePadding + frameHeight)
}
// Create the chart
var bufferPages;
var files = [];
var svg;
var buffer;
var disk;
var makeComp = function() {
$("#chart-"+chartNum).html('');
// Make sure the tooltips are hidden
$(".tooltip").hide();
// viewer size
width = $('.output_subarea').width() - 20;
// Make the files the max possible size
// TODO: make files dynamically resizeable?
fileSize = Math.floor((width - framePadding) / (framePadding + frameWidth) - 2);
// append svg
svg = d3.select("#chart-"+chartNum)
.append('svg')
.attr({
'width': width,
'height': height})
.style('border', '1px solid black');
// append buffer
buffer = appendFrameRowTo(svg, pagePadding, pagePadding, numBufferPages, 'buffer', 'BUFFER', bufferFrameFill, null, null);
// append disk container
var diskY = 2*pagePadding + 2*frameHeight;
disk = svg.append('g')
.attr({
'id': 'disk-g',
'transform': 'translate(' + pagePadding + ',' + diskY + ')'});
disk.append('text')
.text('DISK')
.attr({
'text-anchor': 'middle',
'dominant-baseline': 'middle', // NOTE: doesn't work in IE?
'dx': 0.5*frameWidth,
'dy': 0.5*frameHeight});
disk.append('rect')
.attr({
'id': chartNum + '-disk-box',
'width': width - 2*frameWidth,
'height': diskHeight(1), // Start w/ space for one file
'x': frameWidth,
'y': 0,
'fill': diskBackground,
'stroke': 'none'});
// Animate the page transistions according to the log diff in pagesLog
var success = takeActions(0);
}
var addFile = function(fileId, duration, callback) {
var n = files.length;
if (fileId >= maxDiskSize) {
return null;
}
if (n > 0 && fileId >= n) {
$("#" + chartNum + "-disk-box").height(diskHeight(n+1));
}
var file = appendFrameRowTo(disk, 2*framePadding + frameWidth, framePadding + fileId * (framePadding + frameHeight), fileSize, 'file-' + fileId, 'File ' + fileId, diskFrameFill, duration, callback);
if (fileId < n) {
files[fileId] = file;
} else {
files.push(file);
}
}
var removeFile = function(fileId) {
var file = files[fileId];
file.remove();
files[fileId] = null;
var n = files.length;
var nh = n;
if (n > 0) {
for (var j=n-1; j > 0; j--) {
if (files[j] != null) {
break;
} else {
nh = j;
}
}
$("#" + chartNum + "-disk-box").height(diskHeight(nh));
}
}
// For some reason in certain situations e.g. d3.select("#0-0-1")
// d3.select is not working here?? Workaround...
var getElement = function(elementType, filterFn) {
var e = d3.select('#chart-'+chartNum)
.selectAll(elementType)[0]
.filter(filterFn)[0];
return e;
}
var pageText = function(fid, pid, data) {
var s = fid + '/' + pid + ' : ';
for (var i=0; i < data.length; i++) {
s += (data[i] != null) ? data[i] : '_';
if (i < data.length - 1) {
s += ', ';
}
}
return s;
}
var appendPageTo = function(e, a, x, y, opacity, color) {
var page = e.append('g')
.attr({
'id': chartNum + '-' + a.file + '-' + a.page + '-' + a.newLocation,
'transform': 'translate(' + x + ',' + y + ')',
'class': 'page'})
.style('opacity', opacity);
page.append('rect')
.attr({
'id': 'page-' + a.file + '-' + a.page + '-rect',
'class': 'page',
'width': pageWidth,
'height': pageHeight,
'fill': color,
'stroke': '#444'});
page.append('text')
.attr({
'dx': pagePadding,
'dy': 0.5*pageHeight,
'fill': 'white'})
.text(pageText(a.file, a.page, a.pageData));
return page;
}
var getBufferPageAbsPos = function(bufferIndex) {
var bufferPos = d3.transform(buffer.attr("transform")).translate;
var x = bufferPos[0] + pagePadding + (bufferIndex + 1) * frameWidth;
var y = bufferPos[1] + 2 * pagePadding;
return [x, y];
}
var getFilePageAbsPos = function(file, page) {
var diskPos = d3.transform(disk.attr("transform")).translate;
var filePos = d3.transform(files[file].attr("transform")).translate;
var x = diskPos[0] + filePos[0] + pagePadding + (page + 1) * frameWidth;
var y = diskPos[1] + filePos[1] + 2 * pagePadding;
return [x, y];
}
var placeRelativeTo = function(a, b, dTop, dLeft) {
var bOffset = b.offset();
if (bOffset != null) {
a.offset({'top' : bOffset.top + dTop, 'left' : bOffset.left + dLeft});
}
}
var takeActions = function(step) {
// Get the next action to execute
if (step < pagesLog.length) {
var a = pagesLog[step];
//console.warn(a.operation);
} else {
$(".tooltip").hide();
return null;
}
// Are we in the new part of the log which should be animated?
var animate = (a.show && step >= logStart);
// Update IO counter
$("#chart-" + chartNum + "-bufferReads").html(a.ioCount.bufferReads);
$("#chart-" + chartNum + "-bufferWrites").html(a.ioCount.bufferWrites);
$("#chart-" + chartNum + "-diskReads").html(a.ioCount.diskReads);
$("#chart-" + chartNum + "-diskWrites").html(a.ioCount.diskWrites);
// Show tooltip! -> @ old location, or new if old is null
if (animate) {
var tooltip = $("#chart-" + chartNum + "-tooltip");
var pageId = "#" + chartNum + "-" + a.file + "-" + a.page + "-";
pageId += (a.oldLocation != null) ? a.oldLocation : a.newLocation;
placeRelativeTo(tooltip, $(pageId), 0.7*pageHeight, 0.3*pageWidth);
tooltip.html(a.operation);
tooltip.show();
}
// Check for tooltip animations & handle here
if (a.operation.slice(0, 7) == 'TOOLTIP') {
// TODO: Clean this up
if (a.operation == 'TOOLTIP-2') {
var tooltip = $("#chart-" + chartNum + "-tooltip-2");
var page = $("#" + chartNum + "-" + a.file + "-" + a.page + "-" + a.oldLocation);
placeRelativeTo(tooltip, page, -(tooltip.outerHeight() + 1.5*pagePadding), 0.1*pageWidth);
tooltip.html(a.tooltipContent);
tooltip.show();
}
takeActions(step + 1);
return true;
}
// Check for file actions & handle here
if (a.operation === "NEWFILE") {
addFile(a.file, DURATION, function() { takeActions(step + 1); });
return true;
} else if (a.operation === "DELETEFILE") {
removeFile(a.file);
takeActions(step + 1); // TODO: Do proper animation here
return true;
}
// old -> new location animation
if (a.newLocation != null) {
// Delete the old element
if (!a.keepOld && a.oldLocation != null) {
$("#" + chartNum + '-' + a.file + '-' + a.page + '-' + a.oldLocation).remove();
}
// ANIMATION: If animation is called for, we create a new copy of page *which is above all
// other SVG g layers* to do the animation, then delete it once the animation is done
// The point is to do the animation so nice & visible (not hidden behind g layers)
// But so that the end object is appended to the proper g
var newE = (a.newLocation === "BUFFER") ? buffer : files[a.file];
var newI = (a.newLocation === "BUFFER") ? a.bufferIndex : a.page;
var newX = (newI + 1) * frameWidth + pagePadding;
var newY = 2 * pagePadding;
// Animate only if we are in the new part of the log
if (animate) {
var newPos = (a.newLocation === "BUFFER") ? getBufferPageAbsPos(a.bufferIndex) : getFilePageAbsPos(a.file, a.page);
if (a.oldLocation != null) {
var oldPos = (a.oldLocation === "BUFFER") ? getBufferPageAbsPos(a.bufferIndex) : getFilePageAbsPos(a.file, a.page);
var oldE = (a.oldLocation === "BUFFER") ? buffer : files[a.file];
} else {
var oldPos = newPos;
var oldE = (a.newLocation === "BUFFER") ? buffer : files[a.file];
}
// Append the dummpy page for animation
var p = appendPageTo(svg, a, oldPos[0], oldPos[1], 0, pageColor);
// Animation! Place the new page and proceed to next action at end
p.transition()
.style('opacity', 1).duration(DURATION)
.transition()
.attr('transform', 'translate(' + newPos[0] + ',' + newPos[1] + ')').duration(DURATION)
.each("end", function() {
var page = appendPageTo(newE, a, newX, newY, 1, pageColor);
takeActions(step + 1);
p.remove();
});
// Else if no animation we just place the new element and proceed
} else {
var page = appendPageTo(newE, a, newX, newY, 1, pageColor);
takeActions(step + 1);
}
// If no new location: simple fade out animation
} else {
if (!a.keepOld && a.oldLocation != null) {
var page = $("#" + chartNum + '-' + a.file + '-' + a.page + '-' + a.oldLocation);
if (animate) {
page.fadeOut(DURATION, function() {
page.remove();
takeActions(step + 1);
});
} else {
page.remove();
takeActions(step + 1);
}
} else {
takeActions(step + 1);
}
}
return true;
}