窗口查询 时间偏移量

This commit is contained in:
huguanghan 2024-12-19 11:04:42 +08:00
parent 6290e7f527
commit 96bfd7c2c8

View File

@ -164,9 +164,14 @@ public class DataServiceImpl implements DataService {
String windowType = param.getCalFunction();
Map<String, Map<String, Map<String, Object>>> result = new HashMap<>(param.getDevices().size());
List<SnapshotValueQueryParam> deviceFieldList = param.getDevices();
String interval = param.getInterval();
Long offset = calculateRemainder(Long.parseLong(param.getStartTime()), interval);
String offsetSecond = offset/1000 + "s";
interval = interval + ","+offsetSecond;
System.out.println("不能被整除的部分 (毫秒): " + result);
for (SnapshotValueQueryParam item : deviceFieldList) {
//field分为高频和低频查询
Map<String, Map<String, Map<String, Object>>> values = queryWindowsCurveValues(Long.valueOf(item.getDeviceId()), startTime, endTime, param.getInterval(), item.getAttributes(),windowType);
Map<String, Map<String, Map<String, Object>>> values = queryWindowsCurveValues(Long.valueOf(item.getDeviceId()), startTime, endTime, interval, item.getAttributes(),windowType);
result.putAll(values);
}
Long end = System.currentTimeMillis();
@ -405,6 +410,32 @@ public class DataServiceImpl implements DataService {
return tdEngineService.getTimeAvgValue(tableName, attr.toLowerCase(), startTime, endTime);
}
public long calculateRemainder(long startTime, String interval) {
// 解析interval的数值和单位
long intervalInMilliseconds = parseIntervalToMilliseconds(interval);
// 计算不能被整除的部分
return startTime % intervalInMilliseconds;
}
public long parseIntervalToMilliseconds(String interval) {
// 提取间隔的数字和单位
long intervalValue = Long.parseLong(interval.replaceAll("[^0-9]", ""));
char unit = interval.charAt(interval.length() - 1);
// 根据单位转换为毫秒
return switch (unit) {
case 'm' -> // 分钟
intervalValue * 60 * 1000; // 1 分钟 = 60 * 1000 毫秒
case 'h' -> // 小时
intervalValue * 60 * 60 * 1000; // 1 小时 = 60 * 60 * 1000 毫秒
case 'd' -> //
intervalValue * 24 * 60 * 60 * 1000; // 1 = 24 * 60 * 60 * 1000 毫秒
default -> throw new ServiceException("不支持的单位: " + unit);
};
}
private String mappingFunction(String calFunction){
return switch (calFunction) {
case "average" -> "AVG";