feat(可回溯): 可回溯

增加视频生成
This commit is contained in:
BrandWang
2023-03-28 17:12:46 +08:00
parent 22d2dfe7b6
commit d78492af40
@@ -1,5 +1,7 @@
package com.wabestway.recall.web; package com.wabestway.recall.web;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wabestway.recall.model.Order; import com.wabestway.recall.model.Order;
import com.wabestway.recall.model.OrderList; import com.wabestway.recall.model.OrderList;
import com.wabestway.recall.model.Record; import com.wabestway.recall.model.Record;
@@ -11,7 +13,13 @@ import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
@@ -67,4 +75,75 @@ public class OrderController {
}); });
}); });
} }
@GetMapping("/ffmpeg")
public Mono<ResObj> createMp4() {
Order order = new Order();
order.setComplete("1");
ExampleMatcher matcher = ExampleMatcher.matching()
.withIgnoreNullValues().withIgnorePaths("createAt", "last", "page", "pageSize");
Example<Order> ep = Example.of(order, matcher);
Sort s = Sort.by("createAt");
Flux<Order> flux = orderRepository.findAll(ep, s);
flux.subscribe(fo -> {
Flux<Record> rs = recordRepository.findAllByTraceIdOrderByCreateAtAsc(fo.getTraceId());
Mono<List<List<String>>> es = rs.map(rr -> rr.getEvents()).collectList();
Mono<List<String>> events = es.flatMapIterable(lists -> lists).flatMapIterable(list -> list).collectList();
events.subscribe(e -> {
JSONArray array = JSONArray.parseArray(JSON.toJSONString(e));
try {
createVideo(fo.getOrderId(), array.toJSONString());
} catch (IOException ex) {
ex.printStackTrace();
}
});
});
return Mono.just(ResObj.ok());
}
void createVideo(String dir, String jsonData) throws IOException {
// 创建临时目录
Path tempDir = null;
try {
tempDir = Files.createTempDirectory("dir");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Created temporary directory: " + tempDir);
try {
File tempFile = File.createTempFile("traces", ".json", tempDir.toFile());
FileWriter fw = new FileWriter(tempFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jsonData);
bw.flush();
bw.close();
fw.close();
String[] command = {"ts-node", "/opt/trace-transform/src/index.ts"};
ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.directory(tempDir.toFile());
// 在临时目录下执行命令
Process process = processBuilder.start();
// 读取命令输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
// 等待命令执行完成
int exitCode = process.waitFor();
System.out.println("Command exited with code " + exitCode);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
// 删除临时目录
Files.walk(tempDir)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
} }