From afbc9e451ebdaf36696eea4318323f99e4d73b55 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 20 Feb 2012 21:08:40 +0200 Subject: [PATCH] tariff plans working --- cmd/loader/loader.go | 2 +- timespans/minute_buckets.go | 10 +++++----- timespans/minute_buckets_test.go | 2 +- timespans/mongo_storage.go | 2 ++ timespans/tariff_plans.go | 16 ++++++++-------- timespans/tariff_plans_test.go | 29 ++++++++++++++--------------- timespans/test_tariffplans.json | 8 ++++++++ timespans/userbudget.go | 8 ++++---- timespans/userbudget_test.go | 12 ++++++------ 9 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 timespans/test_tariffplans.json diff --git a/cmd/loader/loader.go b/cmd/loader/loader.go index 40103a2b0..da0ea3e84 100644 --- a/cmd/loader/loader.go +++ b/cmd/loader/loader.go @@ -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) diff --git a/timespans/minute_buckets.go b/timespans/minute_buckets.go index d13f0278c..55a22aac4 100644 --- a/timespans/minute_buckets.go +++ b/timespans/minute_buckets.go @@ -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 } diff --git a/timespans/minute_buckets_test.go b/timespans/minute_buckets_test.go index d8ea8c7f2..9c8fbb484 100644 --- a/timespans/minute_buckets_test.go +++ b/timespans/minute_buckets_test.go @@ -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) diff --git a/timespans/mongo_storage.go b/timespans/mongo_storage.go index b0dca402c..5ffa73c08 100644 --- a/timespans/mongo_storage.go +++ b/timespans/mongo_storage.go @@ -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 } diff --git a/timespans/tariff_plans.go b/timespans/tariff_plans.go index ebadf9f64..ce3221e55 100644 --- a/timespans/tariff_plans.go +++ b/timespans/tariff_plans.go @@ -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) } diff --git a/timespans/tariff_plans_test.go b/timespans/tariff_plans_test.go index 6ca410fa1..29113cdff 100644 --- a/timespans/tariff_plans_test.go +++ b/timespans/tariff_plans_test.go @@ -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) diff --git a/timespans/test_tariffplans.json b/timespans/test_tariffplans.json new file mode 100644 index 000000000..9a51b688b --- /dev/null +++ b/timespans/test_tariffplans.json @@ -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"}]} +] diff --git a/timespans/userbudget.go b/timespans/userbudget.go index 59e7da42c..b88782d09 100644 --- a/timespans/userbudget.go +++ b/timespans/userbudget.go @@ -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 } diff --git a/timespans/userbudget_test.go b/timespans/userbudget_test.go index cd8e3827b..9c87507f8 100644 --- a/timespans/userbudget_test.go +++ b/timespans/userbudget_test.go @@ -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}