diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/AggInterval.java b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/AggInterval.java index d6d1aca6a8..b534002885 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/AggInterval.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/AggInterval.java @@ -31,6 +31,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; @JsonSubTypes.Type(value = WeekInterval.class, name = "WEEK"), @JsonSubTypes.Type(value = WeekSunSatInterval.class, name = "WEEK_SUN_SAT"), @JsonSubTypes.Type(value = MonthInterval.class, name = "MONTH"), + @JsonSubTypes.Type(value = QuarterInterval.class, name = "QUARTER"), @JsonSubTypes.Type(value = YearInterval.class, name = "YEAR"), @JsonSubTypes.Type(value = CustomInterval.class, name = "CUSTOM") }) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/AggIntervalType.java b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/AggIntervalType.java index 62185127ed..1d39f14a03 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/AggIntervalType.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/AggIntervalType.java @@ -22,6 +22,7 @@ public enum AggIntervalType { WEEK, WEEK_SUN_SAT, MONTH, + QUARTER, YEAR, CUSTOM diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/BaseAggInterval.java b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/BaseAggInterval.java index 08bea68d5b..71ccab5f95 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/BaseAggInterval.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/BaseAggInterval.java @@ -47,6 +47,7 @@ public abstract class BaseAggInterval implements AggInterval { case DAY -> Duration.ofDays(1).toMillis(); case WEEK, WEEK_SUN_SAT -> Duration.ofDays(7L).toMillis(); case MONTH -> Duration.ofDays(Math.round(30)).toMillis(); // average + case QUARTER -> Duration.ofDays(Math.round(91)).toMillis(); case YEAR -> Duration.ofDays(Math.round(365)).toMillis(); default -> throw new IllegalArgumentException("Unsupported type: " + getType()); }; @@ -88,6 +89,7 @@ public abstract class BaseAggInterval implements AggInterval { case WEEK -> alignByWeeks(reference, DayOfWeek.MONDAY, next); case WEEK_SUN_SAT -> alignByWeeks(reference, DayOfWeek.SUNDAY, next); case MONTH -> alignByMonths(reference, next); + case QUARTER -> alignByQuarters(reference, next); case YEAR -> alignByYears(reference, next); default -> throw new IllegalArgumentException("Unsupported interval type: " + getType()); }; @@ -114,6 +116,16 @@ public abstract class BaseAggInterval implements AggInterval { return next ? base.plusMonths(1) : base; } + private ZonedDateTime alignByQuarters(ZonedDateTime now, boolean next) { + int month = now.getMonthValue(); + int quarterStartMonth = ((month - 1) / 3) * 3 + 1; // 1, 4, 7, 10 + ZonedDateTime base = ZonedDateTime.of( + LocalDate.of(now.getYear(), quarterStartMonth, 1), + LocalTime.MIDNIGHT, + now.getZone()); + return next ? base.plusMonths(3) : base; + } + private ZonedDateTime alignByYears(ZonedDateTime now, boolean next) { ZonedDateTime base = ZonedDateTime.of( LocalDate.of(now.getYear(), 1, 1), diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/MonthInterval.java b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/MonthInterval.java index 7225eaca9f..91fc3d0413 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/MonthInterval.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/MonthInterval.java @@ -27,5 +27,4 @@ public class MonthInterval extends BaseAggInterval { return AggIntervalType.MONTH; } - } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/QuarterInterval.java b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/QuarterInterval.java new file mode 100644 index 0000000000..eb774b2341 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/cf/configuration/aggregation/single/interval/QuarterInterval.java @@ -0,0 +1,30 @@ +/** + * Copyright © 2016-2025 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data.cf.configuration.aggregation.single.interval; + +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class QuarterInterval extends BaseAggInterval { + + @Override + public AggIntervalType getType() { + return AggIntervalType.QUARTER; + } + +}