From 6476c036b10bfb205e822790caa999cdb02477cf Mon Sep 17 00:00:00 2001 From: Trial97 Date: Tue, 14 Sep 2021 16:44:44 +0300 Subject: [PATCH] Added support for in memory db for analyzers --- analyzers/analyzers.go | 4 ++++ analyzers/analyzers_test.go | 45 +++++++++++++++++++++++++++++++++++++ config/configsanity.go | 3 --- config/configsanity_test.go | 5 ----- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/analyzers/analyzers.go b/analyzers/analyzers.go index 1d86c3971..2a2fd4bb9 100644 --- a/analyzers/analyzers.go +++ b/analyzers/analyzers.go @@ -59,6 +59,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 8dd15a7fe..193062a62 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 e577fdf10..8e173051b 100644 --- a/config/configsanity.go +++ b/config/configsanity.go @@ -1075,9 +1075,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 669296427..4402317ef 100644 --- a/config/configsanity_test.go +++ b/config/configsanity_test.go @@ -1692,11 +1692,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