Sort, filter, and limit results at /packets #23

Merged
W1CDN merged 14 commits from sort-filter into main 2023-05-14 10:18:10 -05:00
Showing only changes of commit 6957219468 - Show all commits

View File

@ -65,6 +65,17 @@ def select_all_frames(conn):
rows = cur.fetchall()
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):
def get(self):
# Handle arguments that may or may not exist
@ -72,10 +83,11 @@ class Packets(Resource):
n = int(request.args.get('n'))
except:
n = 10
from_ = None if request.args.get('from') == None else request.args.get('from')
conn = get_db_connection()
# 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)
data.sort(key=operator.itemgetter('created'), reverse=True)
#data.sort(key=created, reverse=True)