Adding remove method on tptimings, modified SetTPTiming to update on exists

This commit is contained in:
DanB
2013-11-15 12:00:52 +01:00
parent c2af82e8b0
commit 751ec1e4c3
6 changed files with 39 additions and 18 deletions

View File

@@ -40,11 +40,6 @@ func (self *ApierV1) SetTPTiming(attrs ApierTPTiming, reply *string) error {
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 {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else if exists {
return errors.New(utils.ERR_DUPLICATE)
}
tm := engine.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())
@@ -92,3 +87,18 @@ func (self *ApierV1) GetTPTimingIds(attrs AttrGetTPTimingIds, reply *[]string) e
}
return nil
}
// Removes specific Timing on Tariff plan
func (self *ApierV1) RemTPTiming(attrs AttrGetTPTiming, reply *string) error {
if missing := utils.MissingStructFields(&attrs, []string{"TPid", "TimingId"}); len(missing) != 0 { //Params missing
return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing)
}
if err := self.StorDb.RemTPTiming(attrs.TPid, attrs.TimingId); err != nil {
return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error())
} else {
*reply = "OK"
}
return nil
}

View File

@@ -189,9 +189,9 @@ func (dbr *DbReader) LoadRatingPlans() error {
return errors.New(fmt.Sprintf("Could not get timing for tag %v", drt.TimingId))
}
drt.Timing = t
drs, exists := dbr.destinationRates[drt.DestRatesId]
drs, exists := dbr.destinationRates[drt.DestinationRatesId]
if !exists {
return errors.New(fmt.Sprintf("Could not find destination rate for tag %v", drt.DestRatesId))
return errors.New(fmt.Sprintf("Could not find destination rate for tag %v", drt.DestinationRatesId))
}
plan, exists := dbr.ratingPlans[drts.RatingPlanId]
@@ -250,11 +250,11 @@ func (dbr *DbReader) LoadRatingPlanByTag(tag string) error {
return fmt.Errorf("No Timings profile with id %s: %v", rp.TimingId, err)
}
rp.Timing = tm[rp.TimingId]
drm, err := dbr.storDb.GetTpDestinationRates(dbr.tpid, rp.DestRatesId)
drm, err := dbr.storDb.GetTpDestinationRates(dbr.tpid, rp.DestinationRatesId)
if err != nil || len(drm) == 0 {
return fmt.Errorf("No DestinationRates profile with id %s: %v", rp.DestRatesId, err)
return fmt.Errorf("No DestinationRates profile with id %s: %v", rp.DestinationRatesId, err)
}
for _, drate := range drm[rp.DestRatesId].DestinationRates {
for _, drate := range drm[rp.DestinationRatesId].DestinationRates {
Logger.Debug(fmt.Sprintf("Destination rate: %v", drate))
rt, err := dbr.storDb.GetTpRates(dbr.tpid, drate.RateId)
if err != nil || len(rt) == 0 {

View File

@@ -105,6 +105,7 @@ type LoadStorage interface {
ExistsTPTiming(string, string) (bool, error)
GetTPTiming(string, string) (*utils.TPTiming, error)
GetTPTimingIds(string) ([]string, error)
RemTPTiming(string, string) error
SetTPDestination(string, *Destination) error
ExistsTPDestination(string, string) (bool, error)
GetTPDestination(string, string) (*Destination, error)

View File

@@ -65,7 +65,7 @@ func (self *SQLStorage) GetTPIds() ([]string, error) {
}
func (self *SQLStorage) SetTPTiming(tpid string, tm *utils.TPTiming) error {
if _, err := self.Db.Exec(fmt.Sprintf("INSERT INTO %s (tpid, tag, years, months, month_days, week_days, time) VALUES('%s','%s','%s','%s','%s','%s','%s')",
if _, err := self.Db.Exec(fmt.Sprintf("INSERT INTO %s (tpid, tag, years, months, month_days, week_days, time) VALUES('%s','%s','%s','%s','%s','%s','%s') ON DUPLICATE KEY UPDATE years=values(years), months=values(months), month_days=values(month_days), week_days=values(week_days), time=values(time)",
utils.TBL_TP_TIMINGS, tpid, tm.Id, tm.Years.Serialize(";"), tm.Months.Serialize(";"), tm.MonthDays.Serialize(";"),
tm.WeekDays.Serialize(";"), tm.StartTime)); err != nil {
return err
@@ -118,6 +118,16 @@ func (self *SQLStorage) GetTPTimingIds(tpid string) ([]string, error) {
return ids, nil
}
func (self *SQLStorage) RemTPTiming(tpid, tag string) error {
q := fmt.Sprintf("DELETE FROM %s WHERE tpid='%s' AND tag='%s'", utils.TBL_TP_TIMINGS, tpid, tag)
if _, err := self.Db.Exec(q); err != nil {
return err
}
return nil
}
// Extracts destinations from StorDB on specific tariffplan id
func (self *SQLStorage) GetTPDestinationIds(tpid string) ([]string, error) {
rows, err := self.Db.Query(fmt.Sprintf("SELECT DISTINCT tag FROM %s where tpid='%s'", utils.TBL_TP_DESTINATIONS, tpid))
@@ -363,7 +373,7 @@ func (self *SQLStorage) SetTPRatingPlans(tpid string, drts map[string][]*utils.R
qry += ","
}
qry += fmt.Sprintf("('%s','%s','%s','%s',%f)",
tpid, drtId, drt.DestRatesId, drt.TimingId, drt.Weight)
tpid, drtId, drt.DestinationRatesId, drt.TimingId, drt.Weight)
i++
}
}
@@ -1084,7 +1094,7 @@ func (self *SQLStorage) GetTpRatingPlans(tpid, tag string) (*utils.TPRatingPlan,
return nil, err
}
rt := &utils.RatingPlan{
DestRatesId: destination_rates_tag,
DestinationRatesId: destination_rates_tag,
Weight: weight,
TimingId: timings_tag,
}

View File

@@ -213,7 +213,7 @@ func (self *TPCSVImporter) importRatingPlans(fn string) error {
}
drt := []*utils.RatingPlan{
&utils.RatingPlan{
DestRatesId: record[1],
DestinationRatesId: record[1],
Weight: weight,
TimingId: record[2],
},

View File

@@ -68,10 +68,10 @@ type TPRatingPlan struct {
}
type RatingPlan struct {
DestRatesId string // The DestinationRate identity
DestinationRatesId 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
Timing *TPTiming
Timing *TPTiming //?
}
type TPRatingProfile struct {
@@ -91,7 +91,7 @@ type TPRatingProfile struct {
type RatingActivation struct {
ActivationTime string // Time when this profile will become active, defined as unix epoch time
DestRateTimingId string // Id of DestRateTiming profile
// FallbackKeys []string
// FallbackKeys []string //??
}
type AttrTPRatingProfileIds struct {
@@ -152,7 +152,7 @@ type ApiActionTrigger struct {
type ApiTPAccountActions struct {
TPid string // Tariff plan id
AccountActionsId string // AccountActions id
AccountActionsId string // AccountActions id, used to group actions on a load
Tenant string // Tenant's Id
Account string // Account name
Direction string // Traffic direction