From f83cb02ad5c21b956ad739e18fb112f9f1cf46f4 Mon Sep 17 00:00:00 2001 From: Eugene Crosser Date: Sun, 4 Dec 2022 23:13:08 +0100 Subject: [PATCH] Ignore pmod registrations older than an hour --- loctrkd/evstore.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/loctrkd/evstore.py b/loctrkd/evstore.py index 6d6edd0..7c92e3a 100644 --- a/loctrkd/evstore.py +++ b/loctrkd/evstore.py @@ -28,7 +28,8 @@ SCHEMA = ( )""", """create table if not exists pmodmap ( imei text not null unique, - pmod text not null + pmod text not null, + tstamp real not null default (unixepoch()) )""", ) @@ -37,8 +38,25 @@ def initdb(dbname: str) -> None: global DB DB = connect(dbname) DB.row_factory = Row + need_populate_pmodmap = False + try: + DB.execute("select count(pmod) from pmodmap") + try: + DB.execute("select count(tstamp) from pmodmap") + except OperationalError: + need_populate_pmodmap = True + DB.execute("alter table pmodmap rename to old_pmodmap") + except OperationalError: + pass # DB was empty for stmt in SCHEMA: DB.execute(stmt) + if need_populate_pmodmap: + DB.execute( + """insert into pmodmap(imei, pmod) + select imei, pmod from old_pmodmap""" + ) + DB.execute("drop table old_pmodmap") + DB.commit() def stow(**kwargs: Any) -> None: @@ -124,7 +142,11 @@ def fetchpmod(imei: str) -> Optional[Any]: assert DB is not None ret = None cur = DB.cursor() - cur.execute("select pmod from pmodmap where imei = ?", (imei,)) + cur.execute( + """select pmod from pmodmap where imei = ? + and tstamp > unixepoch() - 3600.0""", + (imei,), + ) result = cur.fetchone() if result: ret = result[0] -- 2.39.2