Skip to content

Commit

Permalink
feat(lib): add stats.Cache to the lib instance as stats
Browse files Browse the repository at this point in the history
added `stats` field of type `stats.Stats` to the `InstanceOptions` and `Instance` struct.

`newStats` func is called in `NewInstance`. We create a new Cache based on the `cfg.Stats.Type` given in config (for now only option is "fs"), with a MaxSize of `cfg.Stats.MaxSize` (by default 25MiB). The cache is saved based on the `repoPath`, or `cfg.Stats.Path` if it's given.
  • Loading branch information
ramfox committed Nov 8, 2019
1 parent ba85eac commit a8646f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/qri-io/qri/repo"
fsrepo "github.com/qri-io/qri/repo/fs"
"github.com/qri-io/qri/repo/profile"
"github.com/qri-io/qri/stats"
"github.com/qri-io/qri/update"
"github.com/qri-io/qri/update/cron"
)
Expand Down Expand Up @@ -104,6 +105,7 @@ type InstanceOptions struct {
store cafs.Filestore
qfs qfs.Filesystem
regclient *regclient.Client
stats *stats.Stats

// use OptRemoteOptions to set this
remoteOptsFunc func(*remote.Options)
Expand Down Expand Up @@ -222,6 +224,14 @@ func OptRegistryClient(cli *regclient.Client) Option {
}
}

// OptStatsCache overrides the configured stats cache
func OptStatsCache(stats *stats.Stats) Option {
return func(o *InstanceOptions) error {
o.stats = stats
return nil
}
}

// NewInstance creates a new Qri Instance, if no Option funcs are provided,
// New uses a default set of Option funcs. Any Option functions passed to this
// function must check whether their fields are nil or not.
Expand Down Expand Up @@ -331,6 +341,12 @@ func NewInstance(ctx context.Context, repoPath string, opts ...Option) (qri *Ins
}
}

if o.stats != nil {
inst.stats = o.stats
} else if inst.stats == nil {
inst.stats = newStats(inst.repoPath, cfg)
}

if inst.repo != nil {
inst.fsi = fsi.NewFSI(inst.repo)
}
Expand Down Expand Up @@ -476,6 +492,27 @@ func newRepo(path string, cfg *config.Config, store cafs.Filestore, fs qfs.Files
}
}

func newStats(repoPath string, cfg *config.Config) *stats.Stats {
// The stats cache default location is repoPath/stats
// can be overridden in the config: cfg.Stats.Path
path := filepath.Join(repoPath, "stats")
if cfg.Stats.Path != "" {
path = cfg.Stats.Path
}
switch cfg.Stats.Type {
case "fs":
return stats.New(stats.NewOSCache(path, cfg.Stats.MaxSize))
// TODO (ramfox): return a mem and/or postgres version of the stats.Stats
// once those are implemented
// case "mem":
// return stats.New(stats.NewMemCache(path, cfg.Stats.MaxSize))
// case "postgres":
// return stats.New(stats.NewSqlCache(path, cfg.Stats.MaxSize))
default:
return stats.New(nil)
}
}

func newFilesystem(cfg *config.Config, store cafs.Filestore) (qfs.Filesystem, error) {
mux := map[string]qfs.Filesystem{
"local": localfs.NewFS(),
Expand Down Expand Up @@ -575,6 +612,7 @@ type Instance struct {
remote *remote.Remote
remoteClient *remote.Client
registry *regclient.Client
stats *stats.Stats

rpc *rpc.Client
}
Expand Down
3 changes: 3 additions & 0 deletions lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func TestNewDefaultInstance(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if _, err = os.Stat(filepath.Join(tempDir, "stats")); os.IsNotExist(err) {
t.Errorf("NewInstance error: stats cache never created")
}
}

func CompareInstances(a, b *Instance) error {
Expand Down

0 comments on commit a8646f4

Please sign in to comment.