优化周期统计
This commit is contained in:
parent
f5c3f3ca6f
commit
4cc08b53a0
|
|
@ -0,0 +1,66 @@
|
|||
package org.jetlinks.community;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class Interval {
|
||||
|
||||
public static String year = "y";
|
||||
public static String quarter = "q";
|
||||
public static String month = "M";
|
||||
public static String weeks = "w";
|
||||
public static String days = "d";
|
||||
public static String hours = "h";
|
||||
public static String minutes = "m";
|
||||
public static String seconds = "s";
|
||||
|
||||
private BigDecimal number;
|
||||
|
||||
private String expression;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return (number) + expression;
|
||||
}
|
||||
|
||||
public static Interval ofSeconds(int seconds) {
|
||||
return of(seconds, Interval.seconds);
|
||||
}
|
||||
|
||||
public static Interval ofDays(int days) {
|
||||
return of(days, Interval.days);
|
||||
}
|
||||
|
||||
public static Interval ofHours(int hours) {
|
||||
return of(hours, Interval.hours);
|
||||
}
|
||||
|
||||
public static Interval ofMonth(int month) {
|
||||
return of(month, Interval.month);
|
||||
}
|
||||
|
||||
public static Interval of(int month, String expression) {
|
||||
return new Interval(new BigDecimal(month), expression);
|
||||
}
|
||||
|
||||
public static Interval of(String expr) {
|
||||
|
||||
char[] number = new char[32];
|
||||
int numIndex = 0;
|
||||
for (char c : expr.toCharArray()) {
|
||||
if (c == '-' || c == '.' || (c >= '0' && c <= '9')) {
|
||||
number[numIndex++] = c;
|
||||
continue;
|
||||
}
|
||||
BigDecimal val = new BigDecimal(number, 0, numIndex);
|
||||
return new Interval(val, expr.substring(numIndex));
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("can not parse interval expression:" + expr);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -38,6 +38,17 @@ public interface ValueObject {
|
|||
.map(TimeUtils::parse);
|
||||
}
|
||||
|
||||
default Optional<Interval> getInterval(String name) {
|
||||
return getString(name)
|
||||
.map(Interval::of);
|
||||
}
|
||||
|
||||
default Interval getInterval(String name,Interval defaultValue) {
|
||||
return getString(name)
|
||||
.map(Interval::of)
|
||||
.orElse(defaultValue);
|
||||
}
|
||||
|
||||
default Duration getDuration(String name, Duration defaultValue) {
|
||||
return getDuration(name)
|
||||
.orElse(defaultValue);
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ public class DefaultAggregationService implements AggregationService {
|
|||
|
||||
protected BucketAggregationsStructure convertAggGroupTimeStructure(AggregationQueryParam param) {
|
||||
BucketAggregationsStructure structure = new BucketAggregationsStructure();
|
||||
structure.setInterval(durationFormat(param.getGroupByTime().getInterval()));
|
||||
structure.setInterval(param.getGroupByTime().toString());
|
||||
structure.setType(BucketType.DATE_HISTOGRAM);
|
||||
structure.setFormat(param.getGroupByTime().getFormat());
|
||||
structure.setName(param.getGroupByTime().getAlias());
|
||||
|
|
@ -241,11 +241,11 @@ public class DefaultAggregationService implements AggregationService {
|
|||
|
||||
private static long calculateStartWithTime(AggregationQueryParam param) {
|
||||
long startWithParam = param.getStartWithTime();
|
||||
if (param.getGroupByTime() != null && param.getGroupByTime().getInterval() != null) {
|
||||
long timeInterval = param.getGroupByTime().getInterval().toMillis() * param.getLimit();
|
||||
long tempStartWithParam = param.getEndWithTime() - timeInterval;
|
||||
startWithParam = Math.max(tempStartWithParam, startWithParam);
|
||||
}
|
||||
// if (param.getGroupByTime() != null && param.getGroupByTime().getInterval() != null) {
|
||||
// long timeInterval = param.getGroupByTime().getInterval().toMillis() * param.getLimit();
|
||||
// long tempStartWithParam = param.getEndWithTime() - timeInterval;
|
||||
// startWithParam = Math.max(tempStartWithParam, startWithParam);
|
||||
// }
|
||||
return startWithParam;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package org.jetlinks.community.gateway.monitor.measurements;
|
||||
|
||||
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
|
||||
import org.jetlinks.community.Interval;
|
||||
import org.jetlinks.community.timeseries.query.Aggregation;
|
||||
import org.jetlinks.core.metadata.ConfigMetadata;
|
||||
import org.jetlinks.core.metadata.DataType;
|
||||
|
|
@ -137,7 +138,7 @@ class DeviceGatewayMeasurement extends StaticMeasurement {
|
|||
|
||||
return AggregationQueryParam.of()
|
||||
.agg(property, parameter.get("agg", Aggregation.class).orElse(defaultAgg))
|
||||
.groupBy(parameter.getDuration("time").orElse(Duration.ofHours(1)),
|
||||
.groupBy(parameter.getInterval("time").orElse(Interval.ofHours(1)),
|
||||
"time",
|
||||
parameter.getString("format").orElse("MM-dd:HH"))
|
||||
.filter(query -> query
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import lombok.Getter;
|
|||
import lombok.Setter;
|
||||
import org.hswebframework.ezorm.core.dsl.Query;
|
||||
import org.hswebframework.ezorm.core.param.QueryParam;
|
||||
import org.jetlinks.community.Interval;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -115,11 +116,11 @@ public class AggregationQueryParam {
|
|||
return agg(property, Aggregation.MIN);
|
||||
}
|
||||
|
||||
public AggregationQueryParam groupBy(Duration time, String alias, String format) {
|
||||
public AggregationQueryParam groupBy(Interval time, String alias, String format) {
|
||||
return groupBy(new TimeGroup(time, alias, format));
|
||||
}
|
||||
|
||||
public AggregationQueryParam groupBy(Duration time, String format) {
|
||||
public AggregationQueryParam groupBy(Interval time, String format) {
|
||||
return groupBy(time, "time", format);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
|
|||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.jetlinks.community.Interval;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
|
|
@ -16,7 +17,7 @@ public class TimeGroup {
|
|||
private String property = "timestamp";
|
||||
|
||||
//时间分组间隔,如: 1d , 30s
|
||||
private Duration interval;
|
||||
private Interval interval;
|
||||
|
||||
private String alias;
|
||||
|
||||
|
|
@ -25,7 +26,7 @@ public class TimeGroup {
|
|||
*/
|
||||
private String format;
|
||||
|
||||
public TimeGroup(Duration interval, String alias, String format) {
|
||||
public TimeGroup(Interval interval, String alias, String format) {
|
||||
this.interval = interval;
|
||||
this.alias = alias;
|
||||
this.format = format;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.jetlinks.community.device.measurements.message;
|
||||
|
||||
import org.jetlinks.community.Interval;
|
||||
import org.jetlinks.core.metadata.ConfigMetadata;
|
||||
import org.jetlinks.core.metadata.DataType;
|
||||
import org.jetlinks.core.metadata.DefaultConfigMetadata;
|
||||
|
|
@ -112,7 +113,7 @@ class DeviceMessageMeasurement extends StaticMeasurement {
|
|||
|
||||
return AggregationQueryParam.of()
|
||||
.sum("count")
|
||||
.groupBy(parameter.getDuration("time", Duration.ofHours(1)),
|
||||
.groupBy(parameter.getInterval("time", Interval.ofHours(1)),
|
||||
parameter.getString("format", "MM月dd日 HH时"))
|
||||
.filter(query ->
|
||||
query.where("name", "message-count")
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.jetlinks.community.device.measurements.status;
|
||||
|
||||
import org.jetlinks.community.Interval;
|
||||
import org.jetlinks.core.message.MessageType;
|
||||
import org.jetlinks.core.metadata.ConfigMetadata;
|
||||
import org.jetlinks.core.metadata.DataType;
|
||||
|
|
@ -90,8 +91,7 @@ class DeviceStatusChangeMeasurement extends StaticMeasurement {
|
|||
|
||||
return AggregationQueryParam.of()
|
||||
.sum("count")
|
||||
.groupBy(parameter.getDuration("time").orElse(Duration.ofHours(1)),
|
||||
"time",
|
||||
.groupBy(parameter.getInterval("time").orElse(Interval.ofHours(1)),
|
||||
parameter.getString("format").orElse("MM月dd日 HH时"))
|
||||
.filter(query ->
|
||||
query.where("name", parameter.getString("type").orElse("online"))
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package org.jetlinks.community.device.measurements.status;
|
||||
|
||||
import org.jetlinks.community.Interval;
|
||||
import org.jetlinks.core.metadata.ConfigMetadata;
|
||||
import org.jetlinks.core.metadata.DataType;
|
||||
import org.jetlinks.core.metadata.DefaultConfigMetadata;
|
||||
|
|
@ -83,7 +84,7 @@ class DeviceStatusRecordMeasurement
|
|||
)
|
||||
.from(parameter.getDate("from").orElse(Date.from(LocalDateTime.now().plusDays(-30).atZone(ZoneId.systemDefault()).toInstant())))
|
||||
.to(parameter.getDate("to").orElse(new Date()))
|
||||
.groupBy(parameter.getDuration("time").orElse(Duration.ofDays(1)),
|
||||
.groupBy(parameter.getInterval("time").orElse(Interval.ofDays(1)),
|
||||
parameter.getString("format").orElse("yyyy年MM月dd日"))
|
||||
.limit(parameter.getInt("limit").orElse(10))
|
||||
.execute(timeSeriesManager.getService(DeviceTimeSeriesMetric.deviceMetrics())::aggregation)
|
||||
|
|
|
|||
Loading…
Reference in New Issue