Implemented Apier.SetTPRate method

This commit is contained in:
DanB
2013-07-10 08:13:09 +02:00
parent b5092ebabe
commit 60bd0c0a08
9 changed files with 35 additions and 8 deletions

View File

@@ -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

View File

@@ -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`)
);
--

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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
}