From 239037b4c6e369f6317ea7d2d3a0eba563ace7e5 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Sun, 25 Dec 2022 13:25:18 -0600 Subject: [PATCH] Add query execution stats to api endpoint (#629) --- pkg/client/client.go | 19 +++++++++++++++++++ pkg/client/result.go | 16 +++++++++++++--- static/js/app.js | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index f1d730778..242d59309 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -386,7 +386,9 @@ func (client *Client) exec(query string, args ...interface{}) (*Result, error) { ctx, cancel := client.context() defer cancel() + queryStart := time.Now() res, err := client.db.ExecContext(ctx, query, args...) + queryFinish := time.Now() if err != nil { return nil, err } @@ -401,6 +403,13 @@ func (client *Client) exec(query string, args ...interface{}) (*Result, error) { Rows: []Row{ {affected}, }, + Stats: &ResultStats{ + ColumnsCount: 1, + RowsCount: 1, + QueryStartTime: queryStart.UTC(), + QueryFinishTime: queryFinish.UTC(), + QueryDuration: queryFinish.Sub(queryStart).Milliseconds(), + }, } return &result, nil @@ -437,7 +446,9 @@ func (client *Client) query(query string, args ...interface{}) (*Result, error) ctx, cancel := client.context() defer cancel() + queryStart := time.Now() rows, err := client.db.QueryxContext(ctx, query, args...) + queryFinish := time.Now() if err != nil { if command.Opts.Debug { log.Println("Failed query:", query, "\nArgs:", args) @@ -481,6 +492,14 @@ func (client *Client) query(query string, args ...interface{}) (*Result, error) } } + result.Stats = &ResultStats{ + ColumnsCount: len(cols), + RowsCount: len(result.Rows), + QueryStartTime: queryStart.UTC(), + QueryFinishTime: queryFinish.UTC(), + QueryDuration: queryFinish.Sub(queryStart).Milliseconds(), + } + result.PostProcess() return &result, nil diff --git a/pkg/client/result.go b/pkg/client/result.go index c0e3de667..0db791247 100644 --- a/pkg/client/result.go +++ b/pkg/client/result.go @@ -42,9 +42,19 @@ type ( } Result struct { - Pagination *Pagination `json:"pagination,omitempty"` - Columns []string `json:"columns"` - Rows []Row `json:"rows"` + Pagination *Pagination `json:"pagination,omitempty"` + Columns []string `json:"columns"` + Rows []Row `json:"rows"` + Stats *ResultStats `json:"stats,omitempty"` + } + + ResultStats struct { + ColumnsCount int `json:"columns_count"` + RowsCount int `json:"rows_count"` + RowsAffected int64 `json:"rows_affected"` + QueryStartTime time.Time `json:"query_start_time"` + QueryFinishTime time.Time `json:"query_finish_time"` + QueryDuration int64 `json:"query_duration_ms"` } Object struct { diff --git a/static/js/app.js b/static/js/app.js index 62213c604..f4b4d9cdc 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -416,7 +416,7 @@ function buildTable(results, sortColumn, sortOrder, options) { $("#results_body").html(rows); // Show number of rows rendered on the page - $("#result-rows-count").html(results.rows.length + " rows"); + $("#result-rows-count").html(results.stats.rows_count + " rows in " + results.stats.query_duration_ms + " ms"); } function setCurrentTab(id) {