package database import ( "context" "time" ) // var store database.HashrateStore = database.DefaultStore() // store.InsertHashratePoint(ctx, "xmrig", "xmrig", point, database.ResolutionHigh) // store.Cleanup(30) type HashrateStore interface { // store.InsertHashratePoint(ctx, "xmrig", "xmrig", HashratePoint{Timestamp: time.Now(), Hashrate: 1234}, ResolutionHigh) InsertHashratePoint(ctx context.Context, minerName, minerType string, point HashratePoint, resolution Resolution) error // points, err := store.GetHashrateHistory("xmrig", ResolutionHigh, time.Now().Add(-time.Hour), time.Now()) GetHashrateHistory(minerName string, resolution Resolution, since, until time.Time) ([]HashratePoint, error) // stats, err := store.GetHashrateStats("xmrig") // if stats != nil { log.Printf("avg: %d H/s", stats.AverageRate) } GetHashrateStats(minerName string) (*HashrateStats, error) // allStats, err := store.GetAllMinerStats() // for _, stats := range allStats { log.Printf("%s avg: %d H/s", stats.MinerName, stats.AverageRate) } GetAllMinerStats() ([]HashrateStats, error) // store.Cleanup(30) // remove data older than 30 days Cleanup(retentionDays int) error // store.Close() Close() error } // store := database.DefaultStore() // wraps the global database connection type defaultStore struct{} // store := database.DefaultStore() // store.GetHashrateStats("xmrig") func DefaultStore() HashrateStore { return &defaultStore{} } func (s *defaultStore) InsertHashratePoint(ctx context.Context, minerName, minerType string, point HashratePoint, resolution Resolution) error { return InsertHashratePoint(ctx, minerName, minerType, point, resolution) } func (s *defaultStore) GetHashrateHistory(minerName string, resolution Resolution, since, until time.Time) ([]HashratePoint, error) { return GetHashrateHistory(minerName, resolution, since, until) } func (s *defaultStore) GetHashrateStats(minerName string) (*HashrateStats, error) { return GetHashrateStats(minerName) } func (s *defaultStore) GetAllMinerStats() ([]HashrateStats, error) { return GetAllMinerStats() } func (s *defaultStore) Cleanup(retentionDays int) error { return Cleanup(retentionDays) } func (s *defaultStore) Close() error { return Close() } // store := database.NopStore() // all methods are no-ops; use when database is disabled func NopStore() HashrateStore { return &nopStore{} } type nopStore struct{} func (s *nopStore) InsertHashratePoint(ctx context.Context, minerName, minerType string, point HashratePoint, resolution Resolution) error { return nil } func (s *nopStore) GetHashrateHistory(minerName string, resolution Resolution, since, until time.Time) ([]HashratePoint, error) { return nil, nil } func (s *nopStore) GetHashrateStats(minerName string) (*HashrateStats, error) { return nil, nil } func (s *nopStore) GetAllMinerStats() ([]HashrateStats, error) { return nil, nil } func (s *nopStore) Cleanup(retentionDays int) error { return nil } func (s *nopStore) Close() error { return nil }