package com.gyee.impala.controller.diagnose; import com.alibaba.fastjson.JSONObject; import com.gyee.impala.common.cache.InfoCache; import com.gyee.impala.common.result.JsonResult; import com.gyee.impala.common.result.ResultCode; import com.gyee.impala.model.custom.diagnose.*; import com.gyee.impala.model.master.diagnose.AlgorithmType; import com.gyee.impala.model.master.diagnose.Diagnosetrainhistory; import com.gyee.impala.service.custom.diagnose.DataService; import com.gyee.impala.service.custom.diagnose.SupervisedCmdService; import com.gyee.impala.service.master.diagnose.DiagnosetrainhistoryService; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.*; /** * 有监督学习控制器 * * @author xysn */ @CrossOrigin @RestController @RequestMapping("/api/supervised") public class SupervisedController { /** * 缓存 */ @Resource private InfoCache infoCache; /** * 命令执行 */ @Resource private SupervisedCmdService supervisedCmdService; /** * 数据服务 */ @Resource private DataService dataService; /** * 线程池 */ @Resource private ThreadPoolTaskExecutor taskExecutor; /** * 历史记录数据库操作 */ @Resource private DiagnosetrainhistoryService historyService; private static final Object locker = new Object(); private ExecuteInfo executeInfo; /** * 更新缓存数据 * * @param ci * @return */ @PostMapping("/") public JSONObject updateCoordinateCache(@RequestBody String ci) { infoCache.setSupervised(ci); System.out.print("su "); return JsonResult.success(); } /** * 报告进度 * * @param process * @return */ @GetMapping("/progress/{process}") public JSONObject updateCoordinateCache(@PathVariable Double process) { supervisedCmdService.setProcess(process); System.out.print("sp" + process + " "); return JsonResult.success(); } /** * 获取具体故障信息 * * @return */ @GetMapping("fault-info") public JSONObject getFaultInfo() { ExecuteInfo info = supervisedCmdService.getExecuteInfo(); return JsonResult.successData(ResultCode.SUCCESS, info); } /** * 获取进度 * * @return */ @GetMapping("/progress") public JSONObject updateCoordinateCache() { Value value = new Value(supervisedCmdService.getProcess()); return JsonResult.successData(ResultCode.SUCCESS, value); } /** * 获取数据 * * @return */ @GetMapping("/") public JSONObject getCoordinateCache() { Value value = new Value(infoCache.getSupervised()); return JsonResult.successData(ResultCode.SUCCESS, value); } @GetMapping("/complete/{fname}") public JSONObject setCompleteFileName(@PathVariable String fname) { Diagnosetrainhistory history = supervisedCmdService.getHistory(fname, infoCache.getSupervised()); historyService.insertItem(history); return JsonResult.success(); } /** * 获取数据并执行脚本 * * @return */ @PostMapping("/execute") public JSONObject executeScript(@RequestBody ExecuteInfo ais) { if (ais == null || ais.getDataInfos().length <= 0) { return JsonResult.error(4000, "训练内容不能为空!"); } if (!supervisedCmdService.isComplete()) { return JsonResult.error(4000, "命令正在执行..."); } synchronized (locker) { executeInfo = ais; taskExecutor.submit(this::execute); } return JsonResult.success(); } private void execute() { infoCache.setSupervised(""); supervisedCmdService.setProcess(-1); supervisedCmdService.exec(executeInfo); } /** * 获取UniformCodes * * @return */ @GetMapping("/uniform") public JSONObject getUniformCodes() { Map map = dataService.getUniformCodeInfoMap(); return JsonResult.successData(ResultCode.SUCCESS, map); } /** * 获取UniformCodes * * @return */ @GetMapping("/data") public JSONObject getData() { Map map = new HashMap<>(); String data = dataService.getFormData(supervisedCmdService.getExecuteInfo()); map.put("info", executeInfo); map.put("data", data); return JsonResult.successData(ResultCode.SUCCESS, data); } /** * 获取算法信息 * * @return */ @GetMapping("/algorithm") public JSONObject getAlgorithm() { Algorithm[] algorithms = infoCache.getAlgorithms(); return JsonResult.successData(ResultCode.SUCCESS, algorithms); } @GetMapping("/history") public JSONObject getHistory() { List hs = new ArrayList<>(); List ls = historyService.getAll(AlgorithmType.supervised); for (Diagnosetrainhistory he : ls) { History h = new History(); h.setCoordinate(he.getContext()); h.setFaultIds(Arrays.asList(he.getFaultids().split(","))); h.setName(he.getName()); hs.add(h); } return JsonResult.successData(ResultCode.SUCCESS, hs); } @GetMapping("/history/remove") public JSONObject removeHistory(@RequestParam(value = "names") String names) { if (names == null) { return JsonResult.error(4000, "记录名不能为空!"); } String[] ns = names.split(","); for (String s : ns) { if (s == null || s.equals("")) { continue; } historyService.deleteById(s); } return JsonResult.success(); } }