f4867aad by astaxie

Merge pull request #744 from lei-cao/develop

ajax refresh gc message
2 parents 7d1b03ee 17006cfb
...@@ -11,6 +11,7 @@ package beego ...@@ -11,6 +11,7 @@ package beego
11 11
12 import ( 12 import (
13 "bytes" 13 "bytes"
14 "encoding/json"
14 "fmt" 15 "fmt"
15 "net/http" 16 "net/http"
16 "text/template" 17 "text/template"
...@@ -59,6 +60,7 @@ func init() { ...@@ -59,6 +60,7 @@ func init() {
59 func adminIndex(rw http.ResponseWriter, r *http.Request) { 60 func adminIndex(rw http.ResponseWriter, r *http.Request) {
60 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) 61 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
61 tmpl = template.Must(tmpl.Parse(indexTpl)) 62 tmpl = template.Must(tmpl.Parse(indexTpl))
63 tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
62 data := make(map[interface{}]interface{}) 64 data := make(map[interface{}]interface{})
63 tmpl.Execute(rw, data) 65 tmpl.Execute(rw, data)
64 } 66 }
...@@ -68,6 +70,7 @@ func adminIndex(rw http.ResponseWriter, r *http.Request) { ...@@ -68,6 +70,7 @@ func adminIndex(rw http.ResponseWriter, r *http.Request) {
68 func qpsIndex(rw http.ResponseWriter, r *http.Request) { 70 func qpsIndex(rw http.ResponseWriter, r *http.Request) {
69 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) 71 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
70 tmpl = template.Must(tmpl.Parse(qpsTpl)) 72 tmpl = template.Must(tmpl.Parse(qpsTpl))
73 tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
71 data := make(map[interface{}]interface{}) 74 data := make(map[interface{}]interface{})
72 data["Content"] = toolbox.StatisticsMap.GetMap() 75 data["Content"] = toolbox.StatisticsMap.GetMap()
73 76
...@@ -127,6 +130,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) { ...@@ -127,6 +130,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
127 130
128 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) 131 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
129 tmpl = template.Must(tmpl.Parse(configTpl)) 132 tmpl = template.Must(tmpl.Parse(configTpl))
133 tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
130 134
131 data["Content"] = m 135 data["Content"] = m
132 136
...@@ -158,6 +162,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) { ...@@ -158,6 +162,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
158 data["Title"] = "Routers" 162 data["Title"] = "Routers"
159 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) 163 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
160 tmpl = template.Must(tmpl.Parse(routerAndFilterTpl)) 164 tmpl = template.Must(tmpl.Parse(routerAndFilterTpl))
165 tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
161 tmpl.Execute(rw, data) 166 tmpl.Execute(rw, data)
162 case "filter": 167 case "filter":
163 resultList := new([][]string) 168 resultList := new([][]string)
...@@ -250,6 +255,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) { ...@@ -250,6 +255,7 @@ func listConf(rw http.ResponseWriter, r *http.Request) {
250 data["Title"] = "Filters" 255 data["Title"] = "Filters"
251 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) 256 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
252 tmpl = template.Must(tmpl.Parse(routerAndFilterTpl)) 257 tmpl = template.Must(tmpl.Parse(routerAndFilterTpl))
258 tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
253 tmpl.Execute(rw, data) 259 tmpl.Execute(rw, data)
254 260
255 default: 261 default:
...@@ -302,16 +308,35 @@ func printTree(resultList *[][]string, t *Tree) { ...@@ -302,16 +308,35 @@ func printTree(resultList *[][]string, t *Tree) {
302 func profIndex(rw http.ResponseWriter, r *http.Request) { 308 func profIndex(rw http.ResponseWriter, r *http.Request) {
303 r.ParseForm() 309 r.ParseForm()
304 command := r.Form.Get("command") 310 command := r.Form.Get("command")
305 data := make(map[interface{}]interface{}) 311 format := r.Form.Get("format")
312 data := make(map[string]interface{})
306 313
307 var result bytes.Buffer 314 var result bytes.Buffer
308 if command != "" { 315 if command != "" {
309 toolbox.ProcessInput(command, &result) 316 toolbox.ProcessInput(command, &result)
310 data["Content"] = result.String() 317 data["Content"] = result.String()
311 data["Title"] = command
312 318
319 if format == "json" && command == "gc summary" {
320 dataJson, err := json.Marshal(data)
321 if err != nil {
322 http.Error(rw, err.Error(), http.StatusInternalServerError)
323 return
324 }
325
326 rw.Header().Set("Content-Type", "application/json")
327 rw.Write(dataJson)
328 return
329 }
330
331 data["Title"] = command
313 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) 332 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
314 tmpl = template.Must(tmpl.Parse(profillingTpl)) 333 tmpl = template.Must(tmpl.Parse(profillingTpl))
334 if command == "gc summary" {
335 tmpl = template.Must(tmpl.Parse(gcAjaxTpl))
336 } else {
337
338 tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
339 }
315 tmpl.Execute(rw, data) 340 tmpl.Execute(rw, data)
316 } else { 341 } else {
317 } 342 }
...@@ -353,6 +378,7 @@ func healthcheck(rw http.ResponseWriter, req *http.Request) { ...@@ -353,6 +378,7 @@ func healthcheck(rw http.ResponseWriter, req *http.Request) {
353 data["Title"] = "Health Check" 378 data["Title"] = "Health Check"
354 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) 379 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
355 tmpl = template.Must(tmpl.Parse(healthCheckTpl)) 380 tmpl = template.Must(tmpl.Parse(healthCheckTpl))
381 tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
356 tmpl.Execute(rw, data) 382 tmpl.Execute(rw, data)
357 383
358 } 384 }
...@@ -401,6 +427,7 @@ func taskStatus(rw http.ResponseWriter, req *http.Request) { ...@@ -401,6 +427,7 @@ func taskStatus(rw http.ResponseWriter, req *http.Request) {
401 data["Title"] = "Tasks" 427 data["Title"] = "Tasks"
402 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl)) 428 tmpl := template.Must(template.New("dashboard").Parse(dashboardTpl))
403 tmpl = template.Must(tmpl.Parse(tasksTpl)) 429 tmpl = template.Must(tmpl.Parse(tasksTpl))
430 tmpl = template.Must(tmpl.Parse(defaultScriptsTpl))
404 tmpl.Execute(rw, data) 431 tmpl.Execute(rw, data)
405 } 432 }
406 433
......
...@@ -27,11 +27,35 @@ For detail usage please check our document: ...@@ -27,11 +27,35 @@ For detail usage please check our document:
27 var profillingTpl = ` 27 var profillingTpl = `
28 {{define "content"}} 28 {{define "content"}}
29 <h1>{{.Title}}</h1> 29 <h1>{{.Title}}</h1>
30 <pre> 30 <pre id="content">
31 {{.Content}} 31 <div>{{.Content}}</div>
32 </pre> 32 </pre>
33 {{end}}` 33 {{end}}`
34 34
35 var defaultScriptsTpl = ``
36
37 var gcAjaxTpl = `
38 {{define "scripts"}}
39 <script type="text/javascript">
40 var app = app || {};
41 (function() {
42 app.$el = $('#content');
43 app.getGc = function() {
44 var that = this;
45 $.ajax("/prof?command=gc%20summary&format=json").done(function(data) {
46 that.$el.append($('<p>' + data.Content + '</p>'));
47 });
48 };
49 $(document).ready(function() {
50 setInterval(function() {
51 app.getGc();
52 }, 3000);
53 });
54 })();
55 </script>
56 {{end}}
57 `
58
35 var qpsTpl = ` 59 var qpsTpl = `
36 {{define "content"}} 60 {{define "content"}}
37 <h1>Requests statistics</h1> 61 <h1>Requests statistics</h1>
...@@ -311,6 +335,7 @@ Healthcheck ...@@ -311,6 +335,7 @@ Healthcheck
311 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 335 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
312 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> 336 <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
313 337
338 {{template "scripts" .}}
314 </body> 339 </body>
315 </html> 340 </html>
316 ` 341 `
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!