package main import ( "fmt" "log" "strconv" "github.com/rivo/tview" "bnbl.io/pgqt/postgres" ) type querySelector struct { *tview.Table db *postgres.DB stmts []*postgres.PgStatStatement } func newQuerySelector(db *postgres.DB) *querySelector { v := tview.NewTable() v.SetTitle("Queries") v.SetBorder(true) v.SetSelectable(true, false) qs := &querySelector{ Table: v, db: db, } return qs } func (q *querySelector) SelectSchema(schema string) bool { q.Clear() stmts, err := q.db.GetPgStatStatements(schema) if err != nil { log.Printf("could not select schema: %v", err) cell := tview.NewTableCell(fmt.Sprintf("could not find queries: %v", err)) cell.SetSelectable(false) q.SetCell(0, 0, cell) return false } q.stmts = stmts q.SetFixed(1, 0) newHeaderCell := func(t string) *tview.TableCell { cell := tview.NewTableCell(t) cell.SetSelectable(false) return cell } q.SetCell(0, 0, newHeaderCell("User ID")) q.SetCell(0, 1, newHeaderCell("Query")) q.SetCell(0, 2, newHeaderCell("Calls")) q.SetCell(0, 3, newHeaderCell("Total Time")) newTableCell := func(t string) *tview.TableCell { cell := tview.NewTableCell(t) return cell } for idx, stmt := range q.stmts { q.SetCell(idx+1, 0, newTableCell(strconv.Itoa(stmt.UserID))) q.SetCell(idx+1, 1, newTableCell(stmt.Query)) q.SetCell(idx+1, 2, newTableCell(strconv.Itoa(stmt.Calls))) q.SetCell(idx+1, 3, newTableCell(strconv.Itoa(int(stmt.TotalTime)))) } return true } func (q *querySelector) Analyze() ([]*postgres.Explain, error) { row, _ := q.GetSelection() if len(q.stmts) < row { return nil, fmt.Errorf("no queries to analyze") } stmt := q.stmts[row-1] return q.db.Analyze(stmt.Query) }