forked from xuri/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statisticsUtils.go
207 lines (199 loc) · 5.98 KB
/
statisticsUtils.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2016 - 2018 The aurora Authors. All rights reserved. Use of this
// source code is governed by a MIT license that can be found in the LICENSE
// file.
//
// The aurora is a web-based Beanstalk queue server console written in Go
// and works on macOS, Linux and Windows machines. Main idea behind using Go
// for backend development is to utilize ability of the compiler to produce
// zero-dependency binaries for multiple platforms. aurora was created as an
// attempt to build very simple and portable application to work with local or
// remote Beanstalk server.
package main
import (
"bytes"
"container/list"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/xuri/aurora/beanstalk"
)
// statisticPreferenceSave provide method to save statistics preference
// settings.
func statisticPreferenceSave(f url.Values, w http.ResponseWriter, r *http.Request) {
var err error
var collection, frequency string
var tubes []string
alert := `<div class="alert alert-danger" id="sfsa"><button type="button" class="close" onclick="$('#sfsa').fadeOut('fast');">×</button><span> Required fields are not set correct</span></div>`
err = readConf()
if err != nil {
_, _ = io.WriteString(w, tplStatisticSetting(tplStatisticEdit(`<div class="alert alert-danger"><button type="button" class="close" data-dismiss="alert">×</button><span> Read config error</span></div>`)))
return
}
for k, v := range f {
switch k {
case "frequency":
frequency = v[0]
case "collection":
collection = v[0]
case "action":
continue
default:
t := strings.TrimSuffix(strings.TrimPrefix(k, `tubes[`), `]`)
tubes = append(tubes, t)
}
}
if len(tubes) == 0 || collection == "" || frequency == "" {
_, _ = io.WriteString(w, tplStatisticSetting(tplStatisticEdit(alert)))
return
}
err = statisticCashier(collection, frequency, tubes)
if err != nil {
_, _ = io.WriteString(w, tplStatisticSetting(tplStatisticEdit(`<div class="alert alert-danger" id="sfsa"><button type="button" class="close" onclick="$('#sfsa').fadeOut('fast');">×</button><span> Save statistics preference error</span></div>`)))
return
}
_, _ = io.WriteString(w, tplStatisticSetting(tplStatisticEdit(`<div class="alert alert-success" id="sfsa"><button type="button" class="close" onclick="$('#sfsa').fadeOut('fast');">×</button><span> Statistics preference saved</span></div>`)))
}
// statisticCashier validate collection and frequency parameter and send notify
// to statistic Goroutine that the configuration of statistics preference
// settings has changed.
func statisticCashier(collection string, frequency string, tubes []string) error {
c, err := strconv.Atoi(collection)
if err != nil {
return err
}
f, err := strconv.Atoi(frequency)
if err != nil {
return err
}
if c < 1 {
c = 0
}
if f < 1 {
f = 1
}
selfConf.StatisticsCollection = c
selfConf.StatisticsFrequency = f
statisticsDataServer = make(map[string]map[string]map[string]*list.List)
for _, v := range tubes {
addr := strings.Split(v, `:`)
if len(addr) != 3 {
continue
}
tube := make(map[string]map[string]*list.List)
tube[addr[2]] = make(map[string]*list.List)
s, ok := statisticsDataServer[addr[0]+`:`+addr[1]]
if !ok {
statisticsDataServer[addr[0]+`:`+addr[1]] = tube
} else {
s[addr[2]] = tube[addr[2]]
}
}
statisticsData.Lock()
statisticsData.Server = statisticsDataServer
statisticsData.Unlock()
notify <- true
return nil
}
// statistic provide method to control statisticAgent collect the statistics
// data in a Goroutine.
func statistic() {
for {
tick := time.Tick(time.Duration(selfConf.StatisticsFrequency) * time.Second)
NOTIFY:
for {
select {
case <-notify:
break NOTIFY
case <-tick:
for k, v := range statisticsData.Server {
for t := range v {
if selfConf.StatisticsCollection == 0 {
continue
}
err := statisticAgent(k, t)
if err != nil {
continue
}
}
}
}
}
}
}
// statisticAgent collect the statistics data by given server and tube.
func statisticAgent(server string, tube string) error {
var err error
var bstkConn *beanstalk.Conn
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
return err
}
tubeStats := &beanstalk.Tube{
Conn: bstkConn,
Name: tube,
}
statsMap, err := tubeStats.Stats()
if err != nil {
bstkConn.Close()
return err
}
for _, field := range statisticsFields {
for k, v := range field {
t := time.Now()
stats, err := strconv.Atoi(statsMap[v])
if err != nil {
continue
}
statisticsData.Lock()
_, ok := statisticsData.Server[server][tube][k]
if !ok {
statisticsData.Server[server][tube][k] = list.New()
}
if statisticsData.Server[server][tube][k].Len() >= selfConf.StatisticsCollection {
front := statisticsData.Server[server][tube][k].Back()
statisticsData.Server[server][tube][k].Remove(front)
}
statisticsData.Server[server][tube][k].PushFront([]int{t.Year(), int(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Second(), stats})
statisticsData.Unlock()
}
}
bstkConn.Close()
return nil
}
// statisticWaitress return real-time statistics data by given server and tube.
func statisticWaitress(server string, tube string) string {
var buf, b, s, l bytes.Buffer
b.WriteString(`{`)
statisticsData.RLock()
for _, field := range statisticsFields {
for k := range field {
b.WriteString(`"`)
b.WriteString(k)
b.WriteString(`":[`)
_, ok := statisticsData.Server[server][tube][k]
if !ok {
b.WriteString(`],`)
continue
}
s.Reset()
for e := statisticsData.Server[server][tube][k].Front(); e != nil; e = e.Next() {
s.WriteString(`[`)
l.Reset()
for _, v := range e.Value.([]int) {
l.WriteString(strconv.Itoa(v))
l.WriteString(`,`)
}
s.WriteString(strings.TrimSuffix(l.String(), `,`))
s.WriteString(`],`)
}
b.WriteString(strings.TrimSuffix(s.String(), `,`))
b.WriteString(`],`)
}
}
statisticsData.RUnlock()
buf.WriteString(strings.TrimSuffix(b.String(), `,`))
buf.WriteString(`}`)
return buf.String()
}