-
Notifications
You must be signed in to change notification settings - Fork 0
/
vacuum.go
102 lines (88 loc) · 2.21 KB
/
vacuum.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package pgsp
import (
"bytes"
"context"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/noborus/pgsp/str"
"github.com/noborus/pgsp/vertical"
"github.com/olekukonko/tablewriter"
)
// pg_stat_progress_vacuum
type Vacuum struct {
PID int `db:"pid"`
DATID int `db:"datid"`
DATNAME string `db:"datname"`
RELID int `db:"relid"`
PHASE string `db:"phase"`
HeapBLKSTotal int64 `db:"heap_blks_total"`
HeapBLKSScanned int64 `db:"heap_blks_scanned"`
HeapBLKSVacuumed int64 `db:"heap_blks_vacuumed"`
IndexVacuumCount int64 `db:"index_vacuum_count"`
MaxDeadTuples int64 `db:"max_dead_tuples"`
NumDeadTuples int64 `db:"num_dead_tuples"`
}
var (
VacuumTableName = "pg_stat_progress_vacuum"
VacuumQuery string
VacuumColumns []string
)
func GetVacuum(ctx context.Context, db *sqlx.DB) ([]Progress, error) {
if len(VacuumColumns) == 0 {
VacuumColumns = getColumns(Vacuum{})
}
if VacuumQuery == "" {
VacuumQuery = buildQuery(VacuumTableName, VacuumColumns)
}
return selectVacuum(ctx, db, VacuumQuery)
}
func selectVacuum(ctx context.Context, db *sqlx.DB, query string) ([]Progress, error) {
rows, err := db.QueryxContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var as []Progress
for rows.Next() {
var row Vacuum
err = rows.StructScan(&row)
if err != nil {
return nil, err
}
as = append(as, row)
}
return as, nil
}
func (v Vacuum) Name() string {
return VacuumTableName
}
func (v Vacuum) Pid() int {
return v.PID
}
func (v Vacuum) Color() (string, string) {
return "#5A56E0", "#FF7CCB"
}
func (v Vacuum) Table() string {
value := str.ToStrStruct(v)
buff := new(bytes.Buffer)
t := tablewriter.NewWriter(buff)
t.SetHeader(VacuumColumns[0:7])
t.Append(value[0:7])
t.Render()
t2 := tablewriter.NewWriter(buff)
t2.SetHeader(VacuumColumns[7:])
t2.Append(value[7:])
t2.Render()
return buff.String()
}
func (v Vacuum) Vertical() string {
buff := new(bytes.Buffer)
vt := vertical.NewWriter(buff)
vt.SetHeader(VacuumColumns)
vt.AppendStruct(v)
vt.Render()
return buff.String()
}
func (v Vacuum) Progress() float64 {
return float64(v.HeapBLKSScanned) / float64(v.HeapBLKSTotal)
}