some name refactoring

This commit is contained in:
Radu Ioan Fericean
2013-08-07 15:37:17 +03:00
parent cb20ae167f
commit 305dca0fc9
10 changed files with 81 additions and 102 deletions

View File

@@ -41,9 +41,9 @@ CREATE TABLE `tp_rates` (
`tag` varchar(24) NOT NULL,
`connect_fee` decimal(5,4) NOT NULL,
`rate` decimal(5,4) NOT NULL,
`rated_units` int(11) NOT NULL,
`rate_increments` int(11) NOT NULL,
`group_interval` int(11) NOT NULL,
`rate_unit` int(11) NOT NULL,
`rate_increment` int(11) NOT NULL,
`group_interval_start` int(11) NOT NULL,
`rounding_method` varchar(255) NOT NULL,
`rounding_decimals` tinyint(4) NOT NULL,
`weight` decimal(5,2) NOT NULL,

View File

@@ -1,4 +1,4 @@
#Tag,ConnectFee,Rate,RatedUnits,RateIncrements,GroupInterval,RoundingMethod,RoundingDecimals,Weight
#Tag,ConnectFee,Rate,RateUnit,RateIncrement,GroupIntervalStart,RoundingMethod,RoundingDecimals,Weight
LANDLINE_PEAK,0.02,0.02,60s,60s,0,*up,4,10
LANDLINE_PEAK,0.02,0.01,1s,1s,60s,*up,4,10
MOBILE_PEAK,0.02,0.14,60s,60s,0,*up,4,10
1 #Tag ConnectFee Rate RatedUnits RateUnit RateIncrements RateIncrement GroupInterval GroupIntervalStart RoundingMethod RoundingDecimals Weight
2 LANDLINE_PEAK 0.02 0.02 60s 60s 60s 60s 0 0 *up 4 10
3 LANDLINE_PEAK 0.02 0.01 1s 1s 1s 1s 60s 60s *up 4 10
4 MOBILE_PEAK 0.02 0.14 60s 60s 60s 60s 0 0 *up 4 10

File diff suppressed because one or more lines are too long

View File

@@ -45,14 +45,14 @@ type Interval struct {
}
type Price struct {
StartSecond time.Duration
Value float64
RateIncrements time.Duration
RatedUnits time.Duration
GroupIntervalStart time.Duration
Value float64
RateIncrement time.Duration
RateUnit time.Duration
}
func (p *Price) Equal(o *Price) bool {
return p.StartSecond == o.StartSecond && p.Value == o.Value && p.RateIncrements == o.RateIncrements
return p.GroupIntervalStart == o.GroupIntervalStart && p.Value == o.Value && p.RateIncrement == o.RateIncrement && p.RateUnit == o.RateUnit
}
type PriceGroups []*Price
@@ -66,7 +66,7 @@ func (pg PriceGroups) Swap(i, j int) {
}
func (pg PriceGroups) Less(i, j int) bool {
return pg[i].StartSecond < pg[j].StartSecond
return pg[i].GroupIntervalStart < pg[j].GroupIntervalStart
}
func (pg PriceGroups) Sort() {
@@ -195,53 +195,30 @@ func (i *Interval) Equal(o *Interval) bool {
}
func (i *Interval) GetCost(duration, startSecond time.Duration) (cost float64) {
price := i.GetPrice(startSecond)
rateIncrements := i.GetRateIncrements(startSecond).Seconds()
price, rateIncrement, rateUnit := i.GetPriceParameters(startSecond)
d := float64(duration.Seconds())
ratedUnits := i.GetRatedUnits(startSecond)
price /= ratedUnits.Seconds()
cost = math.Ceil(d/rateIncrements) * rateIncrements * price
price /= rateUnit.Seconds()
ri := rateIncrement.Seconds()
cost = math.Ceil(d/ri) * ri * price
return utils.Round(cost, i.RoundingDecimals, i.RoundingMethod)
}
// Gets the price for a the provided start second
func (i *Interval) GetPrice(startSecond time.Duration) float64 {
func (i *Interval) GetPriceParameters(startSecond time.Duration) (price float64, rateIncrement, rateUnit time.Duration) {
i.Prices.Sort()
for index, price := range i.Prices {
if price.StartSecond <= startSecond && (index == len(i.Prices)-1 ||
i.Prices[index+1].StartSecond > startSecond) {
return price.Value
}
}
return -1
}
func (i *Interval) GetRateIncrements(startSecond time.Duration) time.Duration {
i.Prices.Sort()
for index, price := range i.Prices {
if price.StartSecond <= startSecond && (index == len(i.Prices)-1 ||
i.Prices[index+1].StartSecond > startSecond) {
if price.RateIncrements == 0 {
price.RateIncrements = 1 * time.Second
if price.GroupIntervalStart <= startSecond && (index == len(i.Prices)-1 ||
i.Prices[index+1].GroupIntervalStart > startSecond) {
if price.RateIncrement == 0 {
price.RateIncrement = 1 * time.Second
}
return price.RateIncrements
}
}
return 1
}
func (i *Interval) GetRatedUnits(startSecond time.Duration) time.Duration {
i.Prices.Sort()
for index, price := range i.Prices {
if price.StartSecond <= startSecond && (index == len(i.Prices)-1 ||
i.Prices[index+1].StartSecond > startSecond) {
if price.RatedUnits == 0 {
price.RatedUnits = 1 * time.Second
if price.RateUnit == 0 {
price.RateUnit = 1 * time.Second
}
return price.RatedUnits
return price.Value, price.RateIncrement, price.RateUnit
}
}
return -1
return -1, -1, -1
}
// Structure to store intervals according to weight

View File

@@ -47,12 +47,12 @@ type TPLoader interface {
}
type Rate struct {
Tag string
ConnectFee, Price float64
RatedUnits, RateIncrements, GroupInterval time.Duration
RoundingMethod string
RoundingDecimals int
Weight float64
Tag string
ConnectFee, Price float64
RateUnit, RateIncrement, GroupIntervalStart time.Duration
RoundingMethod string
RoundingDecimals int
Weight float64
}
func NewRate(tag, connectFee, price, ratedUnits, rateIncrements, groupInterval, roundingMethod, roundingDecimals, weight string) (r *Rate, err error) {
@@ -93,15 +93,15 @@ func NewRate(tag, connectFee, price, ratedUnits, rateIncrements, groupInterval,
}
r = &Rate{
Tag: tag,
ConnectFee: cf,
Price: p,
GroupInterval: gi,
RatedUnits: ru,
RateIncrements: ri,
Weight: wght,
RoundingMethod: roundingMethod,
RoundingDecimals: rd,
Tag: tag,
ConnectFee: cf,
Price: p,
GroupIntervalStart: gi,
RateUnit: ru,
RateIncrement: ri,
Weight: wght,
RoundingMethod: roundingMethod,
RoundingDecimals: rd,
}
return
}
@@ -165,10 +165,10 @@ func (rt *DestinationRateTiming) GetInterval(dr *DestinationRate) (i *Interval)
Weight: rt.Weight,
ConnectFee: dr.Rate.ConnectFee,
Prices: PriceGroups{&Price{
StartSecond: dr.Rate.GroupInterval,
Value: dr.Rate.Price,
RateIncrements: dr.Rate.RateIncrements,
RatedUnits: dr.Rate.RatedUnits,
GroupIntervalStart: dr.Rate.GroupIntervalStart,
Value: dr.Rate.Price,
RateIncrement: dr.Rate.RateIncrement,
RateUnit: dr.Rate.RateUnit,
}},
}
return

View File

@@ -462,10 +462,10 @@ func (d *Destination) Restore(input string) error {
func (pg PriceGroups) Store() (result string, err error) {
for _, p := range pg {
result += p.StartSecond.String() +
result += p.GroupIntervalStart.String() +
":" + strconv.FormatFloat(p.Value, 'f', -1, 64) +
":" + p.RateIncrements.String() +
":" + p.RatedUnits.String() +
":" + p.RateIncrement.String() +
":" + p.RateUnit.String() +
","
}
result = strings.TrimRight(result, ",")
@@ -496,10 +496,10 @@ func (pg *PriceGroups) Restore(input string) error {
return err
}
price := &Price{
StartSecond: ss,
Value: v,
RateIncrements: ri,
RatedUnits: ru,
GroupIntervalStart: ss,
Value: v,
RateIncrement: ri,
RateUnit: ru,
}
*pg = append(*pg, price)
}

View File

@@ -143,7 +143,7 @@ func TestIntervalRestoreFromString(t *testing.T) {
s := ";1,2,3,4,5,6,7,8,9,10,11,12;1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31;1,2,3,4,5,6,0;00:00:00;;10;0;0:0.2:1s:60s;0;"
i := Interval{}
err := i.Restore(s)
if err != nil || !i.Prices.Equal(PriceGroups{&Price{0, 0.2, 1 * time.Second, 1 * time.Second}}) {
if err != nil || !i.Prices.Equal(PriceGroups{&Price{0, 0.2, 1 * time.Second, 60 * time.Second}}) {
t.Errorf("Error restoring inteval period from string %+v", i)
}
}

View File

@@ -213,7 +213,7 @@ func (self *SQLStorage) SetTPRates(tpid string, rts map[string][]*Rate) error {
if len(rts) == 0 {
return nil //Nothing to set
}
qry := fmt.Sprintf("INSERT INTO %s (tpid, tag, connect_fee, rate, rated_units, rate_increments, group_interval, rounding_method, rounding_decimals, weight) VALUES ", utils.TBL_TP_RATES)
qry := fmt.Sprintf("INSERT INTO %s (tpid, tag, connect_fee, rate, rate_unit, rate_increment, group_interval_start, rounding_method, rounding_decimals, weight) VALUES ", utils.TBL_TP_RATES)
i := 0
for rtId, rtRows := range rts {
for _, rt := range rtRows {
@@ -221,7 +221,7 @@ func (self *SQLStorage) SetTPRates(tpid string, rts map[string][]*Rate) error {
qry += ","
}
qry += fmt.Sprintf("('%s', '%s', %f, %f, %d, %d,%d,'%s', %d, %f)",
tpid, rtId, rt.ConnectFee, rt.Price, rt.RatedUnits, rt.RateIncrements, rt.GroupInterval,
tpid, rtId, rt.ConnectFee, rt.Price, rt.RateUnit, rt.RateIncrement, rt.GroupIntervalStart,
rt.RoundingMethod, rt.RoundingDecimals, rt.Weight)
i++
}
@@ -244,13 +244,13 @@ func (self *SQLStorage) GetTPRate(tpid, rtId string) (*utils.TPRate, error) {
i++ //Keep here a reference so we know we got at least one prefix
var connectFee, rate, weight float64
var roundingDecimals int
var ratedUnits, rateIncrements, groupInterval time.Duration
var rateUnit, rateIncrement, groupIntervalStart time.Duration
var roundingMethod string
err = rows.Scan(&connectFee, &rate, &ratedUnits, &rateIncrements, &groupInterval, &roundingMethod, &roundingDecimals, &weight)
err = rows.Scan(&connectFee, &rate, &rateUnit, &rateIncrement, &groupIntervalStart, &roundingMethod, &roundingDecimals, &weight)
if err != nil {
return nil, err
}
rt.RateSlots = append(rt.RateSlots, utils.RateSlot{connectFee, rate, ratedUnits, rateIncrements, groupInterval,
rt.RateSlots = append(rt.RateSlots, utils.RateSlot{connectFee, rate, rateUnit, rateIncrement, groupIntervalStart,
roundingMethod, roundingDecimals, weight})
}
if i == 0 {
@@ -977,21 +977,21 @@ func (self *SQLStorage) GetTpRates(tpid, tag string) (map[string]*Rate, error) {
for rows.Next() {
var tag, roundingMethod string
var connect_fee, rate, weight float64
var rated_units, rate_increments, group_interval time.Duration
var rate_unit, rate_increment, group_interval_start time.Duration
var roundingDecimals int
if err := rows.Scan(&tag, &connect_fee, &rate, &rated_units, &rate_increments, &group_interval, &roundingMethod, &roundingDecimals, &weight); err != nil {
if err := rows.Scan(&tag, &connect_fee, &rate, &rate_unit, &rate_increment, &group_interval_start, &roundingMethod, &roundingDecimals, &weight); err != nil {
return nil, err
}
r := &Rate{
Tag: tag,
ConnectFee: connect_fee,
Price: rate,
RatedUnits: rated_units,
RateIncrements: rate_increments,
GroupInterval: group_interval,
RoundingMethod: roundingMethod,
RoundingDecimals: roundingDecimals,
Weight: weight,
Tag: tag,
ConnectFee: connect_fee,
Price: rate,
RateUnit: rate_unit,
RateIncrement: rate_increment,
GroupIntervalStart: group_interval_start,
RoundingMethod: roundingMethod,
RoundingDecimals: roundingDecimals,
Weight: weight,
}
rts[tag] = r
}

View File

@@ -87,7 +87,9 @@ func (ts *TimeSpan) SetInterval(i *Interval) {
if ts.Interval == nil || ts.Interval.Weight < i.Weight {
ts.Interval = i
}
if ts.Interval.Weight == i.Weight && i.GetPrice(ts.GetGroupStart()) < ts.Interval.GetPrice(ts.GetGroupStart()) {
iPrice, _, _ := i.GetPriceParameters(ts.GetGroupStart())
tsPrice, _, _ := ts.Interval.GetPriceParameters(ts.GetGroupStart())
if ts.Interval.Weight == i.Weight && iPrice < tsPrice {
ts.Interval = i
}
}
@@ -109,9 +111,9 @@ func (ts *TimeSpan) SplitByInterval(i *Interval) (nts *TimeSpan) {
// split by GroupStart
i.Prices.Sort()
for _, price := range i.Prices {
if ts.GetGroupStart() < price.StartSecond && ts.GetGroupEnd() >= price.StartSecond {
if ts.GetGroupStart() < price.GroupIntervalStart && ts.GetGroupEnd() >= price.GroupIntervalStart {
ts.SetInterval(i)
splitTime := ts.TimeStart.Add(price.StartSecond - ts.GetGroupStart())
splitTime := ts.TimeStart.Add(price.GroupIntervalStart - ts.GetGroupStart())
nts = &TimeSpan{TimeStart: splitTime, TimeEnd: ts.TimeEnd}
ts.TimeEnd = splitTime
nts.SetInterval(i)

View File

@@ -196,7 +196,7 @@ func TestTimespanGetCost(t *testing.T) {
if ts1.getCost(cd) != 600 {
t.Error("Expected 10 got ", ts1.getCost(cd))
}
ts1.Interval.Prices[0].RatedUnits = 60 * time.Second
ts1.Interval.Prices[0].RateUnit = 60 * time.Second
if ts1.getCost(cd) != 10 {
t.Error("Expected 6000 got ", ts1.getCost(cd))
}