SupervisedController.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package com.gyee.impala.controller.diagnose;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.gyee.impala.common.cache.InfoCache;
  4. import com.gyee.impala.common.result.JsonResult;
  5. import com.gyee.impala.common.result.ResultCode;
  6. import com.gyee.impala.model.custom.diagnose.*;
  7. import com.gyee.impala.model.master.diagnose.AlgorithmType;
  8. import com.gyee.impala.model.master.diagnose.Diagnosetrainhistory;
  9. import com.gyee.impala.service.custom.diagnose.DataService;
  10. import com.gyee.impala.service.custom.diagnose.SupervisedCmdService;
  11. import com.gyee.impala.service.master.diagnose.DiagnosetrainhistoryService;
  12. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  13. import org.springframework.web.bind.annotation.*;
  14. import javax.annotation.Resource;
  15. import java.util.*;
  16. /**
  17. * 有监督学习控制器
  18. *
  19. * @author xysn
  20. */
  21. @CrossOrigin
  22. @RestController
  23. @RequestMapping("/api/supervised")
  24. public class SupervisedController {
  25. /**
  26. * 缓存
  27. */
  28. @Resource
  29. private InfoCache infoCache;
  30. /**
  31. * 命令执行
  32. */
  33. @Resource
  34. private SupervisedCmdService supervisedCmdService;
  35. /**
  36. * 数据服务
  37. */
  38. @Resource
  39. private DataService dataService;
  40. /**
  41. * 线程池
  42. */
  43. @Resource
  44. private ThreadPoolTaskExecutor taskExecutor;
  45. /**
  46. * 历史记录数据库操作
  47. */
  48. @Resource
  49. private DiagnosetrainhistoryService historyService;
  50. private static final Object locker = new Object();
  51. private ExecuteInfo executeInfo;
  52. /**
  53. * 更新缓存数据
  54. *
  55. * @param ci
  56. * @return
  57. */
  58. @PostMapping("/")
  59. public JSONObject updateCoordinateCache(@RequestBody String ci) {
  60. infoCache.setSupervised(ci);
  61. System.out.print("su ");
  62. return JsonResult.success();
  63. }
  64. /**
  65. * 报告进度
  66. *
  67. * @param process
  68. * @return
  69. */
  70. @GetMapping("/progress/{process}")
  71. public JSONObject updateCoordinateCache(@PathVariable Double process) {
  72. supervisedCmdService.setProcess(process);
  73. System.out.print("sp" + process + " ");
  74. return JsonResult.success();
  75. }
  76. /**
  77. * 获取具体故障信息
  78. *
  79. * @return
  80. */
  81. @GetMapping("fault-info")
  82. public JSONObject getFaultInfo() {
  83. ExecuteInfo info = supervisedCmdService.getExecuteInfo();
  84. return JsonResult.successData(ResultCode.SUCCESS, info);
  85. }
  86. /**
  87. * 获取进度
  88. *
  89. * @return
  90. */
  91. @GetMapping("/progress")
  92. public JSONObject updateCoordinateCache() {
  93. Value value = new Value(supervisedCmdService.getProcess());
  94. return JsonResult.successData(ResultCode.SUCCESS, value);
  95. }
  96. /**
  97. * 获取数据
  98. *
  99. * @return
  100. */
  101. @GetMapping("/")
  102. public JSONObject getCoordinateCache() {
  103. Value value = new Value(infoCache.getSupervised());
  104. return JsonResult.successData(ResultCode.SUCCESS, value);
  105. }
  106. @GetMapping("/complete/{fname}")
  107. public JSONObject setCompleteFileName(@PathVariable String fname) {
  108. Diagnosetrainhistory history = supervisedCmdService.getHistory(fname, infoCache.getSupervised());
  109. historyService.insertItem(history);
  110. return JsonResult.success();
  111. }
  112. /**
  113. * 获取数据并执行脚本
  114. *
  115. * @return
  116. */
  117. @PostMapping("/execute")
  118. public JSONObject executeScript(@RequestBody ExecuteInfo ais) {
  119. if (ais == null || ais.getDataInfos().length <= 0) {
  120. return JsonResult.error(4000, "训练内容不能为空!");
  121. }
  122. if (!supervisedCmdService.isComplete()) {
  123. return JsonResult.error(4000, "命令正在执行...");
  124. }
  125. synchronized (locker) {
  126. executeInfo = ais;
  127. taskExecutor.submit(this::execute);
  128. }
  129. return JsonResult.success();
  130. }
  131. private void execute() {
  132. infoCache.setSupervised("");
  133. supervisedCmdService.setProcess(-1);
  134. supervisedCmdService.exec(executeInfo);
  135. }
  136. /**
  137. * 获取UniformCodes
  138. *
  139. * @return
  140. */
  141. @GetMapping("/uniform")
  142. public JSONObject getUniformCodes() {
  143. Map<String, UniformCodeInfo> map = dataService.getUniformCodeInfoMap();
  144. return JsonResult.successData(ResultCode.SUCCESS, map);
  145. }
  146. /**
  147. * 获取UniformCodes
  148. *
  149. * @return
  150. */
  151. @GetMapping("/data")
  152. public JSONObject getData() {
  153. Map<String, Object> map = new HashMap<>();
  154. String data = dataService.getFormData(supervisedCmdService.getExecuteInfo());
  155. map.put("info", executeInfo);
  156. map.put("data", data);
  157. return JsonResult.successData(ResultCode.SUCCESS, data);
  158. }
  159. /**
  160. * 获取算法信息
  161. *
  162. * @return
  163. */
  164. @GetMapping("/algorithm")
  165. public JSONObject getAlgorithm() {
  166. Algorithm[] algorithms = infoCache.getAlgorithms();
  167. return JsonResult.successData(ResultCode.SUCCESS, algorithms);
  168. }
  169. @GetMapping("/history")
  170. public JSONObject getHistory() {
  171. List<History> hs = new ArrayList<>();
  172. List<Diagnosetrainhistory> ls = historyService.getAll(AlgorithmType.supervised);
  173. for (Diagnosetrainhistory he : ls) {
  174. History h = new History();
  175. h.setCoordinate(he.getContext());
  176. h.setFaultIds(Arrays.asList(he.getFaultids().split(",")));
  177. h.setName(he.getName());
  178. hs.add(h);
  179. }
  180. return JsonResult.successData(ResultCode.SUCCESS, hs);
  181. }
  182. @GetMapping("/history/remove")
  183. public JSONObject removeHistory(@RequestParam(value = "names") String names) {
  184. if (names == null) {
  185. return JsonResult.error(4000, "记录名不能为空!");
  186. }
  187. String[] ns = names.split(",");
  188. for (String s : ns) {
  189. if (s == null || s.equals("")) {
  190. continue;
  191. }
  192. historyService.deleteById(s);
  193. }
  194. return JsonResult.success();
  195. }
  196. }