TSMerge without compress/decompress

This commit is contained in:
DanB
2016-09-16 10:29:37 +02:00
parent 343c85e505
commit 3e913d03ae
2 changed files with 60 additions and 47 deletions

View File

@@ -300,19 +300,6 @@ func (incs Increments) Equal(other Increments) bool {
return true
}
// Estimate whether the increments are the same, ignoring the CompressFactor
func (incs Increments) SharingSignature(other Increments) bool {
if len(other) < len(incs) { // Protect index in case of not being the same size
return false
}
for index, i := range incs {
if !i.Equal(other[index]) {
return false
}
}
return true
}
func (incs *Increments) Compress() { // must be pointer receiver
var cIncrs Increments
for _, incr := range *incs {
@@ -355,6 +342,29 @@ func (incs *Increments) Decompress() { // must be pointer receiver
*incs = cIncrs
}
// Estimate whether the increments are the same ignoring the CompressFactor
func (incs Increments) SharingSignature(other Increments) bool {
var otherCloned Increments // Clone so we don't affect with decompress the original structure
if err := utils.Clone(other, &otherCloned); err != nil {
return false
}
var thisCloned Increments
if err := utils.Clone(incs, &thisCloned); err != nil {
return false
}
otherCloned.Compress()
thisCloned.Compress()
if len(other) < len(incs) { // Protect index in case of not being the same size
return false
}
for index, i := range incs {
if !i.Equal(other[index]) {
return false
}
}
return true
}
func (incs Increments) GetTotalCost() float64 {
cost := 0.0
for _, increment := range incs {
@@ -721,19 +731,13 @@ func (ts *TimeSpan) hasBetterRateIntervalThan(interval *RateInterval) bool {
ownLeftMargin := ts.RateInterval.Timing.getLeftMargin(ts.TimeStart)
ownDistance := ts.TimeStart.Sub(ownLeftMargin)
//log.Print("OWN LEFT: ", otherLeftMargin)
//log.Print("OWN DISTANCE: ", otherDistance)
//endOtherDistance := ts.TimeEnd.Sub(otherLeftMargin)
// if own interval is closer than its better
//log.Print(ownDistance)
if ownDistance > otherDistance {
return false
}
ownPrice, _, _ := ts.RateInterval.GetRateParameters(ts.GetGroupStart())
otherPrice, _, _ := interval.GetRateParameters(ts.GetGroupStart())
// if own price is smaller than it's better
//log.Print(ownPrice, otherPrice)
if ownPrice < otherPrice {
return true
}
@@ -753,12 +757,16 @@ func (ts *TimeSpan) Equal(other *TimeSpan) bool {
// Estimate if they share charging signature
func (ts *TimeSpan) SharingSignature(other *TimeSpan) bool {
return ts.Increments.SharingSignature(other.Increments) &&
ts.RateInterval.Equal(other.RateInterval) &&
ts.MatchedSubject == other.MatchedSubject &&
ts.MatchedPrefix == other.MatchedPrefix &&
ts.MatchedDestId == other.MatchedDestId &&
ts.RatingPlanId == other.RatingPlanId
if ts.GetCompressFactor() != other.GetCompressFactor() ||
!ts.Increments.SharingSignature(other.Increments) ||
!ts.RateInterval.Equal(other.RateInterval) ||
ts.MatchedSubject != other.MatchedSubject ||
ts.MatchedPrefix != other.MatchedPrefix ||
ts.MatchedDestId != other.MatchedDestId ||
ts.RatingPlanId != other.RatingPlanId {
return false
}
return true
}
func (ts *TimeSpan) GetCompressFactor() int {
@@ -775,16 +783,9 @@ func (ts *TimeSpan) Merge(other *TimeSpan) bool {
} else if !ts.TimeEnd.Equal(other.TimeStart) { // other needs to continue ts for merge to be possible
return false
}
var otherCloned TimeSpan // Clone so we don't affect with decompress the original structure
if err := utils.Clone(*other, &otherCloned); err != nil {
return false
}
otherCloned.Increments.Decompress()
ts.Increments.Decompress()
ts.TimeEnd = otherCloned.TimeEnd
ts.Cost += otherCloned.Cost
ts.DurationIndex = otherCloned.DurationIndex
ts.Increments = append(ts.Increments, otherCloned.Increments...)
ts.Increments.Compress()
ts.TimeEnd = other.TimeEnd
ts.Cost += other.Cost
ts.DurationIndex = other.DurationIndex
ts.Increments = append(ts.Increments, other.Increments...)
return true
}

View File

@@ -1865,7 +1865,7 @@ func TestTSMerge(t *testing.T) {
},
},
},
Cost: 2,
Cost: 3,
DurationIndex: 1 * time.Minute,
Increments: Increments{
&Increment{
@@ -1875,14 +1875,7 @@ func TestTSMerge(t *testing.T) {
Unit: &UnitInfo{UUID: "1", DestinationID: "1", Consumed: 2.3, TOR: utils.VOICE, RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}},
Monetary: &MonetaryInfo{UUID: "2"},
AccountID: "3"},
},
&Increment{
Duration: time.Minute,
Cost: 1,
BalanceInfo: &DebitInfo{
Unit: &UnitInfo{UUID: "1", DestinationID: "1", Consumed: 2.3, TOR: utils.VOICE, RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}},
Monetary: &MonetaryInfo{UUID: "2"},
AccountID: "3"},
CompressFactor: 3,
},
},
}
@@ -1937,7 +1930,7 @@ func TestTSMerge(t *testing.T) {
},
},
},
Cost: 4,
Cost: 5,
DurationIndex: 2 * time.Minute,
Increments: Increments{
&Increment{
@@ -1947,9 +1940,28 @@ func TestTSMerge(t *testing.T) {
Unit: &UnitInfo{UUID: "1", DestinationID: "1", Consumed: 2.3, TOR: utils.VOICE, RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}},
Monetary: &MonetaryInfo{UUID: "2"},
AccountID: "3"},
CompressFactor: 4,
CompressFactor: 3,
},
&Increment{
Duration: time.Minute,
Cost: 1,
BalanceInfo: &DebitInfo{
Unit: &UnitInfo{UUID: "1", DestinationID: "1", Consumed: 2.3, TOR: utils.VOICE, RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}},
Monetary: &MonetaryInfo{UUID: "2"},
AccountID: "3"},
CompressFactor: 1,
},
&Increment{
Duration: time.Minute,
Cost: 1,
BalanceInfo: &DebitInfo{
Unit: &UnitInfo{UUID: "1", DestinationID: "1", Consumed: 2.3, TOR: utils.VOICE, RateInterval: &RateInterval{Rating: &RIRate{Rates: RateGroups{&Rate{GroupIntervalStart: 0, Value: 100, RateIncrement: 10 * time.Second, RateUnit: time.Second}}}}},
Monetary: &MonetaryInfo{UUID: "2"},
AccountID: "3"},
CompressFactor: 1,
},
},
CompressFactor: 1,
}
if merged := tss1.Merge(tss2); !merged {
t.Error("Not merged")