Skip to content

Commit ddbe3ca

Browse files
authored
Merge pull request #83 from markusressel/avoid-constantly-reloading-data
Avoid constantly reloading data
2 parents 7bff037 + f6e54e1 commit ddbe3ca

3 files changed

Lines changed: 56 additions & 47 deletions

File tree

internal/ui/snapshot_browser/snapshot_browser.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (snapshotBrowser *SnapshotBrowserComponent) createLayout() *tview.Pages {
106106
cellColor := tcell.ColorWhite
107107
switch column {
108108
case columnDate:
109-
cellText = entry.Snapshot.Date.Format("2006-01-02 15:04:05")
109+
cellText = entry.Snapshot.Properties.CreationDate.Format("2006-01-02 15:04:05")
110110
case columnName:
111111
cellText = entry.Snapshot.Name
112112
case columnDiff:
@@ -129,11 +129,11 @@ func (snapshotBrowser *SnapshotBrowserComponent) createLayout() *tview.Pages {
129129
cellColor = theme.Colors.SnapshotBrowser.Table.State.Unknown
130130
}
131131
case columnUsed:
132-
cellText = humanize.IBytes(entry.Snapshot.GetUsed())
132+
cellText = humanize.IBytes(entry.Snapshot.Properties.Used)
133133
case columnRefer:
134-
cellText = humanize.IBytes(entry.Snapshot.GetReferenced())
134+
cellText = humanize.IBytes(entry.Snapshot.Properties.Referenced)
135135
case columnRatio:
136-
ratio := entry.Snapshot.GetRatio()
136+
ratio := entry.Snapshot.Properties.CompressionRatio
137137
cellText = fmt.Sprintf("%.2fx", ratio)
138138
}
139139
cell := tview.NewTableCell(cellText).
@@ -153,16 +153,16 @@ func (snapshotBrowser *SnapshotBrowserComponent) createLayout() *tview.Pages {
153153
case columnName:
154154
result = strings.Compare(strings.ToLower(a.Snapshot.Name), strings.ToLower(b.Snapshot.Name))
155155
case columnDate:
156-
result = a.Snapshot.Date.Compare(*b.Snapshot.Date)
156+
result = a.Snapshot.Properties.CreationDate.Compare(*b.Snapshot.Properties.CreationDate)
157157
case columnDiff:
158158
result = int(b.DiffState - a.DiffState)
159159
case columnUsed:
160-
result = int(b.Snapshot.GetUsed() - a.Snapshot.GetUsed())
160+
result = int(b.Snapshot.Properties.Used - a.Snapshot.Properties.Used)
161161
case columnRefer:
162-
result = int(b.Snapshot.GetReferenced() - a.Snapshot.GetReferenced())
162+
result = int(b.Snapshot.Properties.Referenced - a.Snapshot.Properties.Referenced)
163163
case columnRatio:
164-
ratioA := a.Snapshot.GetRatio()
165-
ratioB := b.Snapshot.GetRatio()
164+
ratioA := a.Snapshot.Properties.CompressionRatio
165+
ratioB := b.Snapshot.Properties.CompressionRatio
166166
result = big.NewFloat(ratioA).Cmp(big.NewFloat(ratioB))
167167
}
168168
if inverted {
@@ -215,7 +215,6 @@ func (snapshotBrowser *SnapshotBrowserComponent) createLayout() *tview.Pages {
215215
snapshotBrowser.tableContainer.SetSelectionChangedCallback(func(entry *data.SnapshotBrowserEntry) {
216216
snapshotBrowser.rememberSelectionForDataset(entry)
217217
snapshotBrowser.selectedSnapshotChangedCallback(entry)
218-
snapshotBrowser.updateTableContents()
219218
})
220219

221220
layout.AddPage("snapshot-browser", snapshotBrowser.tableContainer.GetLayout(), true, true)
@@ -501,6 +500,7 @@ func (snapshotBrowser *SnapshotBrowserComponent) openDeleteDialog(selection *dat
501500
Message: status_message.NewErrorStatusMessage(fmt.Sprintf("Failed to destroy snapshot: %s", err)),
502501
})
503502
}
503+
snapshotBrowser.updateTableContents()
504504
return true
505505
default:
506506
return false
@@ -561,9 +561,9 @@ func (snapshotBrowser *SnapshotBrowserComponent) SelectLatest() {
561561
sort.SliceStable(sortedEntries, func(i, j int) bool {
562562
a := entries[i]
563563
b := entries[j]
564-
if a.Snapshot.Date != nil && b.Snapshot.Date != nil {
565-
dateA := a.Snapshot.Date
566-
dateB := b.Snapshot.Date
564+
if a.Snapshot.Properties.CreationDate != nil && b.Snapshot.Properties.CreationDate != nil {
565+
dateA := a.Snapshot.Properties.CreationDate
566+
dateB := b.Snapshot.Properties.CreationDate
567567
result := dateA.After(*dateB)
568568
return result
569569
}

internal/zfs/dataset.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"os"
77
path2 "path"
88
"strconv"
9-
"time"
109
"zfs-file-history/internal/logging"
1110
"zfs-file-history/internal/util"
1211

@@ -91,21 +90,13 @@ func (dataset *Dataset) GetSnapshots() ([]*Snapshot, error) {
9190
for _, file := range snapshotDirs {
9291
_, name := path2.Split(file)
9392

94-
var creationDate time.Time
9593
s := findSnapshot(AllSnapshots[dataset.GetName()], name)
9694
if s != nil {
97-
creationDateProperty := s.Properties[golibzfs.DatasetPropCreation]
98-
creationDateTimestamp, err := strconv.ParseInt(creationDateProperty.Value, 10, 64)
99-
if err != nil {
100-
logging.Error("Could not parse creation date for snapshot %s on dataset %s: %s", name, dataset.GetName(), err.Error())
101-
} else {
102-
creationDate = time.Unix(creationDateTimestamp, 0)
103-
}
10495
} else {
10596
logging.Warning("Could not find snapshot %s on dataset %s", name, dataset.GetName())
10697
}
10798

108-
result = append(result, NewSnapshot(name, file, dataset, &creationDate, s))
99+
result = append(result, NewSnapshot(name, file, dataset, s))
109100
}
110101

111102
return result, nil

internal/zfs/snapshot.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,23 @@ type Snapshot struct {
5555
FullName string
5656
Path string
5757
ParentDataset *Dataset
58-
Date *time.Time
5958

6059
rawGozfsData *gozfs.Dataset
6160
rawGolibzfsData *golibzfs.Dataset
61+
Properties SnapshotProperties
6262
}
6363

6464
func (s *Snapshot) Equal(e Snapshot) bool {
6565
return s.Name == e.Name && s.Path == e.Path
6666
}
6767

68-
func NewSnapshot(name string, path string, parentDataset *Dataset, date *time.Time, s *golibzfs.Dataset) *Snapshot {
68+
func NewSnapshot(name string, path string, parentDataset *Dataset, s *golibzfs.Dataset) *Snapshot {
6969
fullName := fmt.Sprintf("%s@%s", parentDataset.rawGozfsData.Name, name)
7070
snapshot := &Snapshot{
7171
Name: name,
7272
FullName: fullName,
7373
Path: path,
7474
ParentDataset: parentDataset,
75-
Date: date,
7675

7776
rawGolibzfsData: s,
7877
}
@@ -85,6 +84,8 @@ func NewSnapshot(name string, path string, parentDataset *Dataset, date *time.Ti
8584
snapshot.rawGozfsData = rawGoufsData[0]
8685
}
8786

87+
snapshot.FetchDetails()
88+
8889
return snapshot
8990
}
9091

@@ -321,37 +322,38 @@ func (s *Snapshot) DestroyRecursive() error {
321322
return ds.DestroySnapshot(s.Name, true)
322323
}
323324

324-
func (s *Snapshot) GetCreationData() *time.Time {
325-
//propValue, err := s.rawGozfsData.GetProperty("creation")
326-
//if err != nil {
327-
// logging.Error("Could not get creation property: %s", err.Error())
328-
// return nil
329-
//}
330-
//
331-
//timestamp, err := strconv.ParseInt(propValue, 10, 64)
332-
//if err != nil {
333-
// logging.Error("Could not parse creation property: %s", err.Error())
334-
// return nil
335-
//}
336-
//t := time.Unix(timestamp, 0)
337-
//return &t
338-
339-
if s.rawGolibzfsData == nil {
340-
logging.Error("No rawGolibzfsData available")
341-
return nil
342-
}
343-
prop, err := s.rawGolibzfsData.GetProperty(golibzfs.DatasetPropCreation)
325+
func (s *Snapshot) GetCreationDate() *time.Time {
326+
propValue, err := s.rawGozfsData.GetProperty("creation")
344327
if err != nil {
345328
logging.Error("Could not get creation property: %s", err.Error())
346329
return nil
347330
}
348-
timestamp, err := strconv.ParseInt(prop.Value, 10, 64)
331+
332+
timestamp, err := strconv.ParseInt(propValue, 10, 64)
349333
if err != nil {
350334
logging.Error("Could not parse creation property: %s", err.Error())
351335
return nil
352336
}
337+
353338
t := time.Unix(timestamp, 0)
354339
return &t
340+
341+
//if s.rawGolibzfsData == nil {
342+
// logging.Error("No rawGolibzfsData available")
343+
// return nil
344+
//}
345+
//prop, err := s.rawGolibzfsData.GetProperty(golibzfs.DatasetPropCreation)
346+
//if err != nil {
347+
// logging.Error("Could not get creation property: %s", err.Error())
348+
// return nil
349+
//}
350+
//timestamp, err := strconv.ParseInt(prop.Value, 10, 64)
351+
//if err != nil {
352+
// logging.Error("Could not parse creation property: %s", err.Error())
353+
// return nil
354+
//}
355+
//t := time.Unix(timestamp, 0)
356+
//return &t
355357
}
356358

357359
func (s *Snapshot) GetUsed() uint64 {
@@ -408,6 +410,22 @@ func (s *Snapshot) GetRatio() float64 {
408410
return val
409411
}
410412

413+
type SnapshotProperties struct {
414+
CreationDate *time.Time
415+
Used uint64
416+
Referenced uint64
417+
CompressionRatio float64
418+
}
419+
420+
func (s *Snapshot) FetchDetails() {
421+
s.Properties = SnapshotProperties{
422+
CreationDate: s.GetCreationDate(),
423+
Used: s.GetUsed(),
424+
Referenced: s.GetReferenced(),
425+
CompressionRatio: s.GetRatio(),
426+
}
427+
}
428+
411429
func syncFileProperties(dstPath string, stat os.FileInfo) error {
412430
err := os.Chmod(dstPath, stat.Mode())
413431
if err != nil {

0 commit comments

Comments
 (0)