func (tpr *TpReader) LoadDerivedChargersFiltered(filter *TpDerivedCharger, save bool) (err error) { tps, err := tpr.lr.GetTpDerivedChargers(filter) if err != nil { return err } storDcs, err := TpDerivedChargers(tps).GetDerivedChargers() if err != nil { return err } for _, tpDcs := range storDcs { tag := tpDcs.GetDerivedChargersKey() if _, hasIt := tpr.derivedChargers[tag]; !hasIt { tpr.derivedChargers[tag] = make(utils.DerivedChargers, 0) // Load object map since we use this method also from LoadDerivedChargers } for _, tpDc := range tpDcs.DerivedChargers { dc, err := utils.NewDerivedCharger(tpDc.RunId, tpDc.RunFilters, tpDc.ReqTypeField, tpDc.DirectionField, tpDc.TenantField, tpDc.CategoryField, tpDc.AccountField, tpDc.SubjectField, tpDc.DestinationField, tpDc.SetupTimeField, tpDc.PddField, tpDc.AnswerTimeField, tpDc.UsageField, tpDc.SupplierField, tpDc.DisconnectCauseField, tpDc.RatedField, tpDc.CostField) if err != nil { return err } tpr.derivedChargers[tag] = append(tpr.derivedChargers[tag], dc) } } if save { for dcsKey, dcs := range tpr.derivedChargers { if err := tpr.ratingStorage.SetDerivedChargers(dcsKey, dcs); err != nil { return err } } } return nil }
func (dbr *DbReader) LoadDerivedChargersFiltered(filter *utils.TPDerivedChargers) (err error) { tpDcses, err := dbr.storDb.GetTpDerivedChargers(filter) if err != nil { return err } allDcs := make(map[string]utils.DerivedChargers) // We load in map first so we can pre-process data for errors for _, tpDcs := range tpDcses { tag := tpDcs.GetDerivedChargersKey() if _, hasIt := allDcs[tag]; !hasIt { allDcs[tag] = make(utils.DerivedChargers, 0) } for _, tpDc := range tpDcs.DerivedChargers { if dc, err := utils.NewDerivedCharger(tpDc.RunId, tpDc.RunFilters, tpDc.ReqTypeField, tpDc.DirectionField, tpDc.TenantField, tpDc.CategoryField, tpDc.AccountField, tpDc.SubjectField, tpDc.DestinationField, tpDc.SetupTimeField, tpDc.AnswerTimeField, tpDc.UsageField); err != nil { return err } else { allDcs[tag] = append(allDcs[tag], dc) } } } for dcsKey, dcs := range allDcs { if err := dbr.accountDb.SetDerivedChargers(dcsKey, dcs); err != nil { return err } } return nil // Placeholder for now }
func TestParseCfgDerivedChargingDn1(t *testing.T) { eFieldsCfg := []byte(`[derived_charging] run_ids = run1, run2 run_filters =~account:s/^\w+[mpls]\d{6}$//,~account:s/^0\d{9}$//;^account/value/ reqtype_fields = test1, test2 direction_fields = test1, test2 tenant_fields = test1, test2 category_fields = test1, test2 account_fields = test1, test2 subject_fields = test1, test2 destination_fields = test1, test2 setup_time_fields = test1, test2 answer_time_fields = test1, test2 usage_fields = test1, test2 `) eDcs := make(utils.DerivedChargers, 2) if dc, err := utils.NewDerivedCharger("run1", `~account:s/^\w+[mpls]\d{6}$//`, "test1", "test1", "test1", "test1", "test1", "test1", "test1", "test1", "test1", "test1"); err != nil { t.Error("Unexpected error: ", err) } else { eDcs[0] = dc } if dc, err := utils.NewDerivedCharger("run2", `~account:s/^0\d{9}$//;^account/value/`, "test2", "test2", "test2", "test2", "test2", "test2", "test2", "test2", "test2", "test2"); err != nil { t.Error("Unexpected error: ", err) } else { eDcs[1] = dc } if cfg, err := NewCGRConfigFromBytes(eFieldsCfg); err != nil { t.Error("Could not parse the config", err.Error()) } else if !reflect.DeepEqual(cfg.DerivedChargers, eDcs) { dcsJson, _ := json.Marshal(cfg.DerivedChargers) t.Errorf("Received: %s", string(dcsJson)) } }
// Parse the configuration file and returns utils.DerivedChargers instance if no errors func ParseCfgDerivedCharging(c *conf.ConfigFile) (dcs utils.DerivedChargers, err error) { var runIds, runFilters, reqTypeFlds, directionFlds, tenantFlds, torFlds, acntFlds, subjFlds, dstFlds, sTimeFlds, aTimeFlds, durFlds []string cfgVal, _ := c.GetString("derived_charging", "run_ids") if runIds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "run_filters") if runFilters, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "reqtype_fields") if reqTypeFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "direction_fields") if directionFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "tenant_fields") if tenantFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "category_fields") if torFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "account_fields") if acntFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "subject_fields") if subjFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "destination_fields") if dstFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "setup_time_fields") if sTimeFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "answer_time_fields") if aTimeFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } cfgVal, _ = c.GetString("derived_charging", "usage_fields") if durFlds, err = ConfigSlice(cfgVal); err != nil { return nil, err } // We need all to be the same length if len(runFilters) != len(runIds) || len(reqTypeFlds) != len(runIds) || len(directionFlds) != len(runIds) || len(tenantFlds) != len(runIds) || len(torFlds) != len(runIds) || len(acntFlds) != len(runIds) || len(subjFlds) != len(runIds) || len(dstFlds) != len(runIds) || len(sTimeFlds) != len(runIds) || len(aTimeFlds) != len(runIds) || len(durFlds) != len(runIds) { return nil, errors.New("<ConfigSanity> Inconsistent fields length in derivated_charging section") } // Create the individual chargers and append them to the final instance dcs = make(utils.DerivedChargers, 0) if len(runIds) == 1 && len(runIds[0]) == 0 { // Avoid iterating on empty runid return dcs, nil } for runIdx, runId := range runIds { dc, err := utils.NewDerivedCharger(runId, runFilters[runIdx], reqTypeFlds[runIdx], directionFlds[runIdx], tenantFlds[runIdx], torFlds[runIdx], acntFlds[runIdx], subjFlds[runIdx], dstFlds[runIdx], sTimeFlds[runIdx], aTimeFlds[runIdx], durFlds[runIdx]) if err != nil { return nil, err } if dcs, err = dcs.Append(dc); err != nil { return nil, err } } return dcs, nil }