improve db

This commit is contained in:
Alex Grintsvayg 2021-01-22 12:01:20 -05:00
parent 99c64021eb
commit 2d898f802d
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5

View file

@ -6,7 +6,7 @@ import sqlite3
import pickle import pickle
from os import path from os import path
from pprint import pprint from pprint import pprint
import aioupnp from aioupnp import upnp, fault as upnpfault
from aiohttp import web from aiohttp import web
import json import json
@ -30,12 +30,12 @@ async def main():
pass # Not implemented on Windows pass # Not implemented on Windows
peer_manager = peer.PeerManager(loop) peer_manager = peer.PeerManager(loop)
u = await aioupnp.upnp.UPnP.discover() u = await upnp.UPnP.discover()
db = sqlite3.connect(data_dir + "/tracker.sqlite3") db = sqlite3.connect(data_dir + "/tracker.sqlite3")
db.execute( db.execute('CREATE TABLE IF NOT EXISTS announce (local_id TEXT, hash TEXT, node_id TEXT, ip TEXT, port INT, timestamp INT)')
'CREATE TABLE IF NOT EXISTS log (local_id TEXT, hash TEXT, node_id TEXT, ip TEXT, port INT, timestamp INT)' db.execute('CREATE UNIQUE INDEX IF NOT EXISTS node_id_hash_idx ON announce (node_id, hash)')
)
# curr = db.cursor() # curr = db.cursor()
# res = curr.execute("SELECT 1, 2, 3") # res = curr.execute("SELECT 1, 2, 3")
# for items in res: # for items in res:
@ -97,8 +97,14 @@ async def main():
try: try:
cur = db.cursor() cur = db.cursor()
cur.execute('INSERT INTO log (local_id, hash, node_id, ip, port, timestamp) VALUES (?,?,?,?,?,?)', cur.execute(
(local_node_id, bytes.hex(blob_hash), bytes.hex(node_id), ip, port, int(time.time()))) '''
INSERT INTO announce (local_id, hash, node_id, ip, port, timestamp) VALUES (?,?,?,?,?,?)
ON CONFLICT (node_id, hash) DO UPDATE SET
local_id=excluded.local_id, ip=excluded.ip, port=excluded.port, timestamp=excluded.timestamp
''',
(local_node_id, bytes.hex(blob_hash), bytes.hex(node_id), ip, port, int(time.time()))
)
db.commit() db.commit()
cur.close() cur.close()
except sqlite3.Error as err: except sqlite3.Error as err:
@ -111,10 +117,9 @@ async def main():
# print(f'deleting upnp port mapping {n.protocol.udp_port}') # print(f'deleting upnp port mapping {n.protocol.udp_port}')
try: try:
await u.delete_port_mapping(n.protocol.udp_port, "UDP") await u.delete_port_mapping(n.protocol.udp_port, "UDP")
except aioupnp.fault.UPnPError: except upnpfault.UPnPError:
pass pass
state = n.get_state() state = n.get_state()
# keep existing rt if there is one # keep existing rt if there is one
if len(state.routing_table_peers) == 0 and path.exists(state_dir + node_id): if len(state.routing_table_peers) == 0 and path.exists(state_dir + node_id):
@ -183,7 +188,7 @@ async def seeds_handler(request):
try: try:
cur = db.cursor() cur = db.cursor()
c = cur.execute(""" c = cur.execute("""
select count(distinct(node_id)) from log where hash = ? and timestamp > strftime('%s','now','-1 day') select count(distinct(node_id)) from announce where hash = ? and timestamp > strftime('%s','now','-1 day')
""", (blobhash,)).fetchone()[0] """, (blobhash,)).fetchone()[0]
cur.close() cur.close()
return web.Response(text=json.dumps({'seeds': c})+"\n") return web.Response(text=json.dumps({'seeds': c})+"\n")