From a2929757d8289757047d723f193e5d5ba987acf7 Mon Sep 17 00:00:00 2001 From: Avgustin Marinov Date: Mon, 29 Sep 2025 13:14:40 +0300 Subject: [PATCH] Encapsulate time overdue into VirtualPropertyResolver (#2704) Signed-off-by: Avgustin Marinov --- .../repository/TimestampCalculator.java | 68 ------------------- .../rsql/VirtualPropertyResolver.java | 43 ++++++++++-- .../jpa/JpaRepositoryConfiguration.java | 2 +- 3 files changed, 38 insertions(+), 75 deletions(-) delete mode 100644 hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/TimestampCalculator.java diff --git a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/TimestampCalculator.java b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/TimestampCalculator.java deleted file mode 100644 index 3184d4ec1..000000000 --- a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/TimestampCalculator.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright (c) 2025 Contributors to the Eclipse Foundation - * - * This program and the accompanying materials are made - * available under the terms of the Eclipse Public License 2.0 - * which is available at https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - */ -package org.eclipse.hawkbit.repository; - -import java.time.Duration; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.eclipse.hawkbit.repository.model.helper.SystemSecurityContextHolder; -import org.eclipse.hawkbit.repository.model.helper.TenantConfigurationManagementHolder; -import org.eclipse.hawkbit.security.SystemSecurityContext; -import org.eclipse.hawkbit.tenancy.configuration.DurationHelper; -import org.eclipse.hawkbit.tenancy.configuration.PollingTime; -import org.eclipse.hawkbit.tenancy.configuration.PollingTime.PollingInterval; -import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationProperties.TenantConfigurationKey; - -/** - * Calculates non-persistent timestamps , e.g. the point a time a target is - * declared as overdue.
- * Therefore tenant specific configuration may be considered. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TimestampCalculator { - - /** - * Calculates the overdue timestamp (overdue_ts) based on the current timestamp and the intervals for polling and poll-overdue: - *

- * overdue_ts = now_ts - pollingInterval - pollingOverdueInterval;
- * pollingInterval and pollingOverdueInterval are retrieved from tenant-specific system configuration. - *

- * Note: this method checks against the default polling time interval. I.e. overrides are not considered. - * - * @return overdue_ts in milliseconds since Unix epoch as long value - */ - public static long calculateOverdueTimestamp() { - return calculateOverdueTimestamp( - new PollingTime(getRawStringForKey(TenantConfigurationKey.POLLING_TIME)).getPollingInterval(), - DurationHelper.fromString(getRawStringForKey(TenantConfigurationKey.POLLING_OVERDUE_TIME))); - } - - private static long calculateOverdueTimestamp(final PollingInterval pollingInterval, final Duration pollingOverdueTime) { - return System.currentTimeMillis() - - (pollingInterval.getDeviationPercent() == 0 - ? pollingInterval.getInterval().toMillis() - : pollingInterval.getInterval().toMillis() * (100 + pollingInterval.getDeviationPercent()) / 100) - - pollingOverdueTime.toMillis(); - } - - private static String getRawStringForKey(final String key) { - return getSystemSecurityContext().runAsSystem( - () -> getTenantConfigurationManagement().getConfigurationValue(key, String.class).getValue()); - } - - private static SystemSecurityContext getSystemSecurityContext() { - return SystemSecurityContextHolder.getInstance().getSystemSecurityContext(); - } - - private static TenantConfigurationManagement getTenantConfigurationManagement() { - return TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement(); - } -} \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/rsql/VirtualPropertyResolver.java b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/rsql/VirtualPropertyResolver.java index cd3dc41d0..f5b637ce9 100644 --- a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/rsql/VirtualPropertyResolver.java +++ b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/rsql/VirtualPropertyResolver.java @@ -9,11 +9,15 @@ */ package org.eclipse.hawkbit.repository.rsql; -import java.io.Serial; +import java.time.Duration; import org.apache.commons.text.StringSubstitutor; import org.apache.commons.text.lookup.StringLookupFactory; -import org.eclipse.hawkbit.repository.TimestampCalculator; +import org.eclipse.hawkbit.repository.model.helper.SystemSecurityContextHolder; +import org.eclipse.hawkbit.repository.model.helper.TenantConfigurationManagementHolder; +import org.eclipse.hawkbit.tenancy.configuration.DurationHelper; +import org.eclipse.hawkbit.tenancy.configuration.PollingTime; +import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationProperties; /** * Adds macro capabilities to RSQL expressions that are used to filter for targets. @@ -35,9 +39,6 @@ import org.eclipse.hawkbit.repository.TimestampCalculator; */ public class VirtualPropertyResolver { - @Serial - private static final long serialVersionUID = 1L; - private static final StringSubstitutor STRING_SUBSTITUTOR = new StringSubstitutor( StringLookupFactory.builder().get().functionStringLookup(VirtualPropertyResolver::lookup), StringSubstitutor.DEFAULT_PREFIX, StringSubstitutor.DEFAULT_SUFFIX, StringSubstitutor.DEFAULT_ESCAPE); @@ -50,9 +51,39 @@ public class VirtualPropertyResolver { if ("now_ts".equalsIgnoreCase(rhs)) { return String.valueOf(System.currentTimeMillis()); } else if ("overdue_ts".equalsIgnoreCase(rhs)) { - return String.valueOf(TimestampCalculator.calculateOverdueTimestamp()); + return String.valueOf(calculateOverdueTimestamp()); } else { return null; } } + + /** + * Calculates the overdue timestamp (overdue_ts) based on the current timestamp and the intervals for polling and poll-overdue: + *

+ * overdue_ts = now_ts - pollingInterval - pollingOverdueInterval;
+ * pollingInterval and pollingOverdueInterval are retrieved from tenant-specific system configuration. + *

+ * Note: this method checks against the default polling time interval. I.e. overrides are not considered. + * + * @return overdue_ts in milliseconds since Unix epoch as long value + */ + public static long calculateOverdueTimestamp() { + return calculateOverdueTimestamp( + new PollingTime(getRawStringForKey(TenantConfigurationProperties.TenantConfigurationKey.POLLING_TIME)).getPollingInterval(), + DurationHelper.fromString(getRawStringForKey(TenantConfigurationProperties.TenantConfigurationKey.POLLING_OVERDUE_TIME))); + } + + private static long calculateOverdueTimestamp(final PollingTime.PollingInterval pollingInterval, final Duration pollingOverdueTime) { + return System.currentTimeMillis() + - (pollingInterval.getDeviationPercent() == 0 + ? pollingInterval.getInterval().toMillis() + : pollingInterval.getInterval().toMillis() * (100 + pollingInterval.getDeviationPercent()) / 100) + - pollingOverdueTime.toMillis(); + } + + private static String getRawStringForKey(final String key) { + return SystemSecurityContextHolder.getInstance().getSystemSecurityContext().runAsSystem( + () -> TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement() + .getConfigurationValue(key, String.class).getValue()); + } } \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaRepositoryConfiguration.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaRepositoryConfiguration.java index bb694a276..8480efa68 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaRepositoryConfiguration.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaRepositoryConfiguration.java @@ -543,7 +543,7 @@ public class JpaRepositoryConfiguration { * @return The {@link QLSupport} singleton. */ @Bean - QLSupport rsqlUtility() { + QLSupport qlSupport() { return QLSupport.getInstance(); }