TrainFaultPredictController.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package com.gyee.impala.controller.diagnose;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.gyee.impala.common.result.JsonResult;
  4. import com.gyee.impala.common.result.ResultCode;
  5. import com.gyee.impala.common.util.DateUtil;
  6. import com.gyee.impala.model.custom.diagnose.DataInfo;
  7. import com.gyee.impala.model.custom.diagnose.ExecuteInfo;
  8. import com.gyee.impala.model.master.Casefault;
  9. import com.gyee.impala.model.master.diagnose.Diagnosepoint;
  10. import com.gyee.impala.model.master.diagnose.Diagnosetrainhistory;
  11. import com.gyee.impala.service.custom.diagnose.DataPredictService;
  12. import com.gyee.impala.service.custom.diagnose.CmdFaultPredictService;;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  16. import org.springframework.web.bind.annotation.*;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import javax.annotation.Resource;
  19. import java.util.Calendar;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.stream.Collectors;
  24. /**
  25. * 故障预测模型训练
  26. */
  27. @Slf4j
  28. @CrossOrigin
  29. @RestController
  30. @RequestMapping("/api/predict")
  31. public class TrainFaultPredictController {
  32. @Autowired
  33. private CmdFaultPredictService cmdFaultPredictService;
  34. /**
  35. * 参数
  36. */
  37. private ExecuteInfo executeInfo;
  38. /**
  39. * 数据服务
  40. */
  41. @Autowired
  42. private DataPredictService dataDiagnoseService;
  43. /**
  44. * 线程池
  45. */
  46. @Resource
  47. private ThreadPoolTaskExecutor taskExecutor;
  48. /**
  49. * 预测生成文件名
  50. */
  51. private String fileName;
  52. private static final Object locker = new Object();
  53. private String name1;
  54. private String forecastLabel1;
  55. private String[] inputLabel1;
  56. private String host1;
  57. private MultipartFile file1;
  58. /**
  59. * 离线文件模式训练故障预测接口
  60. * @param forecastLabel
  61. * @param inputLabel
  62. * @param host
  63. * @param file
  64. * @return
  65. */
  66. @PostMapping("/trainfile")
  67. @ResponseBody
  68. public JSONObject getTrainfile(String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
  69. if (!cmdFaultPredictService.isComplete()) {
  70. return JsonResult.error(4000, "命令正在执行...");
  71. }
  72. if (file.isEmpty()) {
  73. return JsonResult.error(ResultCode.ERROR_FILE_NO);
  74. }
  75. try {
  76. synchronized (locker) {
  77. forecastLabel1 = forecastLabel;
  78. inputLabel1 = inputLabel;
  79. host1 = host;
  80. file1 = file;
  81. taskExecutor.submit(this::executeFile);
  82. }
  83. return JsonResult.success(ResultCode.SUCCESS);
  84. } catch (Exception e) {
  85. return JsonResult.error(ResultCode.ERROR_DATA_FILE);
  86. }
  87. }
  88. /**
  89. * 调用执行脚本
  90. */
  91. private void executeFile() {
  92. cmdFaultPredictService.exec(forecastLabel1, inputLabel1, host1, file1);
  93. }
  94. /** 开始预测 查询 golden 所有原始数据
  95. * flag ture: 所有数据 ;false: 前10条数据
  96. * **/
  97. @PostMapping("/pointfaultdata")
  98. public JSONObject getPointData(@RequestBody JSONObject json){
  99. if (json == null)
  100. return JsonResult.error(ResultCode.PARAM_IS_BLANK);
  101. log.info("进入预测程序");
  102. boolean flag = json.getBooleanValue("flag");
  103. List<Diagnosepoint> points = JSONObject.parseArray(json.getJSONArray("points").toString(), Diagnosepoint.class);
  104. List<Casefault> faults = JSONObject.parseArray(json.getJSONArray("faults").toString(), Casefault.class);
  105. /** 组装数据 删除添加的故障类型**/
  106. dataDiagnoseService.formatUniformcode(points.stream().filter(a -> !a.getUniformcode().equals("faulttype")).collect(Collectors.toList()));
  107. executeInfo = new ExecuteInfo();
  108. Calendar cal = Calendar.getInstance();
  109. DataInfo[] dataInfos = new DataInfo[faults.size()];
  110. for (int i = 0; i < faults.size(); i++){
  111. DataInfo data = new DataInfo();
  112. data.setId(Long.valueOf(faults.get(i).getId()));
  113. data.setStationId(faults.get(i).getStationen());
  114. data.setThingId(faults.get(i).getWindturbineid());
  115. data.setModelId(faults.get(i).getModel());
  116. data.setTag(faults.get(i).getFaultcode());
  117. data.setFaultTime(faults.get(i).getStarttime());
  118. cal.setTime(DateUtil.parseStrtoDate(faults.get(i).getStarttime(), DateUtil.YYYY_MM_DD_HH_MM_SS));
  119. cal.add(Calendar.MINUTE, -30);
  120. data.setStartTs(cal.getTimeInMillis() + "");
  121. cal.add(Calendar.MINUTE, 30);
  122. data.setEndTs(cal.getTimeInMillis() + "");
  123. dataInfos[i] = data;
  124. }
  125. executeInfo.setDataInfos(dataInfos);
  126. if (flag){
  127. if (!cmdFaultPredictService.isComplete()) {
  128. return JsonResult.error(4000, "已有正在预测的故障模型...");
  129. }
  130. synchronized (locker) {
  131. taskExecutor.submit(this::execute);
  132. }
  133. return JsonResult.success(ResultCode.SUCCESS);
  134. }else {
  135. Map<String, Object> mp = dataDiagnoseService.getFormData(executeInfo);
  136. return JsonResult.successData(ResultCode.SUCCESS, mp);
  137. }
  138. }
  139. /**
  140. * 调用执行脚本
  141. */
  142. private void execute() {
  143. fileName = dataDiagnoseService.getFormDataAll(executeInfo);
  144. cmdFaultPredictService.exec();
  145. }
  146. /**
  147. * py 获取预测数据
  148. *
  149. * @return
  150. */
  151. @GetMapping("/trainfaultdata")
  152. public JSONObject getData() {
  153. Map<String, Object> map = new HashMap<>();
  154. map.put("info", this.executeInfo);
  155. map.put("filename", fileName);
  156. return JsonResult.successData(ResultCode.SUCCESS, map);
  157. }
  158. /**
  159. * 缓存训练预测模型结果
  160. *
  161. * @param history
  162. * @return
  163. */
  164. @PostMapping("/putHistory")
  165. public JSONObject putDiagnosetrainhistory(@RequestBody String history) {
  166. try {
  167. log.info("训练模型结果:" + history);
  168. cmdFaultPredictService.putDiagnosetrainhistory(history);
  169. return JsonResult.success(ResultCode.SUCCESS);
  170. } catch (Exception e) {
  171. log.error("请求错误", e);
  172. return JsonResult.error(ResultCode.ERROR);
  173. }
  174. }
  175. /**
  176. * 获取当前训练预测模型结果
  177. * @return
  178. */
  179. @GetMapping("/getHistory")
  180. public JSONObject getHistory() {
  181. try {
  182. Diagnosetrainhistory d = cmdFaultPredictService.getHistoryQueue();
  183. return JsonResult.successData(ResultCode.SUCCESS, d);
  184. } catch (Exception e) {
  185. log.error("请求错误", e);
  186. return JsonResult.error(ResultCode.ERROR);
  187. }
  188. }
  189. /** 开始预测 查询 golden 所有原始数据
  190. * flag ture: 所有数据 ;false: 前10条数据
  191. * **/
  192. @PostMapping("/pointfaultdata")
  193. public JSONObject getWarningData(@RequestBody JSONObject json){
  194. if (json == null)
  195. return JsonResult.error(ResultCode.PARAM_IS_BLANK);
  196. log.info("进入预测程序");
  197. boolean flag = json.getBooleanValue("flag");
  198. List<Diagnosepoint> points = JSONObject.parseArray(json.getJSONArray("points").toString(), Diagnosepoint.class);
  199. List<Casefault> faults = JSONObject.parseArray(json.getJSONArray("faults").toString(), Casefault.class);
  200. /** 组装数据 删除添加的故障类型**/
  201. dataDiagnoseService.formatUniformcode(points.stream().filter(a -> !a.getUniformcode().equals("faulttype")).collect(Collectors.toList()));
  202. executeInfo = new ExecuteInfo();
  203. Calendar cal = Calendar.getInstance();
  204. DataInfo[] dataInfos = new DataInfo[faults.size()];
  205. for (int i = 0; i < faults.size(); i++){
  206. DataInfo data = new DataInfo();
  207. data.setId(Long.valueOf(faults.get(i).getId()));
  208. data.setStationId(faults.get(i).getStationen());
  209. data.setThingId(faults.get(i).getWindturbineid());
  210. data.setModelId(faults.get(i).getModel());
  211. data.setTag(faults.get(i).getFaultcode());
  212. data.setFaultTime(faults.get(i).getStarttime());
  213. cal.setTime(DateUtil.parseStrtoDate(faults.get(i).getStarttime(), DateUtil.YYYY_MM_DD_HH_MM_SS));
  214. cal.add(Calendar.MINUTE, -30);
  215. data.setStartTs(cal.getTimeInMillis() + "");
  216. cal.add(Calendar.MINUTE, 30);
  217. data.setEndTs(cal.getTimeInMillis() + "");
  218. dataInfos[i] = data;
  219. }
  220. executeInfo.setDataInfos(dataInfos);
  221. if (flag){
  222. if (!cmdFaultPredictService.isComplete()) {
  223. return JsonResult.error(4000, "已有正在预测的故障模型...");
  224. }
  225. synchronized (locker) {
  226. taskExecutor.submit(this::execute);
  227. }
  228. return JsonResult.success(ResultCode.SUCCESS);
  229. }else {
  230. Map<String, Object> mp = dataDiagnoseService.getFormData(executeInfo);
  231. return JsonResult.successData(ResultCode.SUCCESS, mp);
  232. }
  233. }
  234. }