コード例 #1
0
ファイル: scheduler.go プロジェクト: nikbyte/cgrates
func (self *ApierV1) GetScheduledActions(attrs AttrsGetScheduledActions, reply *[]*ScheduledActions) error {
	schedActions := make([]*ScheduledActions, 0)
	if self.Sched == nil {
		return errors.New("SCHEDULER_NOT_ENABLED")
	}
	scheduledActions := self.Sched.GetQueue()
	var min, max int
	if attrs.Paginator.Offset != nil {
		min = *attrs.Paginator.Offset
	}
	if attrs.Paginator.Limit != nil {
		max = *attrs.Paginator.Limit
	}
	if max > len(scheduledActions) {
		max = len(scheduledActions)
	}
	scheduledActions = scheduledActions[min : min+max]
	for _, qActions := range scheduledActions {
		sas := &ScheduledActions{ActionsId: qActions.ActionsId, ActionPlanId: qActions.Id, ActionPlanUuid: qActions.Uuid}
		if attrs.SearchTerm != "" &&
			!(strings.Contains(sas.ActionPlanId, attrs.SearchTerm) ||
				strings.Contains(sas.ActionsId, attrs.SearchTerm)) {
			continue
		}
		sas.NextRunTime = qActions.GetNextStartTime(time.Now())
		if !attrs.TimeStart.IsZero() && sas.NextRunTime.Before(attrs.TimeStart) {
			continue // Filter here only requests in the filtered interval
		}
		if !attrs.TimeEnd.IsZero() && (sas.NextRunTime.After(attrs.TimeEnd) || sas.NextRunTime.Equal(attrs.TimeEnd)) {
			continue
		}
		acntFiltersMatch := false
		for _, acntKey := range qActions.AccountIds {
			directionMatched := len(attrs.Direction) == 0
			tenantMatched := len(attrs.Tenant) == 0
			accountMatched := len(attrs.Account) == 0
			dta, _ := utils.NewDTAFromAccountKey(acntKey)
			sas.Accounts = append(sas.Accounts, dta)
			// One member matching
			if !directionMatched && attrs.Direction == dta.Direction {
				directionMatched = true
			}
			if !tenantMatched && attrs.Tenant == dta.Tenant {
				tenantMatched = true
			}
			if !accountMatched && attrs.Account == dta.Account {
				accountMatched = true
			}
			if directionMatched && tenantMatched && accountMatched {
				acntFiltersMatch = true
			}
		}
		if !acntFiltersMatch {
			continue
		}
		schedActions = append(schedActions, sas)
	}
	*reply = schedActions
	return nil
}
コード例 #2
0
ファイル: action.go プロジェクト: henrylee2cn/cgrates
// Used by cdrLogAction to dynamically parse values out of account and action
func parseTemplateValue(rsrFlds utils.RSRFields, acnt *Account, action *Action) string {
	dta, err := utils.NewDTAFromAccountKey(acnt.Id) // Account information should be valid
	if err != nil {
		dta = new(utils.DirectionTenantAccount) // Init with empty values
	}
	var parsedValue string // Template values
	for _, rsrFld := range rsrFlds {
		switch rsrFld.Id {
		case "account_id":
			parsedValue += rsrFld.ParseValue(acnt.Id)
		case "direction":
			parsedValue += rsrFld.ParseValue(dta.Direction)
		case "tenant":
			parsedValue += rsrFld.ParseValue(dta.Tenant)
		case "account":
			parsedValue += rsrFld.ParseValue(dta.Account)
		case "action_id":
			parsedValue += rsrFld.ParseValue(action.Id)
		case "action_type":
			parsedValue += rsrFld.ParseValue(action.ActionType)
		case "balance_type":
			parsedValue += rsrFld.ParseValue(action.BalanceType)
		case "balance_uuid":
			parsedValue += rsrFld.ParseValue(action.Balance.Uuid)
		case "balance_id":
			parsedValue += rsrFld.ParseValue(action.Balance.Id)
		case "balance_value":
			parsedValue += rsrFld.ParseValue(strconv.FormatFloat(action.Balance.GetValue(), 'f', -1, 64))
		case "destination_id":
			parsedValue += rsrFld.ParseValue(action.Balance.DestinationIds)
		case "extra_params":
			parsedValue += rsrFld.ParseValue(action.ExtraParameters)
		case "rating_subject":
			parsedValue += rsrFld.ParseValue(action.Balance.RatingSubject)
		case "category":
			parsedValue += rsrFld.ParseValue(action.Balance.Category)
		case "shared_group":
			parsedValue += rsrFld.ParseValue(action.Balance.SharedGroup)
		default:
			parsedValue += rsrFld.ParseValue("") // Mostly for static values
		}
	}
	return parsedValue
}
コード例 #3
0
// Test adding the account via API, using the data previously devined in .csv
func TestTutLocalSetAccount(t *testing.T) {
	if !*testLocal {
		return
	}
	var reply string
	attrs := &utils.AttrSetAccount{Tenant: "cgrates.org", Direction: "*out", Account: "tutacnt1", ActionPlanId: "PACKAGE_10", ActionTriggersId: "STANDARD_TRIGGERS"}
	if err := tutLocalRpc.Call("ApierV1.SetAccount", attrs, &reply); err != nil {
		t.Error("Got error on ApierV1.SetAccount: ", err.Error())
	} else if reply != "OK" {
		t.Errorf("Calling ApierV1.SetAccount received: %s", reply)
	}
	type AttrGetAccounts struct {
		Tenant     string
		Direction  string
		AccountIds []string
		Offset     int // Set the item offset
		Limit      int // Limit number of items retrieved
	}
	var acnts []*engine.Account
	if err := tutLocalRpc.Call("ApierV1.GetAccounts", utils.AttrGetAccounts{Tenant: attrs.Tenant, Direction: attrs.Direction, AccountIds: []string{attrs.Account}}, &acnts); err != nil {
		t.Error(err)
	} else if len(acnts) != 1 {
		t.Errorf("Accounts received: %+v", acnts)
	} else {
		acnt := acnts[0]
		dta, _ := utils.NewDTAFromAccountKey(acnt.Id)
		if dta.Direction != attrs.Direction || dta.Tenant != attrs.Tenant || dta.Account != attrs.Account {
			t.Error("Unexpected account id received: ", acnt.Id)
		}
		if balances := acnt.BalanceMap["*monetary*out"]; len(balances) != 1 {
			t.Errorf("Unexpected balances found: %+v", balances)
		}
		if len(acnt.ActionTriggers) != 4 {
			t.Errorf("Unexpected action triggers for account: %+v", acnt.ActionTriggers)
		}
		if acnt.AllowNegative {
			t.Error("AllowNegative should not be set")
		}
		if acnt.Disabled {
			t.Error("Disabled should not be set")
		}
	}
	attrs = &utils.AttrSetAccount{Tenant: "cgrates.org", Direction: "*out", Account: "tutacnt1", AllowNegative: utils.BoolPointer(true), Disabled: utils.BoolPointer(true)}
	if err := tutLocalRpc.Call("ApierV1.SetAccount", attrs, &reply); err != nil {
		t.Error("Got error on ApierV1.SetAccount: ", err.Error())
	} else if reply != "OK" {
		t.Errorf("Calling ApierV1.SetAccount received: %s", reply)
	}
	if err := tutLocalRpc.Call("ApierV1.GetAccounts", utils.AttrGetAccounts{Tenant: attrs.Tenant, Direction: attrs.Direction, AccountIds: []string{attrs.Account}}, &acnts); err != nil {
		t.Error(err)
	} else if len(acnts) != 1 {
		t.Errorf("Accounts received: %+v", acnts)
	} else {
		acnt := acnts[0]
		dta, _ := utils.NewDTAFromAccountKey(acnt.Id)
		if dta.Direction != attrs.Direction || dta.Tenant != attrs.Tenant || dta.Account != attrs.Account {
			t.Error("Unexpected account id received: ", acnt.Id)
		}
		if balances := acnt.BalanceMap["*monetary*out"]; len(balances) != 1 {
			t.Errorf("Unexpected balances found: %+v", balances)
		}
		if len(acnt.ActionTriggers) != 4 {
			t.Errorf("Unexpected action triggers for account: %+v", acnt.ActionTriggers)
		}
		if !acnt.AllowNegative {
			t.Error("AllowNegative should be set")
		}
		if !acnt.Disabled {
			t.Error("Disabled should be set")
		}
	}

}