对比新文件 |
| | |
| | | package com.iailab.module.model.api.mcs.dto; |
| | | |
| | | import com.fasterxml.jackson.annotation.JsonFormat; |
| | | import io.swagger.v3.oas.annotations.media.Schema; |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * @author PanZhibao |
| | | * @Description |
| | | * @createTime 2024年11月14日 |
| | | */ |
| | | @Data |
| | | public class PreDataItemChartReqVO implements Serializable { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | @Schema(description = "预测项ID") |
| | | private String itemId; |
| | | |
| | | @Schema(description = "开始时间") |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") |
| | | private Date startTime; |
| | | |
| | | @Schema(description = "结束时间") |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") |
| | | private Date endTime; |
| | | } |
对比新文件 |
| | |
| | | package com.iailab.module.model.api.mcs.dto; |
| | | |
| | | import com.fasterxml.jackson.annotation.JsonFormat; |
| | | import io.swagger.v3.oas.annotations.media.Schema; |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @author PanZhibao |
| | | * @Description |
| | | * @createTime 2024年11月14日 |
| | | */ |
| | | @Data |
| | | public class PreDataItemChartRespVO implements Serializable { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | @Schema(description = "最后运行时间") |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") |
| | | private Date lastTime; |
| | | |
| | | @Schema(description = "开始时间") |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") |
| | | private Date startTime; |
| | | |
| | | @Schema(description = "结束时间") |
| | | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") |
| | | private Date endTime; |
| | | |
| | | @Schema(description = "图例") |
| | | private List<String> legend; |
| | | |
| | | @Schema(description = "X轴数据") |
| | | private List<String> categories; |
| | | |
| | | @Schema(description = "数据") |
| | | private LinkedHashMap<String, PreDataSampleViewRespDTO> viewMap; |
| | | |
| | | } |
对比新文件 |
| | |
| | | package com.iailab.module.model.api.mcs.dto; |
| | | |
| | | import io.swagger.v3.oas.annotations.media.Schema; |
| | | import lombok.Data; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author PanZhibao |
| | | * @Description |
| | | * @createTime 2024年11月14日 |
| | | */ |
| | | @Schema(description = "RPC 模型 - 预测数据 DTO") |
| | | @Data |
| | | public class PreDataSampleViewRespDTO implements Serializable { |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | @Schema(description = "真实值") |
| | | private List<Object[]> realData; |
| | | |
| | | @Schema(description = "T+N预测值,N表示预测频率") |
| | | private List<Object[]> preDataN; |
| | | } |
| | |
| | | import com.iailab.module.model.mcs.pre.entity.MmItemOutputEntity; |
| | | import com.iailab.module.model.mcs.pre.service.*; |
| | | import com.iailab.module.model.mcs.pre.vo.MmPredictItemRespVO; |
| | | import com.iailab.module.model.mdk.vo.ItemVO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.util.CollectionUtils; |
| | |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public PreDataItemChartRespVO getPreDataItemChart(PreDataItemChartReqVO reqVO) { |
| | | PreDataItemChartRespVO result = new PreDataItemChartRespVO(); |
| | | ItemVO predictItem = mmPredictItemService.getItemById(reqVO.getItemId()); |
| | | if (predictItem == null) { |
| | | return result; |
| | | } |
| | | if (predictItem.getLastTime() == null) { |
| | | return result; |
| | | } |
| | | result.setLastTime(predictItem.getLastTime()); |
| | | Date startTime = reqVO.getStartTime(); |
| | | if (startTime == null) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(predictItem.getLastTime()); |
| | | calendar.add(Calendar.HOUR_OF_DAY, -1); |
| | | startTime = calendar.getTime(); |
| | | } |
| | | Date endTime = reqVO.getEndTime(); |
| | | if (endTime == null) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(predictItem.getLastTime()); |
| | | calendar.add(Calendar.HOUR_OF_DAY, 1); |
| | | endTime = calendar.getTime(); |
| | | } |
| | | List<String> categories = DateUtils.getTimeScale(startTime, endTime, predictItem.getGranularity()); |
| | | List<String> legend = new ArrayList<>(); |
| | | LinkedHashMap<String, PreDataSampleViewRespDTO> viewMap = new LinkedHashMap<>(); |
| | | List<MmItemOutputEntity> outs = mmItemOutputService.getByItemid(reqVO.getItemId()); |
| | | if (CollectionUtils.isEmpty(outs)) { |
| | | return result; |
| | | } |
| | | for (MmItemOutputEntity out : outs) { |
| | | legend.add(out.getResultstr()); |
| | | PreDataSampleViewRespDTO viewDto = new PreDataSampleViewRespDTO(); |
| | | viewDto.setRealData(getHisData(out.getPointid(), startTime, endTime)); |
| | | viewDto.setPreDataN(mmItemResultService.getData(out.getId(), startTime, endTime)); |
| | | viewMap.put(out.getResultstr(), viewDto); |
| | | } |
| | | result.setStartTime(startTime); |
| | | result.setEndTime(endTime); |
| | | result.setCategories(categories); |
| | | result.setLegend(legend); |
| | | result.setViewMap(viewMap); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 获取真实值 |
| | | * |
对比新文件 |
| | |
| | | package com.iailab.module.model.api.controller.admin; |
| | | |
| | | import com.iailab.framework.common.pojo.CommonResult; |
| | | import com.iailab.module.model.api.mcs.McsApi; |
| | | import com.iailab.module.model.api.mcs.dto.PreDataBarLineReqVO; |
| | | import com.iailab.module.model.api.mcs.dto.PreDataBarLineRespVO; |
| | | import com.iailab.module.model.api.mcs.dto.PreDataItemChartReqVO; |
| | | import com.iailab.module.model.api.mcs.dto.PreDataItemChartRespVO; |
| | | import com.iailab.module.model.common.utils.ApiSecurityUtils; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | | import io.swagger.v3.oas.annotations.tags.Tag; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.annotation.security.PermitAll; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | /** |
| | | * @author PanZhibao |
| | | * @Description |
| | | * @createTime 2024年11月14日 |
| | | */ |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping("/model/api/mcs") |
| | | @Tag(name = "数据") |
| | | public class McsApiController { |
| | | |
| | | @Resource |
| | | private ApiSecurityUtils apiSecurityUtils; |
| | | |
| | | @Autowired |
| | | private McsApi mcsApi; |
| | | |
| | | @PermitAll |
| | | @PostMapping("/predict-data/charts") |
| | | @Operation(summary = "预测数据图表") |
| | | public CommonResult<PreDataBarLineRespVO> getPreDataCharts(HttpServletResponse response, HttpServletRequest |
| | | request, @RequestBody PreDataBarLineReqVO reqVO) throws Exception { |
| | | apiSecurityUtils.validate(request); |
| | | PreDataBarLineRespVO respVO = mcsApi.getPreDataCharts(reqVO); |
| | | return CommonResult.success(respVO); |
| | | } |
| | | |
| | | @PermitAll |
| | | @PostMapping("/predict-data/item-chart") |
| | | @Operation(summary = "预测数据图表") |
| | | public CommonResult<PreDataItemChartRespVO> getPreDataItemChart(HttpServletResponse response, HttpServletRequest |
| | | request, @RequestBody PreDataItemChartReqVO reqVO) throws Exception { |
| | | apiSecurityUtils.validate(request); |
| | | PreDataItemChartRespVO respVO = mcsApi.getPreDataItemChart(reqVO); |
| | | return CommonResult.success(respVO); |
| | | } |
| | | } |
对比新文件 |
| | |
| | | package com.iailab.module.model.api.controller; |
对比新文件 |
| | |
| | | package com.iailab.module.model.common.utils; |
| | | |
| | | |
| | | import com.iailab.framework.common.constant.Constant; |
| | | import com.iailab.framework.tenant.core.context.TenantContextHolder; |
| | | import com.iailab.module.system.api.user.AdminUserApi; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * @author PanZhibao |
| | | * @Description |
| | | * @createTime 2023年12月06日 15:55:00 |
| | | */ |
| | | @Component |
| | | public class ApiSecurityUtils { |
| | | |
| | | /*@Resource |
| | | private ApiAppService apiAppService;*/ |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | | |
| | | private Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); |
| | | |
| | | private String getRequestToken(HttpServletRequest httpRequest) { |
| | | //从header中获取token |
| | | String token = httpRequest.getHeader(Constant.TOKEN_HEADER); |
| | | |
| | | //如果header中不存在token,则从参数中获取token |
| | | if (StringUtils.isBlank(token)) { |
| | | token = httpRequest.getParameter(Constant.TOKEN_HEADER); |
| | | } |
| | | |
| | | return token; |
| | | } |
| | | |
| | | private void setTenantId(HttpServletRequest httpRequest) { |
| | | String tenantId = httpRequest.getHeader(Constant.HEAD_TENANT_ID); |
| | | |
| | | if (StringUtils.isBlank(tenantId)) { |
| | | TenantContextHolder.setTenantId(Long.parseLong(tenantId)); |
| | | } |
| | | } |
| | | |
| | | |
| | | public void validate(HttpServletRequest httpRequest) throws Exception { |
| | | setTenantId(httpRequest); |
| | | /*String token = getRequestToken(httpRequest); |
| | | if (StringUtils.isBlank(token)) { |
| | | throw new Exception("token 不能为空!"); |
| | | } |
| | | LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); |
| | | if (ObjectUtils.isEmpty(loginUser)) { |
| | | throw new RuntimeException("用户不能为空"); |
| | | } |
| | | CommonResult<AdminUserRespDTO> user = adminUserApi.getUser(loginUser.getId()); |
| | | if(ObjectUtils.isEmpty(user)) { |
| | | throw new RuntimeException("用户不存在"); |
| | | } |
| | | AdminUserRespDTO userData = user.getData(); |
| | | String username = userData.getUsername();*/ |
| | | /*ApiAppEntity appInfo = apiAppService.getInfoByAppKey(username); |
| | | if (appInfo == null) { |
| | | throw new RuntimeException("应用未授权"); |
| | | }*/ |
| | | //TODO 验证签名 |
| | | // if(!com.iailab.common.utils.JwtUtils.verify(token, appInfo.getAppSecret())){ |
| | | // throw new RuntimeException("签名错误"); |
| | | // } |
| | | } |
| | | |
| | | private boolean isInteger(String str) { |
| | | return pattern.matcher(str).matches(); |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | import com.iailab.module.data.api.point.DataPointApi; |
| | | import com.iailab.module.infra.api.config.ConfigApi; |
| | | import com.iailab.module.system.api.tenant.TenantApi; |
| | | import com.iailab.module.system.api.user.AdminUserApi; |
| | | import org.springframework.cloud.openfeign.EnableFeignClients; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | @Configuration(proxyBeanMethods = false) |
| | | @EnableFeignClients(clients = {DataPointApi.class, PlanItemApi.class, ConfigApi.class, TenantApi.class}) |
| | | @EnableFeignClients(clients = {DataPointApi.class, PlanItemApi.class, ConfigApi.class, TenantApi.class, AdminUserApi.class}) |
| | | public class RpcConfiguration { |
| | | } |
| | |
| | | |
| | | import com.iailab.module.model.mcs.pre.enums.ItemRunStatusEnum; |
| | | |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * @author PanZhibao |
| | | * @Description |
| | |
| | | */ |
| | | public interface MmItemStatusService { |
| | | |
| | | void recordStatus(String itemId, ItemRunStatusEnum status, Long duration); |
| | | void recordStatus(String itemId, ItemRunStatusEnum status, Long duration, Date runTime); |
| | | } |
| | |
| | | @Resource |
| | | public MmItemStatusDao mmItemStatusDao; |
| | | |
| | | public void recordStatus(String itemId, ItemRunStatusEnum status, Long duration) { |
| | | public void recordStatus(String itemId, ItemRunStatusEnum status, Long duration, Date runTime) { |
| | | QueryWrapper<MmItemStatusEntity> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("item_id", itemId); |
| | | MmItemStatusEntity entity = mmItemStatusDao.selectOne(queryWrapper); |
| | |
| | | entity = new MmItemStatusEntity(); |
| | | entity.setId(UUID.randomUUID().toString()); |
| | | entity.setItemId(itemId); |
| | | entity.setLastTime(new Date()); |
| | | entity.setLastTime(runTime); |
| | | entity.setStatus(status.getCode()); |
| | | entity.setDuration(duration); |
| | | mmItemStatusDao.insert(entity); |
| | | } else { |
| | | entity.setLastTime(new Date()); |
| | | entity.setLastTime(runTime); |
| | | entity.setStatus(status.getCode()); |
| | | entity.setDuration(duration); |
| | | mmItemStatusDao.updateById(entity); |
| | |
| | | } |
| | | Long totalDur = 0L; |
| | | try { |
| | | mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.PROCESSING, totalDur); |
| | | mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.PROCESSING, totalDur, predictTime); |
| | | PredictItemHandler predictItemHandler = predictItemFactory.create(predictItem.getId()); |
| | | Instant start = Instant.now(); |
| | | try { |
| | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error(String.valueOf(e)); |
| | | mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.FAIL, totalDur, predictTime); |
| | | continue; |
| | | } |
| | | Instant end = Instant.now(); |
| | | Long drtPre = Duration.between(start, end).getSeconds(); |
| | |
| | | log.info(MessageFormat.format("预测项:{0},保存时间:{1}秒", predictItem.getItemName(), |
| | | drtSave)); |
| | | totalDur = totalDur + drtSave; |
| | | mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.SUCCESS, totalDur); |
| | | mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.SUCCESS, totalDur, predictTime); |
| | | result.put(predictItem.getItemNo(), predictResult); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error(MessageFormat.format("预测项编号:{0},预测项名称:{1},预测失败:{2} 预测时刻:{3}", |
| | | predictItem.getId(), predictItem.getItemName(), e.getMessage(), predictTime)); |
| | | mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.FAIL, totalDur); |
| | | mmItemStatusService.recordStatus(predictItem.getId(), ItemRunStatusEnum.FAIL, totalDur, predictTime); |
| | | } |
| | | } |
| | | return result; |
| | |
| | | * @throws ItemInvokeException |
| | | */ |
| | | @Override |
| | | public PredictResultVO predict(Date predictTime, ItemVO predictItemDto) |
| | | throws ItemInvokeException{ |
| | | public PredictResultVO predict(Date predictTime, ItemVO predictItemDto) throws ItemInvokeException{ |
| | | PredictResultVO predictResult = new PredictResultVO(); |
| | | String itemId = predictItemDto.getId(); |
| | | predictResult.setPredictId(itemId); |
| | |
| | | throw new ItemInvokeException(MessageFormat.format("{0},itemId={1}", |
| | | ItemInvokeException.errorItemFailed, itemId)); |
| | | } |
| | | |
| | | return predictResult; |
| | | } |
| | | } |
| | |
| | | result.setPredictTime(predictTime); |
| | | } catch (Exception ex) { |
| | | log.error("调用发生异常,异常信息为:{}", ex); |
| | | log.error(ex.getMessage()); |
| | | ex.printStackTrace(); |
| | | throw new ModelInvokeException(ex.getMessage()); |
| | | } |
| | | return result; |
| | | } |