package com.iailab.module.mcs.service.impl;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.iailab.common.dto.IndexStatisticDTO;
|
import com.iailab.module.any.dto.AnyAllEvaluationDTO;
|
import com.iailab.module.any.dto.AnyStoreReliabilityDTO;
|
import com.iailab.module.data.dto.FeignQueryPointDTO;
|
import com.iailab.framework.common.page.PageData;
|
import com.iailab.framework.common.service.impl.CrudServiceImpl;
|
import com.iailab.common.utils.DateUtils;
|
import com.iailab.module.mcs.dao.StModelResultDao;
|
import com.iailab.module.mcs.dto.StModelResultDTO;
|
import com.iailab.module.mcs.entity.StModelResultEntity;
|
import com.iailab.module.mcs.service.StModelResultService;
|
import com.iailab.module.model.sample.entity.DataEntity;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 模型返回信息表
|
*
|
* @author lirm ${email}
|
* @since 1.0.0 2023-06-21
|
*/
|
@Service
|
public class StModelResultServiceImpl extends CrudServiceImpl<StModelResultDao, StModelResultEntity, StModelResultDTO> implements StModelResultService {
|
|
@Resource
|
private StModelResultDao stModelResultDao;
|
|
@Override
|
public QueryWrapper<StModelResultEntity> getWrapper(Map<String, Object> params){
|
String id = (String)params.get("id");
|
|
QueryWrapper<StModelResultEntity> wrapper = new QueryWrapper<>();
|
wrapper.eq(StringUtils.isNotBlank(id), "id", id);
|
|
return wrapper;
|
}
|
|
@Override
|
public Map<String, List<IndexStatisticDTO>> getResultList(Map<String, Object> params) {
|
params.put("sortType", "asc");
|
Map<String, List<IndexStatisticDTO>> result = new HashMap<>(5);
|
List<StModelResultEntity> list = stModelResultDao.getResultList(params);
|
if (CollectionUtils.isEmpty(list)) {
|
return result;
|
}
|
Map<String, List<StModelResultEntity>> groupList = list.stream().collect(Collectors.groupingBy(StModelResultEntity::getResultKey));
|
groupList.forEach((k, v) -> {
|
try{
|
List<IndexStatisticDTO> itemList = v.stream().map(item -> {
|
IndexStatisticDTO dto = new IndexStatisticDTO();
|
dto.setData(new BigDecimal(item.getResultValue()));
|
dto.setDateTime(item.getResultTime());
|
return dto;
|
}).collect(Collectors.toList());
|
result.put(k, itemList);
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
});
|
return result;
|
}
|
|
@Override
|
public List<Map<String, Object>> getList(Map<String, Object> params) {
|
if (params.get("isAsc") != null && params.get("isAsc").toString().equals("false")) {
|
params.put("sortType", "desc");
|
} else {
|
params.put("sortType", "asc");
|
}
|
List<Map<String, Object>> result = new ArrayList<>();
|
List<StModelResultEntity> list = stModelResultDao.getResultList(params);
|
List<Date> dateList = list.stream().map(t -> t.getResultTime()).distinct().collect(Collectors.toList());
|
Map<Date, List<StModelResultEntity>> groupList = list.stream().collect(Collectors.groupingBy(StModelResultEntity::getResultTime));
|
dateList.forEach(item -> {
|
Map<String, Object> vt = new HashMap<>();
|
groupList.get(item).forEach(v -> {
|
vt.put(v.getResultKey(), v.getResultValue());
|
});
|
vt.put("resultTime", item);
|
result.add(vt);
|
});
|
return result;
|
}
|
|
@Override
|
public List<StModelResultDTO> getLastResultByCode(Map<String, Object> params) {
|
return baseDao.getLastResultByCode(params);
|
}
|
|
@Override
|
public Map<String, Object> getLastResultMap(Map<String, Object> params) {
|
Map<String, Object> result = new HashMap<>(1);
|
List<StModelResultDTO> list = baseDao.getLastResultByCode(params);
|
if (CollectionUtils.isEmpty(list)) {
|
return result;
|
}
|
list.forEach(item -> {
|
result.put(item.getResultKey(), item.getResultValue());
|
});
|
result.put("result_time", list.get(0).getResultTime());
|
return result;
|
}
|
|
@Override
|
public Map<String, Object> getResultByCodeDate(Map<String, Object> params) {
|
Map<String, Object> result = new HashMap<>(1);
|
List<StModelResultDTO> list = baseDao.getResultByCodeDate(params);
|
if (CollectionUtils.isEmpty(list)) {
|
return result;
|
}
|
list.forEach(item -> {
|
result.put(item.getResultKey(), item.getResultValue());
|
});
|
result.put("result_time", list.get(0).getResultTime());
|
return result;
|
}
|
|
@Override
|
public void migrationModelResult(Map<String, Date> params) {
|
List<StModelResultEntity> list = stModelResultDao.selectList(getDateWrapper(params));
|
if (CollectionUtils.isEmpty(list)){
|
return;
|
}
|
stModelResultDao.migrationModelResult(list);
|
stModelResultDao.delete(getDateWrapper(params));
|
}
|
|
@Override
|
public void addPy(String modelId, List<String> lines, Date runTime) {
|
List<StModelResultEntity> list = new ArrayList<>();
|
|
if (CollectionUtils.isEmpty(lines)) {
|
return;
|
}
|
for (int i = 0; i < lines.size(); i ++) {
|
JSONObject josnObject = JSONObject.parseObject(lines.get(i));
|
for (String key : josnObject.keySet()) {
|
StModelResultEntity entity = new StModelResultEntity();
|
entity.setId(UUID.randomUUID().toString());
|
entity.setModelId(modelId);
|
entity.setLineIndex(i);
|
entity.setResultKey(key);
|
entity.setResultValue(josnObject.getString(key));
|
entity.setResultTime(runTime);
|
list.add(entity);
|
}
|
}
|
baseDao.insertList(list);
|
}
|
|
@Override
|
public List<DataEntity> getValueList(String resultKey, Date startTime, Date endTime) {
|
List<DataEntity> result = new ArrayList<>();
|
String[] params = resultKey.split(":");
|
|
QueryWrapper<StModelResultEntity> wrapper = new QueryWrapper<>();
|
wrapper.eq("result_key", params[1])
|
.ge("result_time", startTime)
|
.le("result_time", endTime)
|
.orderByAsc("result_time");
|
List<StModelResultEntity> list = baseDao.selectList(wrapper);
|
if (CollectionUtils.isEmpty(list)) {
|
return result;
|
}
|
for (int i = 0; i < list.size(); i++) {
|
DataEntity dataEntity = new DataEntity();
|
dataEntity.setTimeStamp(list.get(i).getResultTime());
|
dataEntity.setDataValue(Double.parseDouble(list.get(i).getResultValue()));
|
result.add(dataEntity);
|
}
|
return result;
|
}
|
|
@Override
|
public void addML(String modelId, Map<String,Object> tMap, Date runTime) {
|
if (tMap == null) {
|
return;
|
}
|
List<StModelResultEntity> list = new ArrayList<>();
|
for(String key:tMap.keySet()){
|
StModelResultEntity entity = new StModelResultEntity();
|
entity.setId(UUID.randomUUID().toString());
|
entity.setModelId(modelId);
|
entity.setLineIndex(0);
|
entity.setResultKey(key);
|
entity.setResultValue(tMap.get(key).toString());
|
entity.setResultTime(runTime);
|
list.add(entity);
|
}
|
|
baseDao.insertList(list);
|
}
|
|
@Override
|
public List<IndexStatisticDTO> getModelResultList(FeignQueryPointDTO feignQueryPointDTO) {
|
return baseDao.getModelResultList(feignQueryPointDTO);
|
}
|
|
@Override
|
public PageData<AnyStoreReliabilityDTO> getStorePage(Map<String, Object> params) {
|
IPage<AnyStoreReliabilityDTO> page = baseDao.getStorePageList(
|
getPage(params, "dateTime", false),
|
params
|
);
|
return getPageData(page, AnyStoreReliabilityDTO.class);
|
}
|
|
@Override
|
public PageData<AnyAllEvaluationDTO> getAllEvaluationPage(Map<String, Object> params) {
|
IPage<AnyAllEvaluationDTO> page = baseDao.getEvaluationPageList(
|
getPage(params, "dateTime", false),
|
params
|
);
|
return getPageData(page, AnyAllEvaluationDTO.class);
|
|
}
|
|
public QueryWrapper<StModelResultEntity> getDateWrapper(Map<String, Date> params) {
|
String startDate = DateUtils.format(params.get("startdate"),DateUtils.DATE_TIME_PATTERN);
|
String endDate = DateUtils.format(params.get("enddate"),DateUtils.DATE_TIME_PATTERN);
|
|
QueryWrapper<StModelResultEntity> wrapper = new QueryWrapper<>();
|
wrapper.ge(StringUtils.isNotBlank(startDate), "result_time", startDate);
|
wrapper.le(StringUtils.isNotBlank(endDate), "result_time", endDate);
|
return wrapper;
|
}
|
}
|