mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
Changing DestRateTimings into RatingPlans
This commit is contained in:
@@ -28,41 +28,41 @@ import (
|
||||
)
|
||||
|
||||
// Creates a new DestinationRateTiming profile within a tariff plan
|
||||
func (self *ApierV1) SetTPDestRateTiming(attrs utils.TPDestRateTiming, reply *string) error {
|
||||
func (self *ApierV1) SetTPRatingPlan(attrs utils.TPRatingPlan, reply *string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestRateTimingId", "DestRateTimings"}); len(missing) != 0 {
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if exists, err := self.StorDb.ExistsTPDestRateTiming(attrs.TPid, attrs.DestRateTimingId); err != nil {
|
||||
if exists, err := self.StorDb.ExistsTPRatingPlan(attrs.TPid, attrs.RatingPlanId); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if exists {
|
||||
return errors.New(utils.ERR_DUPLICATE)
|
||||
}
|
||||
drts := make([]*engine.DestinationRateTiming, len(attrs.DestRateTimings))
|
||||
for idx, drt := range attrs.DestRateTimings {
|
||||
drts[idx] = &engine.DestinationRateTiming{Tag: attrs.DestRateTimingId,
|
||||
drts := make([]*engine.DestinationRateTiming, len(attrs.RatingPlans))
|
||||
for idx, drt := range attrs.RatingPlans {
|
||||
drts[idx] = &engine.DestinationRateTiming{Tag: attrs.RatingPlanId,
|
||||
DestinationRatesTag: drt.DestRatesId,
|
||||
Weight: drt.Weight,
|
||||
TimingTag: drt.TimingId,
|
||||
}
|
||||
}
|
||||
if err := self.StorDb.SetTPDestRateTimings(attrs.TPid, map[string][]*engine.DestinationRateTiming{attrs.DestRateTimingId: drts}); err != nil {
|
||||
if err := self.StorDb.SetTPRatingPlans(attrs.TPid, map[string][]*engine.DestinationRateTiming{attrs.RatingPlanId: drts}); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
}
|
||||
*reply = "OK"
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttrGetTPDestRateTiming struct {
|
||||
type AttrGetTPRatingPlan struct {
|
||||
TPid string // Tariff plan id
|
||||
DestRateTimingId string // Rate id
|
||||
RatingPlanId string // Rate id
|
||||
}
|
||||
|
||||
// Queries specific DestRateTiming profile on tariff plan
|
||||
func (self *ApierV1) GetTPDestRateTiming(attrs AttrGetTPDestRateTiming, reply *utils.TPDestRateTiming) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestRateTimingId"}); len(missing) != 0 { //Params missing
|
||||
// Queries specific RatingPlan profile on tariff plan
|
||||
func (self *ApierV1) GetTPRatingPlan(attrs AttrGetTPRatingPlan, reply *utils.TPRatingPlan) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "RatingPlanId"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if dr, err := self.StorDb.GetTPDestRateTiming(attrs.TPid, attrs.DestRateTimingId); err != nil {
|
||||
if dr, err := self.StorDb.GetTPRatingPlan(attrs.TPid, attrs.RatingPlanId); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if dr == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
@@ -72,16 +72,16 @@ func (self *ApierV1) GetTPDestRateTiming(attrs AttrGetTPDestRateTiming, reply *u
|
||||
return nil
|
||||
}
|
||||
|
||||
type AttrTPDestRateTimingIds struct {
|
||||
type AttrGetTPRatingPlanIds struct {
|
||||
TPid string // Tariff plan id
|
||||
}
|
||||
|
||||
// Queries DestRateTiming identities on specific tariff plan.
|
||||
func (self *ApierV1) GetTPDestRateTimingIds(attrs AttrGetTPRateIds, reply *[]string) error {
|
||||
// Queries RatingPlan identities on specific tariff plan.
|
||||
func (self *ApierV1) GetTPRatingPlanIds(attrs AttrGetTPRatingPlanIds, reply *[]string) error {
|
||||
if missing := utils.MissingStructFields(&attrs, []string{"TPid"}); len(missing) != 0 { //Params missing
|
||||
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
|
||||
}
|
||||
if ids, err := self.StorDb.GetTPDestRateTimingIds(attrs.TPid); err != nil {
|
||||
if ids, err := self.StorDb.GetTPRatingPlanIds(attrs.TPid); err != nil {
|
||||
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
|
||||
} else if ids == nil {
|
||||
return errors.New(utils.ERR_NOT_FOUND)
|
||||
|
||||
@@ -139,7 +139,7 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = loader.LoadDestinationRateTimings()
|
||||
err = loader.LoadRatingPlans()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -290,7 +290,7 @@ func (csvr *CSVReader) LoadDestinationRates() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (csvr *CSVReader) LoadDestinationRateTimings() (err error) {
|
||||
func (csvr *CSVReader) LoadRatingPlans() (err error) {
|
||||
csvReader, fp, err := csvr.readerFunc(csvr.destinationratetimingsFn, csvr.sep, utils.DESTRATE_TIMINGS_NRCOLS)
|
||||
if err != nil {
|
||||
log.Print("Could not load rate timings file: ", err)
|
||||
|
||||
@@ -127,7 +127,7 @@ func init() {
|
||||
csvr.LoadTimings()
|
||||
csvr.LoadRates()
|
||||
csvr.LoadDestinationRates()
|
||||
csvr.LoadDestinationRateTimings()
|
||||
csvr.LoadRatingPlans()
|
||||
csvr.LoadRatingProfiles()
|
||||
csvr.LoadActions()
|
||||
csvr.LoadActionTimings()
|
||||
|
||||
@@ -174,8 +174,8 @@ func (dbr *DbReader) LoadDestinationRates() (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dbr *DbReader) LoadDestinationRateTimings() error {
|
||||
drts, err := dbr.storDb.GetTpDestinationRateTimings(dbr.tpid, "")
|
||||
func (dbr *DbReader) LoadRatingPlans() error {
|
||||
drts, err := dbr.storDb.GetTpRatingPlans(dbr.tpid, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -237,7 +237,7 @@ func (dbr *DbReader) LoadRatingProfileByTag(tag string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot parse activation time from %v", ratingProfile.ActivationTime)
|
||||
}
|
||||
drtm, err := dbr.storDb.GetTpDestinationRateTimings(dbr.tpid, ratingProfile.DestRatesTimingTag)
|
||||
drtm, err := dbr.storDb.GetTpRatingPlans(dbr.tpid, ratingProfile.DestRatesTimingTag)
|
||||
if err != nil || len(drtm) == 0 {
|
||||
return fmt.Errorf("No DestRateTimings profile with id %s: %v", ratingProfile.DestRatesTimingTag, err)
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ type TPLoader interface {
|
||||
LoadRates() error
|
||||
LoadDestinationRates() error
|
||||
LoadTimings() error
|
||||
LoadDestinationRateTimings() error
|
||||
LoadRatingPlans() error
|
||||
LoadRatingProfiles() error
|
||||
LoadActions() error
|
||||
LoadActionTimings() error
|
||||
|
||||
@@ -116,10 +116,10 @@ type LoadStorage interface {
|
||||
SetTPDestinationRates(string, map[string][]*DestinationRate) error
|
||||
GetTPDestinationRate(string, string) (*utils.TPDestinationRate, error)
|
||||
GetTPDestinationRateIds(string) ([]string, error)
|
||||
ExistsTPDestRateTiming(string, string) (bool, error)
|
||||
SetTPDestRateTimings(string, map[string][]*DestinationRateTiming) error
|
||||
GetTPDestRateTiming(string, string) (*utils.TPDestRateTiming, error)
|
||||
GetTPDestRateTimingIds(string) ([]string, error)
|
||||
ExistsTPRatingPlan(string, string) (bool, error)
|
||||
SetTPRatingPlans(string, map[string][]*DestinationRateTiming) error
|
||||
GetTPRatingPlan(string, string) (*utils.TPRatingPlan, error)
|
||||
GetTPRatingPlanIds(string) ([]string, error)
|
||||
ExistsTPRatingProfile(string, string) (bool, error)
|
||||
SetTPRatingProfiles(string, map[string][]*RatingProfile) error
|
||||
GetTPRatingProfile(string, string) (*utils.TPRatingProfile, error)
|
||||
@@ -143,7 +143,7 @@ type LoadStorage interface {
|
||||
GetTpTimings(string, string) (map[string]*Timing, error)
|
||||
GetTpRates(string, string) (map[string][]*LoadRate, error)
|
||||
GetTpDestinationRates(string, string) (map[string][]*DestinationRate, error)
|
||||
GetTpDestinationRateTimings(string, string) ([]*DestinationRateTiming, error)
|
||||
GetTpRatingPlans(string, string) ([]*DestinationRateTiming, error)
|
||||
GetTpRatingProfiles(string, string) (map[string]*RatingProfile, error)
|
||||
GetTpActions(string, string) (map[string][]*Action, error)
|
||||
GetTpActionTimings(string, string) (map[string][]*ActionTiming, error)
|
||||
|
||||
@@ -42,7 +42,7 @@ func (self *SQLStorage) Flush() (err error) {
|
||||
// Return a list with all TPids defined in the system, even if incomplete, isolated in some table.
|
||||
func (self *SQLStorage) GetTPIds() ([]string, error) {
|
||||
rows, err := self.Db.Query(
|
||||
fmt.Sprintf("(SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s)", utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_RATES, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_DESTRATE_TIMINGS, utils.TBL_TP_RATE_PROFILES))
|
||||
fmt.Sprintf("(SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s) UNION (SELECT tpid FROM %s)", utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_RATES, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_RATING_PLANS, utils.TBL_TP_RATE_PROFILES))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -343,20 +343,20 @@ func (self *SQLStorage) GetTPDestinationRateIds(tpid string) ([]string, error) {
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) ExistsTPDestRateTiming(tpid, drtId string) (bool, error) {
|
||||
func (self *SQLStorage) ExistsTPRatingPlan(tpid, drtId string) (bool, error) {
|
||||
var exists bool
|
||||
err := self.Db.QueryRow(fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM %s WHERE tpid='%s' AND tag='%s')", utils.TBL_TP_DESTRATE_TIMINGS, tpid, drtId)).Scan(&exists)
|
||||
err := self.Db.QueryRow(fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM %s WHERE tpid='%s' AND tag='%s')", utils.TBL_TP_RATING_PLANS, tpid, drtId)).Scan(&exists)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) SetTPDestRateTimings(tpid string, drts map[string][]*DestinationRateTiming) error {
|
||||
func (self *SQLStorage) SetTPRatingPlans(tpid string, drts map[string][]*DestinationRateTiming) error {
|
||||
if len(drts) == 0 {
|
||||
return nil //Nothing to set
|
||||
}
|
||||
qry := fmt.Sprintf("INSERT INTO %s (tpid, tag, destrates_tag, timing_tag, weight) VALUES ", utils.TBL_TP_DESTRATE_TIMINGS)
|
||||
qry := fmt.Sprintf("INSERT INTO %s (tpid, tag, destrates_tag, timing_tag, weight) VALUES ", utils.TBL_TP_RATING_PLANS)
|
||||
i := 0
|
||||
for drtId, drtRows := range drts {
|
||||
for _, drt := range drtRows {
|
||||
@@ -374,13 +374,13 @@ func (self *SQLStorage) SetTPDestRateTimings(tpid string, drts map[string][]*Des
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) GetTPDestRateTiming(tpid, drtId string) (*utils.TPDestRateTiming, error) {
|
||||
rows, err := self.Db.Query(fmt.Sprintf("SELECT destrates_tag, timing_tag, weight from %s where tpid='%s' and tag='%s'", utils.TBL_TP_DESTRATE_TIMINGS, tpid, drtId))
|
||||
func (self *SQLStorage) GetTPRatingPlan(tpid, drtId string) (*utils.TPRatingPlan, error) {
|
||||
rows, err := self.Db.Query(fmt.Sprintf("SELECT destrates_tag, timing_tag, weight from %s where tpid='%s' and tag='%s'", utils.TBL_TP_RATING_PLANS, tpid, drtId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
drt := &utils.TPDestRateTiming{TPid: tpid, DestRateTimingId: drtId}
|
||||
drt := &utils.TPRatingPlan{TPid: tpid, RatingPlanId: drtId}
|
||||
i := 0
|
||||
for rows.Next() {
|
||||
i++ //Keep here a reference so we know we got at least one result
|
||||
@@ -390,7 +390,7 @@ func (self *SQLStorage) GetTPDestRateTiming(tpid, drtId string) (*utils.TPDestRa
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
drt.DestRateTimings = append(drt.DestRateTimings, utils.DestRateTiming{drTag, timingTag, weight})
|
||||
drt.RatingPlans = append(drt.RatingPlans, utils.RatingPlan{drTag, timingTag, weight})
|
||||
}
|
||||
if i == 0 {
|
||||
return nil, nil
|
||||
@@ -398,8 +398,8 @@ func (self *SQLStorage) GetTPDestRateTiming(tpid, drtId string) (*utils.TPDestRa
|
||||
return drt, nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) GetTPDestRateTimingIds(tpid string) ([]string, error) {
|
||||
rows, err := self.Db.Query(fmt.Sprintf("SELECT DISTINCT tag FROM %s where tpid='%s'", utils.TBL_TP_DESTRATE_TIMINGS, tpid))
|
||||
func (self *SQLStorage) GetTPRatingPlanIds(tpid string) ([]string, error) {
|
||||
rows, err := self.Db.Query(fmt.Sprintf("SELECT DISTINCT tag FROM %s where tpid='%s'", utils.TBL_TP_RATING_PLANS, tpid))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -907,7 +907,7 @@ func (self *SQLStorage) GetRatedCdrs(timeStart, timeEnd time.Time) ([]utils.CDR,
|
||||
var answerTimestamp, duration int64
|
||||
var cost float64
|
||||
var extraFieldsMp map[string]string
|
||||
if err := rows.Scan(&cgrid, &accid, &cdrhost, &reqtype, &direction, &tenant, &tor, &account, &subject, &destination, &answerTimestamp, &duration, &extraFields,&cost); err!=nil {
|
||||
if err := rows.Scan(&cgrid, &accid, &cdrhost, &reqtype, &direction, &tenant, &tor, &account, &subject, &destination, &answerTimestamp, &duration, &extraFields, &cost); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
answerTime := time.Unix(answerTimestamp, 0)
|
||||
@@ -915,15 +915,15 @@ func (self *SQLStorage) GetRatedCdrs(timeStart, timeEnd time.Time) ([]utils.CDR,
|
||||
return nil, err
|
||||
}
|
||||
storCdr := &utils.RatedCDR{
|
||||
CgrId:cgrid, AccId:accid, CdrHost:cdrhost, ReqType:reqtype, Direction:direction, Tenant:tenant,
|
||||
TOR:tor, Account:account, Subject:subject, Destination:destination, AnswerTime:answerTime, Duration:duration,
|
||||
ExtraFields: extraFieldsMp, Cost:cost,
|
||||
}
|
||||
CgrId: cgrid, AccId: accid, CdrHost: cdrhost, ReqType: reqtype, Direction: direction, Tenant: tenant,
|
||||
TOR: tor, Account: account, Subject: subject, Destination: destination, AnswerTime: answerTime, Duration: duration,
|
||||
ExtraFields: extraFieldsMp, Cost: cost,
|
||||
}
|
||||
cdrs = append(cdrs, utils.CDR(storCdr))
|
||||
}
|
||||
return cdrs, nil
|
||||
}
|
||||
|
||||
|
||||
func (self *SQLStorage) GetTpDestinations(tpid, tag string) ([]*Destination, error) {
|
||||
var dests []*Destination
|
||||
q := fmt.Sprintf("SELECT * FROM %s WHERE tpid='%s'", utils.TBL_TP_DESTINATIONS, tpid)
|
||||
@@ -1047,9 +1047,9 @@ func (self *SQLStorage) GetTpTimings(tpid, tag string) (map[string]*Timing, erro
|
||||
return tms, nil
|
||||
}
|
||||
|
||||
func (self *SQLStorage) GetTpDestinationRateTimings(tpid, tag string) ([]*DestinationRateTiming, error) {
|
||||
func (self *SQLStorage) GetTpRatingPlans(tpid, tag string) ([]*DestinationRateTiming, error) {
|
||||
var rts []*DestinationRateTiming
|
||||
q := fmt.Sprintf("SELECT * FROM %s WHERE tpid='%s'", utils.TBL_TP_DESTRATE_TIMINGS, tpid)
|
||||
q := fmt.Sprintf("SELECT * FROM %s WHERE tpid='%s'", utils.TBL_TP_RATING_PLANS, tpid)
|
||||
if tag != "" {
|
||||
q += fmt.Sprintf(" AND tag='%s'", tag)
|
||||
}
|
||||
|
||||
@@ -43,8 +43,8 @@ var fileHandlers = map[string]func(*TPCSVImporter, string) error{
|
||||
utils.DESTINATIONS_CSV: (*TPCSVImporter).importDestinations,
|
||||
utils.RATES_CSV: (*TPCSVImporter).importRates,
|
||||
utils.DESTINATION_RATES_CSV: (*TPCSVImporter).importDestinationRates,
|
||||
utils.RATING_PLANS_CSV: (*TPCSVImporter).importRatingPlans,
|
||||
utils.RATING_PROFILES_CSV: (*TPCSVImporter).importRatingProfiles,
|
||||
utils.RATING_PLANS_CSV: (*TPCSVImporter).importRatingPlans,
|
||||
utils.RATING_PROFILES_CSV: (*TPCSVImporter).importRatingProfiles,
|
||||
utils.ACTIONS_CSV: (*TPCSVImporter).importActions,
|
||||
utils.ACTION_TIMINGS_CSV: (*TPCSVImporter).importActionTimings,
|
||||
utils.ACTION_TRIGGERS_CSV: (*TPCSVImporter).importActionTriggers,
|
||||
@@ -210,7 +210,7 @@ func (self *TPCSVImporter) importRatingPlans(fn string) error {
|
||||
Weight: weight,
|
||||
TimingTag: record[2],
|
||||
}
|
||||
if err := self.StorDb.SetTPDestRateTimings(self.TPid, map[string][]*DestinationRateTiming{drt.Tag: []*DestinationRateTiming{drt}}); err != nil {
|
||||
if err := self.StorDb.SetTPRatingPlans(self.TPid, map[string][]*DestinationRateTiming{drt.Tag: []*DestinationRateTiming{drt}}); err != nil {
|
||||
if self.Verbose {
|
||||
log.Printf("Ignoring line %d, storDb operational error: <%s> ", lineNr, err.Error())
|
||||
}
|
||||
|
||||
@@ -47,13 +47,13 @@ type DestinationRate struct {
|
||||
RateId string // The rate identity
|
||||
}
|
||||
|
||||
type TPDestRateTiming struct {
|
||||
type TPRatingPlan struct {
|
||||
TPid string // Tariff plan id
|
||||
DestRateTimingId string // DestinationRate profile id
|
||||
DestRateTimings []DestRateTiming // Set of destinationid-rateid bindings
|
||||
RatingPlanId string // RatingPlan profile id
|
||||
RatingPlans []RatingPlan // Set of destinationid-rateid bindings
|
||||
}
|
||||
|
||||
type DestRateTiming struct {
|
||||
type RatingPlan struct {
|
||||
DestRatesId string // The DestinationRate identity
|
||||
TimingId string // The timing identity
|
||||
Weight float64 // Binding priority taken into consideration when more DestinationRates are active on a time slot
|
||||
|
||||
@@ -23,7 +23,7 @@ const (
|
||||
TBL_TP_DESTINATIONS = "tp_destinations"
|
||||
TBL_TP_RATES = "tp_rates"
|
||||
TBL_TP_DESTINATION_RATES = "tp_destination_rates"
|
||||
TBL_TP_DESTRATE_TIMINGS = "tp_destrate_timings"
|
||||
TBL_TP_RATING_PLANS = "tp_rating_plans"
|
||||
TBL_TP_RATE_PROFILES = "tp_rating_profiles"
|
||||
TBL_TP_ACTIONS = "tp_actions"
|
||||
TBL_TP_ACTION_TIMINGS = "tp_action_timings"
|
||||
|
||||
Reference in New Issue
Block a user