mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-22 15:48:44 +05:00
better action timing sorting function
This commit is contained in:
@@ -191,13 +191,20 @@ func (atpl ActionTimingPriotityList) Swap(i, j int) {
|
||||
}
|
||||
|
||||
func (atpl ActionTimingPriotityList) Less(i, j int) bool {
|
||||
return atpl[i].GetNextStartTime().Before(atpl[j].GetNextStartTime()) || atpl[i].Weight < atpl[j].Weight
|
||||
if atpl[i].GetNextStartTime().Equal(atpl[j].GetNextStartTime()) {
|
||||
return atpl[i].Weight < atpl[j].Weight
|
||||
}
|
||||
return atpl[i].GetNextStartTime().Before(atpl[j].GetNextStartTime())
|
||||
}
|
||||
|
||||
func (atpl ActionTimingPriotityList) Sort() {
|
||||
sort.Sort(atpl)
|
||||
}
|
||||
|
||||
func (at *ActionTiming) String() string {
|
||||
return at.GetNextStartTime().String() + ",w: " + strconv.FormatFloat(at.Weight, 'f', -1, 64)
|
||||
}
|
||||
|
||||
/*
|
||||
Serializes the action timing for the storage. Used for key-value storages.
|
||||
*/
|
||||
|
||||
@@ -281,6 +281,53 @@ func TestActionTimingLogFunction(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestActionTimingPriotityList(t *testing.T) {
|
||||
at1 := &ActionTiming{Timing: &Interval{
|
||||
Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December},
|
||||
MonthDays: MonthDays{1},
|
||||
StartTime: "00:00:00",
|
||||
Weight: 20,
|
||||
}}
|
||||
at2 := &ActionTiming{Timing: &Interval{
|
||||
Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December},
|
||||
MonthDays: MonthDays{2},
|
||||
StartTime: "00:00:00",
|
||||
Weight: 10,
|
||||
}}
|
||||
var atpl ActionTimingPriotityList
|
||||
atpl = append(atpl, at2, at1)
|
||||
t.Log(atpl)
|
||||
atpl.Sort()
|
||||
if atpl[0] != at1 || atpl[1] != at2 {
|
||||
t.Error("Timing list not sorted correctly: ", atpl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestActionTimingPriotityListWeight(t *testing.T) {
|
||||
at1 := &ActionTiming{
|
||||
Timing: &Interval{
|
||||
Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December},
|
||||
MonthDays: MonthDays{1},
|
||||
StartTime: "00:00:00",
|
||||
},
|
||||
Weight: 10.0,
|
||||
}
|
||||
at2 := &ActionTiming{
|
||||
Timing: &Interval{
|
||||
Months: Months{time.January, time.February, time.March, time.April, time.May, time.June, time.July, time.August, time.September, time.October, time.November, time.December},
|
||||
MonthDays: MonthDays{1},
|
||||
StartTime: "00:00:00",
|
||||
},
|
||||
Weight: 20.0,
|
||||
}
|
||||
var atpl ActionTimingPriotityList
|
||||
atpl = append(atpl, at2, at1)
|
||||
atpl.Sort()
|
||||
if atpl[0] != at1 || atpl[1] != at2 {
|
||||
t.Error("Timing list not sorted correctly: ", atpl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestActionTriggerPriotityList(t *testing.T) {
|
||||
at1 := &ActionTrigger{Weight: 10}
|
||||
at2 := &ActionTrigger{Weight: 20}
|
||||
|
||||
Reference in New Issue
Block a user