nps/lib/mux/web.go

155 lines
3.0 KiB
Go
Raw Normal View History

2019-10-14 15:44:07 +00:00
package mux
import (
"fmt"
2019-10-15 08:32:21 +00:00
"github.com/astaxie/beego/logs"
2019-10-14 15:44:07 +00:00
"net/http"
"sort"
"strconv"
2019-10-15 08:32:21 +00:00
"strings"
"sync"
2019-10-14 15:44:07 +00:00
"time"
)
type connLog struct {
startTime time.Time
isClose bool
logs []string
}
2019-10-15 08:32:21 +00:00
var logms map[int]*connLog
var logmc map[int]*connLog
2019-10-14 15:44:07 +00:00
2019-10-15 08:32:21 +00:00
var copyMaps map[int]*connLog
var copyMapc map[int]*connLog
2019-10-14 15:46:00 +00:00
var stashTimeNow time.Time
2019-10-15 08:32:21 +00:00
var mutex sync.Mutex
2019-10-14 15:44:07 +00:00
2019-10-15 08:32:21 +00:00
func deepCopyMaps() {
copyMaps = make(map[int]*connLog)
for k, v := range logms {
copyMaps[k] = &connLog{
2019-10-14 15:44:07 +00:00
startTime: v.startTime,
isClose: v.isClose,
logs: v.logs,
}
}
}
2019-10-15 08:32:21 +00:00
func deepCopyMapc() {
copyMapc = make(map[int]*connLog)
for k, v := range logmc {
copyMapc[k] = &connLog{
startTime: v.startTime,
isClose: v.isClose,
logs: v.logs,
}
2019-10-14 15:46:00 +00:00
}
}
2019-10-14 15:44:07 +00:00
2019-10-15 08:32:21 +00:00
func init() {
logms = make(map[int]*connLog)
logmc = make(map[int]*connLog)
}
2019-10-14 15:44:07 +00:00
type IntSlice []int
func (s IntSlice) Len() int { return len(s) }
func (s IntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }
func NewLogServer() {
http.HandleFunc("/", index)
http.HandleFunc("/detail", detail)
http.HandleFunc("/stash", stash)
fmt.Println(http.ListenAndServe(":8899", nil))
}
func stash(w http.ResponseWriter, r *http.Request) {
2019-10-15 08:32:21 +00:00
stashTimeNow = time.Now()
deepCopyMaps()
deepCopyMapc()
2019-10-14 15:44:07 +00:00
w.Write([]byte("ok"))
}
2019-10-15 08:32:21 +00:00
func getM(label string, id int) (cL *connLog) {
label = strings.TrimSpace(label)
mutex.Lock()
defer mutex.Unlock()
if label == "nps" {
cL = logms[id]
}
if label == "npc" {
cL = logmc[id]
}
return
}
func setM(label string, id int, cL *connLog) {
label = strings.TrimSpace(label)
mutex.Lock()
defer mutex.Unlock()
if label == "nps" {
logms[id] = cL
}
if label == "npc" {
logmc[id] = cL
}
}
2019-10-14 15:44:07 +00:00
func index(w http.ResponseWriter, r *http.Request) {
var keys []int
2019-10-15 08:32:21 +00:00
for k := range copyMaps {
2019-10-14 15:44:07 +00:00
keys = append(keys, k)
}
sort.Sort(IntSlice(keys))
var s string
2019-10-15 08:32:21 +00:00
s += "<h1>nps</h1>"
for _, v := range keys {
connL := copyMaps[v]
s += "<a href='/detail?id=" + strconv.Itoa(v) + "&label=nps" + "'>" + strconv.Itoa(v) + "</a>----------"
s += strconv.Itoa(int(stashTimeNow.Sub(connL.startTime).Milliseconds())) + "ms----------"
s += strconv.FormatBool(connL.isClose)
s += "<br>"
}
keys = keys[:0]
s += "<h1>npc</h1>"
for k := range copyMapc {
keys = append(keys, k)
}
sort.Sort(IntSlice(keys))
for _, v := range keys {
connL := copyMapc[v]
s += "<a href='/detail?id=" + strconv.Itoa(v) + "&label=npc" + "'>" + strconv.Itoa(v) + "</a>----------"
s += strconv.Itoa(int(stashTimeNow.Sub(connL.startTime).Milliseconds())) + "ms----------"
2019-10-14 15:44:07 +00:00
s += strconv.FormatBool(connL.isClose)
s += "<br>"
}
w.Write([]byte(s))
}
func detail(w http.ResponseWriter, r *http.Request) {
id := r.FormValue("id")
2019-10-15 08:32:21 +00:00
label := r.FormValue("label")
logs.Warn(label)
2019-10-14 15:44:07 +00:00
i, _ := strconv.Atoi(id)
2019-10-15 08:32:21 +00:00
var v *connLog
if label == "nps" {
v, _ = copyMaps[i]
}
if label == "npc" {
v, _ = copyMapc[i]
}
2019-10-14 15:44:07 +00:00
var s string
2019-10-15 08:32:21 +00:00
if v != nil {
for i, vv := range v.logs {
s += "<p>" + strconv.Itoa(i+1) + ":" + vv + "</p>"
}
2019-10-14 15:44:07 +00:00
}
w.Write([]byte(s))
}