|
@@ -4,10 +4,14 @@ package com.gyee.impala.service.master.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.DateUtil;
|
|
|
+import com.gyee.impala.common.util.FileUtil;
|
|
|
import com.gyee.impala.common.util.JudeSystem;
|
|
|
import com.gyee.impala.model.master.diagnose.Diagnosetrainhistory;
|
|
|
import com.gyee.impala.model.master.diagnose.TrainInfo;
|
|
|
import com.gyee.impala.service.custom.SftpFileService;
|
|
|
+import org.apache.ibatis.logging.Log;
|
|
|
+import org.apache.ibatis.logging.LogFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -22,6 +26,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
|
|
public class TrainFileModeService {
|
|
|
|
|
|
|
|
|
+ protected Log log = LogFactory.getLog(getClass());
|
|
|
@Autowired
|
|
|
private SftpFileService fileService;
|
|
|
|
|
@@ -47,14 +52,14 @@ public class TrainFileModeService {
|
|
|
return isComplete;
|
|
|
}
|
|
|
|
|
|
+ //控制太输出信息队列
|
|
|
BlockingQueue<TrainInfo> infoQueue = new LinkedBlockingQueue<TrainInfo>();
|
|
|
+ //零时保存模型队列
|
|
|
BlockingQueue<Diagnosetrainhistory> historyQueue = new LinkedBlockingQueue<Diagnosetrainhistory>();
|
|
|
|
|
|
- Map<String, BlockingQueue<TrainInfo>> mapConsole = new HashMap<>();
|
|
|
-
|
|
|
-
|
|
|
public void exec(String name, String forecastLabel, String[] inputLabel, String host, MultipartFile file) {
|
|
|
|
|
|
+ //判断当前是否有模型在训练
|
|
|
if (!isComplete) {
|
|
|
return;
|
|
|
}
|
|
@@ -64,22 +69,20 @@ public class TrainFileModeService {
|
|
|
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() + "开始执行脚本...");
|
|
|
+ System.out.println(DateUtil.getCurrentDate() + "开始执行脚本" + (isComplete) + "...");
|
|
|
Process p;
|
|
|
+ //组装调用python脚本命令
|
|
|
String cmdPath = gyeeConfig.getDiagnosePath();
|
|
|
String inst = JudeSystem.isWindows() ? "cmd" : "/bin/sh";
|
|
|
String c = JudeSystem.isWindows() ? "/c" : "-c";
|
|
|
-// String[] cmd = {"/bin/sh", "-c", "python " + cmdPath + name + ".py " + filePath};
|
|
|
String[] cmd = {inst, c, "python " + cmdPath + name + ".py " + filePath};
|
|
|
-
|
|
|
- Thread.sleep(3000);
|
|
|
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;
|
|
@@ -89,8 +92,6 @@ public class TrainFileModeService {
|
|
|
}
|
|
|
if (se != null) {
|
|
|
System.err.println(se);
|
|
|
- TrainInfo t = JSONObject.parseObject(se, TrainInfo.class);
|
|
|
- produce(t);
|
|
|
}
|
|
|
}
|
|
|
p.waitFor();
|
|
@@ -101,21 +102,27 @@ public class TrainFileModeService {
|
|
|
isComplete = true;
|
|
|
}
|
|
|
|
|
|
- System.out.println(new Date() + "脚本执行结束...");
|
|
|
+ System.out.println(DateUtil.getCurrentDate() + "脚本执行结束" + (isComplete) + "...");
|
|
|
}
|
|
|
|
|
|
|
|
|
- // 控制台信息
|
|
|
+ /**
|
|
|
+ * 控制台信息
|
|
|
+ * @param trainInfo
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
public void produce(TrainInfo trainInfo) throws Exception {
|
|
|
// put 控制台信息到队列中
|
|
|
-// trainInfo.setComplete(trainInfo.getComplete() ? trainInfo.isComplete() : isComplete);
|
|
|
-
|
|
|
- System.out.println("add log =["+trainInfo.getLog()+"] time = ["+trainInfo.getTime()+"] complete = ["+isComplete+"]");
|
|
|
+ System.out.println("add log =[" + trainInfo.getLog() + "] time = [" + trainInfo.getTime() + "] complete = [" + trainInfo.getComplete() + "]");
|
|
|
infoQueue.put(trainInfo);
|
|
|
}
|
|
|
|
|
|
|
|
|
- // 输出控制台信息
|
|
|
+ /**
|
|
|
+ * 输出控制台信息
|
|
|
+ * @return list
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
public List<TrainInfo> consume() throws Exception {
|
|
|
// take方法取出一条记录
|
|
|
List<TrainInfo> list = new ArrayList<>();
|
|
@@ -136,26 +143,104 @@ public class TrainFileModeService {
|
|
|
*/
|
|
|
public void putDiagnosetrainhistory(String history) throws Exception {
|
|
|
|
|
|
- Diagnosetrainhistory d = JSONObject.parseObject(history, Diagnosetrainhistory.class);
|
|
|
+ String body = JSONObject.toJSONString(history);
|
|
|
+ Object historys = JSONObject.parse(body);
|
|
|
+ Diagnosetrainhistory d = JSONObject.parseObject(historys.toString(), Diagnosetrainhistory.class);
|
|
|
d.setEnable(true);
|
|
|
diagnosetrainhistoryService.insertItem(d);
|
|
|
Diagnosetrainhistory dbhistory = diagnosetrainhistoryService.getHistoryByModel(d.getModel());
|
|
|
historyQueue.put(dbhistory);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- public Diagnosetrainhistory getDiagnosetrainhistory(String id) throws Exception {
|
|
|
- Diagnosetrainhistory history = historyQueue.take();
|
|
|
- return history;
|
|
|
+ /**
|
|
|
+ * 训练完成获取实时模型结果
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public Diagnosetrainhistory consumeHistory() throws Exception {
|
|
|
+ Diagnosetrainhistory d = null;
|
|
|
+ try {
|
|
|
+ if (historyQueue.size() > 0) {
|
|
|
+ d = historyQueue.take();
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return d;
|
|
|
}
|
|
|
|
|
|
|
|
|
- public int editDiagnosetrainhistory(String history) {
|
|
|
+ /**
|
|
|
+ * 编辑模型
|
|
|
+ *
|
|
|
+ * @param history
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void editDiagnosetrainhistory(String history) throws Exception {
|
|
|
Diagnosetrainhistory update = JSONObject.parseObject(history, Diagnosetrainhistory.class);
|
|
|
+ diagnosetrainhistoryService.editDiagnosetrainhistory(update);
|
|
|
+ }
|
|
|
|
|
|
- int code = diagnosetrainhistoryService.editDiagnosetrainhistory(update);
|
|
|
|
|
|
- return code;
|
|
|
+ /**
|
|
|
+ * 查询历史模型
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<Diagnosetrainhistory> getDiagnosetrainhistoryList() {
|
|
|
+ List<Diagnosetrainhistory> list = diagnosetrainhistoryService.getList();
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 预估
|
|
|
+ *
|
|
|
+ * @param jsonObject
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String forecasts(JSONObject jsonObject) {
|
|
|
+
|
|
|
+ String resultvalue = null;
|
|
|
+ try {
|
|
|
+ //解析请求数据
|
|
|
+ String name = jsonObject.get("name").toString();
|
|
|
+ String filename = System.currentTimeMillis() + ".json";
|
|
|
+ //文件路径
|
|
|
+ String filePath = config.getPath() + filename;
|
|
|
+ fileService.uploadFile(filename, FileUtil.convertStringToInputStream(jsonObject.toJSONString()), "10.155.32.14");
|
|
|
+ resultvalue = "";
|
|
|
+ //组装调用脚本命令
|
|
|
+ String cmdPath = gyeeConfig.getDiagnosePath();
|
|
|
+ String inst = JudeSystem.isWindows() ? "cmd" : "/bin/sh";
|
|
|
+ String c = JudeSystem.isWindows() ? "/c" : "-c";
|
|
|
+ String[] cmd = {inst, c, "python " + cmdPath + name + ".py " + filePath};
|
|
|
+ Process p;
|
|
|
+ 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 (si.startsWith("resultvalue:")) {
|
|
|
+ resultvalue = si.replaceAll("resultvalue:", "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (se != null) {
|
|
|
+ System.err.println("预估返回错误信息:" + se);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ p.waitFor();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return resultvalue.trim();
|
|
|
+
|
|
|
+ }
|
|
|
}
|