Skip to content

Commit f48f838

Browse files
authored
PBM-1544 Add new column showing the backup status update (#1144)
* add printStatus to snapshot stats * move print status to defs
1 parent a8011e0 commit f48f838

File tree

4 files changed

+61
-33
lines changed

4 files changed

+61
-33
lines changed

cmd/pbm/list.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,15 @@ func getSnapshotList(
314314
}
315315

316316
s = append(s, snapshotStat{
317-
Name: b.Name,
318-
Namespaces: b.Namespaces,
319-
Status: b.Status,
320-
RestoreTS: int64(b.LastWriteTS.T),
321-
PBMVersion: b.PBMVersion,
322-
Type: b.Type,
323-
SrcBackup: b.SrcBackup,
324-
StoreName: b.Store.Name,
317+
Name: b.Name,
318+
Namespaces: b.Namespaces,
319+
Status: b.Status,
320+
PrintStatus: b.Status.PrintStatus(),
321+
RestoreTS: int64(b.LastWriteTS.T),
322+
PBMVersion: b.PBMVersion,
323+
Type: b.Type,
324+
SrcBackup: b.SrcBackup,
325+
StoreName: b.Store.Name,
325326
})
326327
}
327328

cmd/pbm/main.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,17 +1116,18 @@ func tsUTC(ts int64) string {
11161116
}
11171117

11181118
type snapshotStat struct {
1119-
Name string `json:"name"`
1120-
Namespaces []string `json:"nss,omitempty"`
1121-
Size int64 `json:"size,omitempty"`
1122-
Status defs.Status `json:"status"`
1123-
Err error `json:"-"`
1124-
ErrString string `json:"error,omitempty"`
1125-
RestoreTS int64 `json:"restoreTo"`
1126-
PBMVersion string `json:"pbmVersion"`
1127-
Type defs.BackupType `json:"type"`
1128-
SrcBackup string `json:"src"`
1129-
StoreName string `json:"storage,omitempty"`
1119+
Name string `json:"name"`
1120+
Namespaces []string `json:"nss,omitempty"`
1121+
Size int64 `json:"size,omitempty"`
1122+
Status defs.Status `json:"status"`
1123+
PrintStatus defs.PrintStatus `json:"printStatus"`
1124+
Err error `json:"-"`
1125+
ErrString string `json:"error,omitempty"`
1126+
RestoreTS int64 `json:"restoreTo"`
1127+
PBMVersion string `json:"pbmVersion"`
1128+
Type defs.BackupType `json:"type"`
1129+
SrcBackup string `json:"src"`
1130+
StoreName string `json:"storage,omitempty"`
11301131
}
11311132

11321133
type pitrRange struct {

cmd/pbm/status.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -470,35 +470,22 @@ func (s storageStat) String() string {
470470
return a.RestoreTS > b.RestoreTS
471471
})
472472

473-
const (
474-
statusSuccess = "success"
475-
statusFailed = "failed"
476-
statusOngoing = "ongoing"
477-
)
478-
479-
var printStatus string
480-
481473
for i := range s.Snapshot {
482474
ss := &s.Snapshot[i]
483475
var status string
484476
switch ss.Status {
485477
case defs.StatusDone:
486478
status = fmt.Sprintf("[restore_to_time: %s]", fmtTS(ss.RestoreTS))
487-
printStatus = statusSuccess
488479
case defs.StatusCancelled:
489480
status = fmt.Sprintf("[!canceled: %s]", fmtTS(ss.RestoreTS))
490-
printStatus = statusFailed
491481
case defs.StatusError:
492482
if errors.Is(ss.Err, errIncompatible) {
493483
status = fmt.Sprintf("[incompatible: %s] [%s]", ss.Err.Error(), fmtTS(ss.RestoreTS))
494-
printStatus = statusSuccess
495484
} else {
496485
status = fmt.Sprintf("[ERROR: %s] [%s]", ss.Err.Error(), fmtTS(ss.RestoreTS))
497-
printStatus = statusFailed
498486
}
499487
default:
500488
status = fmt.Sprintf("[running: %s / %s]", ss.Status, fmtTS(ss.RestoreTS))
501-
printStatus = statusOngoing
502489
}
503490

504491
t := string(ss.Type)
@@ -510,7 +497,7 @@ func (s storageStat) String() string {
510497
if ss.StoreName != "" {
511498
t += ", *"
512499
}
513-
ret += fmt.Sprintf(" %s %s <%s> %s %s\n", ss.Name, storage.PrettySize(ss.Size), t, printStatus, status)
500+
ret += fmt.Sprintf(" %s %s <%s> %s %s\n", ss.Name, storage.PrettySize(ss.Size), t, ss.PrintStatus, status)
514501
}
515502

516503
if len(s.PITR.Ranges) == 0 {
@@ -612,6 +599,7 @@ func getStorageStat(
612599
snpsht.Err = err
613600
snpsht.ErrString = err.Error()
614601
}
602+
snpsht.PrintStatus = snpsht.Status.PrintStatus(snpsht.Err)
615603

616604
switch bcp.Status {
617605
case defs.StatusError:
@@ -629,6 +617,7 @@ func getStorageStat(
629617
snpsht.Err = errors.New(errStr)
630618
snpsht.ErrString = errStr
631619
snpsht.Status = defs.StatusError
620+
snpsht.PrintStatus = defs.StatusError.PrintStatus()
632621
}
633622
}
634623

@@ -638,6 +627,7 @@ func getStorageStat(
638627
snpsht.Err = err
639628
snpsht.ErrString = err.Error()
640629
snpsht.Status = defs.StatusError
630+
snpsht.PrintStatus = defs.StatusError.PrintStatus()
641631
}
642632

643633
s.Snapshot = append(s.Snapshot, snpsht)

pbm/defs/defs.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"time"
55

66
"github.com/percona/percona-backup-mongodb/pbm/compress"
7+
"github.com/percona/percona-backup-mongodb/pbm/errors"
78
)
89

910
const (
@@ -125,6 +126,41 @@ func (s Status) IsRunning() bool {
125126
return true
126127
}
127128

129+
type PrintStatus string
130+
131+
const (
132+
statusSuccess PrintStatus = "success"
133+
statusFailed PrintStatus = "failed"
134+
statusOngoing PrintStatus = "ongoing"
135+
)
136+
137+
var ErrIncompatible = errors.New("incompatible")
138+
139+
func (s Status) PrintStatus(errs ...error) PrintStatus {
140+
var err error
141+
if len(errs) > 0 {
142+
err = errs[0]
143+
}
144+
145+
switch s {
146+
case StatusDone:
147+
return statusSuccess
148+
149+
case StatusCancelled:
150+
return statusFailed
151+
152+
case StatusError:
153+
// "incompatible" is treated as success
154+
if err != nil && errors.Is(err, ErrIncompatible) {
155+
return statusSuccess
156+
}
157+
return statusFailed
158+
159+
default:
160+
return statusOngoing
161+
}
162+
}
163+
128164
type Operation string
129165

130166
const (

0 commit comments

Comments
 (0)