mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
added user search ponders
This commit is contained in:
@@ -58,6 +58,6 @@ func (self *CmdGetUsers) PostprocessRpcParams() error {
|
||||
}
|
||||
|
||||
func (self *CmdGetUsers) RpcResult() interface{} {
|
||||
s := []*engine.UserProfile{}
|
||||
s := engine.UserProfiles{}
|
||||
return &s
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
@@ -11,6 +12,25 @@ type UserProfile struct {
|
||||
Tenant string
|
||||
UserName string
|
||||
Profile map[string]string
|
||||
ponder int
|
||||
}
|
||||
|
||||
type UserProfiles []*UserProfile
|
||||
|
||||
func (ups UserProfiles) Len() int {
|
||||
return len(ups)
|
||||
}
|
||||
|
||||
func (ups UserProfiles) Swap(i, j int) {
|
||||
ups[i], ups[j] = ups[j], ups[i]
|
||||
}
|
||||
|
||||
func (ups UserProfiles) Less(j, i int) bool { // get higher ponder in front
|
||||
return ups[i].ponder < ups[j].ponder
|
||||
}
|
||||
|
||||
func (ups UserProfiles) Sort() {
|
||||
sort.Sort(ups)
|
||||
}
|
||||
|
||||
func (ud *UserProfile) GetId() string {
|
||||
@@ -31,7 +51,7 @@ type UserService interface {
|
||||
SetUser(UserProfile, *string) error
|
||||
RemoveUser(UserProfile, *string) error
|
||||
UpdateUser(UserProfile, *string) error
|
||||
GetUsers(UserProfile, *[]*UserProfile) error
|
||||
GetUsers(UserProfile, *UserProfiles) error
|
||||
AddIndex([]string, *string) error
|
||||
GetIndexes(string, *map[string][]string) error
|
||||
}
|
||||
@@ -123,7 +143,7 @@ func (um *UserMap) UpdateUser(up UserProfile, reply *string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (um *UserMap) GetUsers(up UserProfile, results *[]*UserProfile) error {
|
||||
func (um *UserMap) GetUsers(up UserProfile, results *UserProfiles) error {
|
||||
table := um.table // no index
|
||||
|
||||
indexUnionKeys := make(map[string]bool)
|
||||
@@ -156,20 +176,34 @@ func (um *UserMap) GetUsers(up UserProfile, results *[]*UserProfile) error {
|
||||
}
|
||||
}
|
||||
|
||||
var candidates []*UserProfile
|
||||
var candidates UserProfiles
|
||||
for key, values := range table {
|
||||
if up.Tenant != "" && !strings.HasPrefix(key, up.Tenant+utils.CONCATENATED_KEY_SEP) {
|
||||
ponder := 0
|
||||
tableUP := &UserProfile{
|
||||
Profile: values,
|
||||
}
|
||||
tableUP.SetId(key)
|
||||
if up.Tenant != "" && tableUP.Tenant != "" && up.Tenant != tableUP.Tenant {
|
||||
continue
|
||||
}
|
||||
if up.UserName != "" && !strings.HasSuffix(key, utils.CONCATENATED_KEY_SEP+up.UserName) {
|
||||
if tableUP.Tenant != "" {
|
||||
ponder += 1
|
||||
}
|
||||
if up.UserName != "" && tableUP.UserName != "" && up.UserName != tableUP.UserName {
|
||||
continue
|
||||
}
|
||||
if tableUP.UserName != "" {
|
||||
ponder += 1
|
||||
}
|
||||
valid := true
|
||||
for k, v := range up.Profile {
|
||||
if values[k] != v {
|
||||
if tableUP.Profile[k] != "" && tableUP.Profile[k] != v {
|
||||
valid = false
|
||||
break
|
||||
}
|
||||
if tableUP.Profile[k] != "" {
|
||||
ponder += 1
|
||||
}
|
||||
}
|
||||
if !valid {
|
||||
continue
|
||||
@@ -177,11 +211,13 @@ func (um *UserMap) GetUsers(up UserProfile, results *[]*UserProfile) error {
|
||||
// all filters passed, add to candidates
|
||||
nup := &UserProfile{Profile: make(map[string]string)}
|
||||
nup.SetId(key)
|
||||
for k, v := range values {
|
||||
nup.ponder = ponder
|
||||
for k, v := range tableUP.Profile {
|
||||
nup.Profile[k] = v
|
||||
}
|
||||
candidates = append(candidates, nup)
|
||||
}
|
||||
candidates.Sort()
|
||||
*results = candidates
|
||||
return nil
|
||||
}
|
||||
@@ -307,7 +343,7 @@ func (ps *ProxyUserService) UpdateUser(ud UserProfile, reply *string) error {
|
||||
return ps.Client.Call("UsersV1.UpdateUser", ud, reply)
|
||||
}
|
||||
|
||||
func (ps *ProxyUserService) GetUsers(ud UserProfile, users *[]*UserProfile) error {
|
||||
func (ps *ProxyUserService) GetUsers(ud UserProfile, users *UserProfiles) error {
|
||||
return ps.Client.Call("UsersV1.GetUsers", ud, users)
|
||||
}
|
||||
|
||||
@@ -343,7 +379,7 @@ func LoadUserProfile(in interface{}) (interface{}, error) {
|
||||
|
||||
//TODO: add extra fields
|
||||
|
||||
ups := make([]*UserProfile, 0)
|
||||
ups := UserProfiles{}
|
||||
if err := userService.GetUsers(*up, &ups); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -149,9 +149,9 @@ func TestUsersGetFull(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 1 {
|
||||
if len(results) != 3 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -164,9 +164,9 @@ func TestUsersGetTenant(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
if len(results) != 1 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -179,9 +179,9 @@ func TestUsersGetUserName(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
if len(results) != 1 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -194,9 +194,9 @@ func TestUsersGetNotFoundProfile(t *testing.T) {
|
||||
"o": "p",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
if len(results) != 3 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -208,9 +208,9 @@ func TestUsersGetMissingTenant(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 2 {
|
||||
if len(results) != 3 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -222,9 +222,9 @@ func TestUsersGetMissingUserName(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 2 {
|
||||
if len(results) != 3 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -235,7 +235,7 @@ func TestUsersGetMissingId(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 4 {
|
||||
t.Error("error getting users: ", results)
|
||||
@@ -249,13 +249,30 @@ func TestUsersGetMissingIdTwo(t *testing.T) {
|
||||
"x": "y",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 1 {
|
||||
if len(results) != 4 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersGetMissingIdTwoSort(t *testing.T) {
|
||||
up := UserProfile{
|
||||
Profile: map[string]string{
|
||||
"t": "v",
|
||||
"x": "y",
|
||||
},
|
||||
}
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 4 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
if results[0].GetId() != "test1:user1" {
|
||||
t.Errorf("Error sorting profiles: %+v", results[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestUsersAddIndex(t *testing.T) {
|
||||
var r string
|
||||
testMap.AddIndex([]string{"t"}, &r)
|
||||
@@ -298,9 +315,9 @@ func TestUsersGetFullindex(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 1 {
|
||||
if len(results) != 3 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -316,9 +333,9 @@ func TestUsersGetTenantindex(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
if len(results) != 1 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -334,9 +351,9 @@ func TestUsersGetUserNameindex(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
if len(results) != 1 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -352,9 +369,9 @@ func TestUsersGetNotFoundProfileindex(t *testing.T) {
|
||||
"o": "p",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 0 {
|
||||
if len(results) != 3 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -369,9 +386,9 @@ func TestUsersGetMissingTenantindex(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 2 {
|
||||
if len(results) != 3 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -386,9 +403,9 @@ func TestUsersGetMissingUserNameindex(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 2 {
|
||||
if len(results) != 3 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
@@ -402,7 +419,7 @@ func TestUsersGetMissingIdindex(t *testing.T) {
|
||||
"t": "v",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 4 {
|
||||
t.Error("error getting users: ", results)
|
||||
@@ -419,9 +436,9 @@ func TestUsersGetMissingIdTwoINdex(t *testing.T) {
|
||||
"x": "y",
|
||||
},
|
||||
}
|
||||
results := make([]*UserProfile, 0)
|
||||
results := UserProfiles{}
|
||||
testMap.GetUsers(up, &results)
|
||||
if len(results) != 1 {
|
||||
if len(results) != 4 {
|
||||
t.Error("error getting users: ", results)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user