Cache database size
This commit is contained in:
parent
c185d463e6
commit
bf404e65da
2 changed files with 59 additions and 12 deletions
43
stats.go
43
stats.go
|
|
@ -4,10 +4,26 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get the size of the database and return as nearest human readable size
|
type Cache struct {
|
||||||
|
timestamp time.Time
|
||||||
|
data string
|
||||||
|
}
|
||||||
|
|
||||||
|
var cache Cache
|
||||||
|
|
||||||
|
// Get the size of the database and return as nearest human readable size.
|
||||||
|
//
|
||||||
|
// e.g. 1.23 KiB, 4.56 MiB, 7.89 GiB, 0.12 TiB
|
||||||
|
// The size is cached for 10 minutes
|
||||||
func GetDBSize() string {
|
func GetDBSize() string {
|
||||||
|
// If cache is less than 10 minutes old, return cached data
|
||||||
|
if time.Since(cache.timestamp).Minutes() < 10 {
|
||||||
|
return cache.data
|
||||||
|
}
|
||||||
|
|
||||||
fileInfo, err := os.Stat("feedvault.db")
|
fileInfo, err := os.Stat("feedvault.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting file info:", err)
|
log.Println("Error getting file info:", err)
|
||||||
|
|
@ -21,19 +37,22 @@ func GetDBSize() string {
|
||||||
var size float64
|
var size float64
|
||||||
if fileSize < 1024*1024 {
|
if fileSize < 1024*1024 {
|
||||||
size = float64(fileSize) / 1024
|
size = float64(fileSize) / 1024
|
||||||
return fmt.Sprintf("%.2f KiB", size)
|
cache.data = fmt.Sprintf("%.2f KiB", size)
|
||||||
}
|
} else if fileSize < 1024*1024*1024 {
|
||||||
|
|
||||||
if fileSize < 1024*1024*1024 {
|
|
||||||
size = float64(fileSize) / (1024 * 1024)
|
size = float64(fileSize) / (1024 * 1024)
|
||||||
return fmt.Sprintf("%.2f MiB", size)
|
cache.data = fmt.Sprintf("%.2f MiB", size)
|
||||||
}
|
} else if fileSize < 1024*1024*1024*1024 {
|
||||||
|
|
||||||
if fileSize < 1024*1024*1024*1024 {
|
|
||||||
size = float64(fileSize) / (1024 * 1024 * 1024)
|
size = float64(fileSize) / (1024 * 1024 * 1024)
|
||||||
return fmt.Sprintf("%.2f GiB", size)
|
cache.data = fmt.Sprintf("%.2f GiB", size)
|
||||||
|
} else {
|
||||||
|
size = float64(fileSize) / (1024 * 1024 * 1024 * 1024)
|
||||||
|
cache.data = fmt.Sprintf("%.2f TiB", size)
|
||||||
}
|
}
|
||||||
|
|
||||||
size = float64(fileSize) / (1024 * 1024 * 1024 * 1024)
|
// Update cache timestamp
|
||||||
return fmt.Sprintf("%.2f TiB", size)
|
cache.timestamp = time.Now()
|
||||||
|
|
||||||
|
log.Println("Returning database size, it is", cache.data)
|
||||||
|
|
||||||
|
return cache.data
|
||||||
}
|
}
|
||||||
|
|
|
||||||
28
stats_test.go
Normal file
28
stats_test.go
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// If the cache is less than 10 minutes old, return the cached data.
|
||||||
|
func TestCacheLessThan10MinutesOld(t *testing.T) {
|
||||||
|
result := GetDBSize()
|
||||||
|
|
||||||
|
// Assert that the size of the database is returned
|
||||||
|
if result != cache.data {
|
||||||
|
t.Errorf("Expected database size, but got %s", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the cache is more than 10 minutes old, return the size of the database.
|
||||||
|
func TestCacheMoreThan10MinutesOld(t *testing.T) {
|
||||||
|
// Set the cache timestamp to 11 minutes ago
|
||||||
|
cache.timestamp = time.Now().Add(-11 * time.Minute)
|
||||||
|
result := GetDBSize()
|
||||||
|
|
||||||
|
// Assert that the size of the database is returned
|
||||||
|
if result != cache.data {
|
||||||
|
t.Errorf("Expected database size, but got %s", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue