-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
104 lines (90 loc) · 2.12 KB
/
copy.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
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_copy
type Copy struct {
PID int `db:"pid"`
DATID int `db:"datid"`
DATNAME string `db:"datname"`
RELID int `db:"relid"`
COMMAND string `db:"command"`
CTYPE string `db:"type"`
BYTESProcessed int64 `db:"bytes_processed"`
BYTESTotal int64 `db:"bytes_total"`
TUPLESProcessed int64 `db:"tuples_processed"`
TUPLESExcluded int64 `db:"tuples_excluded"`
}
var (
CopyTableName = "pg_stat_progress_copy"
CopyQuery string
CopyColumns []string
)
func GetCopy(ctx context.Context, db *sqlx.DB) ([]Progress, error) {
if len(CopyColumns) == 0 {
CopyColumns = getColumns(Copy{})
}
if CopyQuery == "" {
CopyQuery = buildQuery(CopyTableName, CopyColumns)
}
return selectCopy(ctx, db, CopyQuery)
}
func selectCopy(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 Copy
err = rows.StructScan(&row)
if err != nil {
return nil, err
}
as = append(as, row)
}
return as, nil
}
func (v Copy) Name() string {
return CopyTableName
}
func (v Copy) Pid() int {
return v.PID
}
func (v Copy) Color() (string, string) {
return "#5AF6FF", "#7CFFCB"
}
func (v Copy) Table() string {
value := str.ToStrStruct(v)
buff := new(bytes.Buffer)
t := tablewriter.NewWriter(buff)
t.SetHeader(CopyColumns[0:7])
t.Append(value[0:7])
t.Render()
t2 := tablewriter.NewWriter(buff)
t2.SetHeader(CopyColumns[7:])
t2.Append(value[7:])
t2.Render()
return buff.String()
}
func (v Copy) Vertical() string {
buff := new(bytes.Buffer)
vt := vertical.NewWriter(buff)
vt.SetHeader(CopyColumns)
vt.AppendStruct(v)
vt.Render()
return buff.String()
}
func (v Copy) Progress() float64 {
if v.BYTESTotal == 0 {
return float64(0.5)
}
return float64(v.BYTESProcessed) / float64(v.BYTESTotal)
}