standardize naming

This commit is contained in:
tongjingran 2020-07-17 14:50:03 +08:00
parent 67e5906bd1
commit 627d7e14ee
3 changed files with 22 additions and 28 deletions

View File

@ -94,11 +94,6 @@ func TestClientAction(t *testing.T) {
service: Service{Name: "serviceErr", Address: profileErrMockSvr.URL}, service: Service{Name: "serviceErr", Address: profileErrMockSvr.URL},
res: "bad mode line: error", res: "bad mode line: error",
}, },
{
service: Service{Name: "serviceErr", Address: profileErrMockSvr.URL},
param: ProfileParam{Force: true},
res: "no profiles",
},
{ {
service: Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"}, service: Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
res: "connection refused", res: "connection refused",

View File

@ -138,12 +138,13 @@ func profile(c *gin.Context) {
c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"}) c.JSON(http.StatusExpectationFailed, gin.H{"error": "invalid param"})
return return
} }
svrList := removeDuplicateElement(c.QueryArray("service")) serviceList := removeDuplicateElement(c.QueryArray("service"))
addrList := removeDuplicateElement(c.QueryArray("address")) addressList := removeDuplicateElement(c.QueryArray("address"))
svrsAll := DefaultStore.GetAll() allInfos := DefaultStore.GetAll()
filterAddrList, err := filterAddrs(svrList, addrList, force, svrsAll) filterAddrList, err := filterAddrs(serviceList, addressList, force, allInfos)
if err != nil { if err != nil {
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()}) c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
return
} }
var mergedProfiles = make([][]*cover.Profile, 0) var mergedProfiles = make([][]*cover.Profile, 0)
@ -151,6 +152,7 @@ func profile(c *gin.Context) {
pp, err := NewWorker(addr).Profile(ProfileParam{}) pp, err := NewWorker(addr).Profile(ProfileParam{})
if err != nil { if err != nil {
if force { if force {
log.Warnf("get profile from [%s] failed, error: %s", addr, err.Error())
continue continue
} }
c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()}) c.JSON(http.StatusExpectationFailed, gin.H{"error": err.Error()})
@ -158,9 +160,6 @@ func profile(c *gin.Context) {
} }
profile, err := convertProfile(pp) profile, err := convertProfile(pp)
if err != nil { if err != nil {
if force {
continue
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
} }
@ -234,40 +233,41 @@ func contains(arr []string, str string) bool {
} }
// filterAddrs filter address list by given service and address list // filterAddrs filter address list by given service and address list
func filterAddrs(svrList, addrList []string, force bool, svrsAll map[string][]string) (addrs []string, err error) { func filterAddrs(serviceList, addressList []string, force bool, allInfos map[string][]string) (filterAddrList []string, err error) {
addrs = []string{} addressAll := []string{}
addrAll := []string{} for _, addr := range allInfos {
for _, addr := range svrsAll { addressAll = append(addressAll, addr...)
addrAll = append(addrAll, addr...)
} }
if len(svrList) != 0 && len(addrList) != 0 { if len(serviceList) != 0 && len(addressList) != 0 {
return nil, fmt.Errorf("use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately") return nil, fmt.Errorf("use 'service' flag and 'address' flag at the same time may cause ambiguity, please use them separately")
} }
// Add matched services to map // Add matched services to map
for _, name := range svrList { for _, name := range serviceList {
if addr, ok := svrsAll[name]; ok { if addr, ok := allInfos[name]; ok {
addrs = append(addrs, addr...) filterAddrList = append(filterAddrList, addr...)
continue // jump to match the next service continue // jump to match the next service
} }
if !force { if !force {
return nil, fmt.Errorf("service [%s] not found", name) return nil, fmt.Errorf("service [%s] not found", name)
} }
log.Warnf("service [%s] not found", name)
} }
// Add matched addresses to map // Add matched addresses to map
for _, addr := range addrList { for _, addr := range addressList {
if contains(addrAll, addr) { if contains(addressAll, addr) {
addrs = append(addrs, addr) filterAddrList = append(filterAddrList, addr)
continue continue
} }
if !force { if !force {
return nil, fmt.Errorf("address [%s] not found", addr) return nil, fmt.Errorf("address [%s] not found", addr)
} }
log.Warnf("address [%s] not found", addr)
} }
if len(addrList) == 0 && len(svrList) == 0 { if len(addressList) == 0 && len(serviceList) == 0 {
addrs = addrAll filterAddrList = addressAll
} }
// Return all servers when all param is nil // Return all servers when all param is nil
return addrs, nil return filterAddrList, nil
} }
// removeDuplicateElement remove duplicate element in slice // removeDuplicateElement remove duplicate element in slice

View File

@ -46,7 +46,6 @@ func TestFilterAddrs(t *testing.T) {
{ {
svrList: []string{"unknown"}, svrList: []string{"unknown"},
force: true, force: true,
addrRes: []string{},
}, },
{ {
addrList: []string{"http://127.0.0.1:7777", "http://127.0.0.2:7777"}, addrList: []string{"http://127.0.0.1:7777", "http://127.0.0.2:7777"},