From 2b48ed0bc5572a5ffe37bdedb65ea9b47c3c3d75 Mon Sep 17 00:00:00 2001 From: panxinming Date: Tue, 12 Nov 2019 06:53:55 +0800 Subject: [PATCH 1/3] add ShowHtmlTableData --- core/state.go | 149 ++++++++++++++++++++++++++++++++++++++++++++- dotweb_sysgroup.go | 2 +- 2 files changed, 148 insertions(+), 3 deletions(-) diff --git a/core/state.go b/core/state.go index cd57df7..8d6ddad 100644 --- a/core/state.go +++ b/core/state.go @@ -19,6 +19,120 @@ const ( defaultCheckTimeMinutes = 10 ) +var htmlTable = ` + + New Document + + + + + + + + + + + + + + + + {{body}} +
IndexValue
+ + +` + // NewServerStateInfo return ServerStateInfo which is init func NewServerStateInfo() *ServerStateInfo { state := &ServerStateInfo{ @@ -99,8 +213,8 @@ type ServerStateInfo struct { infoPool *pool } -// ShowHtmlData show server state data html-string format -func (state *ServerStateInfo) ShowHtmlData(version, globalUniqueId string) string { +// ShowHtmlDataRaw show server state data html-string format +func (state *ServerStateInfo) ShowHtmlDataRaw(version, globalUniqueId string) string { data := "
" data += "GlobalUniqueId : " + globalUniqueId data += "
" @@ -145,6 +259,37 @@ func (state *ServerStateInfo) ShowHtmlData(version, globalUniqueId string) strin return data } +// ShowHtmlData show server state data html-table format +func (state *ServerStateInfo) ShowHtmlTableData(version, globalUniqueId string) string { + data := "" + "GlobalUniqueId" + "" + globalUniqueId + "" + data += "" + "HostInfo" + "" + sysx.GetHostName() + "" + data += "" + "CurrentTime" + "" + time.Now().Format("2006-01-02 15:04:05") + "" + data += "" + "ServerVersion" + "" + version + "" + data += "" + "ServerStartTime" + "" + state.ServerStartTime.Format(dateTimeLayout) + "" + data += "" + "TotalRequestCount" + "" + strconv.FormatUint(state.TotalRequestCount, 10) + "" + data += "" + "CurrentRequestCount" + "" + strconv.FormatUint(state.CurrentRequestCount, 10) + "" + data += "" + "TotalErrorCount" + "" + strconv.FormatUint(state.TotalErrorCount, 10) + "" + state.IntervalRequestData.RLock() + data += "" + "IntervalRequestData" + "" + jsonutil.GetJsonString(state.IntervalRequestData.GetCurrentMap()) + "" + state.IntervalRequestData.RUnlock() + state.DetailRequestURLData.RLock() + data += "" + "DetailRequestUrlData" + "" + jsonutil.GetJsonString(state.DetailRequestURLData.GetCurrentMap()) + "" + state.DetailRequestURLData.RUnlock() + state.IntervalErrorData.RLock() + data += "" + "IntervalErrorData" + "" + jsonutil.GetJsonString(state.IntervalErrorData.GetCurrentMap()) + "" + state.IntervalErrorData.RUnlock() + state.DetailErrorPageData.RLock() + data += "" + "DetailErrorPageData" + "" + jsonutil.GetJsonString(state.DetailErrorPageData.GetCurrentMap()) + "" + state.DetailErrorPageData.RUnlock() + state.DetailErrorData.RLock() + data += "" + "DetailErrorData" + "" + jsonutil.GetJsonString(state.DetailErrorData.GetCurrentMap()) + "" + state.DetailErrorData.RUnlock() + state.DetailHTTPCodeData.RLock() + data += "" + "DetailHttpCodeData" + "" + jsonutil.GetJsonString(state.DetailHTTPCodeData.GetCurrentMap()) + "" + state.DetailHTTPCodeData.RUnlock() + return strings.Replace(htmlTable, "{{body}}", data, -1) +} + // QueryIntervalRequestData query request count by query time func (state *ServerStateInfo) QueryIntervalRequestData(queryKey string) uint64 { return state.IntervalRequestData.GetUInt64(queryKey) diff --git a/dotweb_sysgroup.go b/dotweb_sysgroup.go index 66177c4..af5d69f 100644 --- a/dotweb_sysgroup.go +++ b/dotweb_sysgroup.go @@ -50,7 +50,7 @@ func showIntervalData(ctx Context) error { // snow server status func showServerState(ctx Context) error { - return ctx.WriteHtml(ctx.HttpServer().StateInfo().ShowHtmlData(Version, ctx.HttpServer().DotApp.GlobalUniqueID())) + return ctx.WriteHtml(ctx.HttpServer().StateInfo().ShowHtmlTableData(Version, ctx.HttpServer().DotApp.GlobalUniqueID())) } // query server information From b95fe8a3930d1d0b1a08af5a8bfb714fcf4fdeba Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Tue, 12 Nov 2019 17:31:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?####=20Version=201.7.7=20*=20Opt:=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F=E8=B7=AF=E7=94=B1dotweb/st?= =?UTF-8?q?ate=E3=80=81dotweb/routers=E5=B1=95=E7=8E=B0=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E6=96=B9=E4=BE=BF=E9=98=85=E8=AF=BB=E7=9A=84?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E5=BD=A2=E5=BC=8F=E8=BE=93=E5=87=BA=20*=20Fe?= =?UTF-8?q?ature:=20=E6=96=B0=E5=A2=9Ecore.TableHtml\core.CreateTableHtml(?= =?UTF-8?q?)=E7=94=A8=E4=BA=8E=E7=94=9F=E6=88=90=E7=9B=B8=E5=85=B3Html?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=20*=20About:=20=20=20=20=20-=20=E5=8F=AF?= =?UTF-8?q?=E8=AE=BF=E9=97=AEdotweb/state=E6=9F=A5=E7=9C=8B=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E5=AE=9E=E4=BE=8B=E8=BF=90=E8=A1=8C=E6=97=B6=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20=20=20=20=20-=20=E5=8F=AF=E8=AE=BF=E9=97=AEdotweb/r?= =?UTF-8?q?outers=E6=9F=A5=E7=9C=8B=E5=BD=93=E5=89=8D=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=20*=202019-11-12=2018:00=20at=20ShangHai?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/htmlx.go | 21 +++++ core/state.go | 214 +++++++++++++++++++++++++-------------------- dotweb_sysgroup.go | 16 +++- version.MD | 8 ++ 4 files changed, 161 insertions(+), 98 deletions(-) create mode 100644 core/htmlx.go diff --git a/core/htmlx.go b/core/htmlx.go new file mode 100644 index 0000000..5f759f8 --- /dev/null +++ b/core/htmlx.go @@ -0,0 +1,21 @@ +package core + +import "strings" + +func CreateTableHtml(title, header, body string) string { + template := `
+ + + + + + + {{header}} + + {{body}} +
{{title}}
` + html := strings.Replace(template, "{{title}}", title, -1) + html = strings.Replace(html, "{{header}}", header, -1) + html = strings.Replace(html, "{{body}}", body, -1) + return html +} diff --git a/core/state.go b/core/state.go index 8d6ddad..5734f54 100644 --- a/core/state.go +++ b/core/state.go @@ -1,6 +1,7 @@ package core import ( + "fmt" "net/http" "strconv" "strings" @@ -19,116 +20,114 @@ const ( defaultCheckTimeMinutes = 10 ) -var htmlTable = ` - - New Document +var TableHtml = ` + + + + + - + - - - - - - - - - {{body}} -
IndexValue
+
+{{tableBody}} +
` @@ -269,12 +268,6 @@ func (state *ServerStateInfo) ShowHtmlTableData(version, globalUniqueId string) data += "" + "TotalRequestCount" + "" + strconv.FormatUint(state.TotalRequestCount, 10) + "" data += "" + "CurrentRequestCount" + "" + strconv.FormatUint(state.CurrentRequestCount, 10) + "" data += "" + "TotalErrorCount" + "" + strconv.FormatUint(state.TotalErrorCount, 10) + "" - state.IntervalRequestData.RLock() - data += "" + "IntervalRequestData" + "" + jsonutil.GetJsonString(state.IntervalRequestData.GetCurrentMap()) + "" - state.IntervalRequestData.RUnlock() - state.DetailRequestURLData.RLock() - data += "" + "DetailRequestUrlData" + "" + jsonutil.GetJsonString(state.DetailRequestURLData.GetCurrentMap()) + "" - state.DetailRequestURLData.RUnlock() state.IntervalErrorData.RLock() data += "" + "IntervalErrorData" + "" + jsonutil.GetJsonString(state.IntervalErrorData.GetCurrentMap()) + "" state.IntervalErrorData.RUnlock() @@ -287,7 +280,40 @@ func (state *ServerStateInfo) ShowHtmlTableData(version, globalUniqueId string) state.DetailHTTPCodeData.RLock() data += "" + "DetailHttpCodeData" + "" + jsonutil.GetJsonString(state.DetailHTTPCodeData.GetCurrentMap()) + "" state.DetailHTTPCodeData.RUnlock() - return strings.Replace(htmlTable, "{{body}}", data, -1) + header := ` + Index + Value + ` + data = CreateTableHtml("Core State", header, data) + + //show IntervalRequestData + intervalRequestData := "" + state.IntervalRequestData.RLock() + for k, v := range state.IntervalRequestData.GetCurrentMap() { + intervalRequestData += "" + k + "" + fmt.Sprint(v) + "" + } + state.IntervalRequestData.RUnlock() + header = ` + Time + Value + ` + data += CreateTableHtml("IntervalRequestData", header, intervalRequestData) + + //show DetailRequestURLData + detailRequestURLData := "" + state.DetailRequestURLData.RLock() + for k, v := range state.DetailRequestURLData.GetCurrentMap() { + detailRequestURLData += "" + k + "" + fmt.Sprint(v) + "" + } + state.DetailRequestURLData.RUnlock() + header = ` + Url + Value + ` + data += CreateTableHtml("DetailRequestURLData", header, detailRequestURLData) + html := strings.Replace(TableHtml, "{{tableBody}}", data, -1) + + return html } // QueryIntervalRequestData query request count by query time diff --git a/dotweb_sysgroup.go b/dotweb_sysgroup.go index af5d69f..d7c1241 100644 --- a/dotweb_sysgroup.go +++ b/dotweb_sysgroup.go @@ -1,8 +1,9 @@ package dotweb import ( + "fmt" + "github.com/devfeel/dotweb/core" jsonutil "github.com/devfeel/dotweb/framework/json" - "github.com/devfeel/dotweb/framework/stringx" "runtime" "runtime/debug" "runtime/pprof" @@ -68,11 +69,18 @@ func showQuery(ctx Context) error { func showRouters(ctx Context) error { - result := "" + data := "" + routerCount := len(ctx.HttpServer().router.GetAllRouterExpress()) for k, _ := range ctx.HttpServer().router.GetAllRouterExpress() { method := strings.Split(k, routerExpressSplit)[0] router := strings.Split(k, routerExpressSplit)[1] - result += stringx.CompletionRight(method, " ", 12) + router + "\r\n" + data += "" + method + "" + router + "" } - return ctx.WriteString(result) + header := ` + Method + Router + ` + html := strings.Replace(core.TableHtml, "{{tableBody}}", core.CreateTableHtml("Routers:"+fmt.Sprint(routerCount), header, data), -1) + + return ctx.WriteHtml(html) } diff --git a/version.MD b/version.MD index 4f6b0da..8658d40 100644 --- a/version.MD +++ b/version.MD @@ -1,6 +1,14 @@ ## dotweb版本记录: +#### Version 1.7.7 +* Opt: 优化系统路由dotweb/state、dotweb/routers展现方式,以方便阅读的表格形式输出 +* Feature: 新增core.TableHtml\core.CreateTableHtml()用于生成相关Html代码 +* About: + - 可访问dotweb/state查看当前实例运行时信息 + - 可访问dotweb/routers查看当前实例注册的所有路由信息 +* 2019-11-12 18:00 at ShangHai + #### Version 1.7.6 * Fix: 修复在调用SetMethodNotAllowedHandle时修改StatusCode无效问题 * Opt: 将路由阶段设置405代码逻辑移除,相关逻辑在DefaultMethodNotAllowedHandler实现 From 2e3828b6125c8ca457c1be13e0a8ee0f83e8097f Mon Sep 17 00:00:00 2001 From: "pzrr@qq.com" Date: Tue, 12 Nov 2019 17:32:09 +0800 Subject: [PATCH 3/3] =?UTF-8?q?####=20Version=201.7.7=20*=20Opt:=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F=E8=B7=AF=E7=94=B1dotweb/st?= =?UTF-8?q?ate=E3=80=81dotweb/routers=E5=B1=95=E7=8E=B0=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E6=96=B9=E4=BE=BF=E9=98=85=E8=AF=BB=E7=9A=84?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E5=BD=A2=E5=BC=8F=E8=BE=93=E5=87=BA=20*=20Fe?= =?UTF-8?q?ature:=20=E6=96=B0=E5=A2=9Ecore.TableHtml\core.CreateTableHtml(?= =?UTF-8?q?)=E7=94=A8=E4=BA=8E=E7=94=9F=E6=88=90=E7=9B=B8=E5=85=B3Html?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=20*=20About:=20=20=20=20=20-=20=E5=8F=AF?= =?UTF-8?q?=E8=AE=BF=E9=97=AEdotweb/state=E6=9F=A5=E7=9C=8B=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E5=AE=9E=E4=BE=8B=E8=BF=90=E8=A1=8C=E6=97=B6=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20=20=20=20=20-=20=E5=8F=AF=E8=AE=BF=E9=97=AEdotweb/r?= =?UTF-8?q?outers=E6=9F=A5=E7=9C=8B=E5=BD=93=E5=89=8D=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=20*=202019-11-12=2018:00=20at=20ShangHai?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consts.go b/consts.go index d63267b..717195f 100644 --- a/consts.go +++ b/consts.go @@ -3,7 +3,7 @@ package dotweb // Global define const ( // Version current version - Version = "1.7.6" + Version = "1.7.7" ) // Log define