From 5d647be37332790aab41731d52e8dcadac9d5eb4 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 4 May 2024 19:56:42 -0700 Subject: Improve daily limit throttling --- src/main/java/org/traccar/config/Keys.java | 9 +++++++- .../java/org/traccar/handler/FilterHandler.java | 24 ++++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 0898e8f0a..5d9a43c01 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -1443,12 +1443,19 @@ public final class Keys { List.of(KeyType.CONFIG)); /** - * Filter position if the daily limit is exceeded for the device. + * Throttle positions if the daily limit is exceeded for the device. */ public static final ConfigKey FILTER_DAILY_LIMIT = new IntegerConfigKey( "filter.dailyLimit", List.of(KeyType.CONFIG)); + /** + * Throttling interval if the limit exceeded. The value is in seconds. + */ + public static final ConfigKey FILTER_DAILY_LIMIT_INTERVAL = new IntegerConfigKey( + "filter.dailyLimitInterval", + List.of(KeyType.CONFIG)); + /** * If false, the server expects all locations to come sequentially (for each device). Filter checks for duplicates, * distance, speed, or time period only against the location that was last received by server. diff --git a/src/main/java/org/traccar/handler/FilterHandler.java b/src/main/java/org/traccar/handler/FilterHandler.java index 796c302fb..3bb7ef426 100644 --- a/src/main/java/org/traccar/handler/FilterHandler.java +++ b/src/main/java/org/traccar/handler/FilterHandler.java @@ -53,6 +53,7 @@ public class FilterHandler extends BasePositionHandler { private final int filterMaxSpeed; private final long filterMinPeriod; private final int filterDailyLimit; + private final long filterDailyLimitInterval; private final boolean filterRelative; private final long skipLimit; private final boolean skipAttributes; @@ -77,6 +78,7 @@ public class FilterHandler extends BasePositionHandler { filterMaxSpeed = config.getInteger(Keys.FILTER_MAX_SPEED); filterMinPeriod = config.getInteger(Keys.FILTER_MIN_PERIOD) * 1000L; filterDailyLimit = config.getInteger(Keys.FILTER_DAILY_LIMIT); + filterDailyLimitInterval = config.getInteger(Keys.FILTER_DAILY_LIMIT_INTERVAL) * 1000L; filterRelative = config.getBoolean(Keys.FILTER_RELATIVE); skipLimit = config.getLong(Keys.FILTER_SKIP_LIMIT) * 1000; skipAttributes = config.getBoolean(Keys.FILTER_SKIP_ATTRIBUTES_ENABLE); @@ -164,9 +166,12 @@ public class FilterHandler extends BasePositionHandler { return false; } - private boolean filterDailyLimit(Position position) { - if (filterDailyLimit != 0) { - return statisticsManager.messageStoredCount(position.getDeviceId()) >= filterDailyLimit; + private boolean filterDailyLimit(Position position, Position last) { + if (filterDailyLimit != 0 + && statisticsManager.messageStoredCount(position.getDeviceId()) >= filterDailyLimit) { + long lastTime = last != null ? last.getFixTime().getTime() : 0; + long interval = position.getFixTime().getTime() - lastTime; + return filterDailyLimitInterval <= 0 || interval < filterDailyLimitInterval; } return false; } @@ -216,20 +221,18 @@ public class FilterHandler extends BasePositionHandler { if (filterApproximate(position)) { filterType.append("Approximate "); } - if (filterDailyLimit(position)) { - filterType.append("DailyLimit "); - } // filter out excessive data long deviceId = position.getDeviceId(); - if (filterDuplicate || filterStatic || filterDistance > 0 || filterMaxSpeed > 0 || filterMinPeriod > 0) { - Position preceding = null; + if (filterDuplicate || filterStatic + || filterDistance > 0 || filterMaxSpeed > 0 || filterMinPeriod > 0 || filterDailyLimit > 0) { + Position preceding; if (filterRelative) { try { Date newFixTime = position.getFixTime(); preceding = getPrecedingPosition(deviceId, newFixTime); } catch (StorageException e) { - LOGGER.warn("Error retrieving preceding position; fallbacking to last received position.", e); + LOGGER.warn("Error retrieving preceding position; fall backing to last received position.", e); preceding = cacheManager.getPosition(deviceId); } } else { @@ -250,6 +253,9 @@ public class FilterHandler extends BasePositionHandler { if (filterMinPeriod(position, preceding)) { filterType.append("MinPeriod "); } + if (filterDailyLimit(position, preceding)) { + filterType.append("DailyLimit "); + } } Device device = cacheManager.getObject(Device.class, deviceId); -- cgit v1.2.3