Explorar o código

新增故障训练进度代码

wangchangsheng %!s(int64=2) %!d(string=hai) anos
pai
achega
b9c44b73cc

+ 46 - 6
gyee-sample-impala/src/main/java/com/gyee/impala/controller/diagnose/TrainFileModeController.java

@@ -1,19 +1,16 @@
 package com.gyee.impala.controller.diagnose;
 
 
-import cn.hutool.core.io.FileUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.gyee.impala.common.result.JsonResult;
 import com.gyee.impala.common.result.ResultCode;
-import com.gyee.impala.model.master.diagnose.TrainParam;
+import com.gyee.impala.model.master.diagnose.TrainInfo;
 import com.gyee.impala.service.custom.SftpFileService;
 import com.gyee.impala.service.master.diagnose.TrainFileModeService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
-
-import java.io.IOException;
+import java.util.List;
 
 
 /**
@@ -24,6 +21,7 @@ import java.io.IOException;
 @RequestMapping("/api/filemode")
 public class TrainFileModeController {
 
+
     @Autowired
     private SftpFileService fileService;
 
@@ -34,7 +32,7 @@ public class TrainFileModeController {
     /**
      *
      */
-    @PostMapping("/trainfile")
+/*    @PostMapping("/trainfile")
     @ResponseBody
     public JSONObject getTrainfile(@RequestPart("trainParam") TrainParam trainParam, @RequestParam("file") MultipartFile file) {
         if (file.isEmpty()) {
@@ -47,9 +45,51 @@ public class TrainFileModeController {
         } catch (Exception e) {
             return JsonResult.error(ResultCode.ERROR_DATA_FILE);
         }
+    }*/
+
+
+    /**
+     *
+     */
+    @PostMapping("/trainfile")
+    @ResponseBody
+    public JSONObject getTrainfile(String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
+        if (file.isEmpty()) {
+            return JsonResult.error(ResultCode.ERROR_FILE_NO);
+        }
+
+        try {
+            trainFileModeService.exec(name, forecastLabel, inputLabel, host, file);
+            return JsonResult.success(ResultCode.SUCCESS);
+        } catch (Exception e) {
+            return JsonResult.error(ResultCode.ERROR_DATA_FILE);
+        }
+    }
 
 
+    @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);
+        }
+
     }
 
 
+    @PostMapping("/gettrainInfo")
+    @ResponseBody
+    public JSONObject getConsume() {
+        try {
+            List<TrainInfo> list = trainFileModeService.consume();
+            return JsonResult.successData(ResultCode.SUCCESS, list);
+        } catch (Exception e) {
+            return JsonResult.error(ResultCode.ERROR);
+        }
+    }
+
 }

+ 10 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/model/master/diagnose/TrainInfo.java

@@ -0,0 +1,10 @@
+package com.gyee.impala.model.master.diagnose;
+
+import lombok.Data;
+
+@Data
+public class TrainInfo {
+    private String log;
+    private String time;
+    private  boolean complete;
+}

+ 131 - 7
gyee-sample-impala/src/main/java/com/gyee/impala/service/master/diagnose/TrainFileModeService.java

@@ -1,16 +1,22 @@
 package com.gyee.impala.service.master.diagnose;
 
 
-import com.alibaba.fastjson.JSON;
 import com.gyee.impala.common.config.GyeeConfig;
 import com.gyee.impala.common.config.jsch.JSchConfig;
+import com.gyee.impala.model.master.diagnose.TrainInfo;
 import com.gyee.impala.model.master.diagnose.TrainParam;
 import com.gyee.impala.service.custom.SftpFileService;
 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;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
 
 @Service
 public class TrainFileModeService {
@@ -29,15 +35,25 @@ public class TrainFileModeService {
     private GyeeConfig gyeeConfig;
 
 
-    public synchronized void exec(TrainParam trainParam,MultipartFile file) throws Exception{
+    /**
+     * 是否执行结束
+     */
+    private boolean isComplete = true;
+
+
+    BlockingQueue<TrainInfo> console = new LinkedBlockingQueue<TrainInfo>();
+
+
+    public synchronized void exec(TrainParam trainParam, MultipartFile file) throws Exception {
 
         //获取上传文件的文件名
-        String type  = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));;
+        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
+        ;
         /**上传文件**/
-        fileService.uploadFile(file.getOriginalFilename(), file.getInputStream(),"10.155.32.14");
+        //fileService.uploadFile(file.getOriginalFilename(), file.getInputStream(),"10.155.32.14");
 
         //文件路径
-        String filePath = config.getPath()+file.getOriginalFilename();
+        String filePath = config.getPath() + file.getOriginalFilename();
 
         System.out.println(new Date() + "开始执行脚本...");
         Process p;
@@ -45,22 +61,130 @@ public class TrainFileModeService {
 
 
         try {
-            String[] cmd = {"/bin/sh", "-c", "python " + cmdPath + trainParam.getName() + ".py "+filePath};
+//          String[] cmd = {"/bin/sh", "-c", "python " + cmdPath + trainParam.getName() + ".py "+filePath};
+            String cmd = "cmd /c python " + cmdPath + trainParam.getName() + ".py " + filePath;
 
-            System.out.println(cmd[0] + " " + cmd[1] + " " + cmd[2]);
+//           System.out.println(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);
+                }
+                if (se != null) {
+                    System.out.println(se);
+                }
+            }
+
+            Thread.sleep(5000);
             p.waitFor();
         } catch (Exception e) {
             e.printStackTrace();
+        } finally {
+            isComplete = true;
         }
         System.out.println(new Date() + "脚本执行结束...");
 
     }
 
 
+    public synchronized void exec(String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) throws Exception {
 
+        if (!isComplete) {
+            return;
+        }
+        isComplete = false;
+        //获取上传文件的文件名
+        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
+        ;
+        /**上传文件**/
+        //fileService.uploadFile(file.getOriginalFilename(), file.getInputStream(),"10.155.32.14");
+
+        //文件路径
+        String filePath = config.getPath() + file.getOriginalFilename();
+
+        System.out.println(new Date() + "开始执行脚本...");
+        Process p;
+        String cmdPath = gyeeConfig.getDiagnosePath();
 
 
+        try {
+//          String[] cmd = {"/bin/sh", "-c", "python " + cmdPath + trainParam.getName() + ".py "+filePath};
+            String cmd = "cmd /c python " + cmdPath + name + ".py " + filePath;
+
+//           System.out.println(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);
+                }
+                if (se != null) {
+                    System.out.println(se);
+                }
+            }
+
+            Thread.sleep(10000);
+            p.waitFor();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            isComplete = true;
+        }
+        System.out.println(new Date() + "脚本执行结束...");
+    }
+
+
+    // 控制台信息
+    public void produce(TrainInfo trainInfo) throws Exception {
+        // put 控制台信息到队列中
+        trainInfo.setComplete(isComplete);
+        console.put(trainInfo);
+    }
+
+
+    // 输出控制台信息
+    public List<TrainInfo> consume() throws Exception {
+        // take方法取出一条记录
+        List<TrainInfo> list = new ArrayList<>();
+        int sise = console.size();
+        for (int i = 0; i < sise; i++) {
+            list.add(console.take());
+        }
+        return list;
+    }
+
+  /*  // 定义苹果消费者
+    class Consumer implements Runnable {
+        private String instance;
+        private TrainInfo basket;
+
+        public Consumer(String instance, TrainInfo basket) {
+            this.instance = instance;
+            this.basket = basket;
+        }
+
+        public void run() {
+            try {
+                while (true) {
+                    // 消费苹果
+                    System.out.println("消费者准备消费苹果:" + instance);
+                    System.out.println(basket.getLog());
+                    System.out.println("!消费者消费苹果完毕:" + instance);
+
+                }
+            } catch (Exception ex) {
+                System.out.println("Consumer Interrupted");
+            }
+        }
+    }
+*/
 }