123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- package com.gyee.impala.controller.diagnose;
- import com.alibaba.fastjson.JSONObject;
- import com.gyee.impala.common.result.JsonResult;
- import com.gyee.impala.common.result.ResultCode;
- import com.gyee.impala.common.util.DateUtil;
- import com.gyee.impala.model.custom.diagnose.DataInfo;
- import com.gyee.impala.model.custom.diagnose.ExecuteInfo;
- import com.gyee.impala.model.master.Casefault;
- import com.gyee.impala.model.master.diagnose.Diagnosepoint;
- import com.gyee.impala.model.master.diagnose.Diagnosetrainhistory;
- import com.gyee.impala.model.master.diagnose.TrainInfo;
- import com.gyee.impala.service.custom.diagnose.CmdFaultDiagnoseService;
- import com.gyee.impala.service.custom.diagnose.DataDiagnoseService;
- import com.gyee.impala.service.custom.diagnose.TrainDataModeService;
- import com.gyee.impala.service.master.CasefaultService;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.kudu.client.ListTablesResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.Resource;
- import java.util.*;
- import java.util.stream.Collectors;
- @Slf4j
- @CrossOrigin
- @RestController
- @RequestMapping("/api/diagnose")
- public class TrainFaultDiagnoseController {
- @Autowired
- private DataDiagnoseService dataService;
- @Autowired
- private CasefaultService casefaultService;
- @Autowired
- TrainDataModeService trainDataModeService;
-
- @Resource
- private ThreadPoolTaskExecutor taskExecutor;
- @Autowired
- private CmdFaultDiagnoseService trainFileModeService;
- private static final Object locker = new Object();
- private String name1;
- private String forecastLabel1;
- private String[] inputLabel1;
- private String host1;
- private MultipartFile file1;
- private ExecuteInfo executeInfo;
- private String fileName;
-
- @PostMapping("/trainfile")
- @ResponseBody
- public JSONObject getTrainfile(String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
- if (!trainFileModeService.isComplete()) {
- return JsonResult.error(4000, "命令正在执行...");
- }
- if (file.isEmpty()) {
- return JsonResult.error(ResultCode.ERROR_FILE_NO);
- }
- try {
- synchronized (locker) {
- name1 = name;
- forecastLabel1 = forecastLabel;
- inputLabel1 = inputLabel;
- host1 = host;
- file1 = file;
- taskExecutor.submit(this::execute);
- }
- return JsonResult.success(ResultCode.SUCCESS);
- } catch (Exception e) {
- return JsonResult.error(ResultCode.ERROR_DATA_FILE);
- }
- }
-
- private void execute() {
- trainFileModeService.exec(name1, forecastLabel1, inputLabel1, host1, file1);
- }
-
-
- @GetMapping("/tables")
- public JSONObject getListTables(){
- List<Map<String, String>> list = new ArrayList<>();
- try {
- List<ListTablesResponse.TableInfo> tables = trainDataModeService.getListTables();
- tables.stream().filter(a -> a.getTableName().equals("impala::gyee_sample_kudu.casefault")).forEach(obj -> {
- Map<String, String> map = new HashMap<>();
- String name = obj.getTableName().substring(obj.getTableName().lastIndexOf(".") + 1);
- map.put("tableId", obj.getTableId());
- map.put("tableName", name);
- list.add(map);
- });
- } catch (Exception e) { e.getMessage(); }
- return JsonResult.successData(ResultCode.SUCCESS,list);
- }
-
- @GetMapping("/columns")
- public JSONObject getColumns(String table){
- Object columns = null;
- try {
- columns = trainDataModeService.getColumns(table);
- } catch (Exception e) { e.getMessage(); }
- return JsonResult.successData(ResultCode.SUCCESS, columns);
- }
-
- @GetMapping("/data")
- public JSONObject getData(String sql){
- List<Casefault> list = casefaultService.executeSql(sql);
- return JsonResult.successData(ResultCode.SUCCESS, list);
- }
-
- @PostMapping("/pointdata")
- public JSONObject getPointData(@RequestBody JSONObject json){
- if (json == null)
- return JsonResult.error(ResultCode.PARAM_IS_BLANK);
- boolean flag = json.getBooleanValue("flag");
- List<Diagnosepoint> points = JSONObject.parseArray(json.getJSONArray("points").toString(), Diagnosepoint.class);
- List<Casefault> faults = JSONObject.parseArray(json.getJSONArray("faults").toString(), Casefault.class);
-
- dataService.formatUniformcode(points.stream().filter(a -> !a.getUniformcode().equals("faulttype")).collect(Collectors.toList()));
- executeInfo = new ExecuteInfo();
- Calendar cal = Calendar.getInstance();
- DataInfo[] dataInfos = new DataInfo[faults.size()];
- for (int i = 0; i < faults.size(); i++){
- DataInfo data = new DataInfo();
- data.setId(Long.valueOf(faults.get(i).getId()));
- data.setStationId(faults.get(i).getStationen());
- data.setThingId(faults.get(i).getWindturbineid());
- data.setModelId(faults.get(i).getModel());
- data.setTag(faults.get(i).getFaultcode());
- data.setFaultTime(faults.get(i).getStarttime());
- cal.setTime(DateUtil.parseStrtoDate(faults.get(i).getStarttime(), DateUtil.YYYY_MM_DD_HH_MM_SS));
- cal.add(Calendar.MINUTE, -10);
- data.setStartTs(cal.getTimeInMillis() + "");
- cal.add(Calendar.MINUTE, 10);
- data.setEndTs(cal.getTimeInMillis() + "");
- dataInfos[i] = data;
- }
- executeInfo.setDataInfos(dataInfos);
- if (flag){
- if (!trainFileModeService.isComplete()) {
- return JsonResult.error(4000, "已有正在训练的模型...");
- }
- synchronized (locker) {
- taskExecutor.submit(this::execute2);
- }
- return JsonResult.success(ResultCode.SUCCESS);
- }else {
- Map<String, Object> mp = dataService.getFormData(executeInfo);
- return JsonResult.successData(ResultCode.SUCCESS, mp);
- }
- }
-
- private void execute2() {
- fileName = dataService.getFormDataAll(executeInfo);
- trainFileModeService.exec();
- }
-
- @GetMapping("/traindata")
- public JSONObject getData() {
- Map<String, Object> map = new HashMap<>();
- map.put("info", this.executeInfo);
- map.put("filename", fileName);
- return JsonResult.successData(ResultCode.SUCCESS, map);
- }
-
-
- @PostMapping("/addtrainInfo")
- public JSONObject addProducer(@RequestBody String trainInfo) {
- try {
- TrainInfo t = JSONObject.parseObject(trainInfo, TrainInfo.class);
- trainFileModeService.produce(t);
- return JsonResult.success(ResultCode.SUCCESS);
- } catch (Exception e) {
- return JsonResult.error(ResultCode.ERROR);
- }
- }
-
- @GetMapping("/gettrainInfo")
- @ResponseBody
- public JSONObject getConsume() {
- try {
- List<TrainInfo> list = trainFileModeService.consume();
- System.out.println(JsonResult.successData(ResultCode.SUCCESS, list));
- return JsonResult.successData(ResultCode.SUCCESS, list);
- } catch (Exception e) {
- return JsonResult.error(ResultCode.ERROR);
- }
- }
-
- @PostMapping("/putHistory")
- public JSONObject putDiagnosetrainhistory(@RequestBody String history) {
- try {
- log.info("训练模型结果:" + history);
- trainFileModeService.putDiagnosetrainhistory(history);
- return JsonResult.success(ResultCode.SUCCESS);
- } catch (Exception e) {
- log.error("请求错误", e);
- return JsonResult.error(ResultCode.ERROR);
- }
- }
-
- @GetMapping("/getHistory")
- public JSONObject getHistory() {
- try {
- Diagnosetrainhistory d = trainFileModeService.consumeHistory();
- return JsonResult.successData(ResultCode.SUCCESS, d);
- } catch (Exception e) {
- log.error("请求错误", e);
- return JsonResult.error(ResultCode.ERROR);
- }
- }
-
- @PostMapping("/forecasts")
- public JSONObject forecasts(@RequestBody JSONObject jsonObject) {
- try {
- log.warn("预估请求数据:" + jsonObject.toJSONString());
- String resultvalue = trainFileModeService.forecasts(jsonObject);
- return JsonResult.successData(ResultCode.SUCCESS, resultvalue);
- } catch (Exception e) {
- return JsonResult.error(ResultCode.ERROR);
- }
- }
- }
|