-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice_stats.go
More file actions
120 lines (94 loc) · 3.09 KB
/
device_stats.go
File metadata and controls
120 lines (94 loc) · 3.09 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
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
package sdk
import (
"sync"
"time"
)
type DeviceStats struct {
stateLock sync.Mutex
connectEnabled bool
connectStartTime time.Time
connectCount int
netConnectDuration time.Duration
maxConnectDuration time.Duration
netRemoteSendByteCount ByteCount
netRemoteReceiveByteCount ByteCount
successConnectDuration time.Duration
successByteCount ByteCount
}
func newDeviceStats() *DeviceStats {
return &DeviceStats{
connectEnabled: false,
connectStartTime: time.Time{},
connectCount: 0,
netConnectDuration: time.Duration(0),
maxConnectDuration: time.Duration(0),
netRemoteSendByteCount: ByteCount(0),
netRemoteReceiveByteCount: ByteCount(0),
successConnectDuration: 120 * time.Second,
successByteCount: ByteCount(64 * 1024 * 1024),
}
}
func (self *DeviceStats) GetConnectCount() int {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return self.connectCount
}
func (self *DeviceStats) GetNetConnectDurationSeconds() int {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return int(self.netConnectDuration / time.Second)
}
func (self *DeviceStats) GetMaxConnectDurationSeconds() int {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return int(self.maxConnectDuration / time.Second)
}
func (self *DeviceStats) GetNetRemoteSendByteCount() ByteCount {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return self.netRemoteSendByteCount
}
func (self *DeviceStats) GetNetRemoteReceiveByteCount() ByteCount {
self.stateLock.Lock()
defer self.stateLock.Unlock()
return self.netRemoteReceiveByteCount
}
func (self *DeviceStats) GetUserSuccess() bool {
// consider the user successful when all of
// - max connect time is longer than 30m
// - receive byte count more than 64mib
self.stateLock.Lock()
defer self.stateLock.Unlock()
connectTimeCondition := self.successConnectDuration <= self.netConnectDuration || self.connectEnabled && self.connectStartTime.Add(self.successConnectDuration - self.netConnectDuration).Before(time.Now())
receiveByteCountCondition := self.successByteCount <= self.netRemoteReceiveByteCount
return connectTimeCondition && receiveByteCountCondition
}
func (self *DeviceStats) UpdateConnect(connectEnabled bool) {
self.stateLock.Lock()
defer self.stateLock.Unlock()
now := time.Now()
if self.connectEnabled {
self.connectCount += 1
connectDuration := now.Sub(self.connectStartTime)
if self.maxConnectDuration < connectDuration {
self.maxConnectDuration = connectDuration
}
self.netConnectDuration += connectDuration
}
if connectEnabled {
self.connectStartTime = now
self.connectEnabled = true
} else {
self.connectEnabled = false
}
}
func (self *DeviceStats) UpdateRemoteReceive(remoteReceiveByteCount ByteCount) {
self.stateLock.Lock()
defer self.stateLock.Unlock()
self.netRemoteReceiveByteCount += remoteReceiveByteCount
}
func (self *DeviceStats) UpdateRemoteSend(remoteSendByteCount ByteCount) {
self.stateLock.Lock()
defer self.stateLock.Unlock()
self.netRemoteSendByteCount += remoteSendByteCount
}