From cd41ea5f9e6bdeea5f213ae03e10122fcb2f2257 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 2 Mar 2012 16:13:28 +0200 Subject: [PATCH] added newly created functionality to timespan package interface --- cmd/stress/spansstress/spansstress.go | 18 ++++----- timespans/activationperiod.go | 54 ------------------------- timespans/calldesc.go | 58 +++++++++++++++++++++++++++ timespans/userbudget.go | 48 +++++++++++++--------- 4 files changed, 96 insertions(+), 82 deletions(-) diff --git a/cmd/stress/spansstress/spansstress.go b/cmd/stress/spansstress/spansstress.go index d11e294de..f2e6b79d4 100644 --- a/cmd/stress/spansstress/spansstress.go +++ b/cmd/stress/spansstress/spansstress.go @@ -47,20 +47,20 @@ func main() { t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) cd := timespans.CallDescriptor{CstmId: "vdf", Subject: "rif", DestinationPrefix: "0256", TimeStart: t1, TimeEnd: t2} - var result *timespans.CallCost - - getter, _ := timespans.NewRedisStorage("", 10) + //getter, _ := timespans.NewRedisStorage("", 10) + getter, err := timespans.NewKyotoStorage("storage.kch") defer getter.Close() - cd.StorageGetter = getter + cd.SetStorageGetter(getter) - i := 0 log.Printf("Runnning %d cycles...", *runs) + var result *timespans.CallCost + j := 0 - for j := 0; j < *runs; j++ { - result, _ = cd.GetCost() + for i := 0; i < *runs; i++ { + result, err = cd.GetCost() + j = i } - log.Print(result) - log.Print(i) + log.Print(result, j, err) } diff --git a/timespans/activationperiod.go b/timespans/activationperiod.go index 35cd62f4b..2dd6fdf47 100644 --- a/timespans/activationperiod.go +++ b/timespans/activationperiod.go @@ -40,57 +40,3 @@ func (ap *ActivationPeriod) AddInterval(is ...*Interval) { } } -/* -Serializes the activation periods for the storage. Used for key-value storages. -*/ -// func (ap *ActivationPeriod) store() (result string) { -// result += strconv.FormatInt(ap.ActivationTime.UnixNano(), 10) + ";" -// for _, i := range ap.Intervals { -// var is string -// is = strconv.Itoa(int(i.Month)) + "|" -// is += strconv.Itoa(i.MonthDay) + "|" -// for _, wd := range i.WeekDays { -// is += strconv.Itoa(int(wd)) + "," -// } -// is = strings.TrimRight(is, ",") + "|" -// is += i.StartTime + "|" -// is += i.EndTime + "|" -// is += strconv.FormatFloat(i.Ponder, 'f', -1, 64) + "|" -// is += strconv.FormatFloat(i.ConnectFee, 'f', -1, 64) + "|" -// is += strconv.FormatFloat(i.Price, 'f', -1, 64) + "|" -// is += strconv.FormatFloat(i.BillingUnit, 'f', -1, 64) -// result += is + ";" -// } -// return -// } - -/* -De-serializes the activation periods for the storage. Used for key-value storages. -*/ -// func (ap *ActivationPeriod) restore(input string) { -// elements := strings.Split(input, ";") -// unixNano, _ := strconv.ParseInt(elements[0], 10, 64) -// ap.ActivationTime = time.Unix(0, unixNano).In(time.UTC) -// ap.Intervals = make([]*Interval, 0) -// for _, is := range elements[1 : len(elements)-1] { -// i := &Interval{} -// ise := strings.Split(is, "|") -// month, _ := strconv.Atoi(ise[0]) -// i.Month = time.Month(month) -// i.MonthDay, _ = strconv.Atoi(ise[1]) -// for _, d := range strings.Split(ise[2], ",") { -// if d != "" { -// wd, _ := strconv.Atoi(d) -// i.WeekDays = append(i.WeekDays, time.Weekday(wd)) -// } -// } -// i.StartTime = ise[3] -// i.EndTime = ise[4] -// i.Ponder, _ = strconv.ParseFloat(ise[5], 64) -// i.ConnectFee, _ = strconv.ParseFloat(ise[6], 64) -// i.Price, _ = strconv.ParseFloat(ise[7], 64) -// i.BillingUnit, _ = strconv.ParseFloat(ise[8], 64) - -// ap.Intervals = append(ap.Intervals, i) -// } -// } diff --git a/timespans/calldesc.go b/timespans/calldesc.go index 05a5291bb..8594a1c8b 100644 --- a/timespans/calldesc.go +++ b/timespans/calldesc.go @@ -227,6 +227,7 @@ func (cd *CallDescriptor) GetCost() (*CallCost, error) { Returns the cost of a second in the present time conditions. */ func (cd *CallDescriptor) getPresentSecondCost() (cost float64, err error) { + // TODO: remove this method if if not still used _, err = cd.SearchStorageForPrefix() now := time.Now() oneSecond, _ := time.ParseDuration("1s") @@ -283,6 +284,10 @@ func (cd *CallDescriptor) GetMaxSessionTime() (seconds float64, err error) { return 0, nil } +/* +Interface method used to add/substract an amount of cents from user's money budget. +The amount filed has to be filled in call descriptor. +*/ func (cd *CallDescriptor) DebitCents() (left float64, err error) { if userBudget, err := cd.getUserBudget(); err == nil && userBudget != nil { return userBudget.debitMoneyBudget(cd.storageGetter, cd.Amount), nil @@ -290,6 +295,10 @@ func (cd *CallDescriptor) DebitCents() (left float64, err error) { return 0.0, err } +/* +Interface method used to add/substract an amount of units from user's sms budget. +The amount filed has to be filled in call descriptor. +*/ func (cd *CallDescriptor) DebitSMS() (left float64, err error) { if userBudget, err := cd.getUserBudget(); err == nil && userBudget != nil { return userBudget.debitSMSBuget(cd.storageGetter, cd.Amount) @@ -297,6 +306,10 @@ func (cd *CallDescriptor) DebitSMS() (left float64, err error) { return 0, err } +/* +Interface method used to add/substract an amount of seconds from user's minutes budget. +The amount filed has to be filled in call descriptor. +*/ func (cd *CallDescriptor) DebitSeconds() (err error) { if userBudget, err := cd.getUserBudget(); err == nil && userBudget != nil { return userBudget.debitMinutesBudget(cd.storageGetter, cd.Amount, cd.DestinationPrefix) @@ -304,6 +317,51 @@ func (cd *CallDescriptor) DebitSeconds() (err error) { return err } +/* +Interface method used to add an amount to the accumulated placed call seconds +to be used for volume discount. +The amount filed has to be filled in call descriptor. +*/ +func (cd *CallDescriptor) AddVolumeDiscountSeconds() (err error) { + if userBudget, err := cd.getUserBudget(); err == nil && userBudget != nil { + return userBudget.addVolumeDiscountSeconds(cd.storageGetter, cd.Amount) + } + return err +} + +/* +Resets the accumulated volume discount seconds (to zero). +*/ +func (cd *CallDescriptor) ResetVolumeDiscountSeconds() (err error) { + if userBudget, err := cd.getUserBudget(); err == nil && userBudget != nil { + return userBudget.resetVolumeDiscountSeconds(cd.storageGetter) + } + return err +} + +/* +Adds the specified amount of seconds to the recived call seconds. When the threshold specified +in the user's tariff plan is reached then the recived call budget is reseted and the bonus +specified in the tariff plan is applyed. +The amount filed has to be filled in call descriptor. +*/ +func (cd *CallDescriptor) AddRecievedCallSeconds() (err error) { + if userBudget, err := cd.getUserBudget(); err == nil && userBudget != nil { + return userBudget.addReceivedCallSeconds(cd.storageGetter, cd.Amount) + } + return err +} + +/* +Resets user budgets value to the amounts specified in the tariff plan. +*/ +func (cd *CallDescriptor) ResetUserBudget() (err error) { + if userBudget, err := cd.getUserBudget(); err == nil && userBudget != nil { + return userBudget.resetUserBudget(cd.storageGetter) + } + return err +} + /* The output structure that will be returned with the call cost information. */ diff --git a/timespans/userbudget.go b/timespans/userbudget.go index 3d9d2a4c5..36de8884c 100644 --- a/timespans/userbudget.go +++ b/timespans/userbudget.go @@ -188,12 +188,38 @@ func (ub *UserBudget) debitMinutesBudget(sg StorageGetter, amount float64, prefi } /* -Serts the volume discount seconds budget to the specified amount. +Debits some amount of user's SMS budget. Returns the remaining SMS in user's budget. +If the amount is bigger than the budget than nothing wil be debited and an error will be returned */ -func (ub *UserBudget) setVolumeDiscountSeconds(sg StorageGetter, amount float64) error { +func (ub *UserBudget) debitSMSBuget(sg StorageGetter, amount float64) (float64, error) { ub.mux.Lock() defer ub.mux.Unlock() - ub.VolumeDiscountSeconds = amount + if ub.SmsCredit < amount { + return ub.SmsCredit, new(AmountTooBig) + } + ub.SmsCredit -= amount + + sg.SetUserBudget(ub) + return ub.SmsCredit, nil +} + +/* +Adds the the specified amount to volume discount seconds budget. +*/ +func (ub *UserBudget) addVolumeDiscountSeconds(sg StorageGetter, amount float64) error { + ub.mux.Lock() + defer ub.mux.Unlock() + ub.VolumeDiscountSeconds += amount + return sg.SetUserBudget(ub) +} + +/* +Resets the volume discounts seconds (sets zero value). +*/ +func (ub *UserBudget) resetVolumeDiscountSeconds(sg StorageGetter) error { + ub.mux.Lock() + defer ub.mux.Unlock() + ub.VolumeDiscountSeconds = 0 return sg.SetUserBudget(ub) } @@ -245,19 +271,3 @@ func (ub *UserBudget) resetUserBudget(sg StorageGetter) (err error) { } return } - -/* -Debits some amount of user's SMS budget. Returns the remaining SMS in user's budget. -If the amount is bigger than the budget than nothing wil be debited and an error will be returned -*/ -func (ub *UserBudget) debitSMSBuget(sg StorageGetter, amount float64) (float64, error) { - ub.mux.Lock() - defer ub.mux.Unlock() - if ub.SmsCredit < amount { - return ub.SmsCredit, new(AmountTooBig) - } - ub.SmsCredit -= amount - - sg.SetUserBudget(ub) - return ub.SmsCredit, nil -}