diff --git a/src/main/java/com/tem/bocai/controller/ChartController.java b/src/main/java/com/tem/bocai/controller/ChartController.java index e580752..397a69d 100644 --- a/src/main/java/com/tem/bocai/controller/ChartController.java +++ b/src/main/java/com/tem/bocai/controller/ChartController.java @@ -4,7 +4,6 @@ import com.tem.bocai.entity.CompletedToday; import com.tem.bocai.entity.LotteryResult; import com.tem.bocai.repository.CompletedTodayRepository; import com.tem.bocai.repository.LotteryResultRepository; -import com.tem.bocai.util.DateUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; diff --git a/src/main/java/com/tem/bocai/schedules/ExAggregateDataScriptSchedule.java b/src/main/java/com/tem/bocai/schedules/ExAggregateDataScriptSchedule.java new file mode 100644 index 0000000..d4d0fdc --- /dev/null +++ b/src/main/java/com/tem/bocai/schedules/ExAggregateDataScriptSchedule.java @@ -0,0 +1,117 @@ +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 ExAggregateDataScriptSchedule { + + /** + * 处理文件路径,确保路径正确 + * @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; + } + + public static void main(String[] args) { + ExAggregateDataScriptSchedule schedule = new ExAggregateDataScriptSchedule(); + schedule.executePythonScript(); + } + + // 每天凌晨2点执行 + @Scheduled(cron = "0 10 6 * * ?") + public void executePythonScript() { + System.out.println("开始执行Python脚本..."); + + // 执行aggregate_data_v7.py脚本,不需要参数 + String[] params = {}; + executePythonScriptWithParams("aggregate_data_v7.py", params); + } + + /** + * 执行带参数的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)); + + // 创建ProcessBuilder并设置工作目录为PyModel + ProcessBuilder pb = new ProcessBuilder(command); + // 设置工作目录为PyModel + pb.directory(new java.io.File("PyModel")); + + // 执行Python脚本 + Process process = pb.start(); + + // 读取脚本输出 + 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(); + } + } +} diff --git a/src/main/java/com/tem/bocai/schedules/GetPredictResultSchedule.java b/src/main/java/com/tem/bocai/schedules/ExBetScriptSchedule.java similarity index 83% rename from src/main/java/com/tem/bocai/schedules/GetPredictResultSchedule.java rename to src/main/java/com/tem/bocai/schedules/ExBetScriptSchedule.java index 8cf4887..7c21161 100644 --- a/src/main/java/com/tem/bocai/schedules/GetPredictResultSchedule.java +++ b/src/main/java/com/tem/bocai/schedules/ExBetScriptSchedule.java @@ -2,12 +2,16 @@ 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 { +public class ExBetScriptSchedule { /** * 处理文件路径,确保路径正确 @@ -25,22 +29,19 @@ public class GetPredictResultSchedule { return filePath; } + public static void main(String[] args) { + ExBetScriptSchedule schedule = new ExBetScriptSchedule(); + schedule.executePythonScript(); + } + // 每天凌晨2点执行 - @Scheduled(cron = "0 0 2 * * ?") + @Scheduled(cron = "0 10 6 * * ?") 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); + // 执行aggregate_data_v7.py脚本,不需要参数 + String[] params = {}; + executePythonScriptWithParams("batch_predict_betting_v7.py", params); } /** @@ -70,8 +71,13 @@ public class GetPredictResultSchedule { System.out.println("执行命令: " + String.join(" ", command)); + // 创建ProcessBuilder并设置工作目录为PyModel + ProcessBuilder pb = new ProcessBuilder(command); + // 设置工作目录为PyModel + pb.directory(new java.io.File("PyModel")); + // 执行Python脚本 - Process process = Runtime.getRuntime().exec(command); + Process process = pb.start(); // 读取脚本输出 BufferedReader reader = new BufferedReader(