Skip to content

Commit

Permalink
feat(sdk): report mapping template upgrading result (#131)
Browse files Browse the repository at this point in the history
* first installation

* stage upgrade report

* report production upgrade result

* add path
  • Loading branch information
kuangxiang20240501 authored Nov 13, 2024
1 parent c35a541 commit 9a9e24b
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ app:
mef.sonata.api-target-mapper.order.eline.delete: <1,4>
mef.sonata.api-target-mapper.order.uni.read: <2,1>
mef.sonata.api-target-mapper.order.eline.read: <2,2>
mef.sonata.api-target-mapper.order.uni.read.delete: <3,1>
mef.sonata.api-target-mapper.order.eline.read.delete: <3,2>
mef.sonata.api-target-mapper.inventory.uni.list: <1,1>
mef.sonata.api-target-mapper.inventory.eline.list: <1,2>
mef.sonata.api-target-mapper.inventory.uni.read: <2,1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import com.consoleconnect.kraken.operator.auth.service.UserService;
import com.consoleconnect.kraken.operator.controller.dto.UpgradeTuple;
import com.consoleconnect.kraken.operator.controller.model.MgmtProperty;
import com.consoleconnect.kraken.operator.controller.model.SystemInfo;
import com.consoleconnect.kraken.operator.controller.service.upgrade.UpgradeSourceServiceFactory;
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum;
import com.consoleconnect.kraken.operator.core.enums.UpgradeResultEventEnum;
import com.consoleconnect.kraken.operator.core.enums.UpgradeSourceEnum;
import com.consoleconnect.kraken.operator.core.event.PlatformSettingCompletedEvent;
import com.consoleconnect.kraken.operator.core.ingestion.DataIngestionJob;
import com.consoleconnect.kraken.operator.core.service.EventSinkService;
import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService;
import com.consoleconnect.kraken.operator.core.toolkit.AssetsConstants;
import com.consoleconnect.kraken.operator.core.toolkit.Paging;
import java.time.ZonedDateTime;
import java.util.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,6 +25,7 @@
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -32,6 +39,8 @@ public class TemplateIngestService {
private final UserService userService;
private final UpgradeSourceServiceFactory upgradeSourceServiceFactory;
private final TemplateUpgradeService templateUpgradeService;
private final EventSinkService eventSinkService;
private final SystemInfoService systemInfoService;

@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady(Object event) {
Expand All @@ -48,6 +57,8 @@ public void onApplicationReady(Object event) {
log.info("Platform Boot Up Event Received, startup firstly,initialize templates ");
dataIngestionJob.ingestionWorkspace();
eventPublisher.publishEvent(new PlatformSettingCompletedEvent());
// trigger first installation report
reportFirstInstallation();
} else {
log.info(
"Platform Boot Up Event Received, had initialized the templates and ignored for this time ");
Expand All @@ -70,4 +81,30 @@ public void onApplicationReady(Object event) {
eventPublisher.publishEvent(new PlatformSettingCompletedEvent());
log.info("Platform Boot Up Event Completed");
}

private void reportFirstInstallation() {
Paging<UnifiedAssetDto> assetDtoPaging =
unifiedAssetService.findBySpecification(
Tuple2.ofList(
AssetsConstants.FIELD_KIND, AssetKindEnum.PRODUCT_TEMPLATE_UPGRADE.getKind()),
null,
null,
PageRequest.of(0, 1, Sort.Direction.ASC, AssetsConstants.FIELD_CREATE_AT),
null);
if (CollectionUtils.isEmpty(assetDtoPaging.getData())) {
return;
}
UnifiedAssetDto unifiedAssetDto = assetDtoPaging.getData().get(0);
// trigger first installation report
eventSinkService.reportTemplateUpgradeResult(
unifiedAssetDto,
UpgradeResultEventEnum.INSTALLED,
reportEvent -> {
SystemInfo systemInfo = systemInfoService.find();
reportEvent.setProductKey(systemInfo.getProductKey());
reportEvent.setProductSpec(systemInfo.getProductSpec());
reportEvent.setProductVersion(systemInfo.getControlProductVersion());
reportEvent.setInstalledAt(ZonedDateTime.now());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.consoleconnect.kraken.operator.core.model.facet.ComponentAPITargetFacets;
import com.consoleconnect.kraken.operator.core.repo.UnifiedAssetRepository;
import com.consoleconnect.kraken.operator.core.service.CompatibilityCheckService;
import com.consoleconnect.kraken.operator.core.service.EventSinkService;
import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService;
import com.consoleconnect.kraken.operator.core.toolkit.*;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -71,6 +72,7 @@ public class TemplateUpgradeService {
private final ApiComponentService apiComponentService;
private final TransactionService transactionService;
private final CompatibilityCheckService compatibilityCheckService;
private final EventSinkService eventSinkService;

protected String deployProduction(
String templateUpgradeId, String stageEnvId, String productionEnvId, String userId) {
Expand Down Expand Up @@ -116,6 +118,16 @@ protected String deployProduction(
templateUpgradeDeployment,
new SyncMetadata("", "", DateTime.nowInUTCString(), userId),
true);
if (DeployStatusEnum.IN_PROCESS
.name()
.equalsIgnoreCase(templateUpgradeDeployment.getMetadata().getStatus())) {
// report result
String templateDeploymentId = ingestionDataResult.getData().getId().toString();
upgradeSourceServiceFactory
.getUpgradeSourceService(templateUpgradeId)
.reportResult(templateUpgradeId, templateDeploymentId);
log.info("report production upgrade begin {}", templateDeploymentId);
}
addLabels(envDeployment, ingestionDataResult.getData().getId().toString());

return ingestionDataResult.getData().getId().toString();
Expand Down Expand Up @@ -586,7 +598,7 @@ public String controlPlaneUpgrade(String templateUpgradeId, String userId) {
throw KrakenException.badRequest("Current system state is not allowed to upgrade");
}
UnifiedAssetDto productAsset = this.getProductAsset();

ZonedDateTime startTime = ZonedDateTime.now();
String controlDeploymentId =
transactionService.execInNewTransaction(
nil -> {
Expand Down Expand Up @@ -642,6 +654,7 @@ public String controlPlaneUpgrade(String templateUpgradeId, String userId) {
systemInfoService.updateSystemStatusImmediately(SystemStateEnum.CONTROL_PLANE_UPGRADE_DONE);
throw KrakenException.internalError("Control plane upgraded failed:" + e.getMessage());
}

systemInfoService.updateProductVersion(
SystemStateEnum.CONTROL_PLANE_UPGRADE_DONE,
getTemplateVersion(templateUpgradeId),
Expand All @@ -651,6 +664,21 @@ public String controlPlaneUpgrade(String templateUpgradeId, String userId) {
throw KrakenException.internalError(
"Control plane upgraded failed:" + ingestionDataResult.getMessage());
}
ZonedDateTime endTime = ZonedDateTime.now();
UnifiedAssetDto assetDto = unifiedAssetService.findOne(templateUpgradeId);
eventSinkService.reportTemplateUpgradeResult(
assetDto,
UpgradeResultEventEnum.UPGRADE,
event -> {
SystemInfo latestSystemInfo = systemInfoService.find();
event.setUpgradeBeginAt(startTime);
event.setUpgradeEndAt(endTime);
event.setEnvName(EnvNameEnum.CONTROL_PLANE);
event.setProductKey(latestSystemInfo.getProductKey());
event.setProductSpec(latestSystemInfo.getProductSpec());
event.setProductVersion(latestSystemInfo.getControlProductVersion());
});

return ingestionDataResult.getData().getId().toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@
import com.consoleconnect.kraken.operator.controller.dto.UpgradeRecord;
import com.consoleconnect.kraken.operator.controller.dto.UpgradeTuple;
import com.consoleconnect.kraken.operator.controller.model.MgmtProperty;
import com.consoleconnect.kraken.operator.controller.model.SystemInfo;
import com.consoleconnect.kraken.operator.controller.model.TemplateUpgradeDeploymentFacets;
import com.consoleconnect.kraken.operator.controller.service.ApiComponentService;
import com.consoleconnect.kraken.operator.controller.service.SystemInfoService;
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.entity.UnifiedAssetEntity;
import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum;
import com.consoleconnect.kraken.operator.core.enums.DeployStatusEnum;
import com.consoleconnect.kraken.operator.core.enums.EnvNameEnum;
import com.consoleconnect.kraken.operator.core.enums.UpgradeResultEventEnum;
import com.consoleconnect.kraken.operator.core.event.TemplateUpgradeResultEvent;
import com.consoleconnect.kraken.operator.core.ingestion.ResourceLoaderFactory;
import com.consoleconnect.kraken.operator.core.model.AppProperty;
import com.consoleconnect.kraken.operator.core.model.FileContentDescriptor;
import com.consoleconnect.kraken.operator.core.model.UnifiedAsset;
import com.consoleconnect.kraken.operator.core.model.facet.ProductFacets;
import com.consoleconnect.kraken.operator.core.repo.UnifiedAssetRepository;
import com.consoleconnect.kraken.operator.core.service.EventSinkService;
import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService;
import com.consoleconnect.kraken.operator.core.toolkit.AssetsConstants;
import com.consoleconnect.kraken.operator.core.toolkit.LabelConstants;
import com.consoleconnect.kraken.operator.core.toolkit.Paging;
import com.consoleconnect.kraken.operator.core.toolkit.YamlToolkit;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

@AllArgsConstructor
Expand All @@ -31,6 +46,9 @@ public abstract class AbstractUpgradeSourceService implements UpgradeSourceServi
private final UnifiedAssetRepository unifiedAssetRepository;
private final MgmtProperty mgmtProperty;
private final ApiComponentService apiComponentService;
private final UnifiedAssetService unifiedAssetService;
private final SystemInfoService systemInfoService;
private final EventSinkService eventSinkService;
protected static final List<String> CACHED_ASSET_KINDS =
List.of(
AssetKindEnum.COMPONENT_API_TARGET_MAPPER.getKind(),
Expand Down Expand Up @@ -135,4 +153,82 @@ protected List<ComponentExpandDTO> convertFromSource(List<UnifiedAssetDto> allAs
componentList,
assetMap.get(AssetKindEnum.COMPONENT_API_TARGET_MAPPER.getKind()));
}

@Override
public void reportResult(String templateUpgradeId, String templateDeploymentId) {
UnifiedAssetDto unifiedAssetDto = unifiedAssetService.findOne(templateUpgradeId);
Paging<UnifiedAssetDto> assetDtoPaging =
unifiedAssetService.findBySpecification(
Tuple2.ofList(
AssetsConstants.FIELD_KIND, AssetKindEnum.PRODUCT_TEMPLATE_DEPLOYMENT.getKind()),
Tuple2.ofList(LabelConstants.LABEL_APP_TEMPLATE_UPGRADE_ID, templateUpgradeId),
null,
null,
null);
List<UnifiedAssetDto> list = assetDtoPaging.getData();
if (CollectionUtils.isEmpty(list)) {
return;
}
if (StringUtils.isNotBlank(templateDeploymentId)) {
UnifiedAssetDto templateDeployment = unifiedAssetService.findOne(templateDeploymentId);
Optional.ofNullable(templateDeployment).ifPresent(dto -> report(dto, unifiedAssetDto));
}
}

private void report(UnifiedAssetDto deploymentDto, UnifiedAssetDto templateDto) {
boolean isStage =
deploymentDto
.getMetadata()
.getLabels()
.getOrDefault(LabelConstants.LABEL_ENV_NAME, "")
.equalsIgnoreCase(EnvNameEnum.STAGE.name());
EnvNameEnum envName = isStage ? EnvNameEnum.STAGE : EnvNameEnum.PRODUCTION;
SystemInfo systemInfo = systemInfoService.find();
String productVersion =
templateDto.getMetadata().getLabels().get(LabelConstants.LABEL_PRODUCT_VERSION);
if (DeployStatusEnum.SUCCESS.name().equalsIgnoreCase(deploymentDto.getMetadata().getStatus())) {
TemplateUpgradeDeploymentFacets facets =
UnifiedAsset.getFacets(deploymentDto, TemplateUpgradeDeploymentFacets.class);
TemplateUpgradeDeploymentFacets.EnvDeployment envDeployment = facets.getEnvDeployment();

if (CollectionUtils.isEmpty(envDeployment.getSystemDeployments())
&& CollectionUtils.isEmpty(envDeployment.getMapperDeployment())) {
eventSinkService.reportTemplateUpgradeResult(
templateDto,
UpgradeResultEventEnum.UPGRADE,
event -> {
event.setEnvName(envName);
event.setUpgradeBeginAt(ZonedDateTime.now());
event.setUpgradeEndAt(ZonedDateTime.now().plusSeconds(5L));
fillProductInfo(systemInfo, productVersion, event);
});
} else {
eventSinkService.reportTemplateUpgradeResult(
templateDto,
UpgradeResultEventEnum.UPGRADE,
event -> {
event.setEnvName(envName);
event.setUpgradeEndAt(ZonedDateTime.now().plusSeconds(5L));
fillProductInfo(systemInfo, productVersion, event);
});
}

} else {
eventSinkService.reportTemplateUpgradeResult(
templateDto,
UpgradeResultEventEnum.UPGRADE,
event -> {
event.setEnvName(envName);
event.setUpgradeBeginAt(ZonedDateTime.now());
fillProductInfo(systemInfo, productVersion, event);
});
}
}

private void fillProductInfo(
SystemInfo systemInfo, String productVersion, TemplateUpgradeResultEvent event) {
event.setProductKey(systemInfo.getProductKey());
event.setProductSpec(systemInfo.getProductSpec());
event.setProductVersion(productVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.consoleconnect.kraken.operator.controller.dto.UpgradeTuple;
import com.consoleconnect.kraken.operator.controller.model.MgmtProperty;
import com.consoleconnect.kraken.operator.controller.service.ApiComponentService;
import com.consoleconnect.kraken.operator.controller.service.SystemInfoService;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.enums.UpgradeSourceEnum;
import com.consoleconnect.kraken.operator.core.ingestion.ResourceLoaderFactory;
Expand All @@ -12,6 +13,8 @@
import com.consoleconnect.kraken.operator.core.model.facet.ProductFacets;
import com.consoleconnect.kraken.operator.core.model.facet.WorkspaceFacets;
import com.consoleconnect.kraken.operator.core.repo.UnifiedAssetRepository;
import com.consoleconnect.kraken.operator.core.service.EventSinkService;
import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Objects;
Expand All @@ -29,13 +32,19 @@ public ClasspathSourceUpgradeService(
ResourceLoaderFactory resourceLoaderFactory,
UnifiedAssetRepository unifiedAssetRepository,
MgmtProperty mgmtProperty,
UnifiedAssetService unifiedAssetService,
EventSinkService eventSinkService,
SystemInfoService systemInfoService,
ApiComponentService apiComponentService) {
super(
appProperty,
resourceLoaderFactory,
unifiedAssetRepository,
mgmtProperty,
apiComponentService);
apiComponentService,
unifiedAssetService,
systemInfoService,
eventSinkService);
}

@Override
Expand Down
Loading

0 comments on commit 9a9e24b

Please sign in to comment.