worked on budget

This commit is contained in:
Radu Ioan Fericean
2012-02-16 19:28:00 +02:00
parent b15f6bbc1f
commit a37ab4853b
3 changed files with 126 additions and 1 deletions

11
README
View File

@@ -1 +1,10 @@
Rating system for telecom providers.
Rating system designed to be used in VoIP Carriers World.
Features
- Fast RatingEngine, holds all rating information into memory
- Unlimited spanned timebased rating: considers both ActiveFrom/RateStartsAt
- Unlimited rate profiles chanining for fallback
- Rating Fields: ConnectFee, RateIn, RateOut
- High accuracy rating: configurable to miliseconds
- Flexible rates storage - plugin based rates loading
- Flexibility to rate various information from the cdrs as rating subject.

65
timespans/userbudget.go Normal file
View File

@@ -0,0 +1,65 @@
package timespans
import (
"log"
"math"
)
/*
Structure conatining information about user's credit (minutes, cents, sms...).'
*/
type UserBudget struct {
id string
minuteBuckets []*MinuteBucket
credit float64
smsCredit int
tariffPlan *TariffPlan
resetDayOfTheMonth int
}
/*
Returns user's avaliable minutes for the specified destination
*/
func (ub *UserBudget) GetSecondsForPrefix(prefix string) (seconds int) {
if len(ub.minuteBuckets) == 0 {
log.Print("There are no minute buckets to check for user", ub.id)
return
}
bestBucket := ub.minuteBuckets[0]
for _, mb := range ub.minuteBuckets {
if mb.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)))
}
return
}
type Destination struct {
id string
prefixes []string
}
type MinuteBucket struct {
seconds int
priority int
price float64
destination *Destination
}
func (mb *MinuteBucket) containsPrefix(prefix string) bool {
for _, p := range mb.destination.prefixes {
if prefix == p {
return true
}
}
return false
}
type TariffPlan struct {
minuteBuckets []*MinuteBucket
}

View File

@@ -0,0 +1,51 @@
package timespans
import (
"testing"
)
var (
nationale = &Destination{id: "nationale", prefixes: []string{"0257", "0256", "0723"}}
retea = &Destination{id: "retea", prefixes: []string{"0723", "0724"}}
)
func TestGetSeconds(t *testing.T) {
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}
seconds := ub1.GetSecondsForPrefix("0723")
expected := 100
if seconds != expected {
t.Errorf("Expected %v was %v", expected, seconds)
}
}
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}
tf1 := &TariffPlan{minuteBuckets: []*MinuteBucket{b1, b2}}
ub1 := &UserBudget{id: "rif", minuteBuckets: []*MinuteBucket{b1, b2}, credit: 21, tariffPlan: tf1, resetDayOfTheMonth: 10}
seconds := ub1.GetSecondsForPrefix("0723")
expected := 21
if seconds != expected {
t.Errorf("Expected %v was %v", expected, seconds)
}
}
/*********************************** Benchmarks *******************************/
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}
tf1 := &TariffPlan{minuteBuckets: []*MinuteBucket{b1, b2}}
ub1 := &UserBudget{id: "rif", minuteBuckets: []*MinuteBucket{b1, b2}, credit: 21, tariffPlan: tf1, resetDayOfTheMonth: 10}
b.StartTimer()
for i := 0; i < b.N; i++ {
ub1.GetSecondsForPrefix("0723")
}
}