Skip to content

Commit

Permalink
feature: support update scene param
Browse files Browse the repository at this point in the history
Signed-off-by: tiny-x <[email protected]>
  • Loading branch information
tiny-x committed May 10, 2021
1 parent 3293a22 commit e960c6d
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum ExceptionMessageEnum {
// SCENE
SCENE_IS_NULL(6400, "场景不存在"),
SCENE_EXISTS(6401, "场景已经存在"),
SCENE_FILE_NAME_PARSE_FAIL(6402, "场景名称解析错误"),

// PROBES
PROBES_NO_FOUND(6500, "探针不存在"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public Optional<SceneDO> selectByCodeAndVersion(String code, String version) {
return Optional.ofNullable(sceneMapper.selectOne(queryWrapper));
}

public List<SceneDO> selectByCode(String code) {
QueryWrapper<SceneDO> queryWrapper = QueryWrapperBuilder.build();
queryWrapper.lambda().like(SceneDO::getSceneCode, code);
return sceneMapper.selectList(queryWrapper);
}

public void insertBatch(Collection<SceneDO> collection) {
saveBatch(collection);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alibaba.chaosblade.box.scenario.api.init;

import lombok.Data;

import java.util.List;

/**
* @author yefei
*/
@Data
public class Scene {

private String sceneCode;

private List<SceneParam> sceneParams;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alibaba.chaosblade.box.scenario.api.init;

import com.alibaba.chaosblade.box.common.jackson.JsonToStringDeserializer;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;

/**
* @author yefei
*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SceneParam {

private String name;

private String alias;

private String description;

@JsonDeserialize(using = JsonToStringDeserializer.class)
private String component;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.alibaba.chaosblade.box.scenario.api.init;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.chaosblade.box.common.utils.JsonUtils;
import com.alibaba.chaosblade.box.dao.model.SceneDO;
import com.alibaba.chaosblade.box.dao.model.SceneParamDO;
import com.alibaba.chaosblade.box.dao.repository.SceneParamRepository;
import com.alibaba.chaosblade.box.dao.repository.SceneRepository;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author yefei
*/
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class SceneParamComponentLoader implements InitializingBean {

@Autowired
private SceneRepository sceneRepository;

@Autowired
private SceneParamRepository sceneParamRepository;

@Override
public void afterPropertiesSet() {
InputStream stream = ResourceUtil.getStream("scene_param_component.json");

List<Scene> scenes = JsonUtils.readValue(new TypeReference<List<Scene>>() {
}, IoUtil.readBytes(stream));

if (CollUtil.isEmpty(scenes)) {
return;
}

for (Scene scene : scenes) {
List<SceneDO> sceneDOS = sceneRepository.selectByCode(scene.getSceneCode());

sceneDOS.forEach(sceneDO -> {
List<SceneParam> sceneParams = scene.getSceneParams();
if (CollUtil.isNotEmpty(sceneParams)) {
Map<String, SceneParam> map = sceneParams.stream().collect(Collectors.toMap(SceneParam::getName, u -> u));
List<SceneParamDO> sceneParamDOS = sceneParamRepository
.selectList(SceneParamDO.builder().sceneId(sceneDO.getId()).build());

sceneParamDOS.forEach(sceneParamDO -> {
SceneParam sceneParam = map.get(sceneParamDO.getParamName());
if (sceneParam != null && StrUtil.isEmpty(sceneParamDO.getComponent())) {
sceneParamRepository.updateByPrimaryKey(sceneParamDO.getId(),
SceneParamDO.builder()
.component(sceneParam.getComponent())
.build());
}
});
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
[
{
"sceneCode": "chaosblade.cpu.fullload",
"sceneParams": [
{
"name": "cpu-count",
"alias": "",
"description": "",
"component": {
"type": "number",
"required": false
}
},
{
"name": "cpu-percent",
"alias": "",
"description": "",
"component": {
"type": "range",
"required": false,
"minValue": "0",
"maxValue": "100",
"values": []
}
}
]
},
{
"sceneCode": "chaosblade.mem.load",
"sceneParams": [
{
"name": "reserve",
"alias": "",
"description": "",
"component": {
"type": "number",
"required": false
}
},
{
"name": "rate",
"alias": "",
"description": "",
"component": {
"type": "number",
"required": false
}
},
{
"name": "mem-percent",
"alias": "",
"description": "",
"component": {
"type": "range",
"required": false,
"minValue": "0",
"maxValue": "100",
"values": []
}
},
{
"name": "mode",
"alias": "",
"description": "",
"component": {
"type": "select",
"required": false,
"minValue": "0",
"maxValue": "100",
"values": [
{
"key": 0,
"label": "ram",
"value": "ram"
},
{
"key": 1,
"label": "cache",
"value": "cache"
}
]
}
}
]
},
{
"sceneCode": "chaosblade.jvm.script",
"sceneParams": [
{
"name": "script-type",
"alias": "",
"description": "",
"component": {
"type": "select",
"required": false,
"values": [
{
"key": 0,
"label": "java",
"value": "java"
},
{
"key": 1,
"label": "groovy",
"value": "groovy"
}
]
}
},
{
"name": "script-content",
"alias": "",
"description": "",
"component": {
"type": "script",
"required": false
}
}
]
},
{
"sceneCode": "chaosblade.dubbo.delay",
"sceneParams": [
{
"name": "consumer",
"alias": "",
"description": "",
"component": {
"type": "radio",
"required": false,
"values": [
{
"key": 0,
"label": "",
"value": "true"
},
{
"key": 1,
"label": "",
"value": "false"
}
]
}
},
{
"name": "provider",
"alias": "",
"description": "",
"component": {
"type": "radio",
"required": false,
"values": [
{
"key": 0,
"label": "",
"value": "true"
},
{
"key": 1,
"label": "",
"value": "false"
}
]
}
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public interface SceneParamService {
*/
List<SceneParamResponse> selectSceneParamBySceneId(Long sceneId);

/**
*
* @param sceneParamRequest
*/
void updateSceneParam(SceneParamRequest sceneParamRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ public interface SceneService {
* @param sceneImportRequest
* @return
*/
SceneImportResponse uploadScenarios(SceneImportRequest sceneImportRequest);
SceneImportResponse uploadScenarios(SceneImportRequest sceneImportRequest) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,15 @@ public ExperimentResponse getExperimentById(ExperimentRequest experimentRequest)
ActivityTask activityTask = JsonUtils.readValue(ActivityTask.class, experimentActivity.getActivityDefinition());
SceneResponse scenario = sceneService.getScenarioById(SceneRequest.builder().scenarioId(activityTask.getSceneId()).build());
scenario.setParameters(scenario.getParameters().stream().map(
sceneParamResponse -> SceneParamResponse.builder()
.parameterId(sceneParamResponse.getParameterId())
.paramName(sceneParamResponse.getParamName())
.name(sceneParamResponse.getParamName())
.description(sceneParamResponse.getDescription())
sceneParam -> SceneParamResponse.builder()
.parameterId(sceneParam.getParameterId())
.paramName(sceneParam.getParamName())
.name(sceneParam.getParamName())
.alias(sceneParam.getAlias())
.description(sceneParam.getDescription())
.value(Optional.ofNullable(activityTask.getArguments())
.map(arguments -> arguments.get(sceneParamResponse.getParamName())).orElse(null))
.map(arguments -> arguments.get(sceneParam.getParamName())).orElse(null))
.component(sceneParam.getComponent())
.build()).collect(Collectors.toList()));
experimentActivity.setScene(scenario);
});
Expand All @@ -382,12 +384,14 @@ public ExperimentResponse getExperimentById(ExperimentRequest experimentRequest)
.categories(scenario.getCategories())
.description(scenario.getDescription())
.parameters(scenario.getParameters().stream().map(
sceneParamResponse -> SceneParamResponse.builder()
.parameterId(sceneParamResponse.getParameterId())
.description(sceneParamResponse.getDescription())
.name(sceneParamResponse.getName())
sceneParam -> SceneParamResponse.builder()
.parameterId(sceneParam.getParameterId())
.description(sceneParam.getDescription())
.component(sceneParam.getComponent())
.name(sceneParam.getName())
.alias(sceneParam.getAlias())
.value(Optional.ofNullable(activityTask.getArguments())
.map(arguments -> arguments.get(sceneParamResponse.getParamName())).orElse(null))
.map(arguments -> arguments.get(sceneParam.getParamName())).orElse(null))
.build()).collect(Collectors.toList()))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public List<SceneParamResponse> selectSceneParamBySceneId(Long sceneId) {
return Collections.emptyList();
}
return sceneParamDOS.stream().map(sceneParamDO -> SceneParamResponse.builder()
.parameterId(sceneParamDO.getSceneId())
.parameterId(sceneParamDO.getId())
.alias(sceneParamDO.getAlias())
.paramName(sceneParamDO.getParamName())
.name(sceneParamDO.getParamName())
Expand All @@ -85,4 +85,15 @@ public List<SceneParamResponse> selectSceneParamBySceneId(Long sceneId) {
.build()
).collect(Collectors.toList());
}

@Override
public void updateSceneParam(SceneParamRequest sceneParamRequest) {
sceneParamRepository.updateByPrimaryKey(sceneParamRequest.getParameterId(),
SceneParamDO.builder().alias(sceneParamRequest.getAlias())
.description(sceneParamRequest.getDescription())
.defaultValue(sceneParamRequest.getDefaultValue())
.isRequired(sceneParamRequest.getIsRequired())
.component(JsonUtils.writeValueAsString(sceneParamRequest.getComponent()))
.build());
}
}
Loading

0 comments on commit e960c6d

Please sign in to comment.