Added TTL for analyzers

This commit is contained in:
Trial97
2020-10-26 18:20:57 +02:00
committed by Dan Christian Bogos
parent 08d4f1fc21
commit 6b60d49bf6
18 changed files with 159 additions and 74 deletions

View File

@@ -19,36 +19,53 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package config
import (
"path"
"time"
"github.com/blevesearch/bleve/index/store/boltdb"
"github.com/blevesearch/bleve/index/upsidedown"
"github.com/cgrates/cgrates/utils"
)
// AttributeSCfg is the configuration of attribute service
// AnalyzerSCfg is the configuration of analyzer service
type AnalyzerSCfg struct {
Enabled bool
DBPath string
IndexType string
StoreType string
Enabled bool
DBPath string
IndexType string
TTL time.Duration
CleanupInterval time.Duration
}
func (alS *AnalyzerSCfg) loadFromJsonCfg(jsnCfg *AnalyzerSJsonCfg) (err error) {
func (alS *AnalyzerSCfg) loadFromJSONCfg(jsnCfg *AnalyzerSJsonCfg) (err error) {
if jsnCfg == nil {
return
}
alS.DBPath = path.Join("/home", "trial", "analize")
alS.IndexType = upsidedown.Name
alS.StoreType = boltdb.Name
if jsnCfg.Enabled != nil {
alS.Enabled = *jsnCfg.Enabled
}
if jsnCfg.Db_path != nil {
alS.DBPath = *jsnCfg.Db_path
}
if jsnCfg.Index_type != nil {
alS.IndexType = *jsnCfg.Index_type
}
if jsnCfg.Ttl != nil {
if alS.TTL, err = time.ParseDuration(*jsnCfg.Ttl); err != nil {
return
}
}
if jsnCfg.Cleanup_interval != nil {
if alS.CleanupInterval, err = time.ParseDuration(*jsnCfg.Cleanup_interval); err != nil {
return
}
}
return nil
}
func (alS *AnalyzerSCfg) AsMapInterface() map[string]interface{} {
return map[string]interface{}{
utils.EnabledCfg: alS.Enabled,
utils.DBPathCfg: alS.DBPath,
utils.IndexTypeCfg: alS.IndexType,
utils.TTLCfg: alS.TTL.String(),
utils.CleanupIntervalCfg: alS.CleanupInterval.String(),
}
}

View File

@@ -33,7 +33,7 @@ func TestAnalyzerSCfgloadFromJsonCfg(t *testing.T) {
}
if jsnCfg, err := NewDefaultCGRConfig(); err != nil {
t.Error(err)
} else if err = jsnCfg.analyzerSCfg.loadFromJsonCfg(jsonCfg); err != nil {
} else if err = jsnCfg.analyzerSCfg.loadFromJSONCfg(jsonCfg); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(jsnCfg.analyzerSCfg, expected) {
t.Errorf("Expected %+v \n, received %+v", expected, jsnCfg.analyzerSCfg)

View File

@@ -734,7 +734,7 @@ func (cfg *CGRConfig) loadAnalyzerCgrCfg(jsnCfg *CgrJsonCfg) (err error) {
if jsnAnalyzerCgrCfg, err = jsnCfg.AnalyzerCfgJson(); err != nil {
return
}
return cfg.analyzerSCfg.loadFromJsonCfg(jsnAnalyzerCgrCfg)
return cfg.analyzerSCfg.loadFromJSONCfg(jsnAnalyzerCgrCfg)
}
// loadAPIBanCgrCfg loads the Analyzer section of the configuration

View File

@@ -905,7 +905,11 @@ const CGRATES_CFG_JSON = `
"analyzers":{ // AnalyzerS config
"enabled":false // starts AnalyzerS service: <true|false>.
"enabled": false, // starts AnalyzerS service: <true|false>.
"db_path": "/tmp/analyzers", // path to the folder where to store the information
"index_type": "*scorch", // the type of index for the storage: <*scorch|*boltdb|*leveldb|*mossdb>
"ttl": "10s", // time to wait before removing the API capture
"cleanup_interval": "1h", // the interval we clean the db
},

View File

@@ -110,7 +110,7 @@ func handleConfigSFile(path string, w http.ResponseWriter) {
func (cScfg *ConfigSCfg) AsMapInterface() (initialMP map[string]interface{}) {
initialMP = map[string]interface{}{
utils.EnabledCfg: cScfg.Enabled,
utils.UrlCfg: cScfg.Url,
utils.URLCfg: cScfg.Url,
utils.RootDirCfg: cScfg.RootDir,
}
return

View File

@@ -56,7 +56,7 @@ func TestConfigsAsMapInterface(t *testing.T) {
}`
eMap := map[string]interface{}{
utils.EnabledCfg: true,
utils.UrlCfg: "",
utils.URLCfg: "",
utils.RootDirCfg: "/var/spool/cgrates/configs",
}
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgsJSONStr); err != nil {
@@ -72,7 +72,7 @@ func TestConfigsAsMapInterface2(t *testing.T) {
}`
eMap := map[string]interface{}{
utils.EnabledCfg: false,
utils.UrlCfg: "/configs/",
utils.URLCfg: "/configs/",
utils.RootDirCfg: "/var/spool/cgrates/configs",
}
if cgrCfg, err := NewCGRConfigFromJSONStringWithDefaults(cfgsJSONStr); err != nil {

View File

@@ -130,7 +130,7 @@ func (ca *HttpAgentCfg) loadFromJsonCfg(jsnCfg *HttpAgentJsonCfg, separator stri
func (ca *HttpAgentCfg) AsMapInterface(separator string) (initialMP map[string]interface{}) {
initialMP = map[string]interface{}{
utils.IDCfg: ca.ID,
utils.UrlCfg: ca.Url,
utils.URLCfg: ca.Url,
utils.SessionSConnsCfg: ca.SessionSConns,
utils.RequestPayloadCfg: ca.RequestPayload,
utils.ReplyPayloadCfg: ca.ReplyPayload,

View File

@@ -534,7 +534,7 @@ func TestHttpAgentCfgAsMapInterface(t *testing.T) {
eMap := []map[string]interface{}{
{
utils.IdCfg: "conecto1",
utils.UrlCfg: "/conecto",
utils.URLCfg: "/conecto",
utils.SessionSConnsCfg: []string{"*localhost"},
utils.RequestPayloadCfg: "*url",
utils.ReplyPayloadCfg: "*xml",

View File

@@ -602,7 +602,11 @@ type FcTemplateJsonCfg struct {
// Analyzer service json config section
type AnalyzerSJsonCfg struct {
Enabled *bool
Enabled *bool
Db_path *string
Index_type *string
Ttl *string
Cleanup_interval *string
}
type ApierJsonCfg struct {

View File

@@ -177,7 +177,7 @@ func (self *SureTaxCfg) loadFromJsonCfg(jsnCfg *SureTaxJsonCfg) (err error) {
func (st *SureTaxCfg) AsMapInterface(separator string) (initialMP map[string]interface{}) {
initialMP = map[string]interface{}{
utils.UrlCfg: st.Url,
utils.URLCfg: st.Url,
utils.ClientNumberCfg: st.ClientNumber,
utils.ValidationKeyCfg: st.ValidationKey,
utils.BusinessUnitCfg: st.BusinessUnit,

View File

@@ -328,7 +328,7 @@ func TestSureTaxCfgAsMapInterface(t *testing.T) {
},
}`
eMap := map[string]interface{}{
utils.UrlCfg: utils.EmptyString,
utils.URLCfg: utils.EmptyString,
utils.ClientNumberCfg: utils.EmptyString,
utils.ValidationKeyCfg: utils.EmptyString,
utils.BusinessUnitCfg: utils.EmptyString,