Skip to content

Commit

Permalink
added "Ratio" column
Browse files Browse the repository at this point in the history
  • Loading branch information
markusressel committed Nov 3, 2024
1 parent 9f0fe25 commit 9434586
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/ui/snapshot_browser/snapshot_browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"golang.org/x/exp/slices"
"math/big"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -52,7 +53,7 @@ var (
}
columnDate = &table.Column{
Id: 1,
Title: "Date",
Title: "Creation",
Alignment: tview.AlignLeft,
}
columnDiff = &table.Column{
Expand All @@ -70,8 +71,13 @@ var (
Title: "Refer",
Alignment: tview.AlignCenter,
}
columnRatio = &table.Column{
Id: 5,
Title: "Ratio",
Alignment: tview.AlignCenter,
}
tableColumns = []*table.Column{
columnName, columnDate, columnDiff, columnUsed, columnRefer,
columnName, columnDate, columnDiff, columnUsed, columnRefer, columnRatio,
}
)

Expand Down Expand Up @@ -124,6 +130,9 @@ func (snapshotBrowser *SnapshotBrowserComponent) createLayout() *tview.Pages {
cellText = humanize.IBytes(entry.Snapshot.GetUsed())
} else if column == columnRefer {
cellText = humanize.IBytes(entry.Snapshot.GetReferenced())
} else if column == columnRatio {
ratio := entry.Snapshot.GetRatio()
cellText = fmt.Sprintf("%.2fx", ratio)
}
cell := tview.NewTableCell(cellText).
SetTextColor(cellColor).SetAlign(cellAlign)
Expand All @@ -148,6 +157,10 @@ func (snapshotBrowser *SnapshotBrowserComponent) createLayout() *tview.Pages {
result = int(b.Snapshot.GetUsed() - a.Snapshot.GetUsed())
} else if columnToSortBy == columnRefer {
result = int(b.Snapshot.GetReferenced() - a.Snapshot.GetReferenced())
} else if columnToSortBy == columnRatio {
ratioA := a.Snapshot.GetRatio()
ratioB := b.Snapshot.GetRatio()
result = big.NewFloat(ratioA).Cmp(big.NewFloat(ratioB))
}
if inverted {
result *= -1
Expand Down
10 changes: 10 additions & 0 deletions internal/zfs/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ func (s *Snapshot) GetReferenced() uint64 {
return referenced
}

func (s *Snapshot) GetRatio() float64 {
rawValue := s.internalSnapshot.Properties[golibzfs.DatasetPropRefratio].Value
prop, err := strconv.ParseFloat(rawValue, 64)
if err != nil {
logging.Error("Could not parse ratio property: %s", err.Error())
return 0
}
return prop
}

func syncFileProperties(dstPath string, stat os.FileInfo) error {
err := os.Chmod(dstPath, stat.Mode())
if err != nil {
Expand Down

0 comments on commit 9434586

Please sign in to comment.