diff --git a/engine/lcrs.go b/engine/lcrs.go index d02687a11..4d00ae683 100644 --- a/engine/lcrs.go +++ b/engine/lcrs.go @@ -97,6 +97,19 @@ func (lps LCRProfiles) Sort() { sort.Slice(lps, func(i, j int) bool { return lps[i].Weight > lps[j].Weight }) } +// SuppliersReply is returned as part of GetSuppliers call +type SortedSuppliers struct { + ProfileID string + Sorting string + SortedSuppliers []*SortedSupplier +} + +// SupplierReply represents one supplier in +type SortedSupplier struct { + SupplierID string + SortingData map[string]interface{} // store here extra info like cost or stats +} + // NewLCRService initializes a LCRService func NewLCRService(dm *DataManager, timezone string, filterS *FilterS, indexedFields []string, resourceS, @@ -210,7 +223,7 @@ func (lcrS *LCRService) resourceUsage(resIDs []string) (tUsage float64, err erro // supliersForEvent will return the list of valid supplier IDs // for event based on filters and sorting algorithms -func (lcrS *LCRService) supliersForEvent(ev *LCREvent) (lsIDs []string, err error) { +func (lcrS *LCRService) supliersForEvent(ev *LCREvent) (sortedSuppls *SortedSuppliers, err error) { var lcrPrfls LCRProfiles if lcrPrfls, err = lcrS.matchingLCRProfilesForEvent(ev); err != nil { return @@ -230,5 +243,5 @@ func (lcrS *LCRService) supliersForEvent(ev *LCREvent) (lsIDs []string, err erro } lss = append(lss, s) } - return lcrS.sortDispatcher.SortedSupplierIDs(lcrPrfl.Sorting, lss) + return lcrS.sortDispatcher.SortSuppliers(lcrPrfl.ID, lcrPrfl.Sorting, lss) } diff --git a/engine/liblcrs.go b/engine/liblcrs.go index fa19bc0e6..fece60d7f 100644 --- a/engine/liblcrs.go +++ b/engine/liblcrs.go @@ -27,7 +27,7 @@ import ( // NewSupplierSortDispatcher constructs SupplierSortDispatcher func NewSupplierSortDispatcher(lcrS *LCRService) (ssd SupplierSortDispatcher, err error) { ssd = make(map[string]SuppliersSorting) - ssd[utils.MetaWeight] = new(WeightStrategy) + ssd[utils.MetaWeight] = NewWeightStrategy() ssd[utils.MetaLeastCost] = NewLeastCostStrategy(lcrS) return } @@ -36,17 +36,17 @@ func NewSupplierSortDispatcher(lcrS *LCRService) (ssd SupplierSortDispatcher, er // and dispatch requests to them type SupplierSortDispatcher map[string]SuppliersSorting -func (ssd SupplierSortDispatcher) SortedSupplierIDs(strategy string, - suppls LCRSuppliers) (lsIDs []string, err error) { +func (ssd SupplierSortDispatcher) SortSuppliers(prflID, strategy string, + suppls LCRSuppliers) (sortedSuppls *SortedSuppliers, err error) { sd, has := ssd[strategy] if !has { return nil, fmt.Errorf("unsupported sorting strategy: %s", strategy) } - return sd.SortedSupplierIDs(suppls) + return sd.SortSuppliers(prflID, suppls) } type SuppliersSorting interface { - SortedSupplierIDs(LCRSuppliers) ([]string, error) + SortSuppliers(string, LCRSuppliers) (*SortedSuppliers, error) } // NewLeastCostStrategy constructs LeastCostStrategy @@ -59,19 +59,30 @@ type LeastCostStrategy struct { lcrS *LCRService } -func (lcs *LeastCostStrategy) SortedSupplierIDs(suppls LCRSuppliers) (lsIDs []string, err error) { +func (lcs *LeastCostStrategy) SortSuppliers(prflID string, + suppls LCRSuppliers) (sortedSuppls *SortedSuppliers, err error) { return } +func NewWeightStrategy() *WeightStrategy { + return &WeightStrategy{Sorting: utils.MetaWeight} +} + // WeightStrategy orders suppliers based on their weight, no cost involved type WeightStrategy struct { + Sorting string } -func (ws *WeightStrategy) SortedSupplierIDs(suppls LCRSuppliers) (lsIDs []string, err error) { +func (ws *WeightStrategy) SortSuppliers(prflID string, + suppls LCRSuppliers) (sortedSuppls *SortedSuppliers, err error) { suppls.Sort() - lsIDs = make([]string, len(suppls)) + sortedSuppls = &SortedSuppliers{ProfileID: prflID, + Sorting: ws.Sorting, + SortedSuppliers: make([]*SortedSupplier, len(suppls))} for i, s := range suppls { - lsIDs[i] = s.ID + sortedSuppls.SortedSuppliers[i] = &SortedSupplier{ + SupplierID: s.ID, + SortingData: map[string]interface{}{"Weight": s.Weight}} } return }