func main() { var numbers sort.Float64Slice = []float64{9, 2, 12, 1, 16, 12, 10} numbers.Sort() //numbers are sorted and can be used as is. result := median(numbers) fmt.Println(result) }
// CalculateRanges genetare diapason of ranges func CalculateRanges(list []float32, rangesCount int) ([]float64, error) { if rangesCount < 1 { return nil, errors.New("the ranges count must be positive value more than 1") } if list == nil || len(list) < rangesCount { return nil, errors.New("quantity of set float32 values must be more than the ranges count and more than 1") } // var deltas []float32 var sorted sort.Float64Slice var total float64 for i := 0; i < len(list)-1; i++ { delta := float64(list[i+1] / list[i]) total += delta // deltas = append(deltas, delta) sorted = append(sorted, delta) } sorted.Sort() step := total / float64(rangesCount) var ranges = make([]float64, rangesCount-1) check := false for i := 1; i < rangesCount; i++ { level := step * float64(i) var sum float64 var prev float64 for _, element := range sorted { sum += element if sum > level { ranges[i-1] = (element + prev) / 2 if i > 1 && ranges[i-1] != ranges[i-2] { check = true } break } else { prev = element } } } if check { return ranges, nil } return nil, errors.New("CalculateRanges errer: ranges not vialid") }
func (a *Account) topNTrades(n int64, normalSortOrder bool) []*orders.Order { fslice := sort.Float64Slice{} for e := a.Orders.Front(); e != nil; e = e.Next() { order := e.Value.(*orders.Order) fslice = append(fslice, order.Profit()) } if normalSortOrder { sort.Sort(sort.Reverse(fslice)) } else { fslice.Sort() } trades := []*orders.Order{} count := int64(0) for _, profit := range fslice { if count >= n { break } for e := a.Orders.Front(); e != nil; e = e.Next() { order := e.Value.(*orders.Order) if order.Profit() == profit { trades = append(trades, order) break } } count++ } return trades }
// CalculateEvenRanges2 genetare even diapasons of ranges func CalculateEvenRanges2(list []float32, rangesCount int) ([]float64, error) { if rangesCount < 4 || rangesCount%2 == 1 { return nil, errors.New("the ranges count must be positive even value more than 3") } if list == nil || len(list) < rangesCount { return nil, errors.New("quantity of set float32 values must be more than the ranges count and more than 3") } var sortedB sort.Float64Slice var sortedL sort.Float64Slice var totalB float64 var totalL float64 for i := 0; i < len(list)-1; i++ { delta := float64(list[i+1] / list[i]) if delta == 1 { continue } else { if delta > 1 { totalB += delta - 1 sortedB = append(sortedB, delta-1) } else { totalL += 1 - delta sortedL = append(sortedL, 1-delta) } } } sortedB.Sort() sortedL.Sort() half := rangesCount / 2 resultB := evenHalfRanges2(half, totalB, sortedB) resultL := evenHalfRanges2(half, totalL, sortedL) var ranges = make([]float64, rangesCount-1) lenghtL := len(resultL) for i := 1; i <= lenghtL; i++ { ranges[i-1] = 1 - resultL[lenghtL-i] } ranges[lenghtL] = 1 for i := 0; i < len(resultB); i++ { ranges[lenghtL+1+i] = 1 + resultB[i] } return ranges, nil }
// CalculateEvenRanges genetare even diapasons of ranges func CalculateEvenRanges(list []float32, rangesCount int) ([]float64, error) { if rangesCount < 1 || rangesCount%2 == 1 { return nil, errors.New("the ranges count must be positive even value more than 1") } if list == nil || len(list) < rangesCount { return nil, errors.New("quantity of set float32 values must be more than the ranges count and more than 1") } var sortedB sort.Float64Slice var sortedL sort.Float64Slice var totalB float64 var totalL float64 for i := 0; i < len(list)-1; i++ { delta := float64(list[i+1] / list[i]) if delta == 1 { continue } else { if delta > 1 { totalB += delta sortedB = append(sortedB, delta) } else { totalL += delta sortedL = append(sortedL, delta) } } } sortedB.Sort() sortedL.Sort() half := rangesCount / 2 // Hight & Low rangesB, checkB := evenHalfRanges(half, totalB, sortedB) rangesL, checkL := evenHalfRanges(half, totalL, sortedL) if checkB && checkL { var ranges = make([]float64, rangesCount-1) copy(ranges[0:half-1], rangesL) ranges[half] = 1.0 copy(ranges[half+1:rangesCount-2], rangesB) return ranges, nil } return nil, errors.New("CalculateEvenRanges errer: ranges not vialid") }
func (cd *CallDescriptor) GetLCR(stats StatsInterface, p *utils.Paginator) (*LCRCost, error) { cd.account = nil // make sure it's not cached lcr, err := cd.GetLCRFromStorage() if err != nil { return nil, err } // sort by activation time lcr.Sort() // find if one ore more entries apply to this cd (create lcr timespans) // create timespans and attach lcr entries to them lcrCost := &LCRCost{} for _, lcrActivation := range lcr.Activations { //log.Printf("Activation: %+v", lcrActivation) lcrEntry := lcrActivation.GetLCREntryForPrefix(cd.Destination) //log.Printf("Entry: %+v", lcrEntry) if lcrActivation.ActivationTime.Before(cd.TimeStart) || lcrActivation.ActivationTime.Equal(cd.TimeStart) { lcrCost.Entry = lcrEntry } else { // because lcr is sorted the folowing ones will // only activate later than cd.Timestart break } } if lcrCost.Entry == nil { return lcrCost, nil } //log.Printf("Entry: %+v", lcrCost.Entry) if lcrCost.Entry.Strategy == LCR_STRATEGY_STATIC { for _, supplier := range lcrCost.Entry.GetParams() { lcrCD := cd.Clone() lcrCD.Account = supplier lcrCD.Subject = supplier lcrCD.Category = lcrCost.Entry.RPCategory fullSupplier := utils.ConcatenatedKey(lcrCD.Direction, lcrCD.Tenant, lcrCD.Category, lcrCD.Subject) var cc *CallCost var err error if cd.account, err = accountingStorage.GetAccount(lcrCD.GetAccountKey()); err == nil { if cd.account.Disabled { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, Error: fmt.Sprintf("supplier %s is disabled", supplier), }) continue } cc, err = lcrCD.debit(cd.account, true, true) } else { cc, err = lcrCD.GetCost() } //log.Printf("CC: %+v", cc.Timespans[0].ratingInfo.RateIntervals[0].Rating.Rates[0]) if err != nil || cc == nil { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, Error: err.Error(), }) } else { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, Cost: cc.Cost, Duration: cc.GetDuration(), }) } } } else { // find rating profiles category := lcrCost.Entry.RPCategory if category == utils.META_DEFAULT { category = lcr.Category } ratingProfileSearchKey := utils.ConcatenatedKey(lcr.Direction, lcr.Tenant, lcrCost.Entry.RPCategory) //log.Print("KEY: ", ratingProfileSearchKey) suppliers := cache2go.GetEntriesKeys(utils.RATING_PROFILE_PREFIX + ratingProfileSearchKey) for _, supplier := range suppliers { //log.Print("Supplier: ", supplier) split := strings.Split(supplier, ":") supplier = split[len(split)-1] lcrCD := cd.Clone() lcrCD.Category = category lcrCD.Account = supplier lcrCD.Subject = supplier fullSupplier := utils.ConcatenatedKey(lcrCD.Direction, lcrCD.Tenant, lcrCD.Category, lcrCD.Subject) var qosSortParams []string var asrValues sort.Float64Slice var pddValues sort.Float64Slice var acdValues sort.Float64Slice var tcdValues sort.Float64Slice var accValues sort.Float64Slice var tccValues sort.Float64Slice var ddcValues sort.Float64Slice // track if one value is never calculated asrNeverConsidered := true pddNeverConsidered := true acdNeverConsidered := true tcdNeverConsidered := true accNeverConsidered := true tccNeverConsidered := true ddcNeverConsidered := true if utils.IsSliceMember([]string{LCR_STRATEGY_QOS, LCR_STRATEGY_QOS_THRESHOLD, LCR_STRATEGY_LOAD}, lcrCost.Entry.Strategy) { if stats == nil { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, Error: fmt.Sprintf("Cdr stats service not configured"), }) continue } rpfKey := utils.ConcatenatedKey(ratingProfileSearchKey, supplier) if rpf, err := ratingStorage.GetRatingProfile(rpfKey, false); err != nil { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, Error: fmt.Sprintf("Rating plan error: %s", err.Error()), }) continue } else if rpf != nil { rpf.RatingPlanActivations.Sort() activeRas := rpf.RatingPlanActivations.GetActiveForCall(cd) var cdrStatsQueueIds []string for _, ra := range activeRas { for _, qId := range ra.CdrStatQueueIds { if qId != "" { cdrStatsQueueIds = append(cdrStatsQueueIds, qId) } } } statsErr := false var supplierQueues []*StatsQueue for _, qId := range cdrStatsQueueIds { if lcrCost.Entry.Strategy == LCR_STRATEGY_LOAD { for _, qId := range cdrStatsQueueIds { sq := &StatsQueue{} if err := stats.GetQueue(qId, sq); err == nil { if sq.conf.QueueLength == 0 { //only add qeues that don't have fixed length supplierQueues = append(supplierQueues, sq) } } } } else { statValues := make(map[string]float64) if err := stats.GetValues(qId, &statValues); err != nil { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, Error: fmt.Sprintf("Get stats values for queue id %s, error %s", qId, err.Error()), }) statsErr = true break } if asr, exists := statValues[ASR]; exists { if asr > STATS_NA { asrValues = append(asrValues, asr) } asrNeverConsidered = false } if pdd, exists := statValues[PDD]; exists { if pdd > STATS_NA { pddValues = append(pddValues, pdd) } pddNeverConsidered = false } if acd, exists := statValues[ACD]; exists { if acd > STATS_NA { acdValues = append(acdValues, acd) } acdNeverConsidered = false } if tcd, exists := statValues[TCD]; exists { if tcd > STATS_NA { tcdValues = append(tcdValues, tcd) } tcdNeverConsidered = false } if acc, exists := statValues[ACC]; exists { if acc > STATS_NA { accValues = append(accValues, acc) } accNeverConsidered = false } if tcc, exists := statValues[TCC]; exists { if tcc > STATS_NA { tccValues = append(tccValues, tcc) } tccNeverConsidered = false } if ddc, exists := statValues[TCC]; exists { if ddc > STATS_NA { ddcValues = append(ddcValues, ddc) } ddcNeverConsidered = false } } } if lcrCost.Entry.Strategy == LCR_STRATEGY_LOAD { if len(supplierQueues) > 0 { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, supplierQueues: supplierQueues, }) } continue // next supplier } if statsErr { // Stats error in loop, to go next supplier continue } asrValues.Sort() pddValues.Sort() acdValues.Sort() tcdValues.Sort() accValues.Sort() tccValues.Sort() ddcValues.Sort() //log.Print(asrValues, acdValues) if utils.IsSliceMember([]string{LCR_STRATEGY_QOS_THRESHOLD, LCR_STRATEGY_QOS}, lcrCost.Entry.Strategy) { qosSortParams = lcrCost.Entry.GetParams() } if lcrCost.Entry.Strategy == LCR_STRATEGY_QOS_THRESHOLD { // filter suppliers by qos thresholds asrMin, asrMax, pddMin, pddMax, acdMin, acdMax, tcdMin, tcdMax, accMin, accMax, tccMin, tccMax, ddcMin, ddcMax := lcrCost.Entry.GetQOSLimits() //log.Print(asrMin, asrMax, acdMin, acdMax) // skip current supplier if off limits if asrMin > 0 && len(asrValues) != 0 && asrValues[0] < asrMin { continue } if asrMax > 0 && len(asrValues) != 0 && asrValues[len(asrValues)-1] > asrMax { continue } if pddMin > 0 && len(pddValues) != 0 && pddValues[0] < pddMin.Seconds() { continue } if pddMax > 0 && len(pddValues) != 0 && pddValues[len(pddValues)-1] > pddMax.Seconds() { continue } if acdMin > 0 && len(acdValues) != 0 && acdValues[0] < acdMin.Seconds() { continue } if acdMax > 0 && len(acdValues) != 0 && acdValues[len(acdValues)-1] > acdMax.Seconds() { continue } if tcdMin > 0 && len(tcdValues) != 0 && tcdValues[0] < tcdMin.Seconds() { continue } if tcdMax > 0 && len(tcdValues) != 0 && tcdValues[len(tcdValues)-1] > tcdMax.Seconds() { continue } if accMin > 0 && len(accValues) != 0 && accValues[0] < accMin { continue } if accMax > 0 && len(accValues) != 0 && accValues[len(accValues)-1] > accMax { continue } if tccMin > 0 && len(tccValues) != 0 && tccValues[0] < tccMin { continue } if tccMax > 0 && len(tccValues) != 0 && tccValues[len(tccValues)-1] > tccMax { continue } if ddcMin > 0 && len(ddcValues) != 0 && ddcValues[0] < ddcMin { continue } if ddcMax > 0 && len(ddcValues) != 0 && ddcValues[len(ddcValues)-1] > ddcMax { continue } } } } var cc *CallCost var err error //log.Print("CD: ", lcrCD.GetAccountKey()) if cd.account, err = accountingStorage.GetAccount(lcrCD.GetAccountKey()); err == nil { //log.Print("ACCCOUNT") if cd.account.Disabled { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, Error: fmt.Sprintf("supplier %s is disabled", supplier), }) continue } cc, err = lcrCD.debit(cd.account, true, true) } else { //log.Print("STANDARD") cc, err = lcrCD.GetCost() } //log.Printf("CC: %+v", cc) if err != nil || cc == nil { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: fullSupplier, Error: err.Error(), }) continue } else { supplCost := &LCRSupplierCost{ Supplier: fullSupplier, Cost: cc.Cost, Duration: cc.GetDuration(), } qos := make(map[string]float64, 5) if !asrNeverConsidered { qos[ASR] = utils.AvgNegative(asrValues) } if !pddNeverConsidered { qos[PDD] = utils.AvgNegative(pddValues) } if !acdNeverConsidered { qos[ACD] = utils.AvgNegative(acdValues) } if !tcdNeverConsidered { qos[TCD] = utils.AvgNegative(tcdValues) } if !accNeverConsidered { qos[ACC] = utils.AvgNegative(accValues) } if !tccNeverConsidered { qos[TCC] = utils.AvgNegative(tccValues) } if !ddcNeverConsidered { qos[DDC] = utils.AvgNegative(ddcValues) } if utils.IsSliceMember([]string{LCR_STRATEGY_QOS, LCR_STRATEGY_QOS_THRESHOLD}, lcrCost.Entry.Strategy) { supplCost.QOS = qos supplCost.qosSortParams = qosSortParams } lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, supplCost) } } // sort according to strategy lcrCost.Sort() } if p != nil { if p.Offset != nil && *p.Offset > 0 && *p.Offset < len(lcrCost.SupplierCosts) { lcrCost.SupplierCosts = lcrCost.SupplierCosts[*p.Offset:] } if p.Limit != nil && *p.Limit > 0 && *p.Limit < len(lcrCost.SupplierCosts) { lcrCost.SupplierCosts = lcrCost.SupplierCosts[:*p.Limit] } } return lcrCost, nil }