Sort, filter, and limit results at /packets #23
14
api_app.py
14
api_app.py
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user