コード例 #1
0
ファイル: storage_redis.go プロジェクト: rinor/cgrates
func (rs *RedisStorage) PreloadCacheForPrefix(prefix string) error {
	transID := cache.BeginTransaction()
	cache.RemPrefixKey(prefix, false, transID)
	keyList, err := rs.GetKeysForPrefix(prefix)
	if err != nil {
		cache.RollbackTransaction(transID)
		return err
	}
	switch prefix {
	case utils.RATING_PLAN_PREFIX:
		for _, key := range keyList {
			_, err := rs.GetRatingPlan(key[len(utils.RATING_PLAN_PREFIX):], true, transID)
			if err != nil {
				cache.RollbackTransaction(transID)
				return err
			}
		}
	case utils.ResourceLimitsPrefix:
		for _, key := range keyList {
			_, err = rs.GetResourceLimit(key[len(utils.ResourceLimitsPrefix):], true, transID)
			if err != nil {
				cache.RollbackTransaction(transID)
				return err
			}
		}
	default:
		return utils.ErrInvalidKey
	}
	cache.CommitTransaction(transID)
	return nil
}
コード例 #2
0
ファイル: calldesc.go プロジェクト: cgrates/cgrates
func (cd *CallDescriptor) GetLCR(stats rpcclient.RpcClientConnection, lcrFltr *LCRFilter, 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 {
		lcrEntry := lcrActivation.GetLCREntryForPrefix(cd.Destination)
		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
	}
	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)
		searchKey := utils.RATING_PROFILE_PREFIX + ratingProfileSearchKey
		suppliers := cache.GetEntryKeys(searchKey)
		if len(suppliers) == 0 { // Most probably the data was not cached, do it here, #ToDo: move logic in RAL service
			suppliers, err = ratingStorage.GetKeysForPrefix(searchKey)
			if err != nil {
				return nil, err
			}
			transID := utils.GenUUID()
			for _, dbKey := range suppliers {
				if _, err := ratingStorage.GetRatingProfile(dbKey[len(utils.RATING_PROFILE_PREFIX):], true, transID); err != nil { // cache the keys here
					cache.RollbackTransaction(transID)
					return nil, err
				}
			}
			cache.CommitTransaction(transID)
		}
		for _, supplier := range suppliers {
			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 := RatingProfileSubjectPrefixMatching(rpfKey); 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.Call("CDRStatsV1.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.Call("CDRStatsV1.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
			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()
			}
			if err != nil || cc == nil {
				lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{
					Supplier: fullSupplier,
					Error:    err.Error(),
				})
				continue
			} else {
				if lcrFltr != nil {
					if lcrFltr.MinCost != nil && cc.Cost < *lcrFltr.MinCost {
						continue // MinCost not reached, ignore the supplier
					}
					if lcrFltr.MaxCost != nil && cc.Cost >= *lcrFltr.MaxCost {
						continue // Equal or higher than MaxCost allowed, ignore the supplier
					}
				}
				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
}