脚本新增函数offsetDate, avgv
This commit is contained in:
parent
b42d204623
commit
a25df85d17
@ -0,0 +1,76 @@
|
|||||||
|
package com.das.modules.calc.functions;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateField;
|
||||||
|
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.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aviator扩展函数 - 获取时间维度内平均
|
||||||
|
* 函数格式: avg(deviceId, attr, timedim)
|
||||||
|
* timedim: day - 天, month - 月, year - 年
|
||||||
|
* 返回值:数值, nil - 获取错误
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class FunctionAvgValue extends AbstractFunction {
|
||||||
|
|
||||||
|
private DataService dataService = null;
|
||||||
|
private CacheService cacheService = null;
|
||||||
|
|
||||||
|
|
||||||
|
public FunctionAvgValue(DataService dataService, CacheService cacheService) {
|
||||||
|
this.dataService = dataService;
|
||||||
|
this.cacheService = cacheService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "avgv";
|
||||||
|
}
|
||||||
|
|
||||||
|
@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.getTimeAvgValue(deviceInfoCache.getDeviceId(), attrName, null, null);
|
||||||
|
if (value == null){
|
||||||
|
return AviatorNil.NIL;
|
||||||
|
}
|
||||||
|
//未找到缓存,查询时序API获取数据
|
||||||
|
return AviatorRuntimeJavaType.valueOf(value);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public AviatorObject call(Map<String, Object> env, AviatorObject deviceCode, AviatorObject attr, AviatorObject startTimeData, AviatorObject endTimeData) {
|
||||||
|
//设备Code
|
||||||
|
String code = (String)deviceCode.getValue(env);
|
||||||
|
String attrName = (String)attr.getValue(env);
|
||||||
|
Date startTime = (Date)startTimeData.getValue(env);
|
||||||
|
Date endTime = (Date)endTimeData.getValue(env);
|
||||||
|
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheByCode(code);
|
||||||
|
if (deviceInfoCache == null) {
|
||||||
|
return AviatorNil.NIL;
|
||||||
|
}
|
||||||
|
Double value = dataService.getTimeSumValue(deviceInfoCache.getDeviceId(), attrName, startTime.getTime(), endTime.getTime());
|
||||||
|
if (value == null){
|
||||||
|
return AviatorNil.NIL;
|
||||||
|
}
|
||||||
|
//未找到缓存,查询时序API获取数据
|
||||||
|
return AviatorRuntimeJavaType.valueOf(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.das.modules.calc.functions;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateField;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import com.googlecode.aviator.runtime.function.AbstractFunction;
|
||||||
|
import com.googlecode.aviator.runtime.type.AviatorObject;
|
||||||
|
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aviator扩展函数 - 获取时间维度的起始时间
|
||||||
|
* 函数格式: beginOf(Date, TimeDim)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class FunctionOffsetDate extends AbstractFunction {
|
||||||
|
|
||||||
|
public FunctionOffsetDate() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "offsetDate";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AviatorObject call(Map<String, Object> env, AviatorObject dateData, AviatorObject dimData, AviatorObject offsetData) {
|
||||||
|
Date date = (Date) dateData.getValue(env);
|
||||||
|
String dim = (String) dimData.getValue(env);
|
||||||
|
Integer offset = (Integer) offsetData.getValue(env);
|
||||||
|
|
||||||
|
Date result = null;
|
||||||
|
switch (dim) {
|
||||||
|
case "day":
|
||||||
|
result = DateUtil.offset(date, DateField.DAY_OF_MONTH, offset);
|
||||||
|
break;
|
||||||
|
case "month":
|
||||||
|
result = DateUtil.offset(date, DateField.MONTH, offset);
|
||||||
|
break;
|
||||||
|
case "year":
|
||||||
|
result = DateUtil.offset(date, DateField.YEAR, offset);
|
||||||
|
break;
|
||||||
|
case "hour":
|
||||||
|
result = DateUtil.offset(date, DateField.HOUR, offset);
|
||||||
|
break;
|
||||||
|
case "minute":
|
||||||
|
result = DateUtil.offset(date, DateField.MINUTE, offset);
|
||||||
|
break;
|
||||||
|
case "second":
|
||||||
|
result = DateUtil.offset(date, DateField.SECOND, offset);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
log.error("不支持的维度: {}", dim);
|
||||||
|
}
|
||||||
|
return AviatorRuntimeJavaType.valueOf(result);
|
||||||
|
}
|
||||||
|
}
|
@ -128,6 +128,12 @@ public class CalcService {
|
|||||||
|
|
||||||
FunctionEndOfDate endOfDate = new FunctionEndOfDate();
|
FunctionEndOfDate endOfDate = new FunctionEndOfDate();
|
||||||
aviator.addFunction(endOfDate);
|
aviator.addFunction(endOfDate);
|
||||||
|
|
||||||
|
FunctionAvgValue avgValue = new FunctionAvgValue(dataService, cacheService);
|
||||||
|
aviator.addFunction(avgValue);
|
||||||
|
|
||||||
|
FunctionOffsetDate offsetDate = new FunctionOffsetDate();
|
||||||
|
aviator.addFunction(offsetDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,6 +17,8 @@ public interface DataService {
|
|||||||
|
|
||||||
void updateCalFieldData(List<CalculateRTData> values);
|
void updateCalFieldData(List<CalculateRTData> values);
|
||||||
|
|
||||||
Double getTimeTopValue(Long devcieId, String attr, Long startTime, Long endTime);
|
Double getTimeTopValue(Long deviceId, String attr, Long startTime, Long endTime);
|
||||||
Double getTimeSumValue(Long devcieId, String attr, Long startTime, Long endTime);
|
Double getTimeSumValue(Long deviceId, String attr, Long startTime, Long endTime);
|
||||||
|
|
||||||
|
Double getTimeAvgValue(Long deviceId, String attrName, Long startTime, Long endTime);
|
||||||
}
|
}
|
||||||
|
@ -905,4 +905,52 @@ public class TDEngineService {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Double getTimeAvgValue(String tableName, String attr, Long startTime, Long endTime) {
|
||||||
|
StringBuffer sb = new StringBuffer(256);
|
||||||
|
sb.append("select ");
|
||||||
|
sb.append("avg(");
|
||||||
|
sb.append(attr);
|
||||||
|
sb.append(")");
|
||||||
|
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 = 0.0;
|
||||||
|
try (Connection conn = hikariDataSource.getConnection();
|
||||||
|
Statement smt = conn.createStatement();
|
||||||
|
ResultSet rs = smt.executeQuery(sb.toString())) {
|
||||||
|
if (rs.next()) {
|
||||||
|
result = rs.getDouble(1);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取数据异常",e);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getTimeAvgCalcValue(String tableName, String attr, Long startTime, Long endTime) {
|
||||||
|
StringBuffer sb = new StringBuffer(256);
|
||||||
|
sb.append("select ");
|
||||||
|
sb.append("avg(datavalue)");
|
||||||
|
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 = 0.0;
|
||||||
|
try (Connection conn = hikariDataSource.getConnection();
|
||||||
|
Statement smt = conn.createStatement();
|
||||||
|
ResultSet rs = smt.executeQuery(sb.toString())) {
|
||||||
|
if (rs.next()) {
|
||||||
|
result = rs.getDouble(1);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("获取数据异常",e);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -268,8 +268,8 @@ public class DataServiceImpl implements DataService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Double getTimeTopValue(Long devcieId, String attr, Long startTime, Long endTime){
|
public Double getTimeTopValue(Long deviceId, String attr, Long startTime, Long endTime){
|
||||||
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(devcieId);
|
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(deviceId);
|
||||||
if (deviceInfoCache == null) {
|
if (deviceInfoCache == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -285,8 +285,8 @@ public class DataServiceImpl implements DataService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Double getTimeSumValue(Long devcieId, String attr, Long startTime, Long endTime) {
|
public Double getTimeSumValue(Long deviceId, String attr, Long startTime, Long endTime) {
|
||||||
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(devcieId);
|
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(deviceId);
|
||||||
if (deviceInfoCache == null) {
|
if (deviceInfoCache == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -301,4 +301,22 @@ public class DataServiceImpl implements DataService {
|
|||||||
}
|
}
|
||||||
return tdEngineService.getTimeSumValue(tableName, attr, startTime, endTime);
|
return tdEngineService.getTimeSumValue(tableName, attr, startTime, endTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Double getTimeAvgValue(Long deviceId, String attr, Long startTime, Long endTime) {
|
||||||
|
DeviceInfoCache deviceInfoCache = cacheService.getEquipmentCache().getDeviceInfoCacheById(deviceId);
|
||||||
|
if (deviceInfoCache == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String tableName = "";
|
||||||
|
if (cacheService.getIotModelCache().isCalculate(deviceInfoCache.getIotModelId(), attr)){
|
||||||
|
tableName = String.format("c_%s_%s", deviceInfoCache.getDeviceId(), attr.toLowerCase());
|
||||||
|
return tdEngineService.getTimeAvgCalcValue(tableName, attr, startTime, endTime);
|
||||||
|
} else if (cacheService.getIotModelCache().isHighSpeed(deviceInfoCache.getIotModelId(), attr.toLowerCase())){
|
||||||
|
tableName = String.format("h_%s", deviceInfoCache.getDeviceId());
|
||||||
|
} else if (cacheService.getIotModelCache().isLowSpeed(deviceInfoCache.getIotModelId(), attr.toLowerCase())){
|
||||||
|
tableName = String.format("l_%s", deviceInfoCache.getDeviceId(), attr.toLowerCase());
|
||||||
|
}
|
||||||
|
return tdEngineService.getTimeAvgValue(tableName, attr, startTime, endTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user