|
@@ -1,8 +1,12 @@
|
|
|
package db.service;
|
|
|
|
|
|
-import db.*;
|
|
|
+import db.DBApiEntry;
|
|
|
+import db.DBVal;
|
|
|
+import db.Point;
|
|
|
import db.entity.LogFileMeta;
|
|
|
import db.mapper.LogFileMetaMapper;
|
|
|
+import db.util.ConvertFormat;
|
|
|
+import db.util.SpentTimeCalculator;
|
|
|
import db.util.Util;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.pcap4j.core.*;
|
|
@@ -14,7 +18,10 @@ import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.io.IOException;
|
|
|
import java.text.ParseException;
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
@@ -38,6 +45,18 @@ public class QueryService {
|
|
|
@Value("${server.port}")
|
|
|
private String serverPort;
|
|
|
|
|
|
+ @Value("${ip.host}")
|
|
|
+ private String hostIP;
|
|
|
+
|
|
|
+ @Value("${system.pcap-file.prefix}")
|
|
|
+ private String pcapPrefix;
|
|
|
+
|
|
|
+ @Value("${system.pcap-file.suffix}")
|
|
|
+ private String pcapSuffix;
|
|
|
+
|
|
|
+ @Value("${system.pcap-file.template}")
|
|
|
+ private String pcapTemplate;
|
|
|
+
|
|
|
private DBApiEntry entry;
|
|
|
|
|
|
@PostConstruct
|
|
@@ -45,7 +64,7 @@ public class QueryService {
|
|
|
this.entry = DBApiEntry.initApiEntry(host, port);
|
|
|
}
|
|
|
|
|
|
- public String queryByTimeAndCondition(String start, String end, Map<String, String> tags) throws ParseException,
|
|
|
+ public String queryByTimeWithCondition(String start, String end, Map<String, String> tags) throws ParseException,
|
|
|
IOException, PcapNativeException, NotOpenException {
|
|
|
|
|
|
Point point = new Point(metricName, tags);
|
|
@@ -55,32 +74,41 @@ public class QueryService {
|
|
|
|
|
|
DBApiEntry entry = DBApiEntry.initApiEntry(host, port);
|
|
|
|
|
|
+ SpentTimeCalculator spentTimeCalculator = SpentTimeCalculator.create();
|
|
|
+ spentTimeCalculator.begin();
|
|
|
List<DBVal> lists = entry.getHistRaw(point, startTime, endTime);
|
|
|
+ spentTimeCalculator.end();
|
|
|
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()));
|
|
|
+ log.info("查询ResoDB,查询到条目:{},查询耗时:{}", lists.size(), spentTimeCalculator.getSpendTime());
|
|
|
+
|
|
|
+ spentTimeCalculator.begin();
|
|
|
+ HashMap<Long, ArrayList<Integer>> map = new ConvertFormat().convertToLongAndGetIndex(lists);
|
|
|
+ spentTimeCalculator.end();
|
|
|
+ log.info("记录分片耗时:{}",spentTimeCalculator.getSpendTime());
|
|
|
+
|
|
|
+ ArrayList<byte[]> logItems = new ArrayList<>();
|
|
|
+ for (Long key : map.keySet()) {
|
|
|
+ logItems.addAll(logFileService.querySingleLogFile(key, map.get(key)));
|
|
|
}
|
|
|
- Collections.sort(locs);
|
|
|
|
|
|
- PcapHandle handle = Pcaps.openOffline("./pcap/template.pcap");
|
|
|
- String pcapName = "" + startTime + endTime + System.currentTimeMillis();
|
|
|
- PcapDumper dumper = handle.dumpOpen("./pcap/" + pcapName + ".pcap");
|
|
|
+ spentTimeCalculator.begin();
|
|
|
+ PcapHandle handle = Pcaps.openOffline(pcapTemplate);
|
|
|
+ String pcapName = new StringBuilder().append(System.currentTimeMillis())
|
|
|
+ .append(startTime).append(endTime).toString();
|
|
|
+ PcapDumper dumper = handle.dumpOpen(pcapPrefix + pcapName + pcapSuffix);
|
|
|
|
|
|
- for (Long longIndex : locs) {
|
|
|
- byte[] logItem = logFileService.queryLog(longIndex);
|
|
|
- log.info("查询磁盘,查询到:{}", logItem);
|
|
|
+ for (byte[] logItem : logItems) {
|
|
|
UnknownPacket packet = UnknownPacket.newPacket(logItem, 0, logItem.length);
|
|
|
dumper.dump(packet);
|
|
|
}
|
|
|
+ spentTimeCalculator.end();
|
|
|
+ log.info("生成Pcap文件时间:{}", spentTimeCalculator.getSpendTime());
|
|
|
|
|
|
dumper.close();
|
|
|
handle.close();
|
|
|
|
|
|
StringBuilder returnLink = new StringBuilder();
|
|
|
- returnLink.append("http://").append("127.0.0.1").append(":")
|
|
|
+ returnLink.append("http://").append(hostIP).append(":")
|
|
|
.append(serverPort).append("/download?pcapName=").append(pcapName);
|
|
|
|
|
|
log.info("返回链接:{}", returnLink);
|
|
@@ -92,29 +120,37 @@ public class QueryService {
|
|
|
long startTime = Util.dateStringToUTCMilliSeconds(start);
|
|
|
long endTime = Util.dateStringToUTCMilliSeconds(end);
|
|
|
|
|
|
- log.info("queryByTime,查询条件为:start:{}, end:{}", startTime, endTime);
|
|
|
+ log.debug("queryByTime,查询条件为:start:{}, end:{}", startTime, endTime);
|
|
|
+
|
|
|
+ SpentTimeCalculator spentTimeCalculator = SpentTimeCalculator.create();
|
|
|
+ spentTimeCalculator.begin();
|
|
|
|
|
|
List<LogFileMeta> logFileMetas = logFileMetaMapper.getValidLogFileMetaBetweenTime(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");
|
|
|
+ spentTimeCalculator.end();
|
|
|
+ log.debug("queryByTime,查询sqlite,查询条目:{},查询时间:{}", logFileMetas.size(), spentTimeCalculator.getSpendTime());
|
|
|
|
|
|
- for (LogFileMeta logFileMeta : logFileMetas) {
|
|
|
- List<byte[]> bytes = logFileService.queryLogBetweenTime(logFileMeta, startTime, endTime);
|
|
|
+ PcapHandle handle = Pcaps.openOffline(pcapTemplate);
|
|
|
+ String pcapName = new StringBuilder().append(System.currentTimeMillis())
|
|
|
+ .append(startTime).append(endTime).toString();
|
|
|
+ PcapDumper dumper = handle.dumpOpen(pcapPrefix + pcapName + pcapSuffix);
|
|
|
|
|
|
+ spentTimeCalculator.begin();
|
|
|
+ for (LogFileMeta logFileMeta : logFileMetas) {
|
|
|
+ List<byte[]> bytes = logFileService.queryLogBetweenTimeRange(logFileMeta, startTime, endTime);
|
|
|
for (byte[] item : bytes) {
|
|
|
UnknownPacket packet = UnknownPacket.newPacket(item, 0, item.length);
|
|
|
dumper.dump(packet);
|
|
|
}
|
|
|
}
|
|
|
+ spentTimeCalculator.end();
|
|
|
+ log.info("queryByTime,查询磁盘并生成pcap总时间:{}", spentTimeCalculator.getSpendTime());
|
|
|
+
|
|
|
dumper.close();
|
|
|
handle.close();
|
|
|
|
|
|
-
|
|
|
StringBuilder returnLink = new StringBuilder();
|
|
|
- returnLink.append("http://").append("127.0.0.1").append(":")
|
|
|
+ returnLink.append("http://").append(hostIP).append(":")
|
|
|
.append(serverPort).append("/download?pcapName=").append(pcapName);
|
|
|
|
|
|
log.info("返回链接:{}", returnLink);
|