diff options
author | Wolfgang Müller | 2021-05-02 13:01:42 +0200 |
---|---|---|
committer | Wolfgang Müller | 2021-05-02 13:35:17 +0200 |
commit | c5207eabbb693d441776eadc1082e35fa8fd32b1 (patch) | |
tree | b791a94465c5829ba8dd3607b7a5b23cbe898d7e | |
parent | f95a23e0e5c23b7ec13e63501b6ab6df540cb695 (diff) | |
download | quarg-c5207eabbb693d441776eadc1082e35fa8fd32b1.tar.gz |
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.
-rw-r--r-- | quarg/main.py | 11 |
1 files 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)) |