123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- 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<String, UniformCodeInfo> map = dataService.getUniformCodeInfoMap();
- return JsonResult.successData(ResultCode.SUCCESS, map);
- }
- /**
- * 获取UniformCodes
- *
- * @return
- */
- @GetMapping("/data")
- public JSONObject getData() {
- Map<String, Object> 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<History> hs = new ArrayList<>();
- List<Diagnosetrainhistory> 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();
- }
- }
|