|
@@ -10,77 +10,168 @@ 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.DataDiagnoseService;
|
|
|
-import com.gyee.impala.service.custom.diagnose.TrainFaultDiagnoseService;;
|
|
|
+import com.gyee.impala.service.master.CasefaultService;
|
|
|
+import com.gyee.impala.service.custom.diagnose.TrainDataModeService;
|
|
|
+import com.gyee.impala.service.custom.diagnose.TrainFileModeService;
|
|
|
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.Calendar;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
- * 故障预测
|
|
|
+ * 故障诊断模型训练
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@CrossOrigin
|
|
|
@RestController
|
|
|
- @RequestMapping("/api/diagnose")
|
|
|
+@RequestMapping("/api/diagnose")
|
|
|
public class TrainFaultDiagnoseController {
|
|
|
|
|
|
-
|
|
|
@Autowired
|
|
|
- private TrainFaultDiagnoseService trainFaultDiagnoseService;
|
|
|
+ private DataDiagnoseService dataService;
|
|
|
+ @Autowired
|
|
|
+ private CasefaultService casefaultService;
|
|
|
+ @Autowired
|
|
|
+ TrainDataModeService trainDataModeService;
|
|
|
+
|
|
|
/**
|
|
|
- * 参数
|
|
|
+ * 线程池
|
|
|
*/
|
|
|
+ @Resource
|
|
|
+ private ThreadPoolTaskExecutor taskExecutor;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TrainFileModeService 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;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
- * 数据服务
|
|
|
+ * 文件模式训练接口
|
|
|
+ * @param request
|
|
|
+ * @param name
|
|
|
+ * @param forecastLabel
|
|
|
+ * @param inputLabel
|
|
|
+ * @param host
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
*/
|
|
|
- @Autowired
|
|
|
- private DataDiagnoseService dataDiagnoseService;
|
|
|
+ @PostMapping("/trainfile")
|
|
|
+ @ResponseBody
|
|
|
+ public JSONObject getTrainfile(HttpServletRequest request, 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
- * 线程池
|
|
|
+ * 调用执行脚本
|
|
|
*/
|
|
|
- @Resource
|
|
|
- private ThreadPoolTaskExecutor taskExecutor;
|
|
|
+ private void execute() {
|
|
|
+ trainFileModeService.exec(name1, forecastLabel1, inputLabel1, host1, file1);
|
|
|
+ }
|
|
|
|
|
|
+ /** 在线训练 **/
|
|
|
/**
|
|
|
- * 预测生成文件名
|
|
|
+ * 查询数据库的表
|
|
|
+ * @return
|
|
|
*/
|
|
|
- private String fileName;
|
|
|
-
|
|
|
+ @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);
|
|
|
+ }
|
|
|
|
|
|
- private static final Object locker = new Object();
|
|
|
+ /**
|
|
|
+ * 查询数据库表的列
|
|
|
+ * @param table
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @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);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- /** 开始预测 查询 golden 所有原始数据
|
|
|
- * flag ture: 所有数据 ;false: 前10条数据
|
|
|
+ /** 开始训练 查询 golden 所有原始数据
|
|
|
+ * flag ture: 所有数据
|
|
|
+ * flag false: 前10条数据
|
|
|
* **/
|
|
|
- @PostMapping("/pointfaultdata")
|
|
|
+ @PostMapping("/pointdata")
|
|
|
public JSONObject getPointData(@RequestBody JSONObject json){
|
|
|
if (json == null)
|
|
|
return JsonResult.error(ResultCode.PARAM_IS_BLANK);
|
|
|
|
|
|
-
|
|
|
- log.info("进入预测程序");
|
|
|
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);
|
|
|
|
|
|
/** 组装数据 删除添加的故障类型**/
|
|
|
- dataDiagnoseService.formatUniformcode(points.stream().filter(a -> !a.getUniformcode().equals("faulttype")).collect(Collectors.toList()));
|
|
|
+ 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()];
|
|
@@ -94,58 +185,89 @@ public class TrainFaultDiagnoseController {
|
|
|
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, -30);
|
|
|
+ cal.add(Calendar.MINUTE, -10);
|
|
|
data.setStartTs(cal.getTimeInMillis() + "");
|
|
|
- cal.add(Calendar.MINUTE, 30);
|
|
|
+ cal.add(Calendar.MINUTE, 10);
|
|
|
data.setEndTs(cal.getTimeInMillis() + "");
|
|
|
dataInfos[i] = data;
|
|
|
}
|
|
|
executeInfo.setDataInfos(dataInfos);
|
|
|
if (flag){
|
|
|
- if (!trainFaultDiagnoseService.isComplete()) {
|
|
|
- return JsonResult.error(4000, "已有正在预测的故障模型...");
|
|
|
+ if (!trainFileModeService.isComplete()) {
|
|
|
+ return JsonResult.error(4000, "已有正在训练的模型...");
|
|
|
}
|
|
|
synchronized (locker) {
|
|
|
- taskExecutor.submit(this::execute);
|
|
|
+ taskExecutor.submit(this::execute2);
|
|
|
}
|
|
|
return JsonResult.success(ResultCode.SUCCESS);
|
|
|
}else {
|
|
|
- Map<String, Object> mp = dataDiagnoseService.getFormData(executeInfo);
|
|
|
+ Map<String, Object> mp = dataService.getFormData(executeInfo);
|
|
|
return JsonResult.successData(ResultCode.SUCCESS, mp);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 调用执行脚本
|
|
|
*/
|
|
|
- private void execute() {
|
|
|
-
|
|
|
- fileName = dataDiagnoseService.getFormDataAll(executeInfo);
|
|
|
- trainFaultDiagnoseService.exec();
|
|
|
-
|
|
|
+ private void execute2() {
|
|
|
+ fileName = dataService.getFormDataAll(executeInfo);
|
|
|
+ trainFileModeService.exec();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
- * py 获取预测数据
|
|
|
+ * py 获取在线训练数据
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
- @GetMapping("/trainfaultdata")
|
|
|
+ @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);
|
|
|
}
|
|
|
+ /** 在线训练 **/
|
|
|
+
|
|
|
|
|
|
+ /**
|
|
|
+ * 生产控制台信息
|
|
|
+ * @param trainInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @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);
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 缓存训练预测模型结果
|
|
|
+ * 消费控制台信息
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加训练结果
|
|
|
*
|
|
|
* @param history
|
|
|
* @return
|
|
@@ -154,7 +276,7 @@ public class TrainFaultDiagnoseController {
|
|
|
public JSONObject putDiagnosetrainhistory(@RequestBody String history) {
|
|
|
try {
|
|
|
log.info("训练模型结果:" + history);
|
|
|
- trainFaultDiagnoseService.putDiagnosetrainhistory(history);
|
|
|
+ trainFileModeService.putDiagnosetrainhistory(history);
|
|
|
return JsonResult.success(ResultCode.SUCCESS);
|
|
|
} catch (Exception e) {
|
|
|
log.error("请求错误", e);
|
|
@@ -163,16 +285,14 @@ public class TrainFaultDiagnoseController {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
- * 获取当前训练预测模型结果
|
|
|
+ * 获取当前训练结果
|
|
|
* @return
|
|
|
*/
|
|
|
@GetMapping("/getHistory")
|
|
|
public JSONObject getHistory() {
|
|
|
try {
|
|
|
- Diagnosetrainhistory d = trainFaultDiagnoseService.getHistoryQueue();
|
|
|
+ Diagnosetrainhistory d = trainFileModeService.consumeHistory();
|
|
|
return JsonResult.successData(ResultCode.SUCCESS, d);
|
|
|
} catch (Exception e) {
|
|
|
log.error("请求错误", e);
|
|
@@ -182,4 +302,23 @@ public class TrainFaultDiagnoseController {
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 预测评估
|
|
|
+ *
|
|
|
+ * @param jsonObject
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @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);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|