From c2d82c07ae547110382b6c6e40874b0b93f4404b Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 18 Oct 2016 12:15:08 +0800 Subject: [PATCH] Record and display release delivery time --- .../controller/InstanceConfigController.java | 26 ++++++++++++++++-- .../InstanceConfigControllerTest.java | 24 ++++++++++++++--- .../apollo/biz/entity/InstanceConfig.java | 11 ++++++++ .../apollo/biz/service/InstanceService.java | 1 + .../apollo/common/dto/InstanceConfigDTO.java | 9 +++++++ .../util/InstanceConfigAuditUtil.java | 19 ++++++++++--- .../config/ConfigBaseInfoController.js | 27 ++++++++++--------- .../directive/namespace-panel-directive.js | 2 +- .../views/component/namespace-panel.html | 20 ++++++++------ pom.xml | 2 +- 10 files changed, 110 insertions(+), 31 deletions(-) diff --git a/apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/InstanceConfigController.java b/apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/InstanceConfigController.java index 10268051ff2..e239196a535 100644 --- a/apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/InstanceConfigController.java +++ b/apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/InstanceConfigController.java @@ -61,14 +61,35 @@ public PageDTO getByRelease(@RequestParam("releaseId") long release List instanceDTOs = Collections.emptyList(); if (instanceConfigsPage.hasContent()) { - Set instanceIds = instanceConfigsPage.getContent().stream().map - (InstanceConfig::getInstanceId).collect(Collectors.toSet()); + Multimap instanceConfigMap = HashMultimap.create(); + Set otherReleaseKeys = Sets.newHashSet(); + + for (InstanceConfig instanceConfig : instanceConfigsPage.getContent()) { + instanceConfigMap.put(instanceConfig.getInstanceId(), instanceConfig); + otherReleaseKeys.add(instanceConfig.getReleaseKey()); + } + + Set instanceIds = instanceConfigMap.keySet(); List instances = instanceService.findInstancesByIds(instanceIds); if (!CollectionUtils.isEmpty(instances)) { instanceDTOs = BeanUtils.batchTransform(InstanceDTO.class, instances); } + + for (InstanceDTO instanceDTO : instanceDTOs) { + Collection configs = instanceConfigMap.get(instanceDTO.getId()); + List configDTOs = configs.stream().map(instanceConfig -> { + InstanceConfigDTO instanceConfigDTO = new InstanceConfigDTO(); + //to save some space + instanceConfigDTO.setRelease(null); + instanceConfigDTO.setReleaseDeliveryTime(instanceConfig.getReleaseDeliveryTime()); + instanceConfigDTO.setDataChangeLastModifiedTime(instanceConfig + .getDataChangeLastModifiedTime()); + return instanceConfigDTO; + }).collect(Collectors.toList()); + instanceDTO.setConfigs(configDTOs); + } } return new PageDTO<>(instanceDTOs, pageable, instanceConfigsPage.getTotalElements()); @@ -126,6 +147,7 @@ public List getByReleasesNotIn(@RequestParam("appId") String appId, List configDTOs = configs.stream().map(instanceConfig -> { InstanceConfigDTO instanceConfigDTO = new InstanceConfigDTO(); instanceConfigDTO.setRelease(releaseMap.get(instanceConfig.getReleaseKey())); + instanceConfigDTO.setReleaseDeliveryTime(instanceConfig.getReleaseDeliveryTime()); instanceConfigDTO.setDataChangeLastModifiedTime(instanceConfig .getDataChangeLastModifiedTime()); return instanceConfigDTO; diff --git a/apollo-adminservice/src/test/java/com/ctrip/framework/apollo/adminservice/controller/InstanceConfigControllerTest.java b/apollo-adminservice/src/test/java/com/ctrip/framework/apollo/adminservice/controller/InstanceConfigControllerTest.java index 26a158602c1..27fe417d23d 100644 --- a/apollo-adminservice/src/test/java/com/ctrip/framework/apollo/adminservice/controller/InstanceConfigControllerTest.java +++ b/apollo-adminservice/src/test/java/com/ctrip/framework/apollo/adminservice/controller/InstanceConfigControllerTest.java @@ -72,13 +72,15 @@ public void getByRelease() throws Exception { String someConfigAppId = "someConfigAppId"; String someConfigNamespace = "someNamespace"; String someIp = "someIp"; + Date someReleaseDeliveryTime = new Date(); + Date anotherReleaseDeliveryTime = new Date(); when(releaseService.findOne(someReleaseId)).thenReturn(someRelease); InstanceConfig someInstanceConfig = assembleInstanceConfig(someInstanceId, someConfigAppId, - someConfigNamespace, someReleaseKey); + someConfigNamespace, someReleaseKey, someReleaseDeliveryTime); InstanceConfig anotherInstanceConfig = assembleInstanceConfig(anotherInstanceId, - someConfigAppId, someConfigNamespace, someReleaseKey); + someConfigAppId, someConfigNamespace, someReleaseKey, anotherReleaseDeliveryTime); List instanceConfigs = Lists.newArrayList(someInstanceConfig, anotherInstanceConfig); Page instanceConfigPage = new PageImpl<>(instanceConfigs, pageable, @@ -113,6 +115,12 @@ public void getByRelease() throws Exception { verifyInstance(someInstance, someInstanceDto); verifyInstance(anotherInstance, anotherInstanceDto); + + assertEquals(1, someInstanceDto.getConfigs().size()); + assertEquals(someReleaseDeliveryTime, someInstanceDto.getConfigs().get(0).getReleaseDeliveryTime()); + + assertEquals(1, anotherInstanceDto.getConfigs().size()); + assertEquals(anotherReleaseDeliveryTime, anotherInstanceDto.getConfigs().get(0).getReleaseDeliveryTime()); } @Test(expected = NotFoundException.class) @@ -133,6 +141,9 @@ public void testGetByReleasesNotIn() throws Exception { long anotherReleaseId = 2; String releaseIds = Joiner.on(",").join(someReleaseId, anotherReleaseId); + Date someReleaseDeliveryTime = new Date(); + Date anotherReleaseDeliveryTime = new Date(); + Release someRelease = mock(Release.class); Release anotherRelease = mock(Release.class); String someReleaseKey = "someReleaseKey"; @@ -153,6 +164,8 @@ public void testGetByReleasesNotIn() throws Exception { when(anotherInstanceConfig.getInstanceId()).thenReturn(anotherInstanceId); when(someInstanceConfig.getReleaseKey()).thenReturn(someInstanceConfigReleaseKey); when(anotherInstanceConfig.getReleaseKey()).thenReturn(anotherInstanceConfigReleaseKey); + when(someInstanceConfig.getReleaseDeliveryTime()).thenReturn(someReleaseDeliveryTime); + when(anotherInstanceConfig.getReleaseDeliveryTime()).thenReturn(anotherReleaseDeliveryTime); when(instanceService.findInstanceConfigsByNamespaceWithReleaseKeysNotIn(someConfigAppId, someConfigClusterName, someConfigNamespaceName, Sets.newHashSet(someReleaseKey, anotherReleaseKey))).thenReturn(Lists.newArrayList(someInstanceConfig, @@ -203,6 +216,10 @@ public void testGetByReleasesNotIn() throws Exception { assertEquals(anotherInstanceConfigReleaseKey, anotherInstanceDto.getConfigs().get(0) .getRelease() .getReleaseKey()); + + assertEquals(someReleaseDeliveryTime, someInstanceDto.getConfigs().get(0).getReleaseDeliveryTime()); + assertEquals(anotherReleaseDeliveryTime, anotherInstanceDto.getConfigs().get(0) + .getReleaseDeliveryTime()); } @Test @@ -284,13 +301,14 @@ private Instance assembleInstance(long instanceId, String appId, String clusterN } private InstanceConfig assembleInstanceConfig(long instanceId, String configAppId, String - configNamespaceName, String releaseKey) { + configNamespaceName, String releaseKey, Date releaseDeliveryTime) { InstanceConfig instanceConfig = new InstanceConfig(); instanceConfig.setInstanceId(instanceId); instanceConfig.setConfigAppId(configAppId); instanceConfig.setConfigNamespaceName(configNamespaceName); instanceConfig.setReleaseKey(releaseKey); instanceConfig.setDataChangeLastModifiedTime(new Date()); + instanceConfig.setReleaseDeliveryTime(releaseDeliveryTime); return instanceConfig; } } \ No newline at end of file diff --git a/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/entity/InstanceConfig.java b/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/entity/InstanceConfig.java index 4d525ddb43f..381414356b4 100644 --- a/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/entity/InstanceConfig.java +++ b/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/entity/InstanceConfig.java @@ -38,6 +38,9 @@ public class InstanceConfig { @Column(name = "ReleaseKey", nullable = false) private String releaseKey; + @Column(name = "ReleaseDeliveryTime", nullable = false) + private Date releaseDeliveryTime; + @Column(name = "DataChange_CreatedTime", nullable = false) private Date dataChangeCreatedTime; @@ -123,6 +126,14 @@ public void setConfigClusterName(String configClusterName) { this.configClusterName = configClusterName; } + public Date getReleaseDeliveryTime() { + return releaseDeliveryTime; + } + + public void setReleaseDeliveryTime(Date releaseDeliveryTime) { + this.releaseDeliveryTime = releaseDeliveryTime; + } + @Override public String toString() { return MoreObjects.toStringHelper(this) diff --git a/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/InstanceService.java b/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/InstanceService.java index 63d96751b2f..ed1f4f3099e 100644 --- a/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/InstanceService.java +++ b/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/InstanceService.java @@ -127,6 +127,7 @@ public InstanceConfig updateInstanceConfig(InstanceConfig instanceConfig) { "Instance config %d doesn't exist", instanceConfig.getId())); existedInstanceConfig.setReleaseKey(instanceConfig.getReleaseKey()); + existedInstanceConfig.setReleaseDeliveryTime(instanceConfig.getReleaseDeliveryTime()); existedInstanceConfig.setDataChangeLastModifiedTime(instanceConfig .getDataChangeLastModifiedTime()); diff --git a/apollo-common/src/main/java/com/ctrip/framework/apollo/common/dto/InstanceConfigDTO.java b/apollo-common/src/main/java/com/ctrip/framework/apollo/common/dto/InstanceConfigDTO.java index 36141af5820..fb2b02acd95 100644 --- a/apollo-common/src/main/java/com/ctrip/framework/apollo/common/dto/InstanceConfigDTO.java +++ b/apollo-common/src/main/java/com/ctrip/framework/apollo/common/dto/InstanceConfigDTO.java @@ -7,6 +7,7 @@ */ public class InstanceConfigDTO { private ReleaseDTO release; + private Date releaseDeliveryTime; private Date dataChangeLastModifiedTime; public ReleaseDTO getRelease() { @@ -24,4 +25,12 @@ public Date getDataChangeLastModifiedTime() { public void setDataChangeLastModifiedTime(Date dataChangeLastModifiedTime) { this.dataChangeLastModifiedTime = dataChangeLastModifiedTime; } + + public Date getReleaseDeliveryTime() { + return releaseDeliveryTime; + } + + public void setReleaseDeliveryTime(Date releaseDeliveryTime) { + this.releaseDeliveryTime = releaseDeliveryTime; + } } diff --git a/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/util/InstanceConfigAuditUtil.java b/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/util/InstanceConfigAuditUtil.java index 6ad46552ac3..a6372ae7a05 100644 --- a/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/util/InstanceConfigAuditUtil.java +++ b/apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/util/InstanceConfigAuditUtil.java @@ -88,11 +88,14 @@ void doAudit(InstanceConfigAuditModel auditModel) { InstanceConfig instanceConfig = instanceService.findInstanceConfig(instanceId, auditModel .getConfigAppId(), auditModel.getConfigClusterName(), auditModel.getConfigNamespace()); - //we need to update no matter the release key is the same or not, to ensure the - //last modified time is updated each day if (instanceConfig != null) { - instanceConfig.setReleaseKey(auditModel.getReleaseKey()); - instanceConfig.setDataChangeLastModifiedTime(new Date()); + if (!Objects.equals(instanceConfig.getReleaseKey(), auditModel.getReleaseKey())) { + instanceConfig.setReleaseKey(auditModel.getReleaseKey()); + instanceConfig.setReleaseDeliveryTime(auditModel.getOfferTime()); + } + //we need to update no matter the release key is the same or not, to ensure the + //last modified time is updated each day + instanceConfig.setDataChangeLastModifiedTime(auditModel.getOfferTime()); instanceService.updateInstanceConfig(instanceConfig); return; } @@ -103,6 +106,8 @@ void doAudit(InstanceConfigAuditModel auditModel) { instanceConfig.setConfigClusterName(auditModel.getConfigClusterName()); instanceConfig.setConfigNamespaceName(auditModel.getConfigNamespace()); instanceConfig.setReleaseKey(auditModel.getReleaseKey()); + instanceConfig.setReleaseDeliveryTime(auditModel.getOfferTime()); + instanceConfig.setDataChangeCreatedTime(auditModel.getOfferTime()); try { instanceService.createInstanceConfig(instanceConfig); @@ -174,10 +179,12 @@ public static class InstanceConfigAuditModel { private String configClusterName; private String configNamespace; private String releaseKey; + private Date offerTime; public InstanceConfigAuditModel(String appId, String clusterName, String dataCenter, String clientIp, String configAppId, String configClusterName, String configNamespace, String releaseKey) { + this.offerTime = new Date(); this.appId = appId; this.clusterName = clusterName; this.dataCenter = Strings.isNullOrEmpty(dataCenter) ? "" : dataCenter; @@ -220,6 +227,10 @@ public String getConfigClusterName() { return configClusterName; } + public Date getOfferTime() { + return offerTime; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/apollo-portal/src/main/resources/static/scripts/controller/config/ConfigBaseInfoController.js b/apollo-portal/src/main/resources/static/scripts/controller/config/ConfigBaseInfoController.js index df86c910ed8..945b8340b05 100644 --- a/apollo-portal/src/main/resources/static/scripts/controller/config/ConfigBaseInfoController.js +++ b/apollo-portal/src/main/resources/static/scripts/controller/config/ConfigBaseInfoController.js @@ -83,20 +83,23 @@ function ConfigBaseInfoController($rootScope, $scope, $location, toastr, UserSer var VISITED_APPS_STORAGE_KEY = "VisitedAppsV2"; var visitedAppsObject = JSON.parse(localStorage.getItem(VISITED_APPS_STORAGE_KEY)); var hasSaved = false; - if (visitedAppsObject) { - var visitedApps = visitedAppsObject[$rootScope.pageContext.userId]; - if (visitedApps && visitedApps.length > 0) { - visitedApps.forEach(function (app) { - if (app == appId) { - hasSaved = true; - return; - } - }); - } - } else { + if (!visitedAppsObject) { visitedAppsObject = {}; - visitedAppsObject[$rootScope.pageContext.userId] = []; + } + + if (!visitedAppsObject[$rootScope.pageContext.userId]) { + visitedAppsObject[$rootScope.pageContext.userId] = []; + } + + var visitedApps = visitedAppsObject[$rootScope.pageContext.userId]; + if (visitedApps && visitedApps.length > 0) { + visitedApps.forEach(function (app) { + if (app == appId) { + hasSaved = true; + return; + } + }); } var currentUserVisitedApps = visitedAppsObject[$rootScope.pageContext.userId]; diff --git a/apollo-portal/src/main/resources/static/scripts/directive/namespace-panel-directive.js b/apollo-portal/src/main/resources/static/scripts/directive/namespace-panel-directive.js index fcf24cf3d18..8b100de3931 100644 --- a/apollo-portal/src/main/resources/static/scripts/directive/namespace-panel-directive.js +++ b/apollo-portal/src/main/resources/static/scripts/directive/namespace-panel-directive.js @@ -257,7 +257,7 @@ directive_module.directive('apollonspanel', result.forEach(function (instance) { var configs = instance.configs; - if (configs.length > 0) { + if (configs && configs.length > 0) { configs.forEach(function (instanceConfig) { var release = instanceConfig.release; if (!groupedInstances[release.id]) { diff --git a/apollo-portal/src/main/resources/static/views/component/namespace-panel.html b/apollo-portal/src/main/resources/static/views/component/namespace-panel.html index f1bf76d87de..8a730d3e2af 100644 --- a/apollo-portal/src/main/resources/static/views/component/namespace-panel.html +++ b/apollo-portal/src/main/resources/static/views/component/namespace-panel.html @@ -409,14 +409,16 @@ Cluster Name Data Center IP + 配置获取时间 - - - - + + + + + {{instance.configs && instance.configs.length ? (instance.configs[0].releaseDeliveryTime | date: 'yyyy-MM-dd HH:mm:ss') : ''}} @@ -450,14 +452,16 @@ Cluster Name Data Center IP + 配置获取时间 - - - - + + + + + {{instance.configs && instance.configs.length ? (instance.configs[0].releaseDeliveryTime | date: 'yyyy-MM-dd HH:mm:ss') : ''}} diff --git a/pom.xml b/pom.xml index 660de5bffdc..5b7664d96e6 100644 --- a/pom.xml +++ b/pom.xml @@ -139,7 +139,7 @@ com.ctrip.framework framework-foundation - 1.2.0 + 1.2.1 com.dianping.cat