diff --git a/apier/tptimings.go b/apier/tptimings.go index 84fb8a53b..a456f7c44 100644 --- a/apier/tptimings.go +++ b/apier/tptimings.go @@ -37,7 +37,7 @@ type ApierTPTiming struct { // Creates a new timing within a tariff plan func (self *Apier) SetTPTiming(attrs ApierTPTiming, reply *string) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid", "TimingId", "Years","Months","MonthDays", "WeekDays","Time"}); len(missing) != 0 { + if missing := utils.MissingStructFields(&attrs, []string{"TPid", "TimingId", "Years", "Months", "MonthDays", "WeekDays", "Time"}); len(missing) != 0 { return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } if exists, err := self.StorDb.ExistsTPTiming(attrs.TPid, attrs.TimingId); err != nil { @@ -45,7 +45,7 @@ func (self *Apier) SetTPTiming(attrs ApierTPTiming, reply *string) error { } else if exists { return errors.New(utils.ERR_DUPLICATE) } - tm := rater.NewTiming( attrs.TimingId, attrs.Years, attrs.Months, attrs.MonthDays, attrs.WeekDays, attrs.Time ) + tm := rater.NewTiming(attrs.TimingId, attrs.Years, attrs.Months, attrs.MonthDays, attrs.WeekDays, attrs.Time) if err := self.StorDb.SetTPTiming(attrs.TPid, tm); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } @@ -68,7 +68,7 @@ func (self *Apier) GetTPTiming(attrs AttrGetTPTiming, reply *ApierTPTiming) erro } else if tm == nil { return errors.New(utils.ERR_NOT_FOUND) } else { - *reply = ApierTPTiming{attrs.TPid, tm.Id, tm.Years.Serialize(";"), + *reply = ApierTPTiming{attrs.TPid, tm.Id, tm.Years.Serialize(";"), tm.Months.Serialize(";"), tm.MonthDays.Serialize(";"), tm.WeekDays.Serialize(";"), tm.StartTime} } return nil diff --git a/data/storage/mysql/create_tariffplan_tables.sql b/data/storage/mysql/create_tariffplan_tables.sql index f39f90b1d..8e1973d95 100644 --- a/data/storage/mysql/create_tariffplan_tables.sql +++ b/data/storage/mysql/create_tariffplan_tables.sql @@ -43,7 +43,8 @@ CREATE TABLE `tp_rates` ( `rate_increments` INT(11) NOT NULL, `weight` DECIMAL(5,2) NOT NULL, PRIMARY KEY (`id`), - KEY `tpid` (`tpid`) + KEY `tpid` (`tpid`), + UNIQUE KEY `tpid_rate_weight` (`tpid`,`rate`,`weight`) ); -- diff --git a/rater/loader_csv.go b/rater/loader_csv.go index a1f3fd70d..d6547c702 100644 --- a/rater/loader_csv.go +++ b/rater/loader_csv.go @@ -235,7 +235,7 @@ func (csvr *CSVReader) LoadRates() (err error) { continue } var r *Rate - r, err = NewRate(record[0], record[1], record[2], record[3], record[4]) + r, err = NewRate(record[0], record[1], record[2], record[3], record[4], record[5]) if err != nil { return err } diff --git a/rater/loader_helpers.go b/rater/loader_helpers.go index e5b6c044c..cc156f817 100644 --- a/rater/loader_helpers.go +++ b/rater/loader_helpers.go @@ -44,10 +44,10 @@ type TPLoader interface { type Rate struct { Tag string - ConnectFee, Price, PricedUnits, RateIncrements float64 + ConnectFee, Price, PricedUnits, RateIncrements, Weight float64 } -func NewRate(tag, connectFee, price, pricedUnits, rateIncrements string) (r *Rate, err error) { +func NewRate(tag, connectFee, price, pricedUnits, rateIncrements, weight string) (r *Rate, err error) { cf, err := strconv.ParseFloat(connectFee, 64) if err != nil { log.Printf("Error parsing connect fee from: %v", connectFee) @@ -68,12 +68,18 @@ func NewRate(tag, connectFee, price, pricedUnits, rateIncrements string) (r *Rat log.Printf("Error parsing rates increments from: %v", rateIncrements) return } + wght, err := strconv.ParseFloat(weight, 64) + if err != nil { + log.Printf("Error parsing rates increments from: %s", weight) + return + } r = &Rate{ Tag: tag, ConnectFee: cf, Price: p, PricedUnits: pu, RateIncrements: ri, + Weight: wght, } return } diff --git a/rater/storage_interface.go b/rater/storage_interface.go index 64c1755b5..f1d26a906 100644 --- a/rater/storage_interface.go +++ b/rater/storage_interface.go @@ -67,6 +67,7 @@ type DataStorage interface { ExistsTPDestination(string, string) (bool, error) GetTPDestination(string, string) (*Destination, error) GetTPDestinationIds(string) ([]string, error) + SetTPRate(string, *Rate) error // End Apier functions GetActions(string) (Actions, error) SetActions(string, Actions) error diff --git a/rater/storage_map.go b/rater/storage_map.go index b90b5171b..a724561d1 100644 --- a/rater/storage_map.go +++ b/rater/storage_map.go @@ -101,7 +101,6 @@ func (ms *MapStorage) ExistsTPDestination(tpid, destTag string) (bool, error) { return false, errors.New(utils.ERR_NOT_IMPLEMENTED) } -// Extracts destinations from StorDB on specific tariffplan id func (ms *MapStorage) GetTPDestination(tpid, destTag string) (*Destination, error) { return nil, nil } @@ -110,6 +109,10 @@ func (ms *MapStorage) GetTPDestinationIds(tpid string) ([]string, error) { return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) } +func (ms *MapStorage) SetTPRate(tpid string, rt *Rate) error { + return errors.New(utils.ERR_NOT_IMPLEMENTED) +} + func (ms *MapStorage) GetActions(key string) (as Actions, err error) { if values, ok := ms.dict[ACTION_PREFIX+key]; ok { err = ms.ms.Unmarshal(values, &as) diff --git a/rater/storage_mongo.go b/rater/storage_mongo.go index ec4b6696e..b7c71b17a 100644 --- a/rater/storage_mongo.go +++ b/rater/storage_mongo.go @@ -184,6 +184,10 @@ func (ms *MongoStorage) SetTPDestination(tpid string, dest *Destination) error { return errors.New(utils.ERR_NOT_IMPLEMENTED) } +func (ms *MongoStorage) SetTPRate(tpid string, rt *Rate) error { + return errors.New(utils.ERR_NOT_IMPLEMENTED) +} + func (ms *MongoStorage) GetActions(key string) (as Actions, err error) { result := AcKeyValue{} err = ms.db.C("actions").Find(bson.M{"key": key}).One(&result) diff --git a/rater/storage_redis.go b/rater/storage_redis.go index a1e516b00..7d3790396 100644 --- a/rater/storage_redis.go +++ b/rater/storage_redis.go @@ -139,6 +139,10 @@ func (rs *RedisStorage) SetTPDestination(tpid string, dest *Destination) error { return errors.New(utils.ERR_NOT_IMPLEMENTED) } +func (rs *RedisStorage) SetTPRate(tpid string, rt *Rate) error { + return errors.New(utils.ERR_NOT_IMPLEMENTED) +} + func (rs *RedisStorage) GetActions(key string) (as Actions, err error) { var values string if values, err = rs.db.Get(ACTION_PREFIX + key); err == nil { diff --git a/rater/storage_sql.go b/rater/storage_sql.go index 93fb4f3d3..661efd93e 100644 --- a/rater/storage_sql.go +++ b/rater/storage_sql.go @@ -22,6 +22,7 @@ import ( "database/sql" "encoding/json" "fmt" + //"errors" "github.com/cgrates/cgrates/utils" ) @@ -199,6 +200,13 @@ func (self *SQLStorage) SetTPDestination(tpid string, dest *Destination) error { return nil } +func (self *SQLStorage) SetTPRate(tpid string, rt *Rate) error { + if _, err := self.Db.Exec(fmt.Sprintf("INSERT INTO %s (tpid, tag, connect_fee, rate, rated_units, rate_increments, weight) VALUES ('%s', '%s', %f, %f, %d, %d, %f)", utils.TBL_TP_RATES, tpid, rt.Tag, rt.ConnectFee, rt.Price, int(rt.PricedUnits), int(rt.RateIncrements), rt.Weight)); err != nil { + return err + } + return nil +} + func (self *SQLStorage) GetActions(string) (as Actions, err error) { return }