fully implement location TTL functionality

This commit is contained in:
Jakob Ketterl 2023-08-26 16:23:03 +02:00
parent b2ddb8f3bb
commit 25947ac177
3 changed files with 31 additions and 8 deletions

View file

@ -152,6 +152,7 @@ $(function(){
position: pos,
title: sourceToString(update.source)
}, aprsOptions, getMarkerOpacityOptions(update.lastseen) ));
marker.source = update.source;
marker.lastseen = update.lastseen;
marker.mode = update.mode;
marker.band = update.band;
@ -520,7 +521,7 @@ $(function(){
});
Object.values(markers).forEach(function(m) {
var age = now - m.lastseen;
if (age > retention_time) {
if (age > retention_time || (m.ttl && age > m.ttl)) {
delete markers[sourceToKey(m.source)];
m.setMap();
return;

View file

@ -1,8 +1,7 @@
from abc import ABC
from csdr.module import PickleModule
from math import sqrt, atan2, pi, floor, acos, cos
from owrx.map import LatLngLocation, IncrementalUpdate, Location, Map
from owrx.map import LatLngLocation, IncrementalUpdate, TTLUpdate, Location, Map
from datetime import timedelta
import time
import logging
@ -17,7 +16,7 @@ d_lat_even = 360 / (4 * nz)
d_lat_odd = 360 / (4 * nz - 1)
class AirplaneLocation(LatLngLocation, IncrementalUpdate, ABC):
class AirplaneLocation(IncrementalUpdate, TTLUpdate, LatLngLocation):
mapKeys = [
"icao",
"lat",
@ -66,6 +65,9 @@ class AirplaneLocation(LatLngLocation, IncrementalUpdate, ABC):
dict.update(self.props)
return dict
def getTTL(self) -> timedelta:
return timedelta(seconds=self.ttl)
class CprCache:
def __init__(self):

View file

@ -9,7 +9,6 @@ import sys
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
class Location(object):
@ -121,9 +120,19 @@ class Map(object):
def removeOldPositions(self):
pm = Config.get()
retention = timedelta(seconds=pm["map_position_retention_time"])
cutoff = datetime.now() - retention
now = datetime.now()
cutoff = now - retention
to_be_removed = [key for (key, pos) in self.positions.items() if pos["updated"] < cutoff]
def isExpired(pos):
if pos["updated"] < cutoff:
return True
if isinstance(pos["location"], TTLUpdate):
if now - pos["location"].getTTL() > pos["updated"]:
return True
return False
with self.positionsLock:
to_be_removed = [key for (key, pos) in self.positions.items() if isExpired(pos)]
for key in to_be_removed:
self.removeLocation(key)
@ -157,3 +166,14 @@ class IncrementalUpdate(Location, metaclass=ABCMeta):
@abstractmethod
def update(self, previousLocation: Location):
pass
class TTLUpdate(Location, metaclass=ABCMeta):
@abstractmethod
def getTTL(self) -> timedelta:
pass
def __dict__(self):
res = super().__dict__()
res["ttl"] = self.getTTL().total_seconds() * 1000
return res