Skip to content

Commit

Permalink
Process Restic stats output (#263)
Browse files Browse the repository at this point in the history
* Post process restic stats

* Add tests

* Add processing for stats mode

* Address review comments

* Address review comments
  • Loading branch information
DeepikaDixit authored and mergify[bot] committed Sep 19, 2019
1 parent bd447e0 commit 527612e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,42 @@ func SnapshotIDFromBackupLog(output string) string {
}
return ""
}

// SnapshotStatsFromStatsLog gets the Snapshot Stats from Stats Command log
func SnapshotStatsFromStatsLog(output string) (string, string, string) {
if output == "" {
return "", "", ""
}
var fileCount string
var size string
logs := regexp.MustCompile("[\n]").Split(output, -1)
// Log should contain "Total File Count: xx"
pattern1 := regexp.MustCompile(`Total File Count:\s+(.*?)$`)
// Log should contain "Total Size: xx"
pattern2 := regexp.MustCompile(`Total Size: \s+(.*?)$`)
for _, l := range logs {
match1 := pattern1.FindAllStringSubmatch(l, 1)
if len(match1) > 0 && len(match1[0]) > 1 {
fileCount = match1[0][1]
}
match2 := pattern2.FindAllStringSubmatch(l, 1)
if len(match2) > 0 && len(match2[0]) > 1 {
size = match2[0][1]
}
}
return SnapshotStatsModeFromStatsLog(output), fileCount, size
}

// SnapshotStatsModeFromStatsLog gets the Stats mode from Stats Command log
func SnapshotStatsModeFromStatsLog(output string) string {
logs := regexp.MustCompile("[\n]").Split(output, -1)
// Log should contain "Stats for .... in xx mode"
pattern := regexp.MustCompile(`Stats for.*in\s+(.*?)\s+mode:`)
for _, l := range logs {
match := pattern.FindAllStringSubmatch(l, 1)
if len(match) > 0 && len(match[0]) > 1 {
return match[0][1]
}
}
return ""
}
34 changes: 34 additions & 0 deletions pkg/restic/restic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,37 @@ func (s *ResticDataSuite) TestResticArgs(c *C) {
c.Assert(resticArgs(tc.profile, tc.repo, tc.password), DeepEquals, tc.expected)
}
}

func (s *ResticDataSuite) TestGetSnapshotStatsFromStatsLog(c *C) {
for _, tc := range []struct {
log string
expectedfc string
expectedsize string
}{
{log: "Total File Count: 9", expectedfc: "9", expectedsize: ""},
{log: "Total Size: 10.322 KiB", expectedfc: "", expectedsize: "10.322 KiB"},
{log: "sudhufehfuijbfjbruifhoiwhf", expectedfc: "", expectedsize: ""},
{log: " Total File Count: 9", expectedfc: "9", expectedsize: ""},
{log: " Total Size: 10.322 KiB", expectedfc: "", expectedsize: "10.322 KiB"},
} {
_, fc, s := SnapshotStatsFromStatsLog(tc.log)
c.Assert(fc, Equals, tc.expectedfc)
c.Assert(s, Equals, tc.expectedsize)
}
}

func (s *ResticDataSuite) TestGetSnapshotStatsModeFromStatsLog(c *C) {
for _, tc := range []struct {
log string
expected string
}{
{log: "Stats for all snapshots in restore-size mode:", expected: "restore-size"},
{log: "Stats for 7e17e764 in restore-size mode:", expected: "restore-size"},
{log: "Stats for all snapshots in raw-data mode:", expected: "raw-data"},
{log: "Stats for all snapshots in blobs-per-file mode:", expected: "blobs-per-file"},
{log: "sudhufehfuijbfjbruifhoiwhf", expected: ""},
} {
mode := SnapshotStatsModeFromStatsLog(tc.log)
c.Assert(mode, Equals, tc.expected)
}
}

0 comments on commit 527612e

Please sign in to comment.