-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagehits.js
More file actions
88 lines (76 loc) · 3.14 KB
/
pagehits.js
File metadata and controls
88 lines (76 loc) · 3.14 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
var pageHit = {
MAX_EVENTS : 300, //5 mins = 300 seconds
countArray : [], //Circular buffer of event counts
currIndex : 0, //index of the earliest event currently in buffer
initTime : 0,
initialize : function () {
for (var i = 0; i < this.MAX_EVENTS; i++) {
this.countArray[i] = 0;
}
this.initTime = 0;
},
/*******************************************************
* Function to add an event to the event buffer
* @param event
*******************************************************/
addEvent : function () {
var currTime;
if (pageHit.initTime == 0) {//When it starts for the first time. Both currentTime and initTime will be the same
pageHit.initTime = Math.floor(Date.now() / 1000);
currTime = pageHit.initTime;
}
currTime = Math.floor(Date.now() / 1000);
console.log("currTime"+currTime);
var diff = Math.floor(currTime - pageHit.initTime);
console.log("diff :" + diff);
if ((diff >= pageHit.currIndex) && (diff < pageHit.MAX_EVENTS)) {
pageHit.countArray[diff]++;
pageHit.currIndex = diff;
} else if ((diff >= pageHit.MAX_EVENTS) && (diff < (pageHit.MAX_EVENTS * 2))) {
pageHit.initTime = pageHit.initTime + pageHit.MAX_EVENTS;
var newIndex = Math.abs(pageHit.MAX_EVENTS - diff);
for (var j = 0; j < newIndex; j++) {
pageHit.countArray[j] = 0;
}
pageHit.countArray[newIndex] = 1;
pageHit.currIndex = newIndex;
} else if (diff >= pageHit.MAX_EVENTS * 2) {
pageHit.countArray[0] = 1;
for (var i = 1; i < pageHit.MAX_EVENTS; i++) {
pageHit.countArray[i] = 0;
}
}
console.log(pageHit.countArray);
return "Success";
},
/*****************************************************************
* Get the count of events
* @param option Mins or seconds
* @param duration Num Mins or seconds
* @returns {number}
*****************************************************************/
getCount : function (option, duration) {//option refers to minutes/seconds and duration is the integer value
if ((option != 'min') && (option != 'sec')) {
throw ("Please select the appropriate option : 'min'/'sec'");
}
if (((option == 'min') && (duration > 5)) || ((option == 'sec') && (duration > 300))) {
throw ("Unfortunately,we only provide data for the last five " +
"minutes. Please select a duration of 5 minutes/300 seconds or lower.");
}
if (option == 'min') {
duration = duration * 60;
}
var totalCount = 0;
var index = this.currIndex;
//Assuming that the timestamp correlates to the currIndex point used to determine where the next event needs to be counted.
for (var k = 0; k < duration; k++) {
totalCount = totalCount + pageHit.countArray[index];
index--;
if (index < 0) {
index = 299;
}
}
console.log("page hits :" + totalCount);
return totalCount;
}
}