添加自动执行python脚本

This commit is contained in:
2026-01-22 10:04:38 +08:00
parent 01dd1d5191
commit 7cf7784a89
9 changed files with 947 additions and 1 deletions

View File

@@ -3,8 +3,10 @@ package com.tem.bocai;
import com.tem.bocai.util.SQLiteUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class BocaiApplication {
public static void main(String[] args) {

View File

@@ -0,0 +1,94 @@
package com.tem.bocai.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@RestController
@RequestMapping("/api")
public class ChartController {
// 获取折线图1数据
@GetMapping("/charts/line1")
public Map<String, Object> getLineChartData1() {
Map<String, Object> response = new HashMap<>();
// 模拟数据 - 实际项目中应该从数据库获取
List<Integer> data = Arrays.asList(65, 59, 80, 81, 56, 55, 40);
List<String> labels = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月");
response.put("data", data);
response.put("labels", labels);
response.put("title", "折线图1数据");
return response;
}
// 获取折线图2数据
@GetMapping("/charts/line2")
public Map<String, Object> getLineChartData2() {
Map<String, Object> response = new HashMap<>();
// 模拟数据 - 实际项目中应该从数据库获取
List<Integer> data = Arrays.asList(28, 48, 40, 19, 86, 27, 90);
List<String> labels = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月");
response.put("data", data);
response.put("labels", labels);
response.put("title", "折线图2数据");
return response;
}
// 获取表格数据
@GetMapping("/table")
public List<Map<String, Object>> getTableData() {
List<Map<String, Object>> tableData = new ArrayList<>();
// 模拟数据 - 实际项目中应该从数据库获取
Map<String, Object> item1 = new HashMap<>();
item1.put("id", 1);
item1.put("name", "项目1");
item1.put("value", 120);
item1.put("status", "正常");
tableData.add(item1);
Map<String, Object> item2 = new HashMap<>();
item2.put("id", 2);
item2.put("name", "项目2");
item2.put("value", 230);
item2.put("status", "警告");
tableData.add(item2);
Map<String, Object> item3 = new HashMap<>();
item3.put("id", 3);
item3.put("name", "项目3");
item3.put("value", 180);
item3.put("status", "正常");
tableData.add(item3);
Map<String, Object> item4 = new HashMap<>();
item4.put("id", 4);
item4.put("name", "项目4");
item4.put("value", 90);
item4.put("status", "异常");
tableData.add(item4);
Map<String, Object> item5 = new HashMap<>();
item5.put("id", 5);
item5.put("name", "项目5");
item5.put("value", 320);
item5.put("status", "正常");
tableData.add(item5);
Map<String, Object> item6 = new HashMap<>();
item6.put("id", 6);
item6.put("name", "项目6");
item6.put("value", 270);
item6.put("status", "警告");
tableData.add(item6);
return tableData;
}
}

View File

@@ -0,0 +1,112 @@
package com.tem.bocai.schedules;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@Component
public class GetPredictResultSchedule {
/**
* 处理文件路径,确保路径正确
* @param filePath 文件路径
* @return 处理后的文件路径
*/
private String handleFilePath(String filePath) {
// 处理路径分隔符,统一使用系统默认分隔符
filePath = filePath.replace("/", System.getProperty("file.separator"));
filePath = filePath.replace("\\", System.getProperty("file.separator"));
// 如果路径包含空格,确保路径被正确处理
// Runtime.exec会自动处理带空格的路径不需要手动添加引号
return filePath;
}
// 每天凌晨2点执行
@Scheduled(cron = "0 0 2 * * ?")
public void executePythonScript() {
System.out.println("开始执行Python脚本...");
// 示例1基本参数
// String[] params = {"param1", "param2", "param3"};
// executePythonScriptWithParams("your_script.py", params);
// 示例2传递文件路径作为参数
String[] paramsWithFiles = {
"--input", "data/input.csv",
"--output", "results/output.json",
"--config", "config/settings.ini"
};
executePythonScriptWithParams("scripts/process_data.py", paramsWithFiles);
}
/**
* 执行带参数的Python脚本
* @param scriptPath Python脚本路径
* @param params 脚本参数数组(可以包含文件路径)
*/
public void executePythonScriptWithParams(String scriptPath, String[] params) {
try {
// 处理脚本路径,确保路径正确
scriptPath = handleFilePath(scriptPath);
// 构建命令数组
String[] command = new String[params.length + 2];
command[0] = "python";
command[1] = scriptPath;
// 添加参数,处理文件路径参数
for (int i = 0; i < params.length; i++) {
// 检查参数是否为文件路径(包含路径分隔符)
if (params[i].contains("\\") || params[i].contains("/")) {
command[i + 2] = handleFilePath(params[i]);
} else {
command[i + 2] = params[i];
}
}
System.out.println("执行命令: " + String.join(" ", command));
// 执行Python脚本
Process process = Runtime.getRuntime().exec(command);
// 读取脚本输出
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "UTF-8")
);
String line;
StringBuilder output = new StringBuilder();
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
// 读取错误输出
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(process.getErrorStream(), "UTF-8")
);
StringBuilder errorOutput = new StringBuilder();
while ((line = errorReader.readLine()) != null) {
errorOutput.append(line).append("\n");
}
// 等待脚本执行完成
int exitCode = process.waitFor();
System.out.println("Python脚本执行完成退出码: " + exitCode);
System.out.println("脚本输出:\n" + output.toString());
if (exitCode != 0) {
System.err.println("脚本执行错误:\n" + errorOutput.toString());
}
} catch (IOException | InterruptedException e) {
System.err.println("执行Python脚本时发生错误:");
e.printStackTrace();
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.8 KiB