From c5eab3b584dacc0ea4653fff61b003cc740cbd73 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Tue, 13 Feb 2024 21:09:25 +0000 Subject: [PATCH 1/8] Auto update the system status in admin dashboard - Refactored the system status list into its own template - Changed the backend to return only the system status if the request was initiated by htmx - `hx-get="{{$.Link}}"`: reuse the backend handler - `hx-swap="innerHTML"`: replace the `
`'s innerHTML (essentially the new template) - `hx-trigger="every 5s"`: call every 5 seconds - `hx-indicator=".divider"`: the `is-loading` class shouldn't be added to the div during the request, so set it on an element it has no effect on - Render "Since Last GC Time" with ``, so we send a timestamp Signed-off-by: Yarden Shoham --- routers/web/admin/admin.go | 29 +++++++------ templates/admin/dashboard.tmpl | 65 +----------------------------- templates/admin/system_status.tmpl | 62 ++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 75 deletions(-) create mode 100644 templates/admin/system_status.tmpl diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index d31cb1cd2551c..4c0da31461539 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -28,13 +28,14 @@ import ( ) const ( - tplDashboard base.TplName = "admin/dashboard" - tplSelfCheck base.TplName = "admin/self_check" - tplCron base.TplName = "admin/cron" - tplQueue base.TplName = "admin/queue" - tplStacktrace base.TplName = "admin/stacktrace" - tplQueueManage base.TplName = "admin/queue_manage" - tplStats base.TplName = "admin/stats" + tplDashboard base.TplName = "admin/dashboard" + tplSystemStatus base.TplName = "admin/system_status" + tplSelfCheck base.TplName = "admin/self_check" + tplCron base.TplName = "admin/cron" + tplQueue base.TplName = "admin/queue" + tplStacktrace base.TplName = "admin/stacktrace" + tplQueueManage base.TplName = "admin/queue_manage" + tplStats base.TplName = "admin/stats" ) var sysStatus struct { @@ -72,7 +73,7 @@ var sysStatus struct { // Garbage collector statistics. NextGC string // next run in HeapAlloc time (bytes) - LastGC string // last run in absolute time (ns) + LastGCTime string // last run time PauseTotalNs string PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256] NumGC uint32 @@ -110,7 +111,7 @@ func updateSystemStatus() { sysStatus.OtherSys = base.FileSize(int64(m.OtherSys)) sysStatus.NextGC = base.FileSize(int64(m.NextGC)) - sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000) + sysStatus.LastGCTime = time.Unix(0, int64(m.LastGC)).Format(time.RFC3339) sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000) sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000) sysStatus.NumGC = m.NumGC @@ -128,13 +129,17 @@ func prepareDeprecatedWarningsAlert(ctx *context.Context) { // Dashboard show admin panel dashboard func Dashboard(ctx *context.Context) { + // FIXME: update periodically + updateSystemStatus() + ctx.Data["SysStatus"] = sysStatus + if ctx.Req.Header.Get("HX-Request") == "true" { + ctx.HTML(http.StatusOK, tplSystemStatus) + return + } ctx.Data["Title"] = ctx.Tr("admin.dashboard") ctx.Data["PageIsAdminDashboard"] = true ctx.Data["NeedUpdate"] = updatechecker.GetNeedUpdate(ctx) ctx.Data["RemoteVersion"] = updatechecker.GetRemoteVersion(ctx) - // FIXME: update periodically - updateSystemStatus() - ctx.Data["SysStatus"] = sysStatus ctx.Data["SSH"] = setting.SSH prepareDeprecatedWarningsAlert(ctx) ctx.HTML(http.StatusOK, tplDashboard) diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index f43b4c5385c54..5bddc20fec178 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -75,69 +75,8 @@

{{ctx.Locale.Tr "admin.dashboard.system_status"}}

-
-
-
{{ctx.Locale.Tr "admin.dashboard.server_uptime"}}
-
{{.SysStatus.StartTime}}
-
{{ctx.Locale.Tr "admin.dashboard.current_goroutine"}}
-
{{.SysStatus.NumGoroutine}}
-
-
{{ctx.Locale.Tr "admin.dashboard.current_memory_usage"}}
-
{{.SysStatus.MemAllocated}}
-
{{ctx.Locale.Tr "admin.dashboard.total_memory_allocated"}}
-
{{.SysStatus.MemTotal}}
-
{{ctx.Locale.Tr "admin.dashboard.memory_obtained"}}
-
{{.SysStatus.MemSys}}
-
{{ctx.Locale.Tr "admin.dashboard.pointer_lookup_times"}}
-
{{.SysStatus.Lookups}}
-
{{ctx.Locale.Tr "admin.dashboard.memory_allocate_times"}}
-
{{.SysStatus.MemMallocs}}
-
{{ctx.Locale.Tr "admin.dashboard.memory_free_times"}}
-
{{.SysStatus.MemFrees}}
-
-
{{ctx.Locale.Tr "admin.dashboard.current_heap_usage"}}
-
{{.SysStatus.HeapAlloc}}
-
{{ctx.Locale.Tr "admin.dashboard.heap_memory_obtained"}}
-
{{.SysStatus.HeapSys}}
-
{{ctx.Locale.Tr "admin.dashboard.heap_memory_idle"}}
-
{{.SysStatus.HeapIdle}}
-
{{ctx.Locale.Tr "admin.dashboard.heap_memory_in_use"}}
-
{{.SysStatus.HeapInuse}}
-
{{ctx.Locale.Tr "admin.dashboard.heap_memory_released"}}
-
{{.SysStatus.HeapReleased}}
-
{{ctx.Locale.Tr "admin.dashboard.heap_objects"}}
-
{{.SysStatus.HeapObjects}}
-
-
{{ctx.Locale.Tr "admin.dashboard.bootstrap_stack_usage"}}
-
{{.SysStatus.StackInuse}}
-
{{ctx.Locale.Tr "admin.dashboard.stack_memory_obtained"}}
-
{{.SysStatus.StackSys}}
-
{{ctx.Locale.Tr "admin.dashboard.mspan_structures_usage"}}
-
{{.SysStatus.MSpanInuse}}
-
{{ctx.Locale.Tr "admin.dashboard.mspan_structures_obtained"}}
-
{{.SysStatus.MSpanSys}}
-
{{ctx.Locale.Tr "admin.dashboard.mcache_structures_usage"}}
-
{{.SysStatus.MCacheInuse}}
-
{{ctx.Locale.Tr "admin.dashboard.mcache_structures_obtained"}}
-
{{.SysStatus.MCacheSys}}
-
{{ctx.Locale.Tr "admin.dashboard.profiling_bucket_hash_table_obtained"}}
-
{{.SysStatus.BuckHashSys}}
-
{{ctx.Locale.Tr "admin.dashboard.gc_metadata_obtained"}}
-
{{.SysStatus.GCSys}}
-
{{ctx.Locale.Tr "admin.dashboard.other_system_allocation_obtained"}}
-
{{.SysStatus.OtherSys}}
-
-
{{ctx.Locale.Tr "admin.dashboard.next_gc_recycle"}}
-
{{.SysStatus.NextGC}}
-
{{ctx.Locale.Tr "admin.dashboard.last_gc_time"}}
-
{{.SysStatus.LastGC}}
-
{{ctx.Locale.Tr "admin.dashboard.total_gc_pause"}}
-
{{.SysStatus.PauseTotalNs}}
-
{{ctx.Locale.Tr "admin.dashboard.last_gc_pause"}}
-
{{.SysStatus.PauseNs}}
-
{{ctx.Locale.Tr "admin.dashboard.gc_times"}}
-
{{.SysStatus.NumGC}}
-
+
+ {{template "admin/system_status" .}}
{{template "admin/layout_footer" .}} diff --git a/templates/admin/system_status.tmpl b/templates/admin/system_status.tmpl new file mode 100644 index 0000000000000..7b5c9be6ccce3 --- /dev/null +++ b/templates/admin/system_status.tmpl @@ -0,0 +1,62 @@ +
+
{{ctx.Locale.Tr "admin.dashboard.server_uptime"}}
+
{{.SysStatus.StartTime}}
+
{{ctx.Locale.Tr "admin.dashboard.current_goroutine"}}
+
{{.SysStatus.NumGoroutine}}
+
+
{{ctx.Locale.Tr "admin.dashboard.current_memory_usage"}}
+
{{.SysStatus.MemAllocated}}
+
{{ctx.Locale.Tr "admin.dashboard.total_memory_allocated"}}
+
{{.SysStatus.MemTotal}}
+
{{ctx.Locale.Tr "admin.dashboard.memory_obtained"}}
+
{{.SysStatus.MemSys}}
+
{{ctx.Locale.Tr "admin.dashboard.pointer_lookup_times"}}
+
{{.SysStatus.Lookups}}
+
{{ctx.Locale.Tr "admin.dashboard.memory_allocate_times"}}
+
{{.SysStatus.MemMallocs}}
+
{{ctx.Locale.Tr "admin.dashboard.memory_free_times"}}
+
{{.SysStatus.MemFrees}}
+
+
{{ctx.Locale.Tr "admin.dashboard.current_heap_usage"}}
+
{{.SysStatus.HeapAlloc}}
+
{{ctx.Locale.Tr "admin.dashboard.heap_memory_obtained"}}
+
{{.SysStatus.HeapSys}}
+
{{ctx.Locale.Tr "admin.dashboard.heap_memory_idle"}}
+
{{.SysStatus.HeapIdle}}
+
{{ctx.Locale.Tr "admin.dashboard.heap_memory_in_use"}}
+
{{.SysStatus.HeapInuse}}
+
{{ctx.Locale.Tr "admin.dashboard.heap_memory_released"}}
+
{{.SysStatus.HeapReleased}}
+
{{ctx.Locale.Tr "admin.dashboard.heap_objects"}}
+
{{.SysStatus.HeapObjects}}
+
+
{{ctx.Locale.Tr "admin.dashboard.bootstrap_stack_usage"}}
+
{{.SysStatus.StackInuse}}
+
{{ctx.Locale.Tr "admin.dashboard.stack_memory_obtained"}}
+
{{.SysStatus.StackSys}}
+
{{ctx.Locale.Tr "admin.dashboard.mspan_structures_usage"}}
+
{{.SysStatus.MSpanInuse}}
+
{{ctx.Locale.Tr "admin.dashboard.mspan_structures_obtained"}}
+
{{.SysStatus.MSpanSys}}
+
{{ctx.Locale.Tr "admin.dashboard.mcache_structures_usage"}}
+
{{.SysStatus.MCacheInuse}}
+
{{ctx.Locale.Tr "admin.dashboard.mcache_structures_obtained"}}
+
{{.SysStatus.MCacheSys}}
+
{{ctx.Locale.Tr "admin.dashboard.profiling_bucket_hash_table_obtained"}}
+
{{.SysStatus.BuckHashSys}}
+
{{ctx.Locale.Tr "admin.dashboard.gc_metadata_obtained"}}
+
{{.SysStatus.GCSys}}
+
{{ctx.Locale.Tr "admin.dashboard.other_system_allocation_obtained"}}
+
{{.SysStatus.OtherSys}}
+
+
{{ctx.Locale.Tr "admin.dashboard.next_gc_recycle"}}
+
{{.SysStatus.NextGC}}
+
{{ctx.Locale.Tr "admin.dashboard.last_gc_time"}}
+
{{.SysStatus.LastGCTime}}
+
{{ctx.Locale.Tr "admin.dashboard.total_gc_pause"}}
+
{{.SysStatus.PauseTotalNs}}
+
{{ctx.Locale.Tr "admin.dashboard.last_gc_pause"}}
+
{{.SysStatus.PauseNs}}
+
{{ctx.Locale.Tr "admin.dashboard.gc_times"}}
+
{{.SysStatus.NumGC}}
+
From 09f231d1807f6fcacbfae7fd6acb26cc0d479ea8 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Tue, 13 Feb 2024 21:29:02 +0000 Subject: [PATCH 2/8] Fix back button --- routers/web/admin/admin.go | 17 ++++++++++------- routers/web/web.go | 1 + templates/admin/dashboard.tmpl | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index 4c0da31461539..9955b250c87eb 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -129,22 +129,25 @@ func prepareDeprecatedWarningsAlert(ctx *context.Context) { // Dashboard show admin panel dashboard func Dashboard(ctx *context.Context) { - // FIXME: update periodically - updateSystemStatus() - ctx.Data["SysStatus"] = sysStatus - if ctx.Req.Header.Get("HX-Request") == "true" { - ctx.HTML(http.StatusOK, tplSystemStatus) - return - } ctx.Data["Title"] = ctx.Tr("admin.dashboard") ctx.Data["PageIsAdminDashboard"] = true ctx.Data["NeedUpdate"] = updatechecker.GetNeedUpdate(ctx) ctx.Data["RemoteVersion"] = updatechecker.GetRemoteVersion(ctx) + // FIXME: update periodically + updateSystemStatus() + ctx.Data["SysStatus"] = sysStatus ctx.Data["SSH"] = setting.SSH prepareDeprecatedWarningsAlert(ctx) ctx.HTML(http.StatusOK, tplDashboard) } +func SystemStatus(ctx *context.Context) { + // FIXME: update periodically + updateSystemStatus() + ctx.Data["SysStatus"] = sysStatus + ctx.HTML(http.StatusOK, tplSystemStatus) +} + // DashboardPost run an admin operation func DashboardPost(ctx *context.Context) { form := web.GetForm(ctx).(*forms.AdminDashboardForm) diff --git a/routers/web/web.go b/routers/web/web.go index 92cf5132b45b6..8a5734eaa4c09 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -676,6 +676,7 @@ func registerRoutes(m *web.Route) { // ***** START: Admin ***** m.Group("/admin", func() { m.Get("", admin.Dashboard) + m.Get("/system_status", admin.SystemStatus) m.Post("", web.Bind(forms.AdminDashboardForm{}), admin.DashboardPost) m.Get("/self_check", admin.SelfCheck) diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index 5bddc20fec178..57d8e600135e2 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -75,7 +75,7 @@

{{ctx.Locale.Tr "admin.dashboard.system_status"}}

-
+
{{template "admin/system_status" .}}
From 969352d56322eb7fcca06b9f43c0a01ed8da8f4c Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 14 Feb 2024 17:08:29 +0000 Subject: [PATCH 3/8] Remove comment --- routers/web/admin/admin.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index 9955b250c87eb..9fbd429f74325 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -133,7 +133,6 @@ func Dashboard(ctx *context.Context) { ctx.Data["PageIsAdminDashboard"] = true ctx.Data["NeedUpdate"] = updatechecker.GetNeedUpdate(ctx) ctx.Data["RemoteVersion"] = updatechecker.GetRemoteVersion(ctx) - // FIXME: update periodically updateSystemStatus() ctx.Data["SysStatus"] = sysStatus ctx.Data["SSH"] = setting.SSH @@ -142,7 +141,6 @@ func Dashboard(ctx *context.Context) { } func SystemStatus(ctx *context.Context) { - // FIXME: update periodically updateSystemStatus() ctx.Data["SysStatus"] = sysStatus ctx.HTML(http.StatusOK, tplSystemStatus) From c7f735b75c6cc97f608b277fbf5c39b3ab257c1e Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 14 Feb 2024 20:06:26 +0000 Subject: [PATCH 4/8] Fix select state --- package-lock.json | 6 ++++++ package.json | 1 + templates/admin/dashboard.tmpl | 2 +- templates/base/head.tmpl | 2 +- web_src/js/htmx.js | 3 +++ webpack.config.js | 3 +++ 6 files changed, 15 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 62bf36e7b73f1..b6590c875776d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -27,6 +27,7 @@ "escape-goat": "4.0.0", "fast-glob": "3.3.2", "htmx.org": "1.9.10", + "idiomorph": "0.3.0", "jquery": "3.7.1", "katex": "0.16.9", "license-checker-webpack-plugin": "0.2.1", @@ -6081,6 +6082,11 @@ "postcss": "^8.1.0" } }, + "node_modules/idiomorph": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/idiomorph/-/idiomorph-0.3.0.tgz", + "integrity": "sha512-UhV1Ey5xCxIwR9B+OgIjQa+1Jx99XQ1vQHUsKBU1RpQzCx1u+b+N6SOXgf5mEJDqemUI/ffccu6+71l2mJUsRA==" + }, "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", diff --git a/package.json b/package.json index 46dfdd1055418..5980a27481d14 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "escape-goat": "4.0.0", "fast-glob": "3.3.2", "htmx.org": "1.9.10", + "idiomorph": "0.3.0", "jquery": "3.7.1", "katex": "0.16.9", "license-checker-webpack-plugin": "0.2.1", diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index 57d8e600135e2..51a0605471020 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -75,7 +75,7 @@

{{ctx.Locale.Tr "admin.dashboard.system_status"}}

-
+
{{template "admin/system_status" .}}
diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index b9c050fdd5c15..e910bb0cd9922 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -29,7 +29,7 @@ {{template "base/head_style" .}} {{template "custom/header" .}} - + {{ctx.DataRaceCheck $.Context}} {{template "custom/body_outer_pre" .}} diff --git a/web_src/js/htmx.js b/web_src/js/htmx.js index 92400d1cbe44c..5ca30183082d7 100644 --- a/web_src/js/htmx.js +++ b/web_src/js/htmx.js @@ -1,6 +1,9 @@ import * as htmx from 'htmx.org'; import {showErrorToast} from './modules/toast.js'; +// https://github.com/bigskysoftware/idiomorph#htmx +import 'idiomorph/dist/idiomorph-ext.js'; + // https://htmx.org/reference/#config htmx.config.requestClass = 'is-loading'; htmx.config.scrollIntoViewOnBoost = false; diff --git a/webpack.config.js b/webpack.config.js index 16afa0ff9c1f1..64bac4f84d9f9 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -169,6 +169,9 @@ export default { ], }, plugins: [ + new webpack.ProvidePlugin({ // for htmx extensions + htmx: 'htmx.org', + }), new DefinePlugin({ __VUE_OPTIONS_API__: true, // at the moment, many Vue components still use the Vue Options API __VUE_PROD_DEVTOOLS__: false, // do not enable devtools support in production From 1d55c153b077fa2870b468d1d6bf6bcae5423291 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Wed, 14 Feb 2024 20:11:35 +0000 Subject: [PATCH 5/8] Fix license --- webpack.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/webpack.config.js b/webpack.config.js index 64bac4f84d9f9..d04d970451444 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -209,6 +209,7 @@ export default { override: { 'khroma@*': {licenseName: 'MIT'}, // https://github.com/fabiospampinato/khroma/pull/33 'htmx.org@1.9.10': {licenseName: 'BSD-2-Clause'}, // "BSD 2-Clause" -> "BSD-2-Clause" + 'idiomorph@0.3.0': {licenseName: 'BSD-2-Clause'}, // "BSD 2-Clause" -> "BSD-2-Clause" }, emitError: true, allow: '(Apache-2.0 OR BSD-2-Clause OR BSD-3-Clause OR MIT OR ISC OR CPAL-1.0 OR Unlicense OR EPL-1.0 OR EPL-2.0)', From 5dfd60b7595ca7b4233e5397f9757ef89362e022 Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Thu, 15 Feb 2024 16:06:10 +0200 Subject: [PATCH 6/8] Update templates/admin/dashboard.tmpl Co-authored-by: silverwind --- templates/admin/dashboard.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index 51a0605471020..63328e8bc3e42 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -75,6 +75,7 @@

{{ctx.Locale.Tr "admin.dashboard.system_status"}}

+ {{/* TODO: make this work multi-server deployments, likely needs per-server stats in DB */}}
{{template "admin/system_status" .}}
From df9e1c26d8e765169d1cb8bb61567075f5a76edf Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Thu, 15 Feb 2024 16:07:44 +0200 Subject: [PATCH 7/8] Update templates/admin/dashboard.tmpl Co-authored-by: silverwind --- templates/admin/dashboard.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index 63328e8bc3e42..2f0f564220161 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -75,7 +75,7 @@

{{ctx.Locale.Tr "admin.dashboard.system_status"}}

- {{/* TODO: make this work multi-server deployments, likely needs per-server stats in DB */}} + {{/* TODO: make these stats work in multi-server deployments, likely needs per-server stats in DB */}}
{{template "admin/system_status" .}}
From 7f79dbc828b6d2a88a8721e361ae4476ca84018c Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Thu, 15 Feb 2024 14:55:30 +0000 Subject: [PATCH 8/8] Lint --- templates/admin/dashboard.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/admin/dashboard.tmpl b/templates/admin/dashboard.tmpl index 2f0f564220161..8088315f179e9 100644 --- a/templates/admin/dashboard.tmpl +++ b/templates/admin/dashboard.tmpl @@ -75,7 +75,7 @@

{{ctx.Locale.Tr "admin.dashboard.system_status"}}

- {{/* TODO: make these stats work in multi-server deployments, likely needs per-server stats in DB */}} + {{/* TODO: make these stats work in multi-server deployments, likely needs per-server stats in DB */}}
{{template "admin/system_status" .}}