forked from xuri/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tplNav.go
248 lines (230 loc) · 9.18 KB
/
tplNav.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"bytes"
"net/url"
"sort"
"strconv"
"github.com/Luxurioust/aurora/beanstalk"
"strings"
)
const (
kCurrentJobsDelay = "current-jobs-ready"
kCurrentJobsBuried = "current-jobs-buried"
kCurrentJobsReserved = "current-jobs-reserved"
)
// getServerStatus render a server stats table.
func getServerStatus() string {
var err error
var buf, td, th bytes.Buffer
for _, addr := range selfConf.Servers {
var bstkConn *beanstalk.Conn
if bstkConn, err = beanstalk.Dial("tcp", addr); err != nil {
td.WriteString(`<tr><td>`)
td.WriteString(addr)
td.WriteString(`</td><td colspan="`)
td.WriteString(strconv.Itoa(len(selfConf.Filter)))
td.WriteString(`" class="row-full"> </td><td><a class="btn btn-xs btn-danger" title="Remove from list" href="serversRemove?action=serversRemove&removeServer=`)
td.WriteString(addr)
td.WriteString(`"><span class="glyphicon glyphicon-minus"> </span></a></td></tr>`)
continue
}
s, _ := bstkConn.Stats()
bstkConn.Close()
td.WriteString(`<tr><td><a href="/server?server=`)
td.WriteString(addr)
td.WriteString(`">`)
td.WriteString(addr)
td.WriteString(`</a></td>`)
for _, v := range selfConf.Filter {
td.WriteString(`<td>`)
td.WriteString(s[v])
td.WriteString(`</td>`)
}
td.WriteString(`<td><a class="btn btn-xs btn-danger" title="Remove from list" href="serversRemove?action=serversRemove&removeServer=`)
td.WriteString(addr)
td.WriteString(`"><span class="glyphicon glyphicon-minus"> </span></a></td></tr>`)
}
for _, v := range selfConf.Filter {
th.WriteString(`<th>`)
th.WriteString(v)
th.WriteString(`</th>`)
}
buf.WriteString(`<div class="row"><div class="col-sm-12"><table class="table table-striped table-hover" id="servers-index"><thead><tr><th>name</th>`)
buf.WriteString(th.String())
buf.WriteString(`<th> </th></tr></thead><tbody>`)
buf.WriteString(td.String())
buf.WriteString(`</tbody></table><a href="#servers-add" role="button" class="btn btn-info" id="addServer">Add server</a></div></div>`)
return buf.String()
}
// getServerTubes render a tubes stats table by given server.
func getServerTubes(server string) string {
var err error
var buf, th, tr, td bytes.Buffer
var bstkConn *beanstalk.Conn
// 展示头部
for _, v := range selfConf.TubeFilters {
th.WriteString(`<th>`)
th.WriteString(v)
th.WriteString(`</th>`)
}
// 创建tcp连接
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
buf.WriteString(`<div id="idAllTubes"><section id="summaryTable"><div class="row"><div class="col-sm-12"><table class="table table-striped table-hover"><thead><tr><th>name</th>`)
buf.WriteString(th.String())
buf.WriteString(`</tr></thead><tbody></tbody></table></div></div></section></div>`)
return buf.String()
}
// 创建到Beanstalk的连接
tubes, _ := bstkConn.ListTubes()
// 对tubes进行排序
sort.Strings(tubes)
sortedTubes := make([]string, 0, len(tubes))
for _, v := range tubes {
if !strings.HasSuffix(v, "_test") && v != "default" {
sortedTubes = append(sortedTubes, v)
}
}
// _test 放在后面
for _, v := range tubes {
if strings.HasSuffix(v, "_test") {
sortedTubes = append(sortedTubes, v)
}
}
if len(sortedTubes) < len(tubes) {
sortedTubes = append(sortedTubes, "default")
}
for _, v := range sortedTubes {
tubeStats := &beanstalk.Tube{
Conn: bstkConn,
Name: v,
}
// 获取tube的统计数据
statsMap, err := tubeStats.Stats()
if err != nil {
continue
}
for _, stats := range selfConf.TubeFilters {
if kCurrentJobsDelay == stats && statsMap[stats] != "0" {
td.WriteString(`<td style="background-color:#e4470a;color:#ffffff;font-weight:bold;">`)
} else if kCurrentJobsBuried == stats && statsMap[stats] != "0" {
td.WriteString(`<td style="color:#f00;font-weight:bold;">`)
} else if kCurrentJobsReserved == stats && statsMap[stats] != "0" {
td.WriteString(`<td style="color:#ff00f1;font-weight:bold;">`)
} else {
td.WriteString(`<td>`)
}
td.WriteString(statsMap[stats])
td.WriteString(`</td>`)
}
// 准备TR
tr.WriteString(`<tr><td><a href="tube?server=`)
tr.WriteString(server)
tr.WriteString(`&tube=`)
tr.WriteString(url.QueryEscape(v))
tr.WriteString(`">`)
tr.WriteString(v)
tr.WriteString(`</a></td>`)
// 输出上面的TD
tr.WriteString(td.String())
tr.WriteString(`</tr>`)
td.Reset()
}
bstkConn.Close()
buf.WriteString(`<div id="idAllTubes"><section id="summaryTable"><div class="row"><div class="col-sm-12"><table class="table table-striped table-hover"><thead><tr><th>name</th>`)
buf.WriteString(th.String())
buf.WriteString(`</tr></thead><tbody>`)
buf.WriteString(tr.String())
buf.WriteString(`</tbody></table></div></div></section></div>`)
return buf.String()
}
// dropDownServer render a navigation dropdown menu for server list.
func dropDownServer(currentServer string) string {
var ul bytes.Buffer
if currentServer == "" {
currentServer = `All servers`
}
ul.WriteString(`<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">`)
ul.WriteString(currentServer)
ul.WriteString(` <span class="caret"></span></a><ul class="dropdown-menu">`)
for _, addr := range selfConf.Servers {
if addr == currentServer {
continue
}
ul.WriteString(`<li><a href="./server?server=`)
ul.WriteString(addr)
ul.WriteString(`">`)
ul.WriteString(addr)
ul.WriteString(`</a></li>`)
}
if currentServer != "All servers" {
ul.WriteString(`<li><a href="./public">All servers</a></li>`)
}
ul.WriteString(`</ul></li>`)
return ul.String()
}
// dropDownTube render a navigation dropdown menu for tube list.
func dropDownTube(server string, currentTube string) string {
var ul bytes.Buffer
if currentTube == "" {
currentTube = `All tubes`
}
ul.WriteString(`<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">`)
ul.WriteString(currentTube)
ul.WriteString(` <span class="caret"></span></a><ul class="dropdown-menu">`)
var bstkConn *beanstalk.Conn
var err error
if bstkConn, err = beanstalk.Dial("tcp", server); err != nil {
if currentTube != "" {
ul.WriteString(`<li><a href="./public">All tubes</a></li>`)
}
ul.WriteString(`</ul></li>`)
return ul.String()
}
tubes, _ := bstkConn.ListTubes()
sort.Strings(tubes)
for _, v := range tubes {
ul.WriteString(`<li><a href="./tube?server=`)
ul.WriteString(server)
ul.WriteString(`&tube=`)
ul.WriteString(url.QueryEscape(v))
ul.WriteString(`">`)
ul.WriteString(v)
ul.WriteString(`</a></li>`)
}
bstkConn.Close()
if currentTube != "All tubes" {
ul.WriteString(`<li><a href="./server?server=`)
ul.WriteString(server)
ul.WriteString(`">All tubes</a></li>`)
}
ul.WriteString(`</ul></li>`)
return ul.String()
}
// dropEditSettings render a navigation dropdown menu for set preference.
func dropEditSettings() string {
var buf bytes.Buffer
var isDisabledJSONDecode, isDisabledJobDataHighlight, isEnabledBase64Decode string
if selfConf.IsDisabledJSONDecode != 1 {
isDisabledJSONDecode = `checked="checked"`
}
if selfConf.IsDisabledJobDataHighlight != 1 {
isDisabledJobDataHighlight = `checked="checked"`
}
if selfConf.IsEnabledBase64Decode != 0 {
isEnabledBase64Decode = `checked="checked"`
}
buf.WriteString(`<div id="settings" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="settings-label" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h4 class="modal-title" id="settings-label">Settings</h4></div><div class="modal-body"><fieldset><div class="form-group"><label for="tubePauseSeconds"><b>Tube pause seconds</b> (<i>-1</i> means the default: <i>3600</i>, <i>0</i> is reserved for un-pause)</label><input class="form-control focused" id="tubePauseSeconds" type="number" value="`)
buf.WriteString(strconv.Itoa(selfConf.TubePauseSeconds))
buf.WriteString(`"></div><div class="form-group"><label><b>Auto-refresh interval in milliseconds</b> (Default: <i>500</i>)</label><input class="form-control focused" id="autoRefreshTimeoutMs" type="number" value="`)
buf.WriteString(strconv.Itoa(selfConf.AutoRefreshTimeoutMs))
buf.WriteString(`"></div><div class="form-group"><label><b>Search result limits</b> (Default: <i>25</i>)</label><input class="form-control focused" id="searchResultLimit" type="number" value="`)
buf.WriteString(strconv.Itoa(selfConf.SearchResultLimit))
buf.WriteString(`"></div><div class="form-group"><label><b>Preferred way to deal with job data</b></label><div class="checkbox"><label><input type="checkbox" id="isDisabledJsonDecode" value="1" `)
buf.WriteString(isDisabledJSONDecode)
buf.WriteString(`>before display: JSON decode</label></div><div class="checkbox"><label><input type="checkbox" id="isEnabledBase64Decode" value="1" `)
buf.WriteString(isEnabledBase64Decode)
buf.WriteString(`>before display: Base64 decode</label></div><div class="checkbox"><label><input type="checkbox" id="isDisabledJobDataHighlight" value="1" `)
buf.WriteString(isDisabledJobDataHighlight)
buf.WriteString(`>after display: enable highlight</label></div></div></fieldset></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Close</button></div></div></div></div>`)
return buf.String()
}