TrainDataModeController.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.common.util.DateUtil;
  6. import com.gyee.impala.model.custom.diagnose.DataInfo;
  7. import com.gyee.impala.model.custom.diagnose.ExecuteInfo;
  8. import com.gyee.impala.model.master.Casefault;
  9. import com.gyee.impala.model.master.diagnose.Diagnosepoint;
  10. import com.gyee.impala.service.custom.diagnose.DataPointService;
  11. import com.gyee.impala.service.master.CasefaultService;
  12. import com.gyee.impala.service.master.diagnose.TrainDataModeService;
  13. import org.apache.kudu.client.ListTablesResponse;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.web.bind.annotation.*;
  16. import java.util.*;
  17. import java.util.stream.Collectors;
  18. /**
  19. * 数据源方式训练模型
  20. */
  21. @CrossOrigin
  22. @RestController
  23. @RequestMapping("/api/traindatamode")
  24. public class TrainDataModeController {
  25. @Autowired
  26. private DataPointService dataService;
  27. @Autowired
  28. private CasefaultService casefaultService;
  29. @Autowired
  30. TrainDataModeService trainDataModeService;
  31. /**
  32. * 查询数据库的表
  33. * @return
  34. */
  35. @GetMapping("/tables")
  36. public JSONObject getListTables(){
  37. List<Map<String, String>> list = new ArrayList<>();
  38. try {
  39. List<ListTablesResponse.TableInfo> tables = trainDataModeService.getListTables();
  40. tables.stream().filter(a -> a.getTableName().equals("impala::gyee_sample_kudu.casefault")).forEach(obj -> {
  41. Map<String, String> map = new HashMap<>();
  42. String name = obj.getTableName().substring(obj.getTableName().lastIndexOf(".") + 1);
  43. map.put("tableId", obj.getTableId());
  44. map.put("tableName", name);
  45. list.add(map);
  46. });
  47. } catch (Exception e) { e.getMessage(); }
  48. return JsonResult.successData(ResultCode.SUCCESS,list);
  49. }
  50. /**
  51. * 查询数据库表的列
  52. * @param table
  53. * @return
  54. */
  55. @GetMapping("/columns")
  56. public JSONObject getColumns(String table){
  57. Object columns = null;
  58. try {
  59. columns = trainDataModeService.getColumns(table);
  60. } catch (Exception e) { e.getMessage(); }
  61. return JsonResult.successData(ResultCode.SUCCESS, columns);
  62. }
  63. /** 查询样本原始数据 **/
  64. @GetMapping("/data")
  65. public JSONObject getData(String sql){
  66. List<Casefault> list = casefaultService.executeSql(sql);
  67. return JsonResult.successData(ResultCode.SUCCESS, list);
  68. }
  69. /** 开始训练 查询 golden 所有原始数据
  70. * flag ture: 所有数据
  71. * flag false: 前10条数据
  72. * **/
  73. @PostMapping("/pointdata")
  74. public JSONObject getPointData(@RequestBody JSONObject json){
  75. if (json == null)
  76. return JsonResult.error(ResultCode.PARAM_IS_BLANK);
  77. json = json.getJSONObject("params");
  78. boolean flag = json.getBooleanValue("flag");
  79. List<Diagnosepoint> points = JSONObject.parseArray(json.getJSONArray("points").toString(), Diagnosepoint.class);
  80. List<Casefault> faults = JSONObject.parseArray(json.getJSONArray("faults").toString(), Casefault.class);
  81. Map<String, Object> map = new HashMap<>();
  82. /** 组装数据 删除添加的故障类型**/
  83. dataService.formatUniformcode(points.stream().filter(a -> !a.getUniformcode().equals("faulttype")).collect(Collectors.toList()));
  84. ExecuteInfo executeInfo = new ExecuteInfo();
  85. Calendar cal = Calendar.getInstance();
  86. DataInfo[] dataInfos = new DataInfo[faults.size()];
  87. for (int i = 0; i < faults.size(); i++){
  88. DataInfo data = new DataInfo();
  89. data.setId(Long.valueOf(faults.get(i).getId()));
  90. data.setStationId(faults.get(i).getStationen());
  91. data.setThingId(faults.get(i).getWindturbineid());
  92. data.setModelId(faults.get(i).getModel());
  93. data.setTag(faults.get(i).getFaultcode());
  94. data.setFaultTime(faults.get(i).getStarttime());
  95. data.setStartTs(DateUtil.covertDateTimestamp(faults.get(i).getStarttime()).toString());
  96. if (flag){
  97. data.setEndTs(DateUtil.covertDateTimestamp(faults.get(i).getEndtime()).toString());
  98. }else{
  99. //查看前10条数据时结束时间往后推10s
  100. cal.setTime(DateUtil.parseStrtoDate(faults.get(i).getStarttime(), DateUtil.DATE_TIME_PATTERN));
  101. cal.add(Calendar.SECOND, 10);
  102. data.setEndTs(cal.getTimeInMillis() + "");
  103. }
  104. dataInfos[i] = data;
  105. }
  106. executeInfo.setDataInfos(dataInfos);
  107. if (flag){
  108. String file = dataService.getFormDataAll(executeInfo);
  109. map.put("info", executeInfo);
  110. map.put("filename", file);
  111. return JsonResult.successData(ResultCode.SUCCESS, map);
  112. }else {
  113. Map<String, Object> mp = dataService.getFormData(executeInfo);
  114. return JsonResult.successData(ResultCode.SUCCESS, mp);
  115. }
  116. }
  117. }