Selaa lähdekoodia

故障预测文件模式接口

wangchangsheng 2 vuotta sitten
vanhempi
commit
3a34cd2498

+ 1 - 3
gyee-sample-impala/src/main/java/com/gyee/impala/controller/diagnose/TrainFaultDiagnoseController.java

@@ -23,7 +23,6 @@ import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
-import javax.servlet.http.HttpServletRequest;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -67,7 +66,6 @@ public class TrainFaultDiagnoseController {
 
     /**
      * 文件模式训练接口
-     * @param request
      * @param name
      * @param forecastLabel
      * @param inputLabel
@@ -77,7 +75,7 @@ public class TrainFaultDiagnoseController {
      */
     @PostMapping("/trainfile")
     @ResponseBody
-    public JSONObject getTrainfile(HttpServletRequest request, String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
+    public JSONObject getTrainfile(String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
 
         if (!trainFileModeService.isComplete()) {
             return JsonResult.error(4000, "命令正在执行...");

+ 58 - 7
gyee-sample-impala/src/main/java/com/gyee/impala/controller/diagnose/TrainFaultPredictController.java

@@ -16,6 +16,7 @@ import lombok.extern.slf4j.Slf4j;
 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;
@@ -35,7 +36,7 @@ public class TrainFaultPredictController {
 
 
     @Autowired
-    private CmdFaultPredictService trainFaultDiagnoseService;
+    private CmdFaultPredictService cmdFaultPredictService;
     /**
      * 参数
      */
@@ -63,6 +64,58 @@ public class TrainFaultPredictController {
 
 
 
+    private String name1;
+    private String forecastLabel1;
+    private String[] inputLabel1;
+    private String host1;
+    private MultipartFile file1;
+
+
+
+
+
+    /**
+     * 离线文件模式训练故障预测接口
+     * @param forecastLabel
+     * @param inputLabel
+     * @param host
+     * @param file
+     * @return
+     */
+    @PostMapping("/trainfile")
+    @ResponseBody
+    public JSONObject getTrainfile(String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
+
+        if (!cmdFaultPredictService.isComplete()) {
+            return JsonResult.error(4000, "命令正在执行...");
+        }
+
+        if (file.isEmpty()) {
+            return JsonResult.error(ResultCode.ERROR_FILE_NO);
+        }
+        try {
+            synchronized (locker) {
+                forecastLabel1 = forecastLabel;
+                inputLabel1 = inputLabel;
+                host1 = host;
+                file1 = file;
+                taskExecutor.submit(this::executeFile);
+            }
+
+            return JsonResult.success(ResultCode.SUCCESS);
+        } catch (Exception e) {
+            return JsonResult.error(ResultCode.ERROR_DATA_FILE);
+        }
+    }
+
+
+
+    /**
+     * 调用执行脚本
+     */
+    private void executeFile() {
+        cmdFaultPredictService.exec(forecastLabel1, inputLabel1, host1, file1);
+    }
 
 
     /** 开始预测 查询 golden 所有原始数据
@@ -102,7 +155,7 @@ public class TrainFaultPredictController {
         }
         executeInfo.setDataInfos(dataInfos);
         if (flag){
-            if (!trainFaultDiagnoseService.isComplete()) {
+            if (!cmdFaultPredictService.isComplete()) {
                 return JsonResult.error(4000, "已有正在预测的故障模型...");
             }
             synchronized (locker) {
@@ -122,7 +175,7 @@ public class TrainFaultPredictController {
     private void execute() {
 
         fileName = dataDiagnoseService.getFormDataAll(executeInfo);
-        trainFaultDiagnoseService.exec();
+        cmdFaultPredictService.exec();
 
     }
 
@@ -154,7 +207,7 @@ public class TrainFaultPredictController {
     public JSONObject putDiagnosetrainhistory(@RequestBody String history) {
         try {
             log.info("训练模型结果:" + history);
-            trainFaultDiagnoseService.putDiagnosetrainhistory(history);
+            cmdFaultPredictService.putDiagnosetrainhistory(history);
             return JsonResult.success(ResultCode.SUCCESS);
         } catch (Exception e) {
             log.error("请求错误", e);
@@ -163,8 +216,6 @@ public class TrainFaultPredictController {
     }
 
 
-
-
     /**
      * 获取当前训练预测模型结果
      * @return
@@ -172,7 +223,7 @@ public class TrainFaultPredictController {
     @GetMapping("/getHistory")
     public JSONObject getHistory() {
         try {
-            Diagnosetrainhistory d = trainFaultDiagnoseService.getHistoryQueue();
+            Diagnosetrainhistory d = cmdFaultPredictService.getHistoryQueue();
             return JsonResult.successData(ResultCode.SUCCESS, d);
         } catch (Exception e) {
             log.error("请求错误", e);

+ 10 - 5
gyee-sample-impala/src/main/java/com/gyee/impala/service/custom/diagnose/CmdFaultDiagnoseService.java

@@ -55,10 +55,9 @@ public class CmdFaultDiagnoseService {
         return isComplete;
     }
 
-    //控制输出信息队列
+    //控制输出信息队列
     BlockingQueue<TrainInfo> infoQueue = new LinkedBlockingQueue<TrainInfo>();
-    //零时保存模型队列
-//    BlockingQueue<Diagnosetrainhistory> historyQueue = new LinkedBlockingQueue<Diagnosetrainhistory>();
+    //临时保存模型
     private Diagnosetrainhistory historyQueue =null;
 
     public Diagnosetrainhistory getHistoryQueue() {
@@ -69,6 +68,14 @@ public class CmdFaultDiagnoseService {
         this.historyQueue = historyQueue;
     }
 
+    /**
+     * 文件训练故障诊断模型
+     * @param name
+     * @param forecastLabel
+     * @param inputLabel
+     * @param host
+     * @param file
+     */
     public void exec(String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
 
         //判断当前是否有模型在训练
@@ -77,8 +84,6 @@ public class CmdFaultDiagnoseService {
         }
         try {
             isComplete = false;
-            //获取上传文件的文件名
-            String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
             /**上传文件**/
             fileService.uploadFile(file.getOriginalFilename(), file.getInputStream(), "10.155.32.14");
             //文件路径

+ 70 - 8
gyee-sample-impala/src/main/java/com/gyee/impala/service/custom/diagnose/CmdFaultPredictService.java

@@ -3,11 +3,14 @@ package com.gyee.impala.service.custom.diagnose;
 
 import com.alibaba.fastjson.JSONObject;
 import com.gyee.impala.common.config.GyeeConfig;
+import com.gyee.impala.common.config.jsch.JSchConfig;
 import com.gyee.impala.common.util.JudeSystem;
 import com.gyee.impala.model.master.diagnose.Diagnosetrainhistory;
+import com.gyee.impala.service.custom.SftpFileService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
@@ -16,6 +19,13 @@ import java.io.InputStreamReader;
 @Service
 public class CmdFaultPredictService {
 
+
+    @Autowired
+    private SftpFileService fileService;
+
+    @Autowired
+    private JSchConfig config;
+
     /**
      * 保存脚本的位置
      */
@@ -41,41 +51,93 @@ public class CmdFaultPredictService {
         return historyQueue;
     }
 
+
     /**
-     * 在线训诊断用脚本
+     * 使用文件训练故障预测模型
+     * @param forecastLabel
+     * @param inputLabel
+     * @param host
+     * @param file
+     */
+    public void exec(String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
+        //判断当前是否有模型在训练
+        if (!isComplete) {
+            log.error("已有进程在预测");
+            return;
+        }
+
+        try {
+            isComplete = false;
+            /**上传文件**/
+            log.info("正在上传故障预测文件...");
+            fileService.uploadFile(file.getOriginalFilename(), file.getInputStream(), "10.155.32.14");
+            log.info("上传故障预测文件结束...");
+            //文件路径
+            String filePath = config.getPath() + file.getOriginalFilename();
+            log.info("开始执行离线文件故障预测脚本" + (isComplete) + "...");
+
+            Process p;
+            //组装调用python脚本命令
+            String cmdPath = gyeeConfig.getDiagnosePath();
+            String inst = JudeSystem.isWindows() ? "cmd" : "/bin/sh";
+            String c = JudeSystem.isWindows() ? "/c" : "-c";
+            String[] cmd = {inst, c, "python " + cmdPath + "Faul_prediction.py " + filePath};
+            log.info(cmd[0] + " " + cmd[1] + " " + cmd[2]);
+            //执行命令
+            p = Runtime.getRuntime().exec(cmd);
+
+            p.waitFor();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }finally {
+            isComplete = true;
+            log.info("离线文件故障预测脚本执行结束" + (isComplete) + "...");
+        }
+
+    }
+
+
+
+
+
+
+
+
+
+    /**
+     * 在线数据训练故障预测模型
      */
     public void exec() {
         //判断当前是否有模型在训练
         if (!isComplete) {
-            log.error("已有进程在诊断");
+            log.error("已有进程在预测");
             return;
         }
 
         try {
             isComplete = false;
-            log.info("开始执行诊断脚本" + (isComplete) + "...");
+            log.info("开始执行在线故障预测脚本" + (isComplete) + "...");
 
             //组装调用脚本命令
             Process p;
             String cmdPath = gyeeConfig.getDiagnosePath();
             String inst = JudeSystem.isWindows() ? "cmd" : "/bin/sh";
             String c = JudeSystem.isWindows() ? "/c" : "-c";
-            String[] cmd = {inst, c, "python " + cmdPath +".py "};
+            String[] cmd = {inst, c, "python " + cmdPath +"Faul_prediction.py"};
             log.info(cmd[0] + " " + cmd[1] + " " + cmd[2]);
 
             //执行脚本
             p = Runtime.getRuntime().exec(cmd);
-
             //脚本输出信息
             BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
             BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
             String si = null, se = null;
             while ((si = bri.readLine()) != null || (se = bre.readLine()) != null) {
                 if (si != null) {
-                    System.out.println("诊断返回信息:" + si);
+                    System.out.println("预测返回信息:" + si);
                 }
                 if (se != null) {
-                    System.err.println("诊断返回错误信息:" + se);
+                    System.err.println("预测返回错误信息:" + se);
                 }
             }
             p.waitFor();
@@ -84,7 +146,7 @@ public class CmdFaultPredictService {
             e.printStackTrace();
         }finally {
             isComplete = true;
-            log.info("诊断脚本执行结束" + (isComplete) + "...");
+            log.info("在线故障预测脚本执行结束" + (isComplete) + "...");
         }
     }