Add query execution stats to api endpoint (#629)

This commit is contained in:
Dan Sosedoff 2022-12-25 13:25:18 -06:00 committed by GitHub
parent 12a4bf70c0
commit 239037b4c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 4 deletions

View File

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

View File

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

View File

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