Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加了管理员全局搜索Value值的功能 #5182

Merged
merged 20 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 PageDTO<ItemInfoDTO> getItemInfoBySearch(@RequestParam(value = "key", required = false) String key,
@RequestParam(value = "value", required = false) String value,
Pageable limit) {
return new PageDTO<>(itemService.getItemInfoBySearch(key, value, limit).getContent(), limit, itemService.getItemInfoBySearch(key, value, limit).getTotalElements());
}

@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,18 +21,20 @@
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;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;

Expand All @@ -48,6 +50,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 +155,25 @@ 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";
Page<ItemInfoDTO> itemInfoDTOS = itemService.getItemInfoBySearch(itemKey, itemValue, PageRequest.of(0, 200));
HttpHeaders headers = new HttpHeaders();
HttpEntity<Void> entity = new HttpEntity<>(headers);
ResponseEntity<PageDTO<ItemInfoDTO>> response = restTemplate.exchange(
url("/items-search/key-and-value?key={key}&value={value}&page={page}&size={size}"),
HttpMethod.GET,
entity,
new ParameterizedTypeReference<PageDTO<ItemInfoDTO>>() {},
itemKey, itemValue, 0, 200
);
assertThat(itemInfoDTOS.getContent().toString()).isEqualTo(response.getBody().getContent().toString());
}
klboke marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import com.ctrip.framework.apollo.biz.entity.Item;

import com.ctrip.framework.apollo.common.dto.ItemInfoDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
Expand All @@ -43,6 +45,21 @@ public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {

Item findFirst1ByNamespaceIdOrderByLineNumDesc(Long namespaceId);

@Query("SELECT new com.ctrip.framework.apollo.common.dto.ItemInfoDTO(n.appId, n.clusterName, n.namespaceName, i.key, i.value) " +
"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")
Page<ItemInfoDTO> findItemsByKeyAndValueLike(@Param("key") String key, @Param("value") String value, Pageable pageable);

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

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

@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();
klboke marked this conversation as resolved.
Show resolved Hide resolved

@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,22 +21,25 @@
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;
import com.ctrip.framework.apollo.core.utils.StringUtils;

import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
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;
klboke marked this conversation as resolved.
Show resolved Hide resolved
import java.sql.SQLException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -49,16 +52,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 +152,33 @@ public Page<Item> findItemsByNamespace(String appId, String clusterName, String
return itemRepository.findByNamespaceId(namespace.getId(), pageable);
}

public Page<ItemInfoDTO> getItemInfoBySearch(String key, String value, Pageable limit) {
Page<ItemInfoDTO> itemInfoDTOs;
if (key.isEmpty() && !value.isEmpty()) {
itemInfoDTOs = itemRepository.findItemsByValueLike(value, limit);
} else if (value.isEmpty() && !key.isEmpty()) {
itemInfoDTOs = itemRepository.findItemsByKeyLike(key, limit);
} else {
itemInfoDTOs = itemRepository.findItemsByKeyAndValueLike(key, value, limit);
}

List<Release> releaseItems = releaseRepository.findAll();
klboke marked this conversation as resolved.
Show resolved Hide resolved
for (ItemInfoDTO itemInfoDTO : itemInfoDTOs.getContent()) {
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;
}
klboke marked this conversation as resolved.
Show resolved Hide resolved

@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,17 @@

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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

public class ItemServiceTest extends AbstractIntegrationTest {

@Autowired
Expand Down Expand Up @@ -71,4 +76,27 @@ 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";
Page<ItemInfoDTO> ExpectedItemInfoDTOSByKeyAndValue = itemService.getItemInfoBySearch(itemKey, itemValue, PageRequest.of(0,200));
Page<ItemInfoDTO> ExpectedItemInfoDTOSByKey = itemService.getItemInfoBySearch(itemKey,"", PageRequest.of(0,200));
Page<ItemInfoDTO> ExpectedItemInfoDTOSByValue = itemService.getItemInfoBySearch("", itemValue, PageRequest.of(0,200));
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByKeyAndValue.getContent().get(0).toString());
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByKey.getContent().get(0).toString());
Assert.assertEquals(itemInfoDTO.toString(), ExpectedItemInfoDTOSByValue.getContent().get(0).toString());

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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 ItemInfoDTO(String appId, String clusterName, String namespaceName,
String key, String value) {
this.appId = appId;
this.clusterName = clusterName;
this.namespaceName = namespaceName;
this.status = "0";
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 + '\'' +
'}';
}
klboke marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ public static class ItemAPI extends API {
private final ParameterizedTypeReference<PageDTO<OpenItemDTO>> openItemPageDTO =
new ParameterizedTypeReference<PageDTO<OpenItemDTO>>() {};

private final ParameterizedTypeReference<PageDTO<ItemInfoDTO>> pageItemInfoDTO =
new ParameterizedTypeReference<PageDTO<ItemInfoDTO>>() {};

public List<ItemDTO> findItems(String appId, Env env, String clusterName, String namespaceName) {
ItemDTO[] itemDTOs =
restTemplate.get(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items",
Expand All @@ -198,6 +201,15 @@ public List<ItemDTO> findDeletedItems(String appId, Env env, String clusterName,
return Arrays.asList(itemDTOs);
}

public PageDTO<ItemInfoDTO> getPerEnvItemInfoBySearch(Env env, String key, String value, int page, int size){
ResponseEntity<PageDTO<ItemInfoDTO>>
entity =
restTemplate.get(env,
"items-search/key-and-value?key={key}&value={value}&page={page}&size={size}",
pageItemInfoDTO, key, value, page, size);
return entity.getBody();
}

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
Loading