1 """ sqlite event store """
3 from sqlite3 import connect, OperationalError
5 __all__ = "fetch", "initdb", "stow"
9 SCHEMA = """create table if not exists events (
12 peeraddr text not null,
13 is_incoming int not null default TRUE,
23 DB.execute("""alter table events add column
24 is_incoming int not null default TRUE""")
25 except OperationalError:
32 k: kwargs[k] if k in kwargs else v
34 ("is_incoming", True),
42 assert len(kwargs) <= len(parms)
44 """insert or ignore into events
45 (tstamp, imei, peeraddr, proto, packet, is_incoming)
47 (:when, :imei, :peeraddr, :proto, :packet, :is_incoming)
53 def fetch(imei, protos, backlog):
55 protosel = ", ".join(["?" for _ in range(len(protos))])
57 cur.execute(f"""select packet from events
58 where proto in ({protosel}) and imei = ?
59 order by tstamp desc limit ?""",
60 protos + (imei, backlog))
61 result = [row[0] for row in cur]