Debian Codegraph @

server/statistics/statistics.go

raw
1 package statistics
2
3 import (
4 "container/ring"
5 "sync"
6 "time"
7
8 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
9 )
10
11 type StatsValue struct {
12 val5min int32
13 val60min *ring.Ring
14 val1day *ring.Ring
15 val7day *ring.Ring
16 }
17
18 func NewStatsValue() *StatsValue {
19 result := &StatsValue{
20 val60min: ring.New(12), //12 * 5 min interval = 1 hour
21 val1day: ring.New(288), //288 * 5 min interval = 1 day
22 val7day: ring.New(2016), //2016 * 5 min interval = 7 days
23 }
24 return result
25 }
26
27 func (statsvalue *StatsValue) Accumulate() {
28 statsvalue.val60min.Value = statsvalue.val5min
29 statsvalue.val60min = statsvalue.val60min.Next()
30 statsvalue.val1day.Value = statsvalue.val5min
31 statsvalue.val1day = statsvalue.val1day.Next()
32 statsvalue.val7day.Value = statsvalue.val5min
33 statsvalue.val7day = statsvalue.val7day.Next()
34 }
35
36 func (statsvalue *StatsValue) Reset5min() {
37 statsvalue.val5min = 0
38 }
39
40 func (statsvalue *StatsValue) Report() (int32, int32, int32, int32) {
41
42 var val60min int32 = 0
43 statsvalue.val60min.Do(func(p interface{}) {
44 if p != nil {
45 val60min += p.(int32)
46 }
47 })
48 val60min = val60min / 12
49 var val1day int32 = 0
50 statsvalue.val1day.Do(func(p interface{}) {
51 if p != nil {
52 val1day += p.(int32)
53 }
54 })
55 val1day = val1day / 288
56 var val7day int32 = 0
57 statsvalue.val7day.Do(func(p interface{}) {
58 if p != nil {
59 val7day += p.(int32)
60 }
61 })
62 val7day = val7day / 2016
63 return statsvalue.val5min, val60min, val1day, val7day
64 }
65
66 type Statistics struct {
67 logger logger.Logger
68 //Mutex to protect the statistics from concurrent updates
69 updating sync.Mutex
70
71 //Devices currently online
72 devices_online *StatsValue
73
74 //Total unicasts sent
75 unicasts_total *StatsValue
76
77 //Total broadcasts sent
78 broadcasts_total *StatsValue
79
80 //Device-specific accumulation
81 devices_specific map[string]*StatsValue
82
83 //Channel-specific accumulation
84 channel_specific map[string]*StatsValue
85 }
86
87 func NewStatistics(logger logger.Logger) *Statistics {
88 result := &Statistics{
89 logger: logger,
90 updating: sync.Mutex{},
91 devices_online: NewStatsValue(),
92 unicasts_total: NewStatsValue(),
93 broadcasts_total: NewStatsValue(),
94 }
95 go result.PrintStats()
96 //Enable the following line for testing statistics gathering and aggregation
97 //go result.TestStats()
98 return result
99 }
100
101 func (stats *Statistics) Accumulate() {
102 stats.devices_online.Accumulate()
103 stats.unicasts_total.Accumulate()
104 stats.broadcasts_total.Accumulate()
105 for _, value := range stats.devices_specific {
106 value.Accumulate()
107 }
108 for _, value := range stats.channel_specific {
109 value.Accumulate()
110 }
111 }
112
113 func (stats *Statistics) Reset5min() {
114 stats.unicasts_total.Reset5min()
115 stats.broadcasts_total.Reset5min()
116 }
117
118 func (stats *Statistics) DecreaseDevices(device_name string, channel_name string) {
119 stats.updating.Lock()
120 stats.devices_online.val5min--
121 if stats.devices_specific == nil {
122 stats.devices_specific = make(map[string]*StatsValue)
123 }
124 if stats.devices_specific[device_name] == nil {
125 stats.devices_specific[device_name] = NewStatsValue()
126 }
127 stats.devices_specific[device_name].val5min--
128 if stats.channel_specific == nil {
129 stats.channel_specific = make(map[string]*StatsValue)
130 }
131 if stats.channel_specific[channel_name] == nil {
132 stats.channel_specific[channel_name] = NewStatsValue()
133 }
134 stats.channel_specific[channel_name].val5min--
135
136 stats.updating.Unlock()
137 }
138
139 func (stats *Statistics) IncreaseDevices(device_name string, channel_name string) {
140 stats.updating.Lock()
141 stats.devices_online.val5min++
142 if stats.devices_specific == nil {
143 stats.devices_specific = make(map[string]*StatsValue)
144 }
145 if stats.devices_specific[device_name] == nil {
146 stats.devices_specific[device_name] = NewStatsValue()
147 }
148 stats.devices_specific[device_name].val5min++
149 if stats.channel_specific == nil {
150 stats.channel_specific = make(map[string]*StatsValue)
151 }
152 if stats.channel_specific[channel_name] == nil {
153 stats.channel_specific[channel_name] = NewStatsValue()
154 }
155 stats.channel_specific[channel_name].val5min++
156 stats.updating.Unlock()
157 }
158
159 func (stats *Statistics) IncreaseUnicasts() {
160 stats.updating.Lock()
161 stats.unicasts_total.val5min++
162 stats.updating.Unlock()
163 }
164
165 func (stats *Statistics) IncreaseBroadcasts() {
166 stats.updating.Lock()
167 stats.broadcasts_total.val5min++
168 stats.updating.Unlock()
169 }
170
171 func (stats *Statistics) TestStats() {
172 t := time.NewTicker(time.Millisecond * 500)
173 for {
174 stats.devices_online.val5min = 20
175 stats.IncreaseUnicasts()
176 stats.IncreaseBroadcasts()
177 stats.IncreaseDevices("bacon", "16.04/rc")
178 <-t.C
179 }
180 }
181
182 //Shall be called periodically (every 5 minutes) to output stats
183 func (stats *Statistics) PrintStats() {
184 t := time.NewTicker(time.Minute * 5)
185 for {
186 stats.updating.Lock()
187 stats.Accumulate()
188
189 //Tally the accumulated values
190 devices_online_5min, devices_online_60min, devices_online_1day, devices_online_7day := stats.devices_online.Report()
191 unicasts_total_5min, unicasts_total_60min, unicasts_total_1day, unicasts_total_7day := stats.unicasts_total.Report()
192 broadcasts_total_5min, broadcasts_total_60min, broadcasts_total_1day, broadcasts_total_7day := stats.broadcasts_total.Report()
193
194 stats.logger.Infof("Usage statistics:")
195 stats.logger.Infof(" | Devices | Unicasts | Broadcasts |")
196 stats.logger.Infof("5 mins | %10v | %10v | %10v |", devices_online_5min, unicasts_total_5min, broadcasts_total_5min)
197 stats.logger.Infof("60 mins | %10v | %10v | %10v |", devices_online_60min, unicasts_total_60min, broadcasts_total_60min)
198 stats.logger.Infof("1 day | %10v | %10v | %10v |", devices_online_1day, unicasts_total_1day, broadcasts_total_1day)
199 stats.logger.Infof("7 days | %10v | %10v | %10v |", devices_online_7day, unicasts_total_7day, broadcasts_total_7day)
200 stats.logger.Infof("")
201 stats.logger.Infof("Device statistics:")
202 stats.logger.Infof("%20v | %10v | %10v | %10v | %10v", "Device", "5 mins","60 mins","1 day","7 days")
203 for key, value := range stats.devices_specific {
204 devices_online_5min, devices_online_60min, devices_online_1day, devices_online_7day = value.Report()
205 stats.logger.Infof("%20v | %10v | %10v | %10v | %10v", key, devices_online_5min, devices_online_60min, devices_online_1day, devices_online_7day)
206 }
207 stats.logger.Infof("")
208 stats.logger.Infof("Channel statistics:")
209 stats.logger.Infof("%35v | %10v | %10v | %10v | %10v", "Channel", "5 mins","60 mins","1 day","7 days")
210 for key, value := range stats.channel_specific {
211 devices_online_5min, devices_online_60min, devices_online_1day, devices_online_7day = value.Report()
212 stats.logger.Infof("%35v | %10v | %10v | %10v | %10v", key, devices_online_5min, devices_online_60min, devices_online_1day, devices_online_7day)
213 }
214 stats.Reset5min()
215 stats.updating.Unlock()
216
217 //Wait until timer has elapsed
218 <-t.C
219 }
220 }
221
222 type StatsData struct {
223 DevicesOnline *StatsDataValue `json:"devicesOnline"`
224 UnicastsTotal *StatsDataValue `json:"unicastsTotal"`
225 BroadcastsTotal *StatsDataValue `json:"broadcastsTotal"`
226 Devices *[]StatsDataDetailValue `json:"devices"`
227 Channels *[]StatsDataDetailValue `json:"channels"`
228 }
229 type StatsDataValue struct {
230 Val5min int32 `json:"5min"`
231 Val60min int32 `json:"60min"`
232 Val1day int32 `json:"1day"`
233 Val7day int32 `json:"7day"`
234 }
235 type StatsDataDetailValue struct {
236 Key string `json:"type"`
237 Values *StatsDataValue `json:"data"`
238 }
239
240 func (stats *Statistics) StatsValueToStatsDataValue(statsValue *StatsValue) *StatsDataValue {
241 sVal5min, sVal60min, sVal1day, sVal7day := statsValue.Report()
242 return &StatsDataValue{
243 Val5min: sVal5min,
244 Val60min: sVal60min,
245 Val1day: sVal1day,
246 Val7day: sVal7day,
247 }
248 }
249
250 func (stats *Statistics) GetStats() *StatsData {
251 var channels []StatsDataDetailValue
252 var devices []StatsDataDetailValue
253 for key, value := range stats.channel_specific {
254 channels = append(channels, StatsDataDetailValue{
255 Key: key,
256 Values: stats.StatsValueToStatsDataValue(value),
257 })
258 }
259 for key, value := range stats.devices_specific {
260 devices = append(devices, StatsDataDetailValue{
261 Key: key,
262 Values: stats.StatsValueToStatsDataValue(value),
263 })
264 }
265 statsData := StatsData{
266 DevicesOnline: stats.StatsValueToStatsDataValue(stats.devices_online),
267 UnicastsTotal: stats.StatsValueToStatsDataValue(stats.unicasts_total),
268 BroadcastsTotal: stats.StatsValueToStatsDataValue(stats.broadcasts_total),
269 Devices: &devices,
270 Channels: &channels,
271 }
272 return &statsData
273 }