Allow database stats downloads (#738)

* Add button to download database stats
* Return xml as well
This commit is contained in:
Dan Sosedoff
2024-05-25 10:59:23 -07:00
committed by GitHub
parent 63f1150056
commit 3a32f531a0
4 changed files with 54 additions and 9 deletions

View File

@@ -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

View File

@@ -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