Skip to content

Commit

Permalink
feat: sort resources by LRU
Browse files Browse the repository at this point in the history
  • Loading branch information
marianozunino committed Aug 21, 2024
1 parent 0a87d99 commit ba1ee02
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ release:
github:
owner: marianozunino
name: sdm-ui
name_template: "{{.ProjectName}}-v{{.Version}} {{.Env.USER}}"
name_template: "{{.ProjectName}} v{{.Version}}"

# You can disable this pipe in order to not upload any artifacts to
# GitHub.
Expand Down
1 change: 1 addition & 0 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var syncCmd = &cobra.Command{
program.WithAccount(confData.Email),
program.WithVerbose(confData.Verbose),
program.WithDbPath(confData.DBPath),
program.WithBlacklist(confData.BalcklistPatterns),
).Sync()
},
}
Expand Down
1 change: 1 addition & 0 deletions internal/program/dmenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func (p *Program) handleSelectedEntry(selectedEntry string) error {
}

if err := p.retryCommand(func() error {
p.db.UpdateLastUsed(ds)
return p.sdmWrapper.Connect(ds.Name)
}); err != nil {
return err
Expand Down
10 changes: 8 additions & 2 deletions internal/program/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package program
import (
"io"
"regexp"
"slices"
"strings"

"github.com/marianozunino/sdm-ui/internal/storage"
Expand All @@ -16,8 +17,6 @@ func (p *Program) List(w io.Writer) error {
return err
}

dataSources = p.applyBlacklist(dataSources)

if len(dataSources) == 0 {
log.Info().Msg("No data sources found, syncing...")
if err := p.Sync(); err != nil {
Expand All @@ -30,6 +29,13 @@ func (p *Program) List(w io.Writer) error {
}
}

dataSources = p.applyBlacklist(dataSources)

// sort by LRU
slices.SortFunc(dataSources, func(a, b storage.DataSource) int {
return int(b.LRU - a.LRU)
})

printDataSources(dataSources, w)
return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/storage/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type DataSource struct {
Type string
Tags string
WebURL string
LRU int64 // Unix timestamp to sort on
}

// Encode serializes the DataSource into a byte slice.
Expand Down
65 changes: 63 additions & 2 deletions internal/storage/db.go → internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"path/filepath"
"strconv"
"strings"
"time"

"github.com/rs/zerolog/log"
bolt "go.etcd.io/bbolt"
)

const (
datasourceBucketPrefix = "datasource"
currentDBVersion = 1 // increment this whenever the database schema changes
currentDBVersion = 2 // increment this whenever the database schema changes
retentionPeriod = 2
)

Expand Down Expand Up @@ -58,6 +59,31 @@ func buildBucketKey(account string, version int) []byte {
return []byte(fmt.Sprintf("%s:%s:v%d", account, datasourceBucketPrefix, version))
}

// // StoreServers stores the provided datasources for the specified account.
// func (s *Storage) StoreServers(datasources []DataSource) error {
// bucketKey := buildBucketKey(s.account, currentDBVersion)
// return s.Update(func(tx *bolt.Tx) error {
// log.Debug().Msgf("Storing %d datasources", len(datasources))
// bucket := tx.Bucket(bucketKey)
// if bucket == nil {
// return fmt.Errorf("bucket for account %s not found", s.account)
// }
// for _, ds := range datasources {
// // Encode the DataSource and handle any errors
// encodedData, err := ds.Encode()
// if err != nil {
// return fmt.Errorf("failed to encode datasource %s: %w", ds.Name, err)
// }
// // Store the encoded DataSource in the bucket
// if err := bucket.Put(ds.Key(), encodedData); err != nil {
// return fmt.Errorf("failed to store datasource %s: %w", ds.Name, err)
// }
// }
// log.Debug().Msgf("Successfully stored %d datasources", len(datasources))
// return nil
// })
// }

// StoreServers stores the provided datasources for the specified account.
func (s *Storage) StoreServers(datasources []DataSource) error {
bucketKey := buildBucketKey(s.account, currentDBVersion)
Expand All @@ -68,7 +94,18 @@ func (s *Storage) StoreServers(datasources []DataSource) error {
return fmt.Errorf("bucket for account %s not found", s.account)
}
for _, ds := range datasources {
// Encode the DataSource and handle any errors
// Retrieve the existing DataSource from the database
existingData := bucket.Get(ds.Key())
if existingData != nil {
var existingDS DataSource
if err := existingDS.Decode(existingData); err != nil {
return fmt.Errorf("failed to decode existing datasource %s: %w", ds.Name, err)
}
// Update the LRU value of the existing DataSource
ds.LRU = existingDS.LRU
}

// Encode the updated DataSource and handle any errors
encodedData, err := ds.Encode()
if err != nil {
return fmt.Errorf("failed to encode datasource %s: %w", ds.Name, err)
Expand Down Expand Up @@ -127,6 +164,30 @@ func (s *Storage) GetDatasource(name string) (DataSource, error) {
return datasource, err
}

func (s *Storage) UpdateLastUsed(ds DataSource) error {
ds.LRU = time.Now().Unix()
bucketKey := buildBucketKey(s.account, currentDBVersion)
return s.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(bucketKey)
if bucket == nil {
return fmt.Errorf("bucket for account %s not found", s.account)
}

// Encode the DataSource and handle any errors
encodedData, err := ds.Encode()
if err != nil {
return fmt.Errorf("failed to encode datasource %s: %w", ds.Name, err)
}

// Store the encoded DataSource in the bucket
if err := bucket.Put(ds.Key(), encodedData); err != nil {
return fmt.Errorf("failed to store datasource %s: %w", ds.Name, err)
}

return nil
})
}

// removeOldBuckets removes old buckets that are older than the specified retention period.
func (s *Storage) removeOldBuckets(retentionPeriod int) error {
return s.Update(func(tx *bolt.Tx) error {
Expand Down

0 comments on commit ba1ee02

Please sign in to comment.