Skip to content

Commit 54d7aab

Browse files
committed
Optimized some issues
1 parent eff7d89 commit 54d7aab

11 files changed

Lines changed: 102 additions & 114 deletions

File tree

apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/ItemController.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,10 @@ public List<ItemDTO> findDeletedItems(@PathVariable("appId") String appId,
203203
}
204204

205205
@GetMapping("/items-search/key-and-value")
206-
public List<ItemInfoDTO> getItemInfoBySearch(@RequestParam(value = "key", required = false) String key,
207-
@RequestParam(value = "value", required = false) String value) {
208-
return itemService.getItemInfoBySearch(key, value);
209-
}
210-
211-
@GetMapping("/items-search/key-and-value/count")
212-
public int countItemInfoNumBySearch(@RequestParam(value = "key", required = false) String key,
213-
@RequestParam(value = "value", required = false) String value) {
214-
return itemService.countItemInfoNumBySearch(key, value);
206+
public PageDTO<ItemInfoDTO> getItemInfoBySearch(@RequestParam(value = "key", required = false) String key,
207+
@RequestParam(value = "value", required = false) String value,
208+
Pageable limit) {
209+
return new PageDTO<>(itemService.getItemInfoBySearch(key, value, limit).getContent(), limit, itemService.getItemInfoBySearch(key, value, limit).getTotalElements());
215210
}
216211

217212
@GetMapping("/items/{itemId}")

apollo-adminservice/src/test/java/com/ctrip/framework/apollo/adminservice/controller/ItemControllerTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
import org.junit.Assert;
3131
import org.junit.Test;
3232
import org.springframework.beans.factory.annotation.Autowired;
33+
import org.springframework.core.ParameterizedTypeReference;
34+
import org.springframework.data.domain.Page;
35+
import org.springframework.data.domain.PageRequest;
3336
import org.springframework.data.domain.Pageable;
34-
import org.springframework.http.HttpStatus;
35-
import org.springframework.http.ResponseEntity;
37+
import org.springframework.http.*;
3638
import org.springframework.test.context.jdbc.Sql;
3739
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
3840

@@ -162,10 +164,16 @@ public void testSearch() {
162164

163165
String itemKey = "test-key";
164166
String itemValue = "test-value";
165-
List<ItemInfoDTO> itemInfoDTOS = itemService.getItemInfoBySearch(itemKey, itemValue);
166-
String searchUrl = url("/items-search/key-and-value?key={key}&value={value}");
167-
ItemInfoDTO[] resultArray = restTemplate.getForObject(searchUrl, ItemInfoDTO[].class, itemKey, itemValue);
168-
List<ItemInfoDTO> expectedResult = Arrays.asList(resultArray);
169-
assertThat(itemInfoDTOS.toString()).isEqualTo(expectedResult.toString());
167+
Page<ItemInfoDTO> itemInfoDTOS = itemService.getItemInfoBySearch(itemKey, itemValue, PageRequest.of(0, 200));
168+
HttpHeaders headers = new HttpHeaders();
169+
HttpEntity<Void> entity = new HttpEntity<>(headers);
170+
ResponseEntity<PageDTO<ItemInfoDTO>> response = restTemplate.exchange(
171+
url("/items-search/key-and-value?key={key}&value={value}&page={page}&size={size}"),
172+
HttpMethod.GET,
173+
entity,
174+
new ParameterizedTypeReference<PageDTO<ItemInfoDTO>>() {},
175+
itemKey, itemValue, 0, 200
176+
);
177+
assertThat(itemInfoDTOS.getContent().toString()).isEqualTo(response.getBody().getContent().toString());
170178
}
171179
}

apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/repository/ItemRepository.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,17 @@ public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {
4848
@Query("SELECT new com.ctrip.framework.apollo.common.dto.ItemInfoDTO(n.appId, n.clusterName, n.namespaceName, i.key, i.value) " +
4949
"FROM Item i RIGHT JOIN Namespace n ON i.namespaceId = n.id " +
5050
"WHERE i.key LIKE %:key% AND i.value LIKE %:value% AND i.isDeleted = 0")
51-
List<ItemInfoDTO> findItemsByKeyAndValueLike(@Param("key") String key, @Param("value") String value, Pageable pageable);
51+
Page<ItemInfoDTO> findItemsByKeyAndValueLike(@Param("key") String key, @Param("value") String value, Pageable pageable);
5252

5353
@Query("SELECT new com.ctrip.framework.apollo.common.dto.ItemInfoDTO(n.appId, n.clusterName, n.namespaceName, i.key, i.value) " +
5454
"FROM Item i RIGHT JOIN Namespace n ON i.namespaceId = n.id " +
5555
"WHERE i.key LIKE %:key% AND i.isDeleted = 0")
56-
List<ItemInfoDTO> findItemsByKeyLike(@Param("key") String key, Pageable pageable);
56+
Page<ItemInfoDTO> findItemsByKeyLike(@Param("key") String key, Pageable pageable);
5757

5858
@Query("SELECT new com.ctrip.framework.apollo.common.dto.ItemInfoDTO(n.appId, n.clusterName, n.namespaceName, i.key, i.value) " +
5959
"FROM Item i RIGHT JOIN Namespace n ON i.namespaceId = n.id " +
6060
"WHERE i.value LIKE %:value% AND i.isDeleted = 0")
61-
List<ItemInfoDTO> findItemsByValueLike(@Param("value") String value, Pageable pageable);
62-
63-
@Query("SELECT COUNT(i) FROM Item i RIGHT JOIN Namespace n ON i.namespaceId = n.id WHERE i.key LIKE %:key% AND i.isDeleted = 0")
64-
int countItemNumByKeyLike(@Param("key") String key);
65-
66-
@Query("SELECT COUNT(i) FROM Item i RIGHT JOIN Namespace n ON i.namespaceId = n.id WHERE i.value LIKE %:value% AND i.isDeleted = 0")
67-
int countItemNumByValueLike(@Param("value") String value);
68-
69-
@Query("SELECT COUNT(i) FROM Item i RIGHT JOIN Namespace n ON i.namespaceId = n.id WHERE i.key LIKE %:key% AND i.value LIKE %:value% AND i.isDeleted = 0")
70-
int countItemNumByKeyAndValueLike(@Param("key") String key, @Param("value") String value);
61+
Page<ItemInfoDTO> findItemsByValueLike(@Param("value") String value, Pageable pageable);
7162

7263
@Modifying
7364
@Query("update Item set IsDeleted = true, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?2 where NamespaceId = ?1 and IsDeleted = false")

apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/ItemService.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,18 @@ public Page<Item> findItemsByNamespace(String appId, String clusterName, String
152152
return itemRepository.findByNamespaceId(namespace.getId(), pageable);
153153
}
154154

155-
public List<ItemInfoDTO> getItemInfoBySearch(String key, String value) {
156-
List<ItemInfoDTO> itemInfoDTOs;
157-
Pageable pageable = PageRequest.of(0,200);
155+
public Page<ItemInfoDTO> getItemInfoBySearch(String key, String value, Pageable limit) {
156+
Page<ItemInfoDTO> itemInfoDTOs;
158157
if (key.isEmpty() && !value.isEmpty()) {
159-
itemInfoDTOs = itemRepository.findItemsByValueLike(value, pageable);
158+
itemInfoDTOs = itemRepository.findItemsByValueLike(value, limit);
160159
} else if (value.isEmpty() && !key.isEmpty()) {
161-
itemInfoDTOs = itemRepository.findItemsByKeyLike(key, pageable);
160+
itemInfoDTOs = itemRepository.findItemsByKeyLike(key, limit);
162161
} else {
163-
itemInfoDTOs = itemRepository.findItemsByKeyAndValueLike(key, value, pageable);
162+
itemInfoDTOs = itemRepository.findItemsByKeyAndValueLike(key, value, limit);
164163
}
165164

166165
List<Release> releaseItems = releaseRepository.findAll();
167-
for (ItemInfoDTO itemInfoDTO : itemInfoDTOs) {
166+
for (ItemInfoDTO itemInfoDTO : itemInfoDTOs.getContent()) {
168167
boolean isIncluded = false;
169168
for (Release releaseItem : releaseItems) {
170169
if (releaseItem.getConfigurations().contains(itemInfoDTO.getKey()) && releaseItem.getConfigurations().contains(itemInfoDTO.getValue())) {
@@ -180,16 +179,6 @@ public List<ItemInfoDTO> getItemInfoBySearch(String key, String value) {
180179
return itemInfoDTOs;
181180
}
182181

183-
public int countItemInfoNumBySearch(String key, String value){
184-
if (key.isEmpty() && !value.isEmpty()) {
185-
return itemRepository.countItemNumByValueLike(value);
186-
} else if (value.isEmpty() && !key.isEmpty()) {
187-
return itemRepository.countItemNumByKeyLike(key);
188-
} else {
189-
return itemRepository.countItemNumByKeyAndValueLike(key, value);
190-
}
191-
}
192-
193182
@Transactional
194183
public Item save(Item entity) {
195184
checkItemKeyLength(entity.getKey());

apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/ItemServiceTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.Assert;
2424
import org.junit.Test;
2525
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.data.domain.Page;
27+
import org.springframework.data.domain.PageRequest;
2628
import org.springframework.test.context.jdbc.Sql;
2729

2830
import java.util.List;
@@ -88,12 +90,13 @@ public void testSearchItem() {
8890

8991
String itemKey = "k1";
9092
String itemValue = "v1";
91-
List<ItemInfoDTO> ExpectedItemInfoDTOSByKeyAndValue = itemService.getItemInfoBySearch(itemKey,itemValue);
92-
List<ItemInfoDTO> ExpectedItemInfoDTOSByKey = itemService.getItemInfoBySearch(itemKey,"");
93-
List<ItemInfoDTO> ExpectedItemInfoDTOSByValue = itemService.getItemInfoBySearch("",itemValue);
94-
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByKeyAndValue.get(0).toString());
95-
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByKey.get(0).toString());
96-
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByValue.get(0).toString());
93+
Page<ItemInfoDTO> ExpectedItemInfoDTOSByKeyAndValue = itemService.getItemInfoBySearch(itemKey, itemValue, PageRequest.of(0,200));
94+
Page<ItemInfoDTO> ExpectedItemInfoDTOSByKey = itemService.getItemInfoBySearch(itemKey,"", PageRequest.of(0,200));
95+
Page<ItemInfoDTO> ExpectedItemInfoDTOSByValue = itemService.getItemInfoBySearch("", itemValue, PageRequest.of(0,200));
96+
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByKeyAndValue.getContent().get(0).toString());
97+
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByKey.getContent().get(0).toString());
98+
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByValue.getContent().get(0).toString());
99+
97100
}
98101

99102
}

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ public static class ItemAPI extends API {
184184
private final ParameterizedTypeReference<PageDTO<OpenItemDTO>> openItemPageDTO =
185185
new ParameterizedTypeReference<PageDTO<OpenItemDTO>>() {};
186186

187+
private final ParameterizedTypeReference<PageDTO<ItemInfoDTO>> pageItemInfoDTO =
188+
new ParameterizedTypeReference<PageDTO<ItemInfoDTO>>() {};
189+
187190
public List<ItemDTO> findItems(String appId, Env env, String clusterName, String namespaceName) {
188191
ItemDTO[] itemDTOs =
189192
restTemplate.get(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items",
@@ -198,18 +201,13 @@ public List<ItemDTO> findDeletedItems(String appId, Env env, String clusterName,
198201
return Arrays.asList(itemDTOs);
199202
}
200203

201-
public List<ItemInfoDTO> getPerEnvItemInfoBySearch(Env env, String key, String value){
202-
ItemInfoDTO[] perEnvItemInfoDTOs =
203-
restTemplate.get(env, "items-search/key-and-value?key={key}&value={value}",
204-
ItemInfoDTO[].class, key, value);
205-
return Arrays.asList(perEnvItemInfoDTOs);
206-
}
207-
208-
public int countPerEnvItemInfoNumBySearch(Env env, String key, String value){
209-
Integer count =
210-
restTemplate.get(env, "items-search/key-and-value/count?key={key}&value={value}",
211-
Integer.class, key, value);
212-
return count == null ? 0 : count;
204+
public PageDTO<ItemInfoDTO> getPerEnvItemInfoBySearch(Env env, String key, String value, int page, int size){
205+
ResponseEntity<PageDTO<ItemInfoDTO>>
206+
entity =
207+
restTemplate.get(env,
208+
"items-search/key-and-value?key={key}&value={value}&page={page}&size={size}",
209+
pageItemInfoDTO, key, value, page, size);
210+
return entity.getBody();
213211
}
214212

215213
public ItemDTO loadItem(Env env, String appId, String clusterName, String namespaceName, String key) {

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public List<Env> portalSupportedEnvs() {
8686
return envs;
8787
}
8888

89+
public int getPerEnvSearchMaxResults() {return getIntProperty("apollo.portal.search.perEnvMaxResults", 200);}
90+
8991
/**
9092
* @return the relationship between environment and its meta server. empty if meet exception
9193
*/

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/GlobalSearchValueController.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package com.ctrip.framework.apollo.portal.controller;
1818

1919

20+
import com.ctrip.framework.apollo.common.dto.PageDTO;
2021
import com.ctrip.framework.apollo.portal.component.PortalSettings;
22+
import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
2123
import com.ctrip.framework.apollo.portal.entity.vo.ItemInfo;
2224
import com.ctrip.framework.apollo.portal.environment.Env;
2325
import com.ctrip.framework.apollo.portal.service.GlobalSearchValueService;
@@ -39,14 +41,14 @@
3941

4042
@RestController
4143
public class GlobalSearchValueController {
42-
4344
private final PortalSettings portalSettings;
4445
private final GlobalSearchValueService globalSearchValueService;
45-
private static final int MAX_SEARCH_RESULTS = 200;
46+
private final PortalConfig portalConfig;
4647

47-
public GlobalSearchValueController(final PortalSettings portalSettings, final GlobalSearchValueService globalSearchValueService) {
48+
public GlobalSearchValueController(final PortalSettings portalSettings, final GlobalSearchValueService globalSearchValueService, final PortalConfig portalConfig) {
4849
this.portalSettings = portalSettings;
4950
this.globalSearchValueService = globalSearchValueService;
51+
this.portalConfig = portalConfig;
5052
}
5153

5254
@PreAuthorize(value = "@permissionValidator.isSuperAdmin()")
@@ -74,13 +76,12 @@ public ResponseEntity<?> get_ItemInfo_BySearch(@RequestParam(value = "key", requ
7476
List<String> envBeyondLimit = new ArrayList<>();
7577
AtomicBoolean hasMoreData = new AtomicBoolean(false);
7678
activeEnvs.forEach(env -> {
77-
List<ItemInfo> perEnvItemInfos = globalSearchValueService.get_PerEnv_ItemInfo_BySearch(env, key, value);
78-
int perEnvItemInfoNum = globalSearchValueService.count_PerEnv_ItemInfoNum_BySearch(env, key, value);
79-
if(perEnvItemInfoNum > MAX_SEARCH_RESULTS){
79+
PageDTO<ItemInfo> perEnvItemInfos = globalSearchValueService.get_PerEnv_ItemInfo_BySearch(env, key, value,0, portalConfig.getPerEnvSearchMaxResults());
80+
if(perEnvItemInfos.getTotal() > portalConfig.getPerEnvSearchMaxResults()){
8081
envBeyondLimit.add(env.getName());
8182
hasMoreData.set(true);
8283
}
83-
allEnvItemInfos.addAll(perEnvItemInfos);
84+
allEnvItemInfos.addAll(perEnvItemInfos.getContent());
8485
});
8586

8687
Map<String, Object> body = new HashMap<>();
@@ -89,7 +90,7 @@ public ResponseEntity<?> get_ItemInfo_BySearch(@RequestParam(value = "key", requ
8990
body.put("hasMoreData", true);
9091
body.put("message", String.format(
9192
"In %s , more than %d items found (Exceeded the maximum search quantity for a single environment). Please enter more precise criteria to narrow down the search scope.",
92-
String.join(" , ", envBeyondLimit), MAX_SEARCH_RESULTS));
93+
String.join(" , ", envBeyondLimit), portalConfig.getPerEnvSearchMaxResults()));
9394
return ResponseEntity
9495
.ok()
9596
.contentType(MediaType.APPLICATION_JSON)

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/GlobalSearchValueService.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
package com.ctrip.framework.apollo.portal.service;
1818

1919
import com.ctrip.framework.apollo.common.dto.ItemInfoDTO;
20+
import com.ctrip.framework.apollo.common.dto.PageDTO;
2021
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI;
2122
import com.ctrip.framework.apollo.portal.entity.vo.ItemInfo;
2223
import com.ctrip.framework.apollo.portal.environment.Env;
24+
import org.springframework.data.domain.PageRequest;
2325
import org.springframework.stereotype.Service;
2426

2527
import java.util.ArrayList;
@@ -34,18 +36,14 @@ public GlobalSearchValueService(AdminServiceAPI.ItemAPI itemAPI) {
3436
this.itemAPI = itemAPI;
3537
}
3638

37-
public List<ItemInfo> get_PerEnv_ItemInfo_BySearch(Env env, String key, String value) {
39+
public PageDTO<ItemInfo> get_PerEnv_ItemInfo_BySearch(Env env, String key, String value, int page, int size) {
3840
List<ItemInfo> perEnvItemInfos = new ArrayList<>();
39-
List<ItemInfoDTO> perEnvItemInfoDTOs = itemAPI.getPerEnvItemInfoBySearch(env, key, value);
40-
perEnvItemInfoDTOs.forEach(itemInfoDTO -> {
41+
PageDTO<ItemInfoDTO> perEnvItemInfoDTOs = itemAPI.getPerEnvItemInfoBySearch(env, key, value, page, size);
42+
perEnvItemInfoDTOs.getContent().forEach(itemInfoDTO -> {
4143
ItemInfo itemInfo = new ItemInfo(itemInfoDTO.getAppId(),env.getName(),itemInfoDTO.getClusterName(),itemInfoDTO.getNamespaceName(),itemInfoDTO.getStatus(),itemInfoDTO.getKey(),itemInfoDTO.getValue());
4244
perEnvItemInfos.add(itemInfo);
4345
});
44-
return perEnvItemInfos;
45-
}
46-
47-
public int count_PerEnv_ItemInfoNum_BySearch(Env env, String key, String value){
48-
return itemAPI.countPerEnvItemInfoNumBySearch(env, key, value);
46+
return new PageDTO<>(perEnvItemInfos, PageRequest.of(page, size), perEnvItemInfoDTOs.getTotal());
4947
}
5048

5149
}

0 commit comments

Comments
 (0)