Skip to content

Commit

Permalink
Add query execution stats to api endpoint (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
sosedoff authored Dec 25, 2022
1 parent 12a4bf7 commit 239037b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
19 changes: 19 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions pkg/client/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 239037b

Please # to comment.