Skip to content

Commit

Permalink
Allow database stats downloads (#738)
Browse files Browse the repository at this point in the history
* Add button to download database stats
* Return xml as well
  • Loading branch information
sosedoff committed May 25, 2024
1 parent 63f1150 commit 3a32f53
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
39 changes: 37 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,43 @@ func GetTableConstraints(c *gin.Context) {

// GetTablesStats renders data sizes and estimated rows for all tables in the database
func GetTablesStats(c *gin.Context) {
res, err := DB(c).TablesStats()
serveResult(c, res, err)
db := DB(c)

connCtx, err := db.GetConnContext()
if err != nil {
badRequest(c, err)
return
}

res, err := db.TablesStats()
if err != nil {
badRequest(c, err)
return
}

format := getQueryParam(c, "format")
if format == "" {
format = "json"
}

// Save as attachment if exporting parameter is set
if getQueryParam(c, "export") == "true" {
ts := time.Now().Format(time.DateOnly)

filename := fmt.Sprintf("pgweb-dbstats-%s-%s.%s", connCtx.Database, ts, format)
c.Writer.Header().Set("Content-disposition", "attachment;filename="+filename)
}

switch format {
case "json":
c.JSON(http.StatusOK, res)
case "csv":
c.Data(http.StatusOK, "text/csv", res.CSV())
case "xml":
c.XML(200, res)
default:
badRequest(c, "invalid format")
}
}

// HandleQuery runs the database query
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ func assetContentType(name string) string {

// Send a query result to client
func serveResult(c *gin.Context, result interface{}, err interface{}) {
if err == nil {
successResponse(c, result)
} else {
if err != nil {
badRequest(c, err)
return
}

successResponse(c, result)
}

// Send successful response back to client
Expand Down
4 changes: 3 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ <h3 class="text-center">SSH Connection</h3>
</div>
<div id="current_database_context_menu">
<ul class="dropdown-menu" role="menu">
<li><a href="#" data-action="show_tables_stats">Show Tables Stats</a></li>
<li><a href="#" data-action="show_db_stats">Show Database Stats</a></li>
<li><a href="#" data-action="download_db_stats">Download Database Stats</a></li>
<li class="divider"></li>
<li><a href="#" data-action="export">Export SQL dump</a></li>
</ul>
</div>
Expand Down
13 changes: 10 additions & 3 deletions static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ function showPaginatedTableContent() {
showTableContent(sortColumn, sortOrder);
}

function showTablesStats() {
function showDatabaseStats() {
getTablesStats(function(data) {
buildTable(data);

Expand All @@ -671,6 +671,10 @@ function showTablesStats() {
});
}

function downloadDatabaseStats() {
openInNewWindow("api/tables_stats", { format: "csv", export: "true" });
}

function showTableStructure() {
var name = getCurrentObject().name;

Expand Down Expand Up @@ -1267,8 +1271,11 @@ function bindCurrentDatabaseMenu() {
var menuItem = $(e.target);

switch(menuItem.data("action")) {
case "show_tables_stats":
showTablesStats();
case "show_db_stats":
showDatabaseStats();
break;
case "download_db_stats":
downloadDatabaseStats();
break;
case "export":
openInNewWindow("api/export");
Expand Down

0 comments on commit 3a32f53

Please sign in to comment.