Add basic API call to select by "from" station.

This commit is contained in:
W1CDN 2023-04-16 21:04:26 -05:00
parent cd5d24b641
commit 6957219468

View File

@ -65,6 +65,17 @@ def select_all_frames(conn):
rows = cur.fetchall() rows = cur.fetchall()
return rows return rows
def select_frames(conn, n, from_):
cur = conn.cursor()
# Workaround to deal with missing value in WHERE
from_ = "IS NOT NULL" if from_ == None else "='"+from_+"'"
#sql = "SELECT * FROM frames LIMIT "+n
sql = 'SELECT * FROM frames WHERE "from" {from_} LIMIT {n}'.format(from_=from_, n=n)
print(sql)
cur.execute(sql)
rows = cur.fetchall()
return rows
class Packets(Resource): class Packets(Resource):
def get(self): def get(self):
# Handle arguments that may or may not exist # Handle arguments that may or may not exist
@ -72,10 +83,11 @@ class Packets(Resource):
n = int(request.args.get('n')) n = int(request.args.get('n'))
except: except:
n = 10 n = 10
from_ = None if request.args.get('from') == None else request.args.get('from')
conn = get_db_connection() conn = get_db_connection()
# Limit to number of records requested # Limit to number of records requested
data = select_all_frames(conn)[-n:] data = select_frames(conn, n = n, from_ = from_)
# Sort by created date, descending (https://stackoverflow.com/a/45266808) # Sort by created date, descending (https://stackoverflow.com/a/45266808)
data.sort(key=operator.itemgetter('created'), reverse=True) data.sort(key=operator.itemgetter('created'), reverse=True)
#data.sort(key=created, reverse=True) #data.sort(key=created, reverse=True)