mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
tariff plans working
This commit is contained in:
@@ -42,7 +42,7 @@ func writeToStorage(storage timespans.StorageGetter,
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
log.Print("Reading from ", *apfile, *destfile)
|
||||
log.Printf("Reading from %s, %s, %s", *apfile, *destfile, *tpfile)
|
||||
|
||||
// reading activation periods
|
||||
fin, err := os.Open(*apfile)
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package timespans
|
||||
|
||||
type MinuteBucket struct {
|
||||
seconds int
|
||||
priority int
|
||||
price float64
|
||||
destinationId string
|
||||
Seconds int
|
||||
Priority int
|
||||
Price float64
|
||||
DestinationId string
|
||||
destination *Destination
|
||||
}
|
||||
|
||||
func (mb *MinuteBucket) getDestination(storage StorageGetter) (dest *Destination) {
|
||||
if mb.destination == nil {
|
||||
mb.destination,_ = storage.GetDestination(mb.destinationId)
|
||||
mb.destination,_ = storage.GetDestination(mb.DestinationId)
|
||||
}
|
||||
return mb.destination
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
func TestGetDestination(t *testing.T) {
|
||||
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
|
||||
defer getter.Close()
|
||||
mb := &MinuteBucket{destinationId: "nationale"}
|
||||
mb := &MinuteBucket{DestinationId: "nationale"}
|
||||
d := mb.getDestination(getter)
|
||||
if d.Id != "nationale" || len(d.Prefixes) != 4 {
|
||||
t.Error("Got wrong destination: ", d)
|
||||
|
||||
@@ -26,6 +26,8 @@ func NewMongoStorage(address, db string) (*MongoStorage, error) {
|
||||
err = session.DB(db).C("ap").EnsureIndex(index)
|
||||
index = mgo.Index{Key: []string{"id"}, Unique: true, DropDups: true, Background: true}
|
||||
err = session.DB(db).C("dest").EnsureIndex(index)
|
||||
index = mgo.Index{Key: []string{"id"}, Unique: true, DropDups: true, Background: true}
|
||||
err = session.DB(db).C("tp").EnsureIndex(index)
|
||||
|
||||
return &MongoStorage{db: session.DB(db), session: session}, nil
|
||||
}
|
||||
|
||||
@@ -22,10 +22,10 @@ func (tp *TariffPlan) store() (result string) {
|
||||
result += strconv.Itoa(tp.SmsCredit) + ";"
|
||||
for _, mb := range tp.MinuteBuckets {
|
||||
var mbs string
|
||||
mbs += strconv.Itoa(int(mb.seconds)) + "|"
|
||||
mbs += strconv.Itoa(int(mb.priority)) + "|"
|
||||
mbs += strconv.FormatFloat(mb.price, 'f', -1, 64) + "|"
|
||||
mbs += mb.destinationId
|
||||
mbs += strconv.Itoa(int(mb.Seconds)) + "|"
|
||||
mbs += strconv.Itoa(int(mb.Priority)) + "|"
|
||||
mbs += strconv.FormatFloat(mb.Price, 'f', -1, 64) + "|"
|
||||
mbs += mb.DestinationId
|
||||
result += mbs + ";"
|
||||
}
|
||||
return
|
||||
@@ -40,10 +40,10 @@ func (tp *TariffPlan) restore(input string) {
|
||||
for _, mbs := range elements[1 : len(elements)-1] {
|
||||
mb := &MinuteBucket{}
|
||||
mbse := strings.Split(mbs, "|")
|
||||
mb.seconds,_ = strconv.Atoi(mbse[0])
|
||||
mb.priority,_ = strconv.Atoi(mbse[1])
|
||||
mb.price,_ = strconv.ParseFloat(mbse[2], 64)
|
||||
mb.destinationId = mbse[3]
|
||||
mb.Seconds,_ = strconv.Atoi(mbse[0])
|
||||
mb.Priority,_ = strconv.Atoi(mbse[1])
|
||||
mb.Price,_ = strconv.ParseFloat(mbse[2], 64)
|
||||
mb.DestinationId = mbse[3]
|
||||
|
||||
tp.MinuteBuckets = append(tp.MinuteBuckets, mb)
|
||||
}
|
||||
|
||||
@@ -5,10 +5,9 @@ import (
|
||||
)
|
||||
|
||||
func TestTariffPlanStoreRestore(t *testing.T) {
|
||||
b1 := &MinuteBucket{seconds: 10, priority: 10, price: 0.01, destinationId: "nationale"}
|
||||
b2 := &MinuteBucket{seconds: 100, priority: 20, price: 0.0, destinationId: "retea"}
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
|
||||
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
|
||||
s := seara.store()
|
||||
tp1 := &TariffPlan{Id: "seara"}
|
||||
tp1.restore(s)
|
||||
@@ -20,8 +19,8 @@ func TestTariffPlanStoreRestore(t *testing.T) {
|
||||
func TestTariffPlanKyotoStore(t *testing.T) {
|
||||
getter, _ := NewKyotoStorage("test.kch")
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{seconds: 10, priority: 10, price: 0.01, destinationId: "nationale"}
|
||||
b2 := &MinuteBucket{seconds: 100, priority: 20, price: 0.0, destinationId: "retea"}
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
|
||||
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
getter.SetTariffPlan(seara)
|
||||
result, _ := getter.GetTariffPlan(seara.Id)
|
||||
@@ -33,8 +32,8 @@ func TestTariffPlanKyotoStore(t *testing.T) {
|
||||
func TestTariffPlanRedisStore(t *testing.T) {
|
||||
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{seconds: 10, priority: 10, price: 0.01, destinationId: "nationale"}
|
||||
b2 := &MinuteBucket{seconds: 100, priority: 20, price: 0.0, destinationId: "retea"}
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
|
||||
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
getter.SetTariffPlan(seara)
|
||||
result, _ := getter.GetTariffPlan(seara.Id)
|
||||
@@ -46,8 +45,8 @@ func TestTariffPlanRedisStore(t *testing.T) {
|
||||
func TestTariffPlanMongoStore(t *testing.T) {
|
||||
getter, _ := NewMongoStorage("127.0.0.1", "test")
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{seconds: 10, priority: 10, price: 0.01, destinationId: "nationale"}
|
||||
b2 := &MinuteBucket{seconds: 100, priority: 20, price: 0.0, destinationId: "retea"}
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
|
||||
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
getter.SetTariffPlan(seara)
|
||||
result, _ := getter.GetTariffPlan(seara.Id)
|
||||
@@ -61,8 +60,8 @@ func TestTariffPlanMongoStore(t *testing.T) {
|
||||
func BenchmarkTariffPlanKyotoStoreRestore(b *testing.B) {
|
||||
getter, _ := NewKyotoStorage("test.kch")
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{seconds: 10, priority: 10, price: 0.01, destinationId: "nationale"}
|
||||
b2 := &MinuteBucket{seconds: 100, priority: 20, price: 0.0, destinationId: "retea"}
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
|
||||
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
for i := 0; i < b.N; i++ {
|
||||
getter.SetTariffPlan(seara)
|
||||
@@ -73,8 +72,8 @@ func BenchmarkTariffPlanKyotoStoreRestore(b *testing.B) {
|
||||
func BenchmarkTariffPlanRedisStoreRestore(b *testing.B) {
|
||||
getter, _ := NewRedisStorage("tcp:127.0.0.1:6379", 10)
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{seconds: 10, priority: 10, price: 0.01, destinationId: "nationale"}
|
||||
b2 := &MinuteBucket{seconds: 100, priority: 20, price: 0.0, destinationId: "retea"}
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
|
||||
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
for i := 0; i < b.N; i++ {
|
||||
getter.SetTariffPlan(seara)
|
||||
@@ -85,8 +84,8 @@ func BenchmarkTariffPlanRedisStoreRestore(b *testing.B) {
|
||||
func BenchmarkTariffPlanMongoStoreRestore(b *testing.B) {
|
||||
getter, _ := NewMongoStorage("127.0.0.1", "test")
|
||||
defer getter.Close()
|
||||
b1 := &MinuteBucket{seconds: 10, priority: 10, price: 0.01, destinationId: "nationale"}
|
||||
b2 := &MinuteBucket{seconds: 100, priority: 20, price: 0.0, destinationId: "retea"}
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, Price: 0.01, DestinationId: "nationale"}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, Price: 0.0, DestinationId: "retea"}
|
||||
seara := &TariffPlan{Id: "seara", SmsCredit: 100, MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
for i := 0; i < b.N; i++ {
|
||||
getter.SetTariffPlan(seara)
|
||||
|
||||
8
timespans/test_tariffplans.json
Normal file
8
timespans/test_tariffplans.json
Normal file
@@ -0,0 +1,8 @@
|
||||
[
|
||||
{"Id":"seara","SmsCredit":100,"MinuteBuckets":
|
||||
[{"Seconds":10,"Priority":10,"Price":0.01,"DestinationId":"nationale"},
|
||||
{"Seconds":100,"Priority":20,"Price":0,"DestinationId":"retea"}]},
|
||||
{"Id":"dimineata","SmsCredit":100,"MinuteBuckets":
|
||||
[{"Seconds":100,"Priority":10,"Price":0.01,"DestinationId":"nationale"},
|
||||
{"Seconds":1000,"Priority":20,"Price":0,"DestinationId":"retea"}]}
|
||||
]
|
||||
@@ -29,13 +29,13 @@ func (ub *UserBudget) GetSecondsForPrefix(storage StorageGetter, prefix string)
|
||||
|
||||
for _, mb := range ub.minuteBuckets {
|
||||
d := mb.getDestination(storage)
|
||||
if d.containsPrefix(prefix) && mb.priority > bestBucket.priority {
|
||||
if d.containsPrefix(prefix) && mb.Priority > bestBucket.Priority {
|
||||
bestBucket = mb
|
||||
}
|
||||
}
|
||||
seconds = bestBucket.seconds
|
||||
if bestBucket.price > 0 {
|
||||
seconds = int(math.Min(ub.credit/bestBucket.price, float64(seconds)))
|
||||
seconds = bestBucket.Seconds
|
||||
if bestBucket.Price > 0 {
|
||||
seconds = int(math.Min(ub.credit/bestBucket.Price, float64(seconds)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ var (
|
||||
)
|
||||
|
||||
func TestGetSeconds(t *testing.T) {
|
||||
b1 := &MinuteBucket{seconds: 10, priority: 10, destination: nationale}
|
||||
b2 := &MinuteBucket{seconds: 100, priority: 20, destination: retea}
|
||||
b1 := &MinuteBucket{Seconds: 10, Priority: 10, destination: nationale}
|
||||
b2 := &MinuteBucket{Seconds: 100, Priority: 20, destination: retea}
|
||||
tf1 := &TariffPlan{MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
|
||||
ub1 := &UserBudget{id: "rif", minuteBuckets: []*MinuteBucket{b1, b2}, credit: 200, tariffPlan: tf1, resetDayOfTheMonth: 10}
|
||||
@@ -23,8 +23,8 @@ func TestGetSeconds(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetPricedSeconds(t *testing.T) {
|
||||
b1 := &MinuteBucket{seconds: 10, price: 10, priority: 10, destination: nationale}
|
||||
b2 := &MinuteBucket{seconds: 100, price: 1, priority: 20, destination: retea}
|
||||
b1 := &MinuteBucket{Seconds: 10, Price: 10, Priority: 10, destination: nationale}
|
||||
b2 := &MinuteBucket{Seconds: 100, Price: 1, Priority: 20, destination: retea}
|
||||
tf1 := &TariffPlan{MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
|
||||
ub1 := &UserBudget{id: "rif", minuteBuckets: []*MinuteBucket{b1, b2}, credit: 21, tariffPlan: tf1, resetDayOfTheMonth: 10}
|
||||
@@ -39,8 +39,8 @@ func TestGetPricedSeconds(t *testing.T) {
|
||||
|
||||
func BenchmarkGetSecondForPrefix(b *testing.B) {
|
||||
b.StopTimer()
|
||||
b1 := &MinuteBucket{seconds: 10, price: 10, priority: 10, destination: nationale}
|
||||
b2 := &MinuteBucket{seconds: 100, price: 1, priority: 20, destination: retea}
|
||||
b1 := &MinuteBucket{Seconds: 10, Price: 10, Priority: 10, destination: nationale}
|
||||
b2 := &MinuteBucket{Seconds: 100, Price: 1, Priority: 20, destination: retea}
|
||||
tf1 := &TariffPlan{MinuteBuckets: []*MinuteBucket{b1, b2}}
|
||||
|
||||
ub1 := &UserBudget{id: "rif", minuteBuckets: []*MinuteBucket{b1, b2}, credit: 21, tariffPlan: tf1, resetDayOfTheMonth: 10}
|
||||
|
||||
Reference in New Issue
Block a user