Skip to content

Commit

Permalink
添加了管理员全局搜索Value值的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxianhjy committed Aug 17, 2024
1 parent c28eb49 commit 219d6d9
Show file tree
Hide file tree
Showing 21 changed files with 1,284 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.ctrip.framework.apollo.biz.service.ReleaseService;
import com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.ItemInfoDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
Expand Down Expand Up @@ -201,6 +202,13 @@ public List<ItemDTO> findDeletedItems(@PathVariable("appId") String appId,
return Collections.emptyList();
}

@GetMapping("/items-search/key-and-value")
public List<ItemInfoDTO> getItemInfoBySearch(@RequestParam(value = "key", required = false, defaultValue = "") String key,
@RequestParam(value = "value", required = false, defaultValue = "") String value) {
List<ItemInfoDTO> ItemInfoDTOs = itemService.getItemInfoBySearch(key, value);
return ItemInfoDTOs;
}

@GetMapping("/items/{itemId}")
public ItemDTO get(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import com.ctrip.framework.apollo.biz.entity.Commit;
import com.ctrip.framework.apollo.biz.repository.CommitRepository;
import com.ctrip.framework.apollo.biz.repository.ItemRepository;
import com.ctrip.framework.apollo.common.dto.AppDTO;
import com.ctrip.framework.apollo.common.dto.ClusterDTO;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.biz.service.ItemService;
import com.ctrip.framework.apollo.common.dto.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.junit.Assert;
Expand All @@ -48,6 +48,9 @@ public class ItemControllerTest extends AbstractControllerTest {
@Autowired
private ItemRepository itemRepository;

@Autowired
private ItemService itemService;

@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
Expand Down Expand Up @@ -150,4 +153,21 @@ public void testDelete() {
Pageable.ofSize(10));
assertThat(commitList).hasSize(2);
}

@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testSearch() {
this.testCreate();

String itemKey = "test-key";
String itemValue = "test-value";
List<Object[]> i = itemRepository.findItemsByKeyAndValueLike(itemKey,itemValue);
System.out.println(i.toString());
List<ItemInfoDTO> itemInfoDTOS = itemService.getItemInfoBySearch(itemKey, itemValue);
String searchUrl = url("/items-search/key-and-value?key={key}&value={value}");
ItemInfoDTO[] resultArray = restTemplate.getForObject(searchUrl, ItemInfoDTO[].class, itemKey, itemValue);
List<ItemInfoDTO> expectedResult = Arrays.asList(resultArray);
assertThat(itemInfoDTOS.toString()).isEqualTo(expectedResult.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,52 @@ public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {

Item findFirst1ByNamespaceIdOrderByLineNumDesc(Long namespaceId);

@Query(nativeQuery = true, value =
"SELECT " +
" n.AppId AS appId, " +
" n.ClusterName AS clusterName, " +
" n.NamespaceName AS namespaceName, " +
" i.`Key` AS `key`, " +
" i.`Value` AS `value`" +
"FROM " +
" Item i " +
"RIGHT JOIN Namespace n ON i.NamespaceId = n.Id " +
"WHERE " +
" i.`Key` LIKE %?1% " +
" AND i.`Value` LIKE %?2% " +
" AND i.IsDeleted = 0")
List<Object[]> findItemsByKeyAndValueLike(String key, String value);

@Query(nativeQuery = true, value =
"SELECT "+
" n.AppId AS appId, " +
" n.ClusterName AS clusterName, " +
" n.NamespaceName AS namespaceName, " +
" i.`Key` AS `key`, " +
" i.`Value` AS `value` " +
"FROM " +
" Item i " +
"RIGHT JOIN Namespace n ON i.NamespaceId = n.Id " +
"WHERE " +
" i.`Value` LIKE %?1% " +
" AND i.IsDeleted = 0")
List<Object[]> findItemsByValueLike(String value);

@Query(nativeQuery = true, value =
"SELECT"+
" n.AppId AS appId, " +
" n.ClusterName AS clusterName, " +
" n.NamespaceName AS namespaceName, " +
" i.`Key` AS `key`, " +
" i.`Value` AS `value` " +
"FROM " +
" Item i " +
"RIGHT JOIN Namespace n ON i.NamespaceId = n.Id " +
"WHERE " +
" i.`Key` LIKE %?1% " +
" AND i.IsDeleted = 0 ")
List<Object[]> findItemsByKeyLike(String key);

@Modifying
@Query("update Item set IsDeleted = true, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?2 where NamespaceId = ?1 and IsDeleted = false")
int deleteByNamespaceId(long namespaceId, String operator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Release findFirstByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderBy

List<Release> findByIdIn(Set<Long> releaseIds);

List<Release> findAll();

@Modifying
@Query("update Release set IsDeleted = true, DeletedAt = ROUND(UNIX_TIMESTAMP(NOW(4))*1000), DataChange_LastModifiedBy = ?4 where AppId=?1 and ClusterName=?2 and NamespaceName = ?3 and IsDeleted = false")
int batchDelete(String appId, String clusterName, String namespaceName, String operator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import com.ctrip.framework.apollo.biz.entity.Audit;
import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.biz.entity.Namespace;
import com.ctrip.framework.apollo.biz.entity.Release;
import com.ctrip.framework.apollo.biz.repository.ItemRepository;
import com.ctrip.framework.apollo.biz.repository.ReleaseRepository;
import com.ctrip.framework.apollo.common.dto.ItemInfoDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
Expand All @@ -33,10 +36,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -49,16 +51,19 @@ public class ItemService {
private final NamespaceService namespaceService;
private final AuditService auditService;
private final BizConfig bizConfig;
private final ReleaseRepository releaseRepository;

public ItemService(
final ItemRepository itemRepository,
final @Lazy NamespaceService namespaceService,
final AuditService auditService,
final BizConfig bizConfig) {
final BizConfig bizConfig,
final ReleaseRepository releaseRepository) {
this.itemRepository = itemRepository;
this.namespaceService = namespaceService;
this.auditService = auditService;
this.bizConfig = bizConfig;
this.releaseRepository = releaseRepository;
}


Expand Down Expand Up @@ -146,6 +151,56 @@ public Page<Item> findItemsByNamespace(String appId, String clusterName, String
return itemRepository.findByNamespaceId(namespace.getId(), pageable);
}

public List<ItemInfoDTO> getItemInfoBySearch(String key, String value) {
List<ItemInfoDTO> itemInfoDTOs = new ArrayList<>();
List<Object[]> infos;
if (key.isEmpty() && !value.isEmpty()) {
infos = itemRepository.findItemsByValueLike(value);
} else if (value.isEmpty() && !key.isEmpty()) {
infos = itemRepository.findItemsByKeyLike(key);
} else {
infos = itemRepository.findItemsByKeyAndValueLike(key, value);
}

for (Object[] row : infos) {
ItemInfoDTO itemInfoDTO = new ItemInfoDTO();
itemInfoDTO.setAppId(String.valueOf(row[0]));
itemInfoDTO.setClusterName(String.valueOf(row[1]));
itemInfoDTO.setNamespaceName(String.valueOf(row[2]));
itemInfoDTO.setStatus("0");
itemInfoDTO.setKey(String.valueOf(row[3]));
if (row[4] instanceof String) {
itemInfoDTO.setValue(String.valueOf(row[4]));
} else if (row[4] instanceof Clob) {
try {
Clob clob = (Clob) row[4];
String valueStr = clob.getSubString(1, (int) clob.length());
System.out.println(valueStr);
itemInfoDTO.setValue(valueStr);
} catch (SQLException e) {
e.printStackTrace();
}
}
itemInfoDTOs.add(itemInfoDTO);
}

List<Release> releaseItems = releaseRepository.findAll();
for (ItemInfoDTO itemInfoDTO : itemInfoDTOs) {
boolean isIncluded = false;
for (Release releaseItem : releaseItems) {
if (releaseItem.getConfigurations().contains(itemInfoDTO.getKey()) && releaseItem.getConfigurations().contains(itemInfoDTO.getValue())) {
itemInfoDTO.setStatus("1");
isIncluded = true;
break;
}
}
if (!isIncluded) {
itemInfoDTO.setStatus("0");
}
}
return itemInfoDTOs;
}

@Transactional
public Item save(Item entity) {
checkItemKeyLength(entity.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.common.dto.ItemInfoDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

public class ItemServiceTest extends AbstractIntegrationTest {

@Autowired
Expand Down Expand Up @@ -71,4 +74,26 @@ public void testUpdateItem() {
Assert.assertEquals("v1-new", dbItem.getValue());
}

@Test
@Sql(scripts = {"/sql/namespace-test.sql","/sql/item-test.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testSearchItem() {
ItemInfoDTO itemInfoDTO = new ItemInfoDTO();
itemInfoDTO.setAppId("testApp");
itemInfoDTO.setClusterName("default");
itemInfoDTO.setNamespaceName("application");
itemInfoDTO.setStatus("0");
itemInfoDTO.setKey("k1");
itemInfoDTO.setValue("v1");

String itemKey = "k1";
String itemValue = "v1";
List<ItemInfoDTO> ExpectedItemInfoDTOSByKeyAndValue = itemService.getItemInfoBySearch(itemKey,itemValue);
List<ItemInfoDTO> ExpectedItemInfoDTOSByKey = itemService.getItemInfoBySearch(itemKey,"");
List<ItemInfoDTO> ExpectedItemInfoDTOSByValue = itemService.getItemInfoBySearch("",itemValue);
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByKeyAndValue.get(0).toString());
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByKey.get(0).toString());
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByValue.get(0).toString());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.common.dto;


public class ItemInfoDTO extends BaseDTO{
private String appId;
private String clusterName;
private String namespaceName;
private String status;
private String key;
private String value;

public ItemInfoDTO() {
}

public ItemInfoDTO(String appId, String clusterName, String namespaceName,
String status, String key, String value) {
this.appId = appId;
this.clusterName = clusterName;
this.namespaceName = namespaceName;
this.status = status;
this.key = key;
this.value = value;
}

public String getAppId() {
return appId;
}

public void setAppId(String appId) {
this.appId = appId;
}

public String getClusterName() {
return clusterName;
}

public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}

public String getNamespaceName() {
return namespaceName;
}

public void setNamespaceName(String namespaceName) {
this.namespaceName = namespaceName;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

@Override
public String toString() {
return "ItemInfoDTO{" +
"appId='" + appId + '\'' +
", clusterName='" + clusterName + '\'' +
", namespaceName='" + namespaceName + '\'' +
", status='" + status + '\'' +
", key='" + key + '\'' +
", value='" + value + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ public List<ItemDTO> findDeletedItems(String appId, Env env, String clusterName,
return Arrays.asList(itemDTOs);
}

public List<ItemInfoDTO> getPerEnvItemInfoBySearch(Env env, String key, String value){
ItemInfoDTO[] perEnvItemInfoDTOs =
restTemplate.get(env, "items-search/key-and-value?key={key}&value={value}",
ItemInfoDTO[].class, key, value);
return Arrays.asList(perEnvItemInfoDTOs);
}

public ItemDTO loadItem(Env env, String appId, String clusterName, String namespaceName, String key) {
return restTemplate.get(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key}",
ItemDTO.class, appId, clusterName, namespaceName, key);
Expand Down
Loading

0 comments on commit 219d6d9

Please sign in to comment.