- stats: use --workdir

This commit is contained in:
Simon Zolin 2019-09-06 15:42:21 +03:00
parent d0dcaeaa04
commit 97684368b9
4 changed files with 10 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"sync"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
@ -34,9 +35,9 @@ func initDNSServer(baseDir string) {
log.Fatalf("Cannot create DNS data dir at %s: %s", baseDir, err)
}
config.stats = stats.New("./data/stats.db", int(config.DNS.StatsInterval), nil)
if config.stats == nil {
log.Fatal("config.stats == nil")
config.stats, err = stats.New(filepath.Join(baseDir, "stats.db"), int(config.DNS.StatsInterval), nil)
if err != nil {
log.Fatal("Couldn't initialize statistics module")
}
config.dnsServer = dnsforward.NewServer(baseDir, config.stats)

View File

@ -12,7 +12,7 @@ type unitIDCallback func() int
// filename: DB file name
// limit: time limit (in days)
// unitID: user function to get the current unit ID. If nil, the current time hour is used.
func New(filename string, limit int, unitID unitIDCallback) Stats {
func New(filename string, limit int, unitID unitIDCallback) (Stats, error) {
return createObject(filename, limit, unitID)
}

View File

@ -26,7 +26,7 @@ func UIntArrayEquals(a []uint, b []uint) bool {
}
func TestStats(t *testing.T) {
s := New("./stats.db", 1, nil)
s, _ := New("./stats.db", 1, nil)
e := Entry{}
@ -87,7 +87,7 @@ func TestLargeNumbers(t *testing.T) {
// log.SetLevel(log.DEBUG)
fn := "./stats.db"
os.Remove(fn)
s := New(fn, 1, newID)
s, _ := New(fn, 1, newID)
e := Entry{}
n := 1000 // number of distinct clients and domains every hour

View File

@ -62,7 +62,7 @@ type unitDB struct {
TimeAvg uint // usec
}
func createObject(filename string, limitDays int, unitID unitIDCallback) *statsCtx {
func createObject(filename string, limitDays int, unitID unitIDCallback) (*statsCtx, error) {
s := statsCtx{}
s.limit = limitDays * 24
s.filename = filename
@ -72,7 +72,7 @@ func createObject(filename string, limitDays int, unitID unitIDCallback) *statsC
}
if !s.dbOpen() {
return nil
return nil, fmt.Errorf("open database")
}
id := s.unitID()
@ -116,7 +116,7 @@ func createObject(filename string, limitDays int, unitID unitIDCallback) *statsC
go s.periodicFlush()
log.Debug("Stats: initialized")
return &s
return &s, nil
}
func (s *statsCtx) dbOpen() bool {