Skip to content

Commit

Permalink
charts: Update derived data on db charts data query
Browse files Browse the repository at this point in the history
* Update charts derived dataset on charts db data query

* Persist the cache dump till newer data is available
  • Loading branch information
dmigwi authored and chappjc committed May 10, 2019
1 parent 8a7e3c7 commit 2f2513d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
27 changes: 15 additions & 12 deletions db/cache/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,15 @@ func isfileExists(filePath string) bool {

// writeCacheFile creates the charts cache in the provided file path if it
// doesn't exists. It dumps the ChartsData contents using the .gob encoding.
func (charts *ChartData) writeCacheFile(filePath string) (err error) {
var file *os.File
if !isfileExists(filePath) {
file, err = os.Create(filePath)
} else {
file, err = os.Open(filePath)
// Drops the old .gob dump before creating a new one. Delete the old cache here
// rather than after loading so that a dump will still be available after a crash.
func (charts *ChartData) writeCacheFile(filePath string) error {
if isfileExists(filePath) {
// delete the old dump files before creating new ones.
os.RemoveAll(filePath)
}

file, err := os.Create(filePath)
if err != nil {
return err
}
Expand All @@ -525,8 +526,7 @@ func (charts *ChartData) writeCacheFile(filePath string) (err error) {
}

// readCacheFile reads the contents of the charts cache dump file encoded in
// .gob format if it exists returns an error if otherwise. It then deletes
// the read *.gob cache dump file.
// .gob format if it exists returns an error if otherwise.
func (charts *ChartData) readCacheFile(filePath string) error {
file, err := os.Open(filePath)
if err != nil {
Expand All @@ -535,9 +535,6 @@ func (charts *ChartData) readCacheFile(filePath string) error {

defer func() {
file.Close()

// delete the dump after reading.
os.RemoveAll(filePath)
}()

var gobject = new(ChartGobject)
Expand Down Expand Up @@ -712,10 +709,16 @@ func (charts *ChartData) Update() {
}
cancel()
if err != nil {
log.Warnf("%v", err)
log.Errorf("%v", err)
return
}
}

// Since the charts db data query is complete. Update chart.Days derived dataset.
if err := charts.Lengthen(); err != nil {
log.Errorf("(*ChartData).Lengthen failed %v", err)
return
}
}

// NewChartData constructs a new ChartData.
Expand Down
4 changes: 2 additions & 2 deletions db/cache/charts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func TestChartsCache(t *testing.T) {
}

err = charts.writeCacheFile(path)
if err == nil {
t.Fatal("expected an error but found non")
if err != nil {
t.Fatalf("expected no error but found: %v", err)
}
})

Expand Down

0 comments on commit 2f2513d

Please sign in to comment.