123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package com.sundata.internalevaluation.calc.calcUnit;
- import cn.hutool.core.util.RandomUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.extra.spring.SpringUtil;
- import com.sundata.internalevaluation.calc.model.CalcException;
- import com.sundata.internalevaluation.calc.model.CalcResult;
- import com.sundata.internalevaluation.calc.model.CalcUnit;
- import com.sundata.internalevaluation.calc.model.finals.CalcType;
- import com.sundata.internalevaluation.configuration.model.DataSetConfigModel;
- import com.sundata.internalevaluation.configuration.model.DataSourcesModel;
- import com.sundata.internalevaluation.configuration.service.DataSourcesService;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.atomic.AtomicReference;
- import java.util.stream.Collectors;
- /**
- * 数据集的计算单元
- */
- public class DataSetCalcUnit extends CalcUnit {
- private static final Logger log = LoggerFactory.getLogger(DataSetCalcUnit.class);
- DataSetConfigModel dataSetConfigModel;
- /**
- * 创建数据单元的绝对对象,对象必须包含如下参数
- *
- * @param calcCode 计算对象编号
- * @param calcName 计算对象名称
- * @param initContext 计算单元初始化参数
- */
- public DataSetCalcUnit(String calcCode, String calcName, Map<String, Object> initContext, DataSetConfigModel dataSetConfigModel) {
- super(calcCode, calcName, CalcType.DATAITEM, initContext);
- this.dataSetConfigModel = dataSetConfigModel;
- }
- /**
- * 判断是否已经计算过数据了
- *
- * @param calculateInstanceNumber 计算流水号
- * @return 是否计算过 true 计算过 false 没有计算过
- */
- @Override
- public boolean isCalcFinished(String calculateInstanceNumber) {
- // TODO 计算是否已经计算
- return false;
- // return DataImages.dataSetCalcUnitHashMap.containsKey(calculateInstanceNumber)&&DataImages.dataSetCalcUnitHashMap.get(calculateInstanceNumber).stream().anyMatch(a->a.getCalcCode().equals(this.getCalcCode()));
- }
- /**
- * 初始化计算结果的方法,如果已经计算过,在实现过程中,应当在此方法中根据计算流水号重新初始化 resultContext 结果对象,为其他依赖对象做准备
- * 若明明计算过本单元但再次计算时没有初始化该对象,则计算依赖出现问题无法定位与处理
- *
- * @param calculateInstanceNumber 计算流水号
- */
- @Override
- public void initResultContext(String calculateInstanceNumber) {
- // TODO 初始化
- // List<DataSetCalcUnit> dataSetCalcUnits = DataImages.dataSetCalcUnitHashMap.get(calculateInstanceNumber);
- List<DataSetCalcUnit> dataSetCalcUnits = new ArrayList<>();
- // 筛选并查找对象,如果找不到则报错
- if(dataSetCalcUnits.stream().noneMatch(a -> a.getCalcCode().equals(this.getCalcCode()))){
- throw new CalcException(calculateInstanceNumber, StrUtil.format("无法找到已计算完成的结果,计算单元编号:{},计算流水号为:{}。", this.getCalcCode(),calculateInstanceNumber));
- }
- this.setResultContext(dataSetCalcUnits.stream().filter(a->a.getCalcCode().equals(this.getCalcCode())).findFirst().get().getResultContext());
- }
- /**
- * 根据节点配置获取源节点;
- *
- * @return 所有源头节点
- */
- @Override
- public List<CalcUnit> getSourceCalcUnits() {
- // TODO 获取源头节点
- //DataSetConfigService dataSetService = SpringUtil.getBean(DataSetConfigService.class);
- DataSourcesService sourcesService = SpringUtil.getBean(DataSourcesService.class);
- //dataSetService.getDataList(new DataSetConfigModel());
- List<DataSourcesModel> sourceList =null;
- for (String s :dataSetConfigModel.getDataSourcesNos()) {
- // sourceModel.setDataSourcesNo();
- DataSourcesModel model = sourcesService.selectDetailData(s);
- sourceList.add(model);
- }
- List<CalcUnit> calcList = sourceList.stream().map(model ->
- new DataSourcesCalcUnit(model.getDataSourcesNo(),model.getDataSourcesName(),CalcType.DATASOURCES,Map.of(),model)).collect(Collectors.toList());
- return calcList;
- // return ConfigImages.dataSetCalcUnitListMap.get(this);
- }
- /**
- * 计算之后的方法,可实现为空
- *
- * @param context 计算参数过程数据
- */
- @Override
- public void afterCalc(Map<String, Object> context) {
- context.put(RandomUtil.randomNumbers(5)+"-1", this.getCalcCode());
- log.debug("计算之后的参数结构:{}",context);
- }
- /**
- * 计算之前,可实现空
- *
- * @param context 计算参数过程数据
- */
- @Override
- public void beforeCalc(Map<String, Object> context) {
- context.put(RandomUtil.randomNumbers(5)+"-2", this.getCalcCode());
- log.debug("计算之前的参数结构:{}",context);
- }
- /**
- * 必须实现的主体计算内容
- *
- * @param context 节点计算参数清单
- * @param sourceResults 整个计算过程中的节点结果
- */
- @Override
- public void calc(final CalcResult<String, Object> thisResult, String calculateInstanceNumber,Map<String, Object> context, Map<CalcUnit, CalcResult<String, Object>> sourceResults) {
- // log.debug("模拟计算过程,计算参数为:【{}】,来源结果数据为:【{}】",context, sourceResults);
- // List<User> userList = new ArrayList<>();
- // List<Bussiness> bussinesseList = new ArrayList<>();
- // Faker faker = Faker.instance(Locale.CHINA);
- // thisResult.put(this.getCalcCode()+"-1", faker.number().randomNumber());
- // thisResult.put(this.getCalcCode()+"-2", faker.number().randomNumber());
- //
- // for (int i = 0; i < 1000; i++) {
- // userList.add(new User(faker.number().randomDigitNotZero(),faker.name().fullName(),faker.number().numberBetween(1,90),faker.address().fullAddress(),faker.internet().emailAddress("sundatasoft.com"), faker.phoneNumber().cellPhone(),faker.date().birthday()));
- // }
- // for (int i = 0; i < 100_000; i++) {
- // bussinesseList.add(new Bussiness(faker.number().randomDigitNotZero(),faker.name().fullName(),faker.number().randomDouble(6,100,1000000),faker.number().numberBetween(1,90)));
- // }
- // thisResult.put(this.getCalcCode()+"-user", userList);
- // thisResult.put(this.getCalcCode()+"-buss", bussinesseList);
- //
- //// log.debug("模拟计算完成,结果对象为:【{}】",thisResult);
- //
- // context.put(calculateInstanceNumber+"-"+this.getCalcCode(),thisResult);
- // log.debug("对参数二次修改:{}",context);
- // TODO 实际的计算过程
- AtomicReference<CalcResult<String, Object>> resultMap = new AtomicReference<>();
- // 合并数据集
- sourceResults.forEach((calcUnit,result)-> {
- resultMap.setOpaque(result);
- });
- thisResult.put(this.getCalcCode(),resultMap);
- // DataSourcesModel sourceModel = new DataSourcesModel();
- // CalcTaskResult calc = CalcUtil.calc("INDEX"+System.currentTimeMillis(),
- // new DataSourcesCalcUnit(sourceModel.getDataSourcesNo(),sourceModel.getDataSourcesName(),CalcType.DATASOURCES,Map.of(),sourceModel),Map.of());
- }
- }
|