2023-05-01 10:01:03 +00:00
|
|
|
package restore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jsonPrinter struct {
|
2024-05-31 18:38:51 +00:00
|
|
|
terminal term
|
|
|
|
verbosity uint
|
2023-05-01 10:01:03 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 18:38:51 +00:00
|
|
|
func NewJSONProgress(terminal term, verbosity uint) ProgressPrinter {
|
2023-05-01 10:01:03 +00:00
|
|
|
return &jsonPrinter{
|
2024-05-31 18:38:51 +00:00
|
|
|
terminal: terminal,
|
|
|
|
verbosity: verbosity,
|
2023-05-01 10:01:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *jsonPrinter) print(status interface{}) {
|
|
|
|
t.terminal.Print(ui.ToJSONString(status))
|
|
|
|
}
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
func (t *jsonPrinter) Update(p State, duration time.Duration) {
|
2023-05-01 10:01:03 +00:00
|
|
|
status := statusUpdate{
|
|
|
|
MessageType: "status",
|
|
|
|
SecondsElapsed: uint64(duration / time.Second),
|
2024-05-31 11:43:57 +00:00
|
|
|
TotalFiles: p.FilesTotal,
|
|
|
|
FilesRestored: p.FilesFinished,
|
2024-05-31 12:12:06 +00:00
|
|
|
FilesSkipped: p.FilesSkipped,
|
2024-05-31 11:43:57 +00:00
|
|
|
TotalBytes: p.AllBytesTotal,
|
|
|
|
BytesRestored: p.AllBytesWritten,
|
2024-05-31 12:12:06 +00:00
|
|
|
BytesSkipped: p.AllBytesSkipped,
|
2023-05-01 10:01:03 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
if p.AllBytesTotal > 0 {
|
|
|
|
status.PercentDone = float64(p.AllBytesWritten) / float64(p.AllBytesTotal)
|
2023-05-01 10:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.print(status)
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:38:51 +00:00
|
|
|
func (t *jsonPrinter) CompleteItem(messageType ItemAction, item string, size uint64) {
|
|
|
|
if t.verbosity < 3 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var action string
|
|
|
|
switch messageType {
|
|
|
|
case ActionDirRestored:
|
|
|
|
action = "restored"
|
|
|
|
case ActionFileRestored:
|
|
|
|
action = "restored"
|
|
|
|
case ActionFileUpdated:
|
|
|
|
action = "updated"
|
|
|
|
case ActionFileUnchanged:
|
|
|
|
action = "unchanged"
|
|
|
|
default:
|
|
|
|
panic("unknown message type")
|
|
|
|
}
|
|
|
|
|
|
|
|
status := verboseUpdate{
|
|
|
|
MessageType: "verbose_status",
|
|
|
|
Action: action,
|
|
|
|
Item: item,
|
|
|
|
Size: size,
|
|
|
|
}
|
|
|
|
t.print(status)
|
|
|
|
}
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
func (t *jsonPrinter) Finish(p State, duration time.Duration) {
|
2023-05-01 10:01:03 +00:00
|
|
|
status := summaryOutput{
|
|
|
|
MessageType: "summary",
|
|
|
|
SecondsElapsed: uint64(duration / time.Second),
|
2024-05-31 11:43:57 +00:00
|
|
|
TotalFiles: p.FilesTotal,
|
|
|
|
FilesRestored: p.FilesFinished,
|
2024-05-31 12:12:06 +00:00
|
|
|
FilesSkipped: p.FilesSkipped,
|
2024-05-31 11:43:57 +00:00
|
|
|
TotalBytes: p.AllBytesTotal,
|
|
|
|
BytesRestored: p.AllBytesWritten,
|
2024-05-31 12:12:06 +00:00
|
|
|
BytesSkipped: p.AllBytesSkipped,
|
2023-05-01 10:01:03 +00:00
|
|
|
}
|
|
|
|
t.print(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
type statusUpdate struct {
|
|
|
|
MessageType string `json:"message_type"` // "status"
|
|
|
|
SecondsElapsed uint64 `json:"seconds_elapsed,omitempty"`
|
|
|
|
PercentDone float64 `json:"percent_done"`
|
|
|
|
TotalFiles uint64 `json:"total_files,omitempty"`
|
2023-06-09 10:01:53 +00:00
|
|
|
FilesRestored uint64 `json:"files_restored,omitempty"`
|
2024-05-31 12:12:06 +00:00
|
|
|
FilesSkipped uint64 `json:"files_skipped,omitempty"`
|
2023-05-01 10:01:03 +00:00
|
|
|
TotalBytes uint64 `json:"total_bytes,omitempty"`
|
2023-06-09 10:01:53 +00:00
|
|
|
BytesRestored uint64 `json:"bytes_restored,omitempty"`
|
2024-05-31 12:12:06 +00:00
|
|
|
BytesSkipped uint64 `json:"bytes_skipped,omitempty"`
|
2023-05-01 10:01:03 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 18:38:51 +00:00
|
|
|
type verboseUpdate struct {
|
|
|
|
MessageType string `json:"message_type"` // "verbose_status"
|
|
|
|
Action string `json:"action"`
|
|
|
|
Item string `json:"item"`
|
|
|
|
Size uint64 `json:"size"`
|
|
|
|
}
|
|
|
|
|
2023-05-01 10:01:03 +00:00
|
|
|
type summaryOutput struct {
|
|
|
|
MessageType string `json:"message_type"` // "summary"
|
|
|
|
SecondsElapsed uint64 `json:"seconds_elapsed,omitempty"`
|
|
|
|
TotalFiles uint64 `json:"total_files,omitempty"`
|
2023-06-09 10:01:53 +00:00
|
|
|
FilesRestored uint64 `json:"files_restored,omitempty"`
|
2024-05-31 12:12:06 +00:00
|
|
|
FilesSkipped uint64 `json:"files_skipped,omitempty"`
|
2023-05-01 10:01:03 +00:00
|
|
|
TotalBytes uint64 `json:"total_bytes,omitempty"`
|
2023-06-09 10:01:53 +00:00
|
|
|
BytesRestored uint64 `json:"bytes_restored,omitempty"`
|
2024-05-31 12:12:06 +00:00
|
|
|
BytesSkipped uint64 `json:"bytes_skipped,omitempty"`
|
2023-05-01 10:01:03 +00:00
|
|
|
}
|