|
@@ -5,6 +5,8 @@ import db.entity.LogFileMeta;
|
|
|
import db.mapper.LogFileMetaMapper;
|
|
|
import db.util.Util;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.pcap4j.core.*;
|
|
|
+import org.pcap4j.packet.UnknownPacket;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -33,6 +35,9 @@ public class QueryService {
|
|
|
@Value("${reso-db.metric-name}")
|
|
|
private String metricName;
|
|
|
|
|
|
+ @Value("${server.port}")
|
|
|
+ private String serverPort;
|
|
|
+
|
|
|
private DBApiEntry entry;
|
|
|
|
|
|
@PostConstruct
|
|
@@ -40,10 +45,9 @@ public class QueryService {
|
|
|
this.entry = DBApiEntry.initApiEntry(host, port);
|
|
|
}
|
|
|
|
|
|
- public List<byte[]> queryByTimeAndCondition(String start, String end, Map<String, String> tags) throws ParseException,
|
|
|
- IOException {
|
|
|
+ public String queryByTimeAndCondition(String start, String end, Map<String, String> tags) throws ParseException,
|
|
|
+ IOException, PcapNativeException, NotOpenException {
|
|
|
|
|
|
- List<byte[]> res = new ArrayList<>();
|
|
|
Point point = new Point(metricName, tags);
|
|
|
|
|
|
long startTime = Util.dateStringToUTCMilliSeconds(start);
|
|
@@ -54,23 +58,36 @@ public class QueryService {
|
|
|
List<DBVal> lists = entry.getHistRaw(point, startTime, endTime);
|
|
|
log.info("queryByTimeAndCondition,查询条件为:start:{}, end:{}, point:{}", startTime, endTime, point);
|
|
|
log.info("查询ResoDB,查询到:{}", lists);
|
|
|
-
|
|
|
// 根据list查对应报文位置
|
|
|
List<Long> locs = new ArrayList<>();
|
|
|
for (DBVal dbVal : lists) {
|
|
|
locs.add(Double.doubleToRawLongBits(dbVal.getValue()));
|
|
|
}
|
|
|
Collections.sort(locs);
|
|
|
+
|
|
|
+ PcapHandle handle = Pcaps.openOffline("./pcap/template.pcap");
|
|
|
+ String pcapName = "" + startTime + endTime + System.currentTimeMillis();
|
|
|
+ PcapDumper dumper = handle.dumpOpen("./pcap/" + pcapName + ".pcap");
|
|
|
+
|
|
|
for (Long longIndex : locs) {
|
|
|
- byte[] log = logFileService.queryLog(longIndex);
|
|
|
- res.add(log);
|
|
|
+ byte[] logItem = logFileService.queryLog(longIndex);
|
|
|
+ log.info("查询磁盘,查询到:{}", logItem);
|
|
|
+ UnknownPacket packet = UnknownPacket.newPacket(logItem, 0, logItem.length);
|
|
|
+ dumper.dump(packet);
|
|
|
}
|
|
|
- log.info("查询磁盘,查询到:{}", res);
|
|
|
- return res;
|
|
|
+
|
|
|
+ dumper.close();
|
|
|
+ handle.close();
|
|
|
+
|
|
|
+ StringBuilder returnLink = new StringBuilder();
|
|
|
+ returnLink.append("http://").append("127.0.0.1").append(":")
|
|
|
+ .append(serverPort).append("/download?pcapName=").append(pcapName);
|
|
|
+
|
|
|
+ log.info("返回链接:{}", returnLink);
|
|
|
+ return String.valueOf(returnLink);
|
|
|
}
|
|
|
|
|
|
- public List<byte[]> queryByTime(String start, String end) throws ParseException, IOException {
|
|
|
- List<byte[]> res = new ArrayList<>();
|
|
|
+ public String queryByTime(String start, String end) throws ParseException, IOException, PcapNativeException, NotOpenException {
|
|
|
|
|
|
long startTime = Util.dateStringToUTCMilliSeconds(start);
|
|
|
long endTime = Util.dateStringToUTCMilliSeconds(end);
|
|
@@ -79,11 +96,29 @@ public class QueryService {
|
|
|
|
|
|
List<LogFileMeta> logFileMetas = logFileMetaMapper.getLogFileMetaBetweenTime(startTime, endTime);
|
|
|
log.info("queryByTime,查询B+树,查询到:{}", logFileMetas);
|
|
|
+
|
|
|
+ PcapHandle handle = Pcaps.openOffline("./pcap/udp.pcap");
|
|
|
+ String pcapName = "" + startTime + endTime + System.currentTimeMillis();
|
|
|
+ PcapDumper dumper = handle.dumpOpen("./pcap/" + pcapName + ".pcap");
|
|
|
+
|
|
|
for (LogFileMeta logFileMeta : logFileMetas) {
|
|
|
List<byte[]> bytes = logFileService.queryLogBetweenTime(logFileMeta, startTime, endTime);
|
|
|
- res.addAll(bytes);
|
|
|
+
|
|
|
+ for (byte[] item : bytes) {
|
|
|
+ UnknownPacket packet = UnknownPacket.newPacket(item, 0, item.length);
|
|
|
+ dumper.dump(packet);
|
|
|
+ }
|
|
|
+
|
|
|
+ dumper.close();
|
|
|
+ handle.close();
|
|
|
}
|
|
|
- log.info("查询磁盘,查询到:{}", res);
|
|
|
- return res;
|
|
|
+
|
|
|
+
|
|
|
+ StringBuilder returnLink = new StringBuilder();
|
|
|
+ returnLink.append("http://").append("127.0.0.1").append(":")
|
|
|
+ .append(serverPort).append("/download?pcapName=").append(pcapName);
|
|
|
+
|
|
|
+ log.info("返回链接:{}", returnLink);
|
|
|
+ return String.valueOf(returnLink);
|
|
|
}
|
|
|
}
|