Skip to content

Commit bd6d20c

Browse files
committed
Add Clones property to Snapshot for caching and update table handling for Clones column
1 parent ba00307 commit bd6d20c

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

internal/ui/snapshot_browser/table_container.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func createSnapshotBrowserTableCells(row int, columns []*table.Column, entry *da
6262
ratio := entry.Snapshot.Properties.CompressionRatio
6363
cellText = fmt.Sprintf("%.2fx", ratio)
6464
case columnClones:
65-
cellText = fmt.Sprintf("%d", entry.Snapshot.GetClones())
65+
cellText = fmt.Sprintf("%d", entry.Snapshot.Properties.Clones)
6666
}
6767
cell := tview.NewTableCell(cellText).
6868
SetTextColor(cellColor).SetAlign(cellAlign)
@@ -93,7 +93,16 @@ func createSnapshotBrowserTableSortFunction(entries []*data.SnapshotBrowserEntry
9393
ratioB := b.Snapshot.Properties.CompressionRatio
9494
result = big.NewFloat(ratioA).Cmp(big.NewFloat(ratioB))
9595
case columnClones:
96-
result = int(b.Snapshot.GetClones() - a.Snapshot.GetClones())
96+
clonesA := a.Snapshot.Properties.Clones
97+
clonesB := b.Snapshot.Properties.Clones
98+
switch {
99+
case clonesA < clonesB:
100+
result = -1
101+
case clonesA > clonesB:
102+
result = 1
103+
default:
104+
result = 0
105+
}
97106
}
98107
if inverted {
99108
result *= -1
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package snapshot_browser
2+
3+
import (
4+
"math"
5+
"testing"
6+
"zfs-file-history/internal/data"
7+
"zfs-file-history/internal/data/diff_state"
8+
"zfs-file-history/internal/ui/table"
9+
"zfs-file-history/internal/zfs"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCreateSnapshotBrowserTableCells_ClonesColumn(t *testing.T) {
15+
entry := &data.SnapshotBrowserEntry{
16+
Snapshot: &zfs.Snapshot{
17+
Name: "snap-a",
18+
Properties: zfs.SnapshotProperties{
19+
Clones: 42,
20+
},
21+
},
22+
DiffState: diff_state.Unknown,
23+
}
24+
25+
cells := createSnapshotBrowserTableCells(0, []*table.Column{columnClones}, entry)
26+
27+
if assert.Len(t, cells, 1) {
28+
assert.Equal(t, "42", cells[0].Text)
29+
}
30+
}
31+
32+
func TestCreateSnapshotBrowserTableSortFunction_ClonesAscendingAndDescending(t *testing.T) {
33+
entries := []*data.SnapshotBrowserEntry{
34+
newSnapshotEntryWithClones("low", 0),
35+
newSnapshotEntryWithClones("high", math.MaxUint64),
36+
newSnapshotEntryWithClones("mid", 1),
37+
}
38+
39+
ascending := append([]*data.SnapshotBrowserEntry{}, entries...)
40+
createSnapshotBrowserTableSortFunction(ascending, columnClones, false)
41+
assert.Equal(t, []string{"low", "mid", "high"}, snapshotNames(ascending))
42+
43+
descending := append([]*data.SnapshotBrowserEntry{}, entries...)
44+
createSnapshotBrowserTableSortFunction(descending, columnClones, true)
45+
assert.Equal(t, []string{"high", "mid", "low"}, snapshotNames(descending))
46+
}
47+
48+
func newSnapshotEntryWithClones(name string, clones uint64) *data.SnapshotBrowserEntry {
49+
return &data.SnapshotBrowserEntry{
50+
Snapshot: &zfs.Snapshot{
51+
Name: name,
52+
Properties: zfs.SnapshotProperties{
53+
Clones: clones,
54+
},
55+
},
56+
DiffState: diff_state.Unknown,
57+
}
58+
}
59+
60+
func snapshotNames(entries []*data.SnapshotBrowserEntry) []string {
61+
result := make([]string, 0, len(entries))
62+
for _, entry := range entries {
63+
result = append(result, entry.Snapshot.Name)
64+
}
65+
return result
66+
}

internal/zfs/snapshot.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ type SnapshotProperties struct {
428428
Used uint64
429429
Referenced uint64
430430
CompressionRatio float64
431+
Clones uint64
431432
}
432433

433434
func (s *Snapshot) FetchDetails() {
@@ -436,6 +437,7 @@ func (s *Snapshot) FetchDetails() {
436437
Used: s.GetUsed(),
437438
Referenced: s.GetReferenced(),
438439
CompressionRatio: s.GetRatio(),
440+
Clones: s.GetClones(),
439441
}
440442
}
441443

0 commit comments

Comments
 (0)