diff --git a/apier/v1/suppliers.go b/apier/v1/suppliers.go index 725040fae..1d85ca7ee 100644 --- a/apier/v1/suppliers.go +++ b/apier/v1/suppliers.go @@ -82,7 +82,7 @@ func (splv1 *SupplierSv1) Call(serviceMethod string, } // GetSuppliers returns sorted list of suppliers for Event -func (splv1 *SupplierSv1) GetSuppliers(args *engine.SupplierEvent, +func (splv1 *SupplierSv1) GetSuppliers(args *utils.CGREvent, reply *engine.SortedSuppliers) error { return splv1.splS.V1GetSuppliers(args, reply) } diff --git a/apier/v1/suppliers_it_test.go b/apier/v1/suppliers_it_test.go index 42c386daf..291817990 100644 --- a/apier/v1/suppliers_it_test.go +++ b/apier/v1/suppliers_it_test.go @@ -119,7 +119,7 @@ func testV1SplSFromFolder(t *testing.T) { } func testV1SplSGetWeightSuppliers(t *testing.T) { - ev := &engine.SupplierEvent{ + ev := &utils.CGREvent{ Tenant: "cgrates.org", ID: "testV1SplSGetWeightSuppliers", Event: map[string]interface{}{ diff --git a/engine/libsuppliers.go b/engine/libsuppliers.go index 020570024..1dcc481d4 100644 --- a/engine/libsuppliers.go +++ b/engine/libsuppliers.go @@ -21,7 +21,7 @@ package engine import ( "fmt" "sort" - "time" + // "time" "github.com/cgrates/cgrates/utils" ) @@ -57,76 +57,9 @@ func (sSpls *SortedSuppliers) SortCost() { }) } -// SupplierEvent is an event processed by Supplier Service -type SupplierEvent struct { - Tenant string - ID string - Event map[string]interface{} -} - -func (se *SupplierEvent) CheckMandatoryFields(fldNames []string) error { - for _, fldName := range fldNames { - if _, has := se.Event[fldName]; !has { - return utils.NewErrMandatoryIeMissing(fldName) - } - } - return nil -} - -// AnswerTime returns the AnswerTime of SupplierEvent -func (le *SupplierEvent) FieldAsString(fldName string) (val string, err error) { - iface, has := le.Event[fldName] - if !has { - return "", utils.ErrNotFound - } - val, canCast := utils.CastFieldIfToString(iface) - if !canCast { - return "", fmt.Errorf("cannot cast %s to string", fldName) - } - return val, nil -} - -// FieldAsTime returns the a field as Time instance -func (le *SupplierEvent) FieldAsTime(fldName string, timezone string) (t time.Time, err error) { - iface, has := le.Event[fldName] - if !has { - err = utils.ErrNotFound - return - } - var canCast bool - if t, canCast = iface.(time.Time); canCast { - return - } - s, canCast := iface.(string) - if !canCast { - err = fmt.Errorf("cannot cast %s to string", fldName) - return - } - return utils.ParseTimeDetectLayout(s, timezone) -} - -// FieldAsTime returns the a field as Time instance -func (le *SupplierEvent) FieldAsDuration(fldName string) (d time.Duration, err error) { - iface, has := le.Event[fldName] - if !has { - err = utils.ErrNotFound - return - } - var canCast bool - if d, canCast = iface.(time.Duration); canCast { - return - } - s, canCast := iface.(string) - if !canCast { - err = fmt.Errorf("cannot cast %s to string", fldName) - return - } - return utils.ParseDurationWithNanosecs(s) -} - // SuppliersSorter is the interface which needs to be implemented by supplier sorters type SuppliersSorter interface { - SortSuppliers(string, []*Supplier, *SupplierEvent) (*SortedSuppliers, error) + SortSuppliers(string, []*Supplier, *utils.CGREvent) (*SortedSuppliers, error) } // NewSupplierSortDispatcher constructs SupplierSortDispatcher @@ -142,7 +75,7 @@ func NewSupplierSortDispatcher(lcrS *SupplierService) (ssd SupplierSortDispatche type SupplierSortDispatcher map[string]SuppliersSorter func (ssd SupplierSortDispatcher) SortSuppliers(prflID, strategy string, - suppls []*Supplier, suplEv *SupplierEvent) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent) (sortedSuppls *SortedSuppliers, err error) { sd, has := ssd[strategy] if !has { return nil, fmt.Errorf("unsupported sorting strategy: %s", strategy) @@ -160,7 +93,7 @@ type WeightSorter struct { } func (ws *WeightSorter) SortSuppliers(prflID string, - suppls []*Supplier, suplEv *SupplierEvent) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, suplEv *utils.CGREvent) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: ws.sorting, SortedSuppliers: make([]*SortedSupplier, len(suppls))} diff --git a/engine/libsuppliers_test.go b/engine/libsuppliers_test.go index 0ce3ad36e..a78ddb739 100644 --- a/engine/libsuppliers_test.go +++ b/engine/libsuppliers_test.go @@ -121,7 +121,7 @@ func TestLibSuppliersSortWeight(t *testing.T) { }, }, } - se := &SupplierEvent{ + se := &utils.CGREvent{ Tenant: "cgrates.org", ID: "supplierevent1", Event: make(map[string]interface{}), diff --git a/engine/spls_leastcost.go b/engine/spls_leastcost.go index 45312585d..b55eaabaf 100644 --- a/engine/spls_leastcost.go +++ b/engine/spls_leastcost.go @@ -36,7 +36,7 @@ type LeastCostSorter struct { } func (lcs *LeastCostSorter) SortSuppliers(prflID string, - suppls []*Supplier, ev *SupplierEvent) (sortedSuppls *SortedSuppliers, err error) { + suppls []*Supplier, ev *utils.CGREvent) (sortedSuppls *SortedSuppliers, err error) { sortedSuppls = &SortedSuppliers{ProfileID: prflID, Sorting: lcs.sorting, SortedSuppliers: make([]*SortedSupplier, 0)} diff --git a/engine/suppliers.go b/engine/suppliers.go index 5fe401a7f..315717fd5 100644 --- a/engine/suppliers.go +++ b/engine/suppliers.go @@ -111,7 +111,7 @@ func (spS *SupplierService) Shutdown() error { } // matchingSupplierProfilesForEvent returns ordered list of matching resources which are active by the time of the call -func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *SupplierEvent) (sPrfls SupplierProfiles, err error) { +func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *utils.CGREvent) (sPrfls SupplierProfiles, err error) { matchingLPs := make(map[string]*SupplierProfile) sPrflIDs, err := matchingItemIDsForEvent(ev.Event, spS.indexedFields, spS.dm, utils.SupplierProfilesStringIndex+ev.Tenant) @@ -168,7 +168,7 @@ func (spS *SupplierService) matchingSupplierProfilesForEvent(ev *SupplierEvent) // costForEvent will compute cost out of accounts and rating plans for event // returns map[string]interface{} with cost and relevant matching information inside -func (spS *SupplierService) costForEvent(ev *SupplierEvent, +func (spS *SupplierService) costForEvent(ev *utils.CGREvent, acntIDs, rpIDs []string) (costData map[string]interface{}, err error) { if err = ev.CheckMandatoryFields([]string{utils.ACCOUNT, utils.DESTINATION, utils.ANSWER_TIME, utils.USAGE}); err != nil { @@ -272,7 +272,7 @@ func (spS *SupplierService) resourceUsage(resIDs []string) (tUsage float64, err // supliersForEvent will return the list of valid supplier IDs // for event based on filters and sorting algorithms -func (spS *SupplierService) sortedSuppliersForEvent(ev *SupplierEvent) (sortedSuppls *SortedSuppliers, err error) { +func (spS *SupplierService) sortedSuppliersForEvent(ev *utils.CGREvent) (sortedSuppls *SortedSuppliers, err error) { var suppPrfls SupplierProfiles if suppPrfls, err = spS.matchingSupplierProfilesForEvent(ev); err != nil { return @@ -296,7 +296,7 @@ func (spS *SupplierService) sortedSuppliersForEvent(ev *SupplierEvent) (sortedSu } // V1GetSuppliersForEvent returns the list of valid supplier IDs -func (spS *SupplierService) V1GetSuppliers(args *SupplierEvent, reply *SortedSuppliers) (err error) { +func (spS *SupplierService) V1GetSuppliers(args *utils.CGREvent, reply *SortedSuppliers) (err error) { if missing := utils.MissingStructFields(args, []string{"Tenant", "ID"}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } diff --git a/engine/suppliers_test.go b/engine/suppliers_test.go index 65d19ac98..37966d273 100644 --- a/engine/suppliers_test.go +++ b/engine/suppliers_test.go @@ -28,7 +28,7 @@ import ( var ( splserv SupplierService - sev *SupplierEvent + sev *utils.CGREvent dmspl *DataManager sprsmatch SupplierProfiles ) @@ -197,9 +197,9 @@ func TestSuppliersPopulateSupplierService(t *testing.T) { ev["UsageInterval"] = "1s" ev["PddInterval"] = "1s" ev["Weight"] = "20.0" - sev = &SupplierEvent{ + sev = &utils.CGREvent{ Tenant: "cgrates.org", - ID: "supplierevent1", + ID: "utils.CGREvent1", Event: ev, } sprsmatch = SupplierProfiles{