From c5207eabbb693d441776eadc1082e35fa8fd32b1 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Sun, 2 May 2021 13:01:42 +0200 Subject: Add option to limit the number of matches This is useful if the user already expects a lot of matches but is only interested in a limited number of them. An upcoming commit will introduce the option to have quarg list matches in ascending or descending order (currently we do not pick a default - effectively listing matches in ascending timestamp order), making it possible to select the first or last N matches. --- quarg/main.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/quarg/main.py b/quarg/main.py index 82474b8..8b9987b 100644 --- a/quarg/main.py +++ b/quarg/main.py @@ -18,6 +18,7 @@ cli = argparse.ArgumentParser() cli.add_argument('keyword', nargs='*', help='match messages containing this keyword') cli.add_argument('-d', action='store_true', dest='debug', help='print debug and SQL query information') cli.add_argument('-e', action='store_true', dest='expr', help='interpret keywords as LIKE expression') +cli.add_argument('-l', dest='limit', metavar='NUM', type=int, help='limit the number of matches') matchers = cli.add_argument_group('matching message context') matchers.add_argument('-b', action='append', dest='buffer', help='match this buffer') matchers.add_argument('-B', action=actions.ParseBufferType, dest='buftype', help='match buffers of this type') @@ -94,7 +95,7 @@ def collect_predicates(args): else: yield fun(value) -def run_query(session, predicates): +def run_query(session, predicates, limit): start = timer() query = session.query(Backlog).join(Sender).join(Buffer).join(Network).join(QuasselUser) @@ -102,7 +103,11 @@ def run_query(session, predicates): for predicate in predicates: query = query.filter(predicate) - rows = query.order_by(Backlog.time).all() + query = query.order_by(Backlog.time) + if limit: + query = query.limit(limit) + + rows = query.all() end = timer() @@ -125,7 +130,7 @@ def main(): if not predicates: errx('Nothing to match.') - rows, time = run_query(session, predicates) + rows, time = run_query(session, predicates, args.limit) for row in rows: print(format_from(row)) -- cgit v1.2.3-2-gb3c3