From f5f78eb32eaaa5713de244a32fc99f5e22410291 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Sun, 15 Dec 2024 14:46:01 -0800 Subject: [PATCH] Add server settings view (#768) --- pkg/api/api.go | 6 ++++++ pkg/api/routes.go | 1 + pkg/client/client.go | 4 ++++ pkg/client/client_test.go | 31 +++++++++++++++++++++++++++++-- pkg/statements/sql.go | 3 +++ pkg/statements/sql/settings.sql | 1 + static/index.html | 2 ++ static/js/app.js | 15 +++++++++++++++ 8 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 pkg/statements/sql/settings.sql diff --git a/pkg/api/api.go b/pkg/api/api.go index c1d8b4ffe..5e4d1c2f0 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -486,6 +486,12 @@ func GetConnectionInfo(c *gin.Context) { successResponse(c, info) } +// GetServerSettings renders a list of all server settings +func GetServerSettings(c *gin.Context) { + res, err := DB(c).ServerSettings() + serveResult(c, res, err) +} + // GetActivity renders a list of running queries func GetActivity(c *gin.Context) { res, err := DB(c).Activity() diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 01aaae950..e97960358 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -35,6 +35,7 @@ func SetupRoutes(router *gin.Engine) { api.POST("/switchdb", SwitchDb) api.GET("/databases", GetDatabases) api.GET("/connection", GetConnectionInfo) + api.GET("/server_settings", GetServerSettings) api.GET("/activity", GetActivity) api.GET("/schemas", GetSchemas) api.GET("/objects", GetObjects) diff --git a/pkg/client/client.go b/pkg/client/client.go index 5bb4d44d5..8bff28a76 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -386,6 +386,10 @@ func (client *Client) TablesStats() (*Result, error) { return client.query(statements.TablesStats) } +func (client *Client) ServerSettings() (*Result, error) { + return client.query(statements.Settings) +} + // Returns all active queriers on the server func (client *Client) Activity() (*Result, error) { if client.serverType == cockroachType { diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 04c3afee9..7e84e0c09 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -11,10 +11,10 @@ import ( "testing" "time" - "github.com/sosedoff/pgweb/pkg/command" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/sosedoff/pgweb/pkg/command" ) var ( @@ -717,6 +717,32 @@ func testConnContext(t *testing.T) { assert.Equal(t, "default", result.Mode) } +func testServerSettings(t *testing.T) { + expectedColumns := []string{ + "name", + "setting", + "unit", + "category", + "short_desc", + "extra_desc", + "context", + "vartype", + "source", + "min_val", + "max_val", + "enumvals", + "boot_val", + "reset_val", + "sourcefile", + "sourceline", + "pending_restart", + } + + result, err := testClient.ServerSettings() + assert.NoError(t, err) + assert.Equal(t, expectedColumns, result.Columns) +} + func TestAll(t *testing.T) { if onWindows() { t.Log("Unit testing on Windows platform is not supported.") @@ -756,6 +782,7 @@ func TestAll(t *testing.T) { testDumpExport(t) testTablesStats(t) testConnContext(t) + testServerSettings(t) teardownClient() teardown(t, true) diff --git a/pkg/statements/sql.go b/pkg/statements/sql.go index e358c75c1..ec031d2d6 100644 --- a/pkg/statements/sql.go +++ b/pkg/statements/sql.go @@ -47,6 +47,9 @@ var ( //go:embed sql/function.sql Function string + //go:embed sql/settings.sql + Settings string + // Activity queries for specific PG versions Activity = map[string]string{ "default": "SELECT * FROM pg_stat_activity WHERE datname = current_database()", diff --git a/pkg/statements/sql/settings.sql b/pkg/statements/sql/settings.sql new file mode 100644 index 000000000..2192d4c95 --- /dev/null +++ b/pkg/statements/sql/settings.sql @@ -0,0 +1 @@ +SELECT * FROM pg_settings diff --git a/static/index.html b/static/index.html index d92fa7dd3..71ccaf7c0 100644 --- a/static/index.html +++ b/static/index.html @@ -327,6 +327,8 @@

SSH Connection

  • Show Database Stats
  • Download Database Stats
  • +
  • Show Server Settings
  • +
  • Export SQL dump
  • diff --git a/static/js/app.js b/static/js/app.js index e71dd533d..d5b26d4ef 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -99,6 +99,7 @@ function apiCall(method, path, params, cb) { function getInfo(cb) { apiCall("get", "/info", {}, cb); } function getConnection(cb) { apiCall("get", "/connection", {}, cb); } +function getServerSettings(cb) { apiCall("get", "/server_settings", {}, cb); } function getSchemas(cb) { apiCall("get", "/schemas", {}, cb); } function getObjects(cb) { apiCall("get", "/objects", {}, cb); } function getTables(cb) { apiCall("get", "/tables", {}, cb); } @@ -678,6 +679,17 @@ function downloadDatabaseStats() { openInNewWindow("api/tables_stats", { format: "csv", export: "true" }); } +function showServerSettings() { + getServerSettings(function(data) { + buildTable(data); + + setCurrentTab("table_content"); + $("#input").hide(); + $("#body").prop("class", "full"); + $("#results").addClass("no-crop"); + }); +} + function showTableStructure() { var name = getCurrentObject().name; @@ -1280,6 +1292,9 @@ function bindCurrentDatabaseMenu() { case "download_db_stats": downloadDatabaseStats(); break; + case "server_settings": + showServerSettings(); + break; case "export": openInNewWindow("api/export"); break;