-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.js
334 lines (296 loc) · 10.5 KB
/
lib.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
'use strict';
var debugMode = true;
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var thousand = 1000;
var million = thousand * 1000;
var billion = million * 1000;
var trillion = billion * 1000;
function printDayShort(date) {
if (date == null) {
return null;
}
var result = months[date.getUTCMonth()] + " " + date.getUTCDate() + " " + (date.getUTCFullYear());
// debug("printDayShort: " + date + " -> " + result, null);
return result;
}
function el(elementId) {
return document.getElementById(elementId);
}
function getQueryParameterFromURL(paramName) {
var url = new URL(window.location);
var searchParams = new URLSearchParams(url.search);
return searchParams.get(paramName);
}
function loadWebResource(docName, resultHandler) {
debug("loadWebResource loading: " + docName);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
debug("loadWebResource loaded: " + docName);
resultHandler(this.responseText);
}
};
xhttp.open("GET", docName, true);
xhttp.send();
}
function printNumberShort(number) {
number = Math.floor(number);
var numberToCheck = number < 0 ? (number * -1) : number;
var result = 0;
if (numberToCheck >= trillion) {
number = (number / (trillion * 1.0))
result = number.toFixed(2) + "t";
} else if (numberToCheck >= billion) {
number = (number / (billion * 1.0))
result = number.toFixed(2) + "b";
} else if (numberToCheck >= million) {
number = (number / (million * 1.0))
result = number.toFixed(2) + "m";
} else if (numberToCheck < 10000) {
result = number.toLocaleString();
} else if (numberToCheck >= thousand) {
number = (number / (thousand * 1.0))
result = Math.floor(number) + "k";
} else {
result = number.toLocaleString();
}
// debug("printNumberShort " + number.toLocaleString() + " -> " + result);
return result;
}
function debug(message, details) {
if (debugMode) {
if (details == null) {
console.log(message);
} else {
console.log(message, details);
}
}
}
//from: https://stackoverflow.com/questions/3885817/how-do-i-check-that-a-number-is-Int-or-integer
function isInteger(x) {
return typeof x === "number" && isFinite(x) && Math.floor(x) === x;
}
//from: https://stackoverflow.com/questions/3885817/how-do-i-check-that-a-number-is-Int-or-integer
function isFloat(x) {
return !!(x % 1);
}
/* numberInt = 100, percentFloat = 25.0 (ie 25%), answer = 25 */
function multiplyAndRound(numberInt, percentFloat) {
if (!isInteger(numberInt)) {
debug("multiplyAndRound error, numberInt is not an integer: " + numberInt);
console.trace();
return null;
}
if (!isFloat(percentFloat) && !isInteger(percentFloat)) {
debug("multiplyAndRound error, percentFloat is not a float or integer: " + percentFloat);
console.trace();
return null;
}
var result = (numberInt * 1.0) * ((percentFloat * 1.0) / 100.0);
var flooredResult = Math.floor(result);
// debug("Result of multiply, numberInt: " + numberInt + ", percentFloat: " + percentFloat
// + ", result: " + result + ", flooredResult: " + flooredResult);
return flooredResult;
}
function getPercentage(currentValueInt, maxValueInt) {
return Math.round(getPercentageFloat(currentValueInt, maxValueInt) * 100.0);
}
function getPercentageFloat(currentValueInt, maxValueInt) {
if (!isInteger(currentValueInt)) {
debug("getPercentage error, currentValueInt is not an integer: " + currentValueInt);
console.trace();
return null;
}
if (!isInteger(maxValueInt)) {
debug("getPercentage error, maxValueInt is not an integer: " + maxValueInt);
console.trace();
return null;
}
var result = (currentValueInt * 1.0) / (maxValueInt * 1.0);
// debug("Result of percentage, currentValueInt: " + currentValueInt + ", maxValueInt: " + maxValueInt + ", result: " + result);
return result;
}
function incrementDate(date, dayCountInt) {
var d = new Date(date.getTime());
d.setUTCDate(d.getUTCDate() + dayCountInt);
// debug("Incremented date: " + date + " by " + dayCountInt + ": " + d);
return d;
}
function getSliderValue(sliderId) {
var slider = el(sliderId);
if (slider.getAttribute("format") == "percentage") {
return ((slider.valueAsNumber * 1.0) / 10.0);
}
var scaleControl = el(slider.getAttribute("scaleControl"));
var multiplier = 1;
if (scaleControl != null) {
if (scaleControl.value == "Billion") {
multiplier = billion;
} else if (scaleControl.value == "Million") {
multiplier = million;
} else if (scaleControl.value == "Thousand") {
multiplier = thousand;
}
}
return slider.valueAsNumber * multiplier;
}
function updateSliderLabel(slider) {
var sliderValue = getSliderValue(slider.id);
var text = slider.previousElementSibling.innerText;
if (text.indexOf(" [") != -1) {
text = text.substr(0, text.indexOf(" ["));
}
if (slider.getAttribute("format") == "percentage") {
text += " [" + sliderValue + "%]";
} else {
text += " [" + sliderValue.toLocaleString() + "]";
if (sliderValue > (100 * thousand)) {
text += " (" + printNumberShort(sliderValue) + ")";
}
}
slider.previousElementSibling.innerText = text;
}
function populateTemplate(template, dayStats, totalStats, currentHospitalizedInt, coronaSimSettings) {
var value = "";
var percentage = "";
var templateCopy = template;
var rowDate = incrementDate(coronaSimSettings.initialDate, dayStats.dayNumberInt);
value = "#" + dayStats.dayNumberInt + ": " + printDayShort(rowDate);
templateCopy = templateCopy.replace("${day}", value);
value = "" + printNumberShort(dayStats.infectionsInt) + " new today.";
templateCopy = templateCopy.replace("${infectionsToday}", value);
value = "" + printNumberShort(dayStats.testedInt) + " new today, ";
value += printNumberShort(dayStats.positiveTestsInt) + " tested positive.";
templateCopy = templateCopy.replace("${testResultsToday}", value);
value = "" + printNumberShort(dayStats.hospitalizationsInt) + " of today's infections need to be hospitalized, ";
value += printNumberShort(dayStats.deathsInt) + " will die.";
templateCopy = templateCopy.replace("${mortalityToday}", value);
var bedsUsed = currentHospitalizedInt;
var bedsFree = coronaSimSettings.hospitalBedsInt - bedsUsed;
percentage = " (" + getPercentage(bedsUsed, coronaSimSettings.hospitalBedsInt) + "%)";
value = "" + printNumberShort(bedsUsed) + percentage + " used, ";
percentage = " (" + getPercentage(bedsFree, coronaSimSettings.hospitalBedsInt) + "%)";
value += printNumberShort(bedsFree) + percentage + " free.";
if (bedsFree < 0) {
value = "<span style='color:red;'>" + value + "</span>";
}
templateCopy = templateCopy.replace("${hospitalBedsToday}", value);
value = "" + printNumberShort(totalStats.totalInfectionsInt) + " infected, ";
value += printNumberShort(totalStats.totalTestedInt) + " tested, ";
value += printNumberShort(totalStats.totalPositiveTestsInt) + " tested positive, ";
var survivors = totalStats.totalInfectionsInt - totalStats.totalDeathsInt;
value += printNumberShort(survivors) + " survivors, ";
value += printNumberShort(totalStats.totalDeathsInt) + " deaths.";
templateCopy = templateCopy.replace("${totals}", value);
return templateCopy;
}
function generateSimulationOutput(template, coronaSimSettings) {
var simulator = new CoronaSimulator(coronaSimSettings);
var tableDataArray = [];
//add first day's stats
tableDataArray.push({
dayStats: simulator.currentDayStats,
totalStats: simulator.totalStats.copy(),
currentHospitalizedInt: simulator.hospitalizationsForDayInt(simulator.currentDayInt),
});
for (var i = 0; i < coronaSimSettings.simulatonDaysInt; i++) {
simulator.moveForwardOneDay();
tableDataArray.push({
dayStats: simulator.currentDayStats,
totalStats: simulator.totalStats.copy(),
currentHospitalizedInt: simulator.hospitalizationsForDayInt(simulator.currentDayInt),
});
var totalInfections = simulator.totalStats.totalInfectionsInt;
if (totalInfections > coronaSimSettings.populationInt) {
debug("stopping simulation, hit population limit: " + totalInfections.toLocaleString());
break;
}
}
var html = "<div class='testResults'>\n";
for (var tableEntry of tableDataArray) {
html += populateTemplate(template, tableEntry.dayStats, tableEntry.totalStats,
tableEntry.currentHospitalizedInt, coronaSimSettings);
}
html += "\n</div>";
return html;
}
function setSelectOptions(selectorId, optionStringsArray) {
var selector = el(selectorId);
while (selector.options.length > 0) {
selector.remove(0);
}
if (optionStringsArray == null || optionStringsArray.length == 0) {
selector.enabled = false;
return;
}
selector.enabled = true;
for (var value of optionStringsArray) {
selector.add(new Option(value, value));
}
selector.value = optionStringsArray[0];
}
function changeSelectSelectedValue(selectorId, selectedValue) {
debug("Change select '" + selectorId +"' selected value to: " + selectedValue);
if (selectedValue == null) {
return;
}
el(selectorId).value = selectedValue;
}
function denull(x) {
return x == null ? 0 : x;
}
function hideElement(elementId) {
var element = el(elementId)
if (element == null) {
return;
}
if (element.ccOldDisplayStyle == null) {
element.ccOldDisplayStyle = element.style.display;
}
element.style.display = "none";
}
function showElement(elementId, displayStyle) {
var element = el(elementId)
if (element == null) {
return;
}
if (displayStyle == null && element.ccOldDisplayStyle != null) {
displayStyle = element.ccOldDisplayStyle;
}
displayStyle == (displayStyle == null) ? "block" : displayStyle;
element.style.display = displayStyle;
}
function sortItems(itemArray, propertyName, sortAscending) {
if (itemArray == null || itemArray.length == 0) {
return;
}
var sortFunction = function(firstItem, secondItem) {
var a = firstItem == null ? null : firstItem[propertyName];
var b = secondItem == null ? null : secondItem[propertyName];
var result = null;
if (a == null) {
result = b == null ? 0 : 1;
} else if (b == null) { //a is not null, b is null
result = 1;
} else if (a == b) {
result = 0;
}
if (result == null) {
if (Number.isInteger(a) && Number.isInteger(b)) {
result = (a > b) ? 1 : -1;
} else if (a instanceof Date && b instanceof Date) {
if (a.getTime() == b.getTime()) {
result = 0;
} else {
result = (a.getTime() > b.getTime()) ? 1 : -1;
}
} else {
result = a.toString().localeCompare(b.toString());
}
}
result = sortAscending ? result : result * -1;
debug("Comparison result: " + result, {a:a, b:b, result:result });
return result;
}
itemArray.sort(sortFunction);
}