-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze.go
112 lines (95 loc) · 2.57 KB
/
analyze.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
103
104
105
106
107
108
109
110
111
112
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_analyze.
type Analyze struct {
PID int `db:"pid"`
DATID int `db:"datid"`
DATNAME string `db:"datname"`
RELID int `db:"relid"`
PHASE string `db:"phase"`
SampleBLKSTotal int64 `db:"sample_blks_total"`
SampleBLKSScanned int64 `db:"sample_blks_scanned"`
ExtStatsTotal int64 `db:"ext_stats_total"`
ExtStatsComputed int64 `db:"ext_stats_computed"`
ChildTablesTotal int64 `db:"child_tables_total"`
ChildTablesDone int64 `db:"child_tables_done"`
CurrentChildTableRelid int `db:"current_child_table_relid"`
}
var (
AnalyzeTableName = "pg_stat_progress_analyze"
AnalyzeQuery string
AnalyzeColumns []string
)
func GetAnalyze(ctx context.Context, db *sqlx.DB) ([]Progress, error) {
if len(AnalyzeColumns) == 0 {
AnalyzeColumns = getColumns(Analyze{})
}
if AnalyzeQuery == "" {
AnalyzeQuery = buildQuery(AnalyzeTableName, AnalyzeColumns)
}
return selectAnalyze(ctx, db, AnalyzeQuery)
}
func selectAnalyze(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 Analyze
err = rows.StructScan(&row)
if err != nil {
return nil, err
}
as = append(as, row)
}
return as, rows.Err()
}
func (v Analyze) Name() string {
return AnalyzeTableName
}
func (v Analyze) Pid() int {
return v.PID
}
func (v Analyze) Color() (string, string) {
return "#FF7CCB", "#FDFF8C"
}
func (v Analyze) Table() string {
value := str.ToStrStruct(v)
buff := new(bytes.Buffer)
t := tablewriter.NewWriter(buff)
t.SetHeader(AnalyzeColumns[0:7])
t.Append(value[0:7])
t.Render()
t2 := tablewriter.NewWriter(buff)
t2.SetHeader(AnalyzeColumns[7:])
t2.Append(value[7:])
t2.Render()
return buff.String()
}
func (v Analyze) Vertical() string {
buff := new(bytes.Buffer)
vt := vertical.NewWriter(buff)
vt.SetHeader(AnalyzeColumns)
vt.AppendStruct(v)
vt.Render()
return buff.String()
}
func (v Analyze) Progress() float64 {
if v.ChildTablesTotal != 0 {
return float64(v.ChildTablesDone) / float64(v.ChildTablesTotal)
}
if v.ExtStatsTotal != 0 {
return float64(v.ExtStatsComputed) / float64(v.ExtStatsTotal)
}
return float64(v.SampleBLKSScanned) / float64(v.SampleBLKSTotal)
}