-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.js
59 lines (52 loc) · 1.34 KB
/
statistics.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
var statisticsConfig = [
{
min:200,
max:800,
maxAddition:5,
minAddition:-5,
interval:30000,
format:"{0}"
},
{
min:1.2,
max:4,
maxAddition:0.1,
minAddition:-0.1,
interval:30*60000,
format:"{0}K"
}
];
var values=[];
var strings=[];
String.prototype.format = function() {
var formatted = this;
for( var arg in arguments ) {
formatted = formatted.replace("{" + arg + "}", arguments[arg]);
}
return formatted;
};
exports.start = function() {
for (i = 0; i < statisticsConfig.length; i++) {
values[i] = getRandom(statisticsConfig[i].min,statisticsConfig[i].max);
strings[i] =getForamttedStatString(i);
startUpdating(i);
}
};
function getForamttedStatString(statisticsIndex) {
val = values[statisticsIndex];
if (val%1>0) val= val.toFixed(1);
return statisticsConfig[statisticsIndex].format.format( val);
}
function startUpdating(statisticsIndex) {
setInterval(function() {
var i=statisticsIndex;
values[i]=Math.min(Math.max(values[i]+getRandom(statisticsConfig[i].minAddition,statisticsConfig[i].maxAddition),statisticsConfig[i].min),statisticsConfig[i].max);
strings[i] =getForamttedStatString(i);
}, statisticsConfig[i].interval);
}
exports.getStatistics = function () {
return strings.slice(0);
};
function getRandom(min,max) {
return Math.floor(Math.random()*(max-min+1))+min;
}