-
Notifications
You must be signed in to change notification settings - Fork 0
/
basebackup.go
98 lines (83 loc) · 2.16 KB
/
basebackup.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
package pgsp
import (
"bytes"
"context"
"database/sql"
"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_basebackup.
type BaseBackup struct {
PID int `db:"pid"`
PHASE string `db:"phase"`
BackupTotal sql.NullInt64 `db:"backup_total"`
BackupStreamed int64 `db:"backup_streamed"`
TablespacesTotal int64 `db:"tablespaces_total"`
TablespacesStreamed int64 `db:"tablespaces_streamed"`
}
var (
BaseBackupTableName = "pg_stat_progress_basebackup"
BaseBackupQuery string
BaseBackupColumns []string
)
func GetBaseBackup(ctx context.Context, db *sqlx.DB) ([]Progress, error) {
if len(BaseBackupColumns) == 0 {
BaseBackupColumns = getColumns(BaseBackup{})
}
if BaseBackupQuery == "" {
BaseBackupQuery = buildQuery(BaseBackupTableName, BaseBackupColumns)
}
return selectBaseBackup(ctx, db, BaseBackupQuery)
}
func selectBaseBackup(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 BaseBackup
err = rows.StructScan(&row)
if err != nil {
return nil, err
}
as = append(as, row)
}
return as, rows.Err()
}
func (v BaseBackup) Name() string {
return BaseBackupTableName
}
func (v BaseBackup) Pid() int {
return v.PID
}
func (v BaseBackup) Color() (string, string) {
return "#FDFF8C", "#FF7CCB"
}
func (v BaseBackup) Table() string {
buff := new(bytes.Buffer)
t := tablewriter.NewWriter(buff)
t.SetHeader(BaseBackupColumns)
t.Append(str.ToStrStruct(v))
t.Render()
return buff.String()
}
func (v BaseBackup) Vertical() string {
buff := new(bytes.Buffer)
vt := vertical.NewWriter(buff)
vt.SetHeader(BaseBackupColumns)
vt.AppendStruct(v)
vt.Render()
return buff.String()
}
func (v BaseBackup) Progress() float64 {
total := v.BackupTotal.Int64
if total != 0 {
return float64(v.BackupStreamed) / float64(total)
}
return float64(v.TablespacesStreamed) / float64(v.TablespacesTotal)
}