-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.js
93 lines (84 loc) · 3 KB
/
session.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
'use strict'
let Logger = require('./logger.js')
let log = new Logger('TimeoutBasedSession')
let _ = require('underscore')
class TimeoutBasedSession {
constructor(rawlogs, timeField, fixedTimeout) {
if (timeField != null) {
this.timeField = timeField
} else {
this.timeField = 'ts'
}
this.fixedTimeout = fixedTimeout
let _this = this
if (rawlogs != null && rawlogs.length > 0) {
this.rawlogs = rawlogs.sort(function(a,b){
return a[_this.timeField] - b[_this.timeField]
})
}
}
groupToSessions() {
if (this.rawlogs == null || this.rawlogs.length == 0) {
return new Array()
}
let session = this
log.info("calculating session timeout value...")
let timeout = this.calculateTimeout()
log.info("session timeout is " + timeout + " seconds")
//group to sessions based on interval
let sessions = new Array()
var lastTime = null
var currentSession = new Array()
_.forEach(this.rawlogs, function(rawlog){
let time = rawlog[session.timeField]
if (lastTime == null || (time - lastTime) < timeout) {
currentSession.push(rawlog)
} else {
//break to a new session
sessions.push(currentSession)
currentSession = new Array()
currentSession.push(rawlog)
}
lastTime = time
})
sessions.push(currentSession)
return sessions
}
calculateTimeout() {
var session = this
if (this.fixedTimeout != null) {
return this.fixedTimeout
} else if (this.rawlogs.length == 1) {
return 0
} else {
//mapping each time diff to 60 seconds bucket and calculate the frequence for each bucket
let buckets = new Object()
let bucketUnit = 60 //1 minute
var lastTime = null
_.forEach(this.rawlogs, function(rawlog){
let time = rawlog[session.timeField]
if (lastTime != null) {
let diff = time - lastTime
let diffKey = Math.floor(diff / bucketUnit)
if (buckets[diffKey] != null) {
buckets[diffKey].count = buckets[diffKey].count + 1
} else {
buckets[diffKey] = {
count : 1,
time : (diffKey + 1) * 60
}
}
}
lastTime = time
})
if (log.isDebugEnabled()) {
log.debug("time interval distribution:")
log.debug(buckets)
}
return Object.keys(buckets).map(k => buckets[k]).sort(function(a, b){
return b.count - a.count
})[0].time
}
}
}
module.exports = TimeoutBasedSession