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 implements StModelResultService { @Resource private StModelResultDao stModelResultDao; @Override public QueryWrapper getWrapper(Map params){ String id = (String)params.get("id"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(id), "id", id); return wrapper; } @Override public Map> getResultList(Map params) { params.put("sortType", "asc"); Map> result = new HashMap<>(5); List list = stModelResultDao.getResultList(params); if (CollectionUtils.isEmpty(list)) { return result; } Map> groupList = list.stream().collect(Collectors.groupingBy(StModelResultEntity::getResultKey)); groupList.forEach((k, v) -> { try{ List 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> getList(Map params) { if (params.get("isAsc") != null && params.get("isAsc").toString().equals("false")) { params.put("sortType", "desc"); } else { params.put("sortType", "asc"); } List> result = new ArrayList<>(); List list = stModelResultDao.getResultList(params); List dateList = list.stream().map(t -> t.getResultTime()).distinct().collect(Collectors.toList()); Map> groupList = list.stream().collect(Collectors.groupingBy(StModelResultEntity::getResultTime)); dateList.forEach(item -> { Map 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 getLastResultByCode(Map params) { return baseDao.getLastResultByCode(params); } @Override public Map getLastResultMap(Map params) { Map result = new HashMap<>(1); List 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 getResultByCodeDate(Map params) { Map result = new HashMap<>(1); List 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 params) { List list = stModelResultDao.selectList(getDateWrapper(params)); if (CollectionUtils.isEmpty(list)){ return; } stModelResultDao.migrationModelResult(list); stModelResultDao.delete(getDateWrapper(params)); } @Override public void addPy(String modelId, List lines, Date runTime) { List 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 getValueList(String resultKey, Date startTime, Date endTime) { List result = new ArrayList<>(); String[] params = resultKey.split(":"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("result_key", params[1]) .ge("result_time", startTime) .le("result_time", endTime) .orderByAsc("result_time"); List 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 tMap, Date runTime) { if (tMap == null) { return; } List 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 getModelResultList(FeignQueryPointDTO feignQueryPointDTO) { return baseDao.getModelResultList(feignQueryPointDTO); } @Override public PageData getStorePage(Map params) { IPage page = baseDao.getStorePageList( getPage(params, "dateTime", false), params ); return getPageData(page, AnyStoreReliabilityDTO.class); } @Override public PageData getAllEvaluationPage(Map params) { IPage page = baseDao.getEvaluationPageList( getPage(params, "dateTime", false), params ); return getPageData(page, AnyAllEvaluationDTO.class); } public QueryWrapper getDateWrapper(Map params) { String startDate = DateUtils.format(params.get("startdate"),DateUtils.DATE_TIME_PATTERN); String endDate = DateUtils.format(params.get("enddate"),DateUtils.DATE_TIME_PATTERN); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.ge(StringUtils.isNotBlank(startDate), "result_time", startDate); wrapper.le(StringUtils.isNotBlank(endDate), "result_time", endDate); return wrapper; } }