diff --git a/analyzers/analyzers.go b/analyzers/analyzers.go index 11823423f..2f9f61f5a 100644 --- a/analyzers/analyzers.go +++ b/analyzers/analyzers.go @@ -61,6 +61,10 @@ func (aS *AnalyzerService) SetFilterS(fS *engine.FilterS) { } func (aS *AnalyzerService) initDB() (err error) { + if aS.cfg.AnalyzerSCfg().DBPath == utils.EmptyString { + aS.db, err = bleve.NewMemOnly(bleve.NewIndexMapping()) + return + } dbPath := path.Join(aS.cfg.AnalyzerSCfg().DBPath, utils.AnzDBDir) if _, err = os.Stat(dbPath); err == nil { aS.db, err = bleve.Open(dbPath) diff --git a/analyzers/analyzers_test.go b/analyzers/analyzers_test.go index a787b6dcf..9c19936dc 100644 --- a/analyzers/analyzers_test.go +++ b/analyzers/analyzers_test.go @@ -440,3 +440,48 @@ func TestAnalyzersV1Search(t *testing.T) { t.Fatal(err) } } + +func TestAnalyzerSLogTrafficInternalDB(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + + cfg.AnalyzerSCfg().DBPath = utils.EmptyString + cfg.AnalyzerSCfg().TTL = 30 * time.Minute + anz, err := NewAnalyzerService(cfg) + if err != nil { + t.Fatal(err) + } + t1 := time.Now().Add(-time.Hour) + if err = anz.logTrafic(0, utils.AnalyzerSv1Ping, "status", "result", "error", + utils.MetaJSON, "127.0.0.1:5565", "127.0.0.1:2012", t1, t1.Add(time.Second)); err != nil { + t.Fatal(err) + } + if err = anz.logTrafic(0, utils.CoreSv1Status, "status", "result", "error", + utils.MetaJSON, "127.0.0.1:5565", "127.0.0.1:2012", t1, t1.Add(time.Second)); err != nil { + t.Fatal(err) + } + t1 = time.Now().Add(-10 * time.Minute) + if err = anz.logTrafic(0, utils.CoreSv1Status, "status", "result", "error", + utils.MetaJSON, "127.0.0.1:5565", "127.0.0.1:2012", t1, t1.Add(time.Second)); err != nil { + t.Fatal(err) + } + if cnt, err := anz.db.DocCount(); err != nil { + t.Fatal(err) + } else if cnt != 2 { + t.Errorf("Expected only 2 documents received:%v", cnt) + } + if err = anz.clenaUp(); err != nil { + t.Fatal(err) + } + if cnt, err := anz.db.DocCount(); err != nil { + t.Fatal(err) + } else if cnt != 1 { + t.Errorf("Expected only one document received:%v", cnt) + } + + if err = anz.db.Close(); err != nil { + t.Fatal(err) + } + if err = anz.clenaUp(); err != bleve.ErrorIndexClosed { + t.Errorf("Expected error: %v,received: %+v", bleve.ErrorIndexClosed, err) + } +} diff --git a/config/configsanity.go b/config/configsanity.go index 011a12e2b..7aa18dc69 100644 --- a/config/configsanity.go +++ b/config/configsanity.go @@ -1004,9 +1004,6 @@ func (cfg *CGRConfig) checkConfigSanity() error { } if cfg.analyzerSCfg.Enabled { - if _, err := os.Stat(cfg.analyzerSCfg.DBPath); err != nil && os.IsNotExist(err) { - return fmt.Errorf("<%s> nonexistent DB folder: %q", utils.AnalyzerS, cfg.analyzerSCfg.DBPath) - } if !utils.AnzIndexType.Has(cfg.analyzerSCfg.IndexType) { return fmt.Errorf("<%s> unsuported index type: %q", utils.AnalyzerS, cfg.analyzerSCfg.IndexType) } diff --git a/config/configsanity_test.go b/config/configsanity_test.go index f35f587d8..2a2650cd0 100644 --- a/config/configsanity_test.go +++ b/config/configsanity_test.go @@ -1567,11 +1567,6 @@ func TestConfigSanityAnalyzer(t *testing.T) { t.Errorf("Expecting: %+q received: %+q", expected, err) } - cfg.analyzerSCfg.DBPath = "/inexistent/Path" - expected = " nonexistent DB folder: \"/inexistent/Path\"" - if err := cfg.checkConfigSanity(); err == nil || err.Error() != expected { - t.Errorf("Expecting: %+q received: %+q", expected, err) - } cfg.analyzerSCfg.DBPath = "/" cfg.analyzerSCfg.IndexType = utils.MetaScorch diff --git a/services/analyzers.go b/services/analyzers.go index 66a378939..90dd68661 100644 --- a/services/analyzers.go +++ b/services/analyzers.go @@ -76,7 +76,6 @@ func (anz *AnalyzerService) Start(_ *context.Context, shtDwn context.CancelFunc) utils.Logger.Crit(fmt.Sprintf("<%s> Error: %s listening for packets", utils.AnalyzerS, err.Error())) shtDwn() } - return }(anz.anz) anz.server.SetAnalyzer(anz.anz) go anz.start() @@ -115,7 +114,7 @@ func (anz *AnalyzerService) Shutdown() (err error) { anz.server.SetAnalyzer(nil) anz.anz.Shutdown() anz.anz = nil - <-anz.connChan + // <-anz.connChan anz.Unlock() return } diff --git a/services/datadb.go b/services/datadb.go index ffb16c794..4e594b7fc 100644 --- a/services/datadb.go +++ b/services/datadb.go @@ -141,8 +141,8 @@ func (db *DataDBService) ServiceName() string { } // ShouldRun returns if the service should be running -func (db *DataDBService) ShouldRun() bool { - return db.mandatoryDB() || db.cfg.SessionSCfg().Enabled +func (db *DataDBService) ShouldRun() bool { // db should allways run + return true // ||db.mandatoryDB() || db.cfg.SessionSCfg().Enabled } // mandatoryDB returns if the current configuration needs the DB diff --git a/services/datadb_test.go b/services/datadb_test.go index c3f1219fb..cdfe17657 100644 --- a/services/datadb_test.go +++ b/services/datadb_test.go @@ -131,7 +131,7 @@ func TestDataDBCoverage(t *testing.T) { t.Errorf("\nExpecting <%+v>,\n Received <%+v>", utils.DataDB, serviceName) } shouldRun := db.ShouldRun() - if !reflect.DeepEqual(shouldRun, false) { - t.Errorf("\nExpecting ,\n Received <%+v>", shouldRun) + if !reflect.DeepEqual(shouldRun, true) { + t.Errorf("\nExpecting ,\n Received <%+v>", shouldRun) } }