1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
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)
}
|