Commit 17261b68 authored by Jared Tan's avatar Jared Tan Committed by kezhenxu94

support dataChangeLastModifiedBy display after edited in a namespace (#2680)

parent 2b48b3d0
......@@ -12,6 +12,11 @@ import com.ctrip.framework.apollo.common.dto.ItemDTO;
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 java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -21,8 +26,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ItemController {
......@@ -135,6 +138,21 @@ public class ItemController {
return BeanUtils.batchTransform(ItemDTO.class, itemService.findItemsWithOrdered(appId, clusterName, namespaceName));
}
@GetMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/deleted")
public List<ItemDTO> findDeletedItems(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName) {
List<Commit> commits = commitService.find(appId, clusterName, namespaceName, null);
if (Objects.nonNull(commits)) {
List<Item> deletedItems = commits.stream()
.map(item -> ConfigChangeContentBuilder.convertJsonString(item.getChangeSets()).getDeleteItems())
.flatMap(Collection::stream)
.collect(Collectors.toList());
return BeanUtils.batchTransform(ItemDTO.class, deletedItems);
}
return Collections.emptyList();
}
@GetMapping("/items/{itemId}")
public ItemDTO get(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
......
package com.ctrip.framework.apollo.biz.utils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
......@@ -84,4 +82,19 @@ public class ConfigChangeContentBuilder {
return target;
}
public static ConfigChangeContentBuilder convertJsonString(String content) {
return gson.fromJson(content, ConfigChangeContentBuilder.class);
}
public List<Item> getCreateItems() {
return createItems;
}
public List<ItemPair> getUpdateItems() {
return updateItems;
}
public List<Item> getDeleteItems() {
return deleteItems;
}
}
package com.ctrip.framework.apollo.biz;
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.entity.ServerConfig;
......@@ -51,4 +52,14 @@ public class MockBeanFactory {
return instance;
}
public static Item mockItem(long id, long namespaceId, String itemKey, String itemValue, int lineNum) {
Item item = new Item();
item.setId(id);
item.setKey(itemKey);
item.setValue(itemValue);
item.setLineNum(lineNum);
item.setNamespaceId(namespaceId);
return item;
}
}
package com.ctrip.framework.apollo.biz.utils;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.ctrip.framework.apollo.biz.MockBeanFactory;
import com.ctrip.framework.apollo.biz.entity.Item;
import org.junit.Before;
import org.junit.Test;
/**
* @author jian.tan
*/
public class ConfigChangeContentBuilderTest {
private final ConfigChangeContentBuilder configChangeContentBuilder = new ConfigChangeContentBuilder();
private String configString;
@Before
public void initConfig() {
Item createdItem = MockBeanFactory.mockItem(1, 1, "timeout", "100", 1);
Item updatedItem = MockBeanFactory.mockItem(1, 1, "timeout", "1001", 1);
configChangeContentBuilder.createItem(createdItem);
configChangeContentBuilder.updateItem(createdItem, updatedItem);
configChangeContentBuilder.deleteItem(updatedItem);
configString = configChangeContentBuilder.build();
}
@Test
public void testHasContent() {
assertTrue(configChangeContentBuilder.hasContent());
}
@Test
public void testConvertJsonString() {
ConfigChangeContentBuilder contentBuilder = ConfigChangeContentBuilder
.convertJsonString(configString);
assertNotNull(contentBuilder.getCreateItems());
assertNotNull(contentBuilder.getUpdateItems().get(0).oldItem);
assertNotNull(contentBuilder.getUpdateItems().get(0).newItem);
assertNotNull(contentBuilder.getDeleteItems());
}
}
......@@ -159,6 +159,13 @@ public class AdminServiceAPI {
return Arrays.asList(itemDTOs);
}
public List<ItemDTO> findDeletedItems(String appId, Env env, String clusterName, String namespaceName) {
ItemDTO[] itemDTOs =
restTemplate.get(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/deleted",
ItemDTO[].class, appId, clusterName, namespaceName);
return Arrays.asList(itemDTOs);
}
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);
......
......@@ -2,7 +2,7 @@ package com.ctrip.framework.apollo.portal.entity.bo;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
public class ItemBO {
public class ItemBO {
private ItemDTO item;
private boolean isModified;
private boolean isDeleted;
......
......@@ -110,6 +110,10 @@ public class ItemService {
return itemAPI.findItems(appId, env, clusterName, namespaceName);
}
public List<ItemDTO> findDeletedItems(String appId, Env env, String clusterName, String namespaceName) {
return itemAPI.findDeletedItems(appId, env, clusterName, namespaceName);
}
public ItemDTO loadItem(Env env, String appId, String clusterName, String namespaceName, String key) {
return itemAPI.loadItem(env, appId, clusterName, namespaceName, key);
}
......
......@@ -30,7 +30,6 @@ import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -224,6 +223,7 @@ public class NamespaceService {
//latest Release
ReleaseDTO latestRelease;
Map<String, String> releaseItems = new HashMap<>();
Map<String, ItemDTO> deletedItemDTOs = new HashMap<>();
latestRelease = releaseService.loadLatestRelease(appId, env, clusterName, namespaceName);
if (latestRelease != null) {
releaseItems = gson.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
......@@ -244,7 +244,11 @@ public class NamespaceService {
}
//deleted items
List<ItemBO> deletedItems = parseDeletedItems(items, releaseItems);
itemService.findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> {
deletedItemDTOs.put(item.getKey(),item);
});
List<ItemBO> deletedItems = parseDeletedItems(items, releaseItems, deletedItemDTOs);
itemBOs.addAll(deletedItems);
modifiedItemCnt += deletedItems.size();
......@@ -281,7 +285,7 @@ public class NamespaceService {
namespace.setPublic(isPublic);
}
private List<ItemBO> parseDeletedItems(List<ItemDTO> newItems, Map<String, String> releaseItems) {
private List<ItemBO> parseDeletedItems(List<ItemDTO> newItems, Map<String, String> releaseItems, Map<String, ItemDTO> deletedItemDTOs) {
Map<String, ItemDTO> newItemMap = BeanUtils.mapByKey("key", newItems);
List<ItemBO> deletedItems = new LinkedList<>();
......@@ -291,7 +295,7 @@ public class NamespaceService {
ItemBO deletedItem = new ItemBO();
deletedItem.setDeleted(true);
ItemDTO deletedItemDto = new ItemDTO();
ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO());
deletedItemDto.setKey(key);
String oldValue = entry.getValue();
deletedItem.setItem(deletedItemDto);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment