TrainFileModeController.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.model.master.diagnose.Diagnosetrainhistory;
  6. import com.gyee.impala.model.master.diagnose.TrainInfo;
  7. import com.gyee.impala.service.custom.ShiroService;
  8. import com.gyee.impala.service.master.diagnose.TrainFileModeService;
  9. import org.apache.ibatis.logging.Log;
  10. import org.apache.ibatis.logging.LogFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  13. import org.springframework.web.bind.annotation.*;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import javax.annotation.Resource;
  16. import javax.servlet.http.HttpServletRequest;
  17. import java.util.List;
  18. /**
  19. * 文件方式训练模型
  20. */
  21. @CrossOrigin
  22. @RestController
  23. @RequestMapping("/api/filemode")
  24. public class TrainFileModeController {
  25. protected Log log = LogFactory.getLog(getClass());
  26. /**
  27. * 线程池
  28. */
  29. @Resource
  30. private ThreadPoolTaskExecutor taskExecutor;
  31. @Autowired
  32. private TrainFileModeService trainFileModeService;
  33. private static final Object locker = new Object();
  34. private String name1;
  35. private String forecastLabel1;
  36. private String[] inputLabel1;
  37. private String host1;
  38. private MultipartFile file1;
  39. /**
  40. * 文件模式训练接口
  41. * @param request
  42. * @param name
  43. * @param forecastLabel
  44. * @param inputLabel
  45. * @param host
  46. * @param file
  47. * @return
  48. */
  49. @PostMapping("/trainfile")
  50. @ResponseBody
  51. public JSONObject getTrainfile(HttpServletRequest request, String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
  52. if (!trainFileModeService.isComplete()) {
  53. return JsonResult.error(4000, "命令正在执行...");
  54. }
  55. if (file.isEmpty()) {
  56. return JsonResult.error(ResultCode.ERROR_FILE_NO);
  57. }
  58. try {
  59. synchronized (locker) {
  60. name1 = name;
  61. forecastLabel1 = forecastLabel;
  62. inputLabel1 = inputLabel;
  63. host1 = host;
  64. file1 = file;
  65. taskExecutor.submit(this::execute);
  66. }
  67. return JsonResult.success(ResultCode.SUCCESS);
  68. } catch (Exception e) {
  69. return JsonResult.error(ResultCode.ERROR_DATA_FILE);
  70. }
  71. }
  72. /**
  73. * 调用执行脚本
  74. */
  75. private void execute() {
  76. trainFileModeService.exec(name1, forecastLabel1, inputLabel1, host1, file1);
  77. }
  78. /**
  79. * 生产控制台信息
  80. * @param trainInfo
  81. * @return
  82. */
  83. @PostMapping("/addtrainInfo")
  84. public JSONObject addProducer(@RequestBody String trainInfo) {
  85. try {
  86. TrainInfo t = JSONObject.parseObject(trainInfo, TrainInfo.class);
  87. trainFileModeService.produce(t);
  88. return JsonResult.success(ResultCode.SUCCESS);
  89. } catch (Exception e) {
  90. return JsonResult.error(ResultCode.ERROR);
  91. }
  92. }
  93. /**
  94. * 消费控制台信息
  95. * @return
  96. */
  97. @GetMapping("/gettrainInfo")
  98. @ResponseBody
  99. public JSONObject getConsume() {
  100. try {
  101. List<TrainInfo> list = trainFileModeService.consume();
  102. System.out.println(JsonResult.successData(ResultCode.SUCCESS, list));
  103. return JsonResult.successData(ResultCode.SUCCESS, list);
  104. } catch (Exception e) {
  105. return JsonResult.error(ResultCode.ERROR);
  106. }
  107. }
  108. /**
  109. * 添加训练结果
  110. *
  111. * @param history
  112. * @return
  113. */
  114. @PostMapping("/putHistory")
  115. public JSONObject putDiagnosetrainhistory(@RequestBody String history) {
  116. try {
  117. log.warn(history);
  118. trainFileModeService.putDiagnosetrainhistory(history);
  119. return JsonResult.success(ResultCode.SUCCESS);
  120. } catch (Exception e) {
  121. log.error("请求错误", e);
  122. return JsonResult.error(ResultCode.ERROR);
  123. }
  124. }
  125. /**
  126. * 获取当前训练结果
  127. * @return
  128. */
  129. @GetMapping("/getHistory")
  130. public JSONObject getHistory() {
  131. try {
  132. Diagnosetrainhistory d = trainFileModeService.consumeHistory();
  133. return JsonResult.successData(ResultCode.SUCCESS, d);
  134. } catch (Exception e) {
  135. log.error("请求错误", e);
  136. return JsonResult.error(ResultCode.ERROR);
  137. }
  138. }
  139. /**
  140. * 编辑最终
  141. *
  142. * @param history
  143. * @return
  144. */
  145. @PostMapping("/editHistory")
  146. public JSONObject editDiagnosetrainhistory(@RequestBody String history) {
  147. try {
  148. trainFileModeService.editDiagnosetrainhistory(history);
  149. return JsonResult.success(ResultCode.SUCCESS);
  150. } catch (Exception e) {
  151. return JsonResult.error(ResultCode.ERROR_DATA);
  152. }
  153. }
  154. /**
  155. * @return
  156. */
  157. @GetMapping("/getHistoryList")
  158. public JSONObject getDiagnosetrainhistoryList() {
  159. try {
  160. List<Diagnosetrainhistory> list = trainFileModeService.getDiagnosetrainhistoryList();
  161. return JsonResult.successData(ResultCode.SUCCESS, list);
  162. } catch (Exception e) {
  163. return JsonResult.error(ResultCode.ERROR);
  164. }
  165. }
  166. /**
  167. * 预测评估
  168. *
  169. * @param jsonObject
  170. * @return
  171. */
  172. @PostMapping("/forecasts")
  173. public JSONObject forecasts(@RequestBody JSONObject jsonObject) {
  174. try {
  175. log.warn("预估请求数据:" + jsonObject.toJSONString());
  176. String resultvalue = trainFileModeService.forecasts(jsonObject);
  177. return JsonResult.successData(ResultCode.SUCCESS, resultvalue);
  178. } catch (Exception e) {
  179. return JsonResult.error(ResultCode.ERROR);
  180. }
  181. }
  182. }