From f6333c73fe8bdd0027bb16a715193e1587b934a2 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 25 Jul 2016 08:59:36 +0500 Subject: - Move lastPosition update to the end of pipeline - Optimize MotionEventHandler to do not use Device.motion - Remove Device.motion from model and database - some optimizations --- src/org/traccar/database/DeviceManager.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 4dd7b41cb..5a0c87985 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -105,7 +105,6 @@ public class DeviceManager implements IdentityManager { } } device.setStatus(Device.STATUS_OFFLINE); - device.setMotion(Device.STATUS_STOPPED); } } for (Long cachedDeviceId : devicesById.keySet()) { @@ -176,7 +175,6 @@ public class DeviceManager implements IdentityManager { if (devicesById.containsKey(device.getId())) { Device cachedDevice = devicesById.get(device.getId()); cachedDevice.setStatus(device.getStatus()); - cachedDevice.setMotion(device.getMotion()); } } @@ -191,10 +189,14 @@ public class DeviceManager implements IdentityManager { positions.remove(deviceId); } + public boolean isLatestPosition(Position position) { + Position lastPosition = getLastPosition(position.getDeviceId()); + return lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0; + } + public void updateLatestPosition(Position position) throws SQLException { - Position lastPosition = getLastPosition(position.getDeviceId()); - if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0) { + if (isLatestPosition(position)) { dataManager.updateLatestPosition(position); -- cgit v1.2.3 From 451c358879e1f6f2fda26e4b85ef97982fbeaba5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 25 Jul 2016 16:49:08 +1200 Subject: Improve history clearing logic --- debug.xml | 4 ++-- src/org/traccar/Main.java | 9 ++++----- src/org/traccar/database/DataManager.java | 27 +++++---------------------- 3 files changed, 11 insertions(+), 29 deletions(-) (limited to 'src/org/traccar/database') diff --git a/debug.xml b/debug.xml index 346eda292..304ae7f3d 100644 --- a/debug.xml +++ b/debug.xml @@ -323,8 +323,8 @@ DELETE FROM notifications WHERE id = :id; - - DELETE FROM positions WHERE id != :positionId and deviceid = :deviceId and servertime < :serverTime; + + DELETE FROM positions WHERE serverTime < :serverTime AND id NOT IN (SELECT positionId FROM devices); diff --git a/src/org/traccar/Main.java b/src/org/traccar/Main.java index e7e8d8ab9..e992691ad 100644 --- a/src/org/traccar/Main.java +++ b/src/org/traccar/Main.java @@ -23,8 +23,8 @@ import java.util.TimerTask; import java.util.Locale; public final class Main { - static final long CLEAN_DELAY = 0; - static final long CLEAN_PERIOD = 24 * 60 * 60 * 1000; + + private static final long CLEAN_PERIOD = 24 * 60 * 60 * 1000; private Main() { } @@ -40,8 +40,7 @@ public final class Main { Context.getWebServer().start(); } - Timer timer = new Timer(); - timer.scheduleAtFixedRate(new TimerTask() { + new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { @@ -50,7 +49,7 @@ public final class Main { Log.warning(error); } } - }, CLEAN_DELAY, CLEAN_PERIOD); + }, 0, CLEAN_PERIOD); Runtime.getRuntime().addShutdownHook(new Thread() { @Override diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 04d0d44ea..78f1b4109 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -305,28 +305,11 @@ public class DataManager { } public void clearPositionsHistory() throws SQLException { - int histDays = config.getInteger("database.positionsHistoryDays"); - if (histDays == 0) { - return; - } - - String sql = getQuery("database.clearPositionsHistory"); - if (sql == null) { - return; - } - - for (Device device : getAllDevices()) { - Date lastUpdate = device.getLastUpdate(); - if (lastUpdate != null) { - - Date dateBefore = new Date(lastUpdate.getTime() - histDays * 24 * 3600 * 1000); - - QueryBuilder.create(dataSource, sql) - .setLong("positionId", device.getPositionId()) - .setLong("deviceId", device.getId()) - .setDate("serverTime", dateBefore) - .executeUpdate(); - } + int historyDays = config.getInteger("database.positionsHistoryDays"); + if (historyDays != 0) { + QueryBuilder.create(dataSource, getQuery("database.deletePositions")) + .setDate("serverTime", new Date(System.currentTimeMillis() - historyDays * 24 * 3600 * 1000)) + .executeUpdate(); } } -- cgit v1.2.3 From 70f684945754b6bdadd2bf038ab955d93a95977d Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Jul 2016 13:44:05 +0500 Subject: Organized server cache Moved lookupAttribute function to DeviceManager --- debug.xml | 3 +- src/org/traccar/api/resource/ServerResource.java | 5 ++- src/org/traccar/database/DeviceManager.java | 32 ++++++++++++++++++ src/org/traccar/database/PermissionsManager.java | 11 ++++++- src/org/traccar/events/OverspeedEventHandler.java | 40 +++-------------------- 5 files changed, 51 insertions(+), 40 deletions(-) (limited to 'src/org/traccar/database') diff --git a/debug.xml b/debug.xml index 62e34d5d7..4c3b63d42 100644 --- a/debug.xml +++ b/debug.xml @@ -41,9 +41,10 @@ all target/tracker-server.log + true + true true - true true true true diff --git a/src/org/traccar/api/resource/ServerResource.java b/src/org/traccar/api/resource/ServerResource.java index 9e42687ab..0ca0d62aa 100644 --- a/src/org/traccar/api/resource/ServerResource.java +++ b/src/org/traccar/api/resource/ServerResource.java @@ -37,14 +37,13 @@ public class ServerResource extends BaseResource { @PermitAll @GET public Server get() throws SQLException { - return Context.getDataManager().getServer(); + return Context.getPermissionsManager().getServer(); } @PUT public Response update(Server entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); - Context.getDataManager().updateServer(entity); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().updateServer(entity); return Response.ok(entity).build(); } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 5a0c87985..3476139f2 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -32,6 +32,7 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.Position; +import org.traccar.model.Server; public class DeviceManager implements IdentityManager { @@ -40,6 +41,7 @@ public class DeviceManager implements IdentityManager { private final Config config; private final DataManager dataManager; private final long dataRefreshDelay; + private boolean lookupGroupsAttribute; private Map devicesById; private Map devicesByUniqueId; @@ -54,6 +56,7 @@ public class DeviceManager implements IdentityManager { this.dataManager = dataManager; this.config = Context.getConfig(); dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; + lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute"); if (dataManager != null) { try { updateGroupCache(true); @@ -312,4 +315,33 @@ public class DeviceManager implements IdentityManager { dataManager.removeGroup(groupId); groupsById.remove(groupId); } + + public String lookupAttribute(long deviceId, String attributeName) { + String result = null; + Device device = getDeviceById(deviceId); + if (device != null) { + if (device.getAttributes().containsKey(attributeName)) { + result = (String) device.getAttributes().get(attributeName); + } + if (result == null && lookupGroupsAttribute) { + long groupId = device.getGroupId(); + while (groupId != 0) { + if (getGroupById(groupId) != null) { + result = (String) getGroupById(groupId).getAttributes().get(attributeName); + if (result != null) { + break; + } + groupId = getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } + } + } + if (result == null) { + Server server = Context.getPermissionsManager().getServer(); + result = (String) server.getAttributes().get(attributeName); + } + } + return result; + } } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index d786dcc4e..92fcc3ebd 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -34,7 +34,7 @@ public class PermissionsManager { private final DataManager dataManager; - private Server server; + private volatile Server server; private final Map users = new HashMap<>(); @@ -153,4 +153,13 @@ public class PermissionsManager { } } + public Server getServer() { + return server; + } + + public void updateServer(Server server) throws SQLException { + dataManager.updateServer(server); + this.server = server; + } + } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 0328b9109..a1efbde48 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -15,7 +15,6 @@ */ package org.traccar.events; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -24,19 +23,15 @@ import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.model.Server; -import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; public class OverspeedEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; - private boolean checkGroups; private boolean riseOnce; public OverspeedEventHandler() { - checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); } @@ -54,37 +49,12 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); double speedLimit = 0; - - if (device.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) device.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - } - if (speedLimit == 0 && checkGroups) { - long groupId = device.getGroupId(); - while (groupId != 0) { - if (Context.getDeviceManager().getGroupById(groupId).getAttributes() - .containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) Context.getDeviceManager().getGroupById(groupId) - .getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - if (speedLimit != 0) { - break; - } - } - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } + String speedLimitAttribute = Context.getDeviceManager().lookupAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT); + if (speedLimitAttribute != null) { + speedLimit = Double.parseDouble(speedLimitAttribute); } if (speedLimit == 0) { - try { - Server server = Context.getDataManager().getServer(); - if (server.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) server.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - } - } catch (SQLException error) { - Log.warning(error); - } + return null; } double oldSpeed = 0; if (riseOnce) { @@ -94,7 +64,7 @@ public class OverspeedEventHandler extends BaseEventHandler { } } speedLimit = UnitsConverter.knotsFromKph(speedLimit); - if (speedLimit != 0 && speed > speedLimit && oldSpeed <= speedLimit) { + if (speed > speedLimit && oldSpeed <= speedLimit) { events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); } return events; -- cgit v1.2.3