计算量增加求和函数
This commit is contained in:
parent
bc3d2b7f09
commit
335ee75bab
@ -0,0 +1,59 @@
|
||||
package com.das.modules.calc.functions;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.das.modules.cache.domain.DeviceInfoCache;
|
||||
import com.das.modules.cache.service.CacheService;
|
||||
import com.das.modules.data.service.DataService;
|
||||
import com.googlecode.aviator.runtime.function.AbstractFunction;
|
||||
import com.googlecode.aviator.runtime.type.AviatorNil;
|
||||
import com.googlecode.aviator.runtime.type.AviatorObject;
|
||||
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Aviator扩展函数 - 获取时间维度内最早的一条数据
|
||||
* 函数格式: topv(deviceId, attr, timedim)
|
||||
* timedim: day - 天, month - 月, year - 年
|
||||
* 返回值:数值, nil - 获取错误
|
||||
*/
|
||||
@Slf4j
|
||||
public class FunctionSumValue extends AbstractFunction {
|
||||
|
||||
private DataService dataService = null;
|
||||
private CacheService cacheService = null;
|
||||
|
||||
|
||||
public FunctionSumValue(DataService dataService, CacheService cacheService) {
|
||||
this.dataService = dataService;
|
||||
this.cacheService = cacheService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "sumv";
|
||||
}
|
||||
|
||||
@Override
|
||||
public AviatorObject call(Map<String, Object> env, AviatorObject deviceCode, AviatorObject attr) {
|
||||
//设备Code
|
||||
String code = (String)deviceCode.getValue(env);
|
||||
String attrName = (String)attr.getValue(env);
|
||||
|
||||
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheByCode(code);
|
||||
if (deviceInfoCache == null) {
|
||||
return AviatorNil.NIL;
|
||||
}
|
||||
Double value = dataService.getTimeTopValue(deviceInfoCache.getDeviceId(), attrName, null, null);
|
||||
if (value == null){
|
||||
return AviatorNil.NIL;
|
||||
}
|
||||
//未找到缓存,查询时序API获取数据
|
||||
return AviatorRuntimeJavaType.valueOf(value);
|
||||
}
|
||||
|
||||
}
|
@ -17,5 +17,6 @@ public interface DataService {
|
||||
|
||||
void updateCalFieldData(List<CalculateRTData> values);
|
||||
|
||||
Double getTimeTopValue(Long devcieId, String attr, long startTime, long endTime);
|
||||
Double getTimeTopValue(Long devcieId, String attr, Long startTime, Long endTime);
|
||||
Double getTimeSumValue(Long devcieId, String attr, Long startTime, Long endTime);
|
||||
}
|
||||
|
@ -784,7 +784,7 @@ public class TDEngineService {
|
||||
}
|
||||
}
|
||||
|
||||
public Double getTimeTopValue(String tableName, String attr, long startTime, long endTime) {
|
||||
public Double getTimeTopValue(String tableName, String attr, Long startTime, Long endTime) {
|
||||
StringBuffer sb = new StringBuffer(256);
|
||||
sb.append("select ");
|
||||
sb.append("first(");
|
||||
@ -792,8 +792,10 @@ public class TDEngineService {
|
||||
sb.append(") as value");
|
||||
sb.append(" from ");
|
||||
sb.append(tableName);
|
||||
sb.append(" where ");
|
||||
sb.append(String.format(" updatetime >= %d and updatetime < %d ", startTime, endTime));
|
||||
if (startTime != null && endTime != null) {
|
||||
sb.append(" where ");
|
||||
sb.append(String.format(" updatetime >= %d and updatetime < %d ", startTime, endTime));
|
||||
}
|
||||
Double result = null;
|
||||
try (Connection conn = hikariDataSource.getConnection();
|
||||
Statement smt = conn.createStatement();
|
||||
@ -855,4 +857,29 @@ public class TDEngineService {
|
||||
log.error("修改Tag值失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Double getTimeSumValue(String tableName, String attr, Long startTime, Long endTime) {
|
||||
StringBuffer sb = new StringBuffer(256);
|
||||
sb.append("select ");
|
||||
sb.append("sum(");
|
||||
sb.append(attr);
|
||||
sb.append(") as value");
|
||||
sb.append(" from ");
|
||||
sb.append(tableName);
|
||||
if (startTime != null && endTime != null) {
|
||||
sb.append(" where ");
|
||||
sb.append(String.format(" updatetime >= %d and updatetime < %d ", startTime, endTime));
|
||||
}
|
||||
Double result = null;
|
||||
try (Connection conn = hikariDataSource.getConnection();
|
||||
Statement smt = conn.createStatement();
|
||||
ResultSet rs = smt.executeQuery(sb.toString())) {
|
||||
if (rs.next()) {
|
||||
result = rs.getDouble("value");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取数据异常", e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -268,7 +268,7 @@ public class DataServiceImpl implements DataService {
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Double getTimeTopValue(Long devcieId, String attr, long startTime, long endTime){
|
||||
public Double getTimeTopValue(Long devcieId, String attr, Long startTime, Long endTime){
|
||||
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(devcieId);
|
||||
if (deviceInfoCache == null) {
|
||||
return null;
|
||||
@ -283,4 +283,21 @@ public class DataServiceImpl implements DataService {
|
||||
}
|
||||
return tdEngineService.getTimeTopValue(tableName, attr, startTime, endTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getTimeSumValue(Long devcieId, String attr, Long startTime, Long endTime) {
|
||||
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(devcieId);
|
||||
if (deviceInfoCache == null) {
|
||||
return null;
|
||||
}
|
||||
String tableName = "";
|
||||
if (cacheService.getIotModelCache().isHighSpeed(deviceInfoCache.getIotModelId(), attr)){
|
||||
tableName = String.format("h_%s", deviceInfoCache.getDeviceId());
|
||||
} else if (cacheService.getIotModelCache().isLowSpeed(deviceInfoCache.getIotModelId(), attr)){
|
||||
tableName = String.format("l_%s", deviceInfoCache.getDeviceId());
|
||||
} else if (cacheService.getIotModelCache().isCalculate(deviceInfoCache.getIotModelId(), attr)){
|
||||
tableName = String.format("c_%s", deviceInfoCache.getDeviceId());
|
||||
}
|
||||
return tdEngineService.getTimeSumValue(tableName, attr, startTime, endTime);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user