From 1c31cd1e27c515a311d75ff43102cc74cd6fb93b Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Wed, 5 Jan 2022 11:40:59 +0100 Subject: [PATCH] Replace --query-display-mode option by --wrap-query flag The new option is now a flag which toggles query wrapping in the column. This effectively disable to (previous default) mode that displays the query wrapped over the whole line (not just the column). The --verbose-mode option, deprecated since version 2.2.0, is dropped. Accordingly, we bump the version to 3.0.0. --- docs/man/pg_activity.1 | 8 ++++---- docs/man/pg_activity.pod | 4 ++-- pgactivity/__init__.py | 2 +- pgactivity/cli.py | 22 ++++++---------------- pgactivity/handlers.py | 20 ++++++++++---------- pgactivity/keys.py | 4 ++-- pgactivity/types.py | 17 ++--------------- pgactivity/ui.py | 6 ++---- pgactivity/views.py | 37 +++++-------------------------------- tests/test_ui.txt | 8 ++++---- tests/test_views.txt | 35 ++++++----------------------------- 11 files changed, 44 insertions(+), 119 deletions(-) diff --git a/docs/man/pg_activity.1 b/docs/man/pg_activity.1 index 2fcd44cb..c821ddf9 100644 --- a/docs/man/pg_activity.1 +++ b/docs/man/pg_activity.1 @@ -133,7 +133,7 @@ .\" ======================================================================== .\" .IX Title "PG_ACTIVITY 1" -.TH PG_ACTIVITY 1 "2021-08-05" "pg_activity 2.2.1" "Command line tool for PostgreSQL server activity monitoring." +.TH PG_ACTIVITY 1 "2022-01-05" "pg_activity 3.0.0" "Command line tool for PostgreSQL server activity monitoring." .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l @@ -316,10 +316,10 @@ required by another session. It shows following information: .Vb 1 \& Skip total size of DB. .Ve -.IP "\fB\-w \s-1DISPLAY_MODE,\s0 \-\-query\-display\-mode=DISPLAY_MODE\fR" 2 -.IX Item "-w DISPLAY_MODE, --query-display-mode=DISPLAY_MODE" +.IP "\fB\-w, \-\-wrap\-query\fR" 2 +.IX Item "-w, --wrap-query" .Vb 1 -\& Queries display mode. Values: 1\-TRUNCATED, 2\-FULL(default), 3\-INDENTED +\& Wrap query column instead of truncating .Ve .IP "\fB\-\-duration\-mode=DURATION_MODE\fR" 2 .IX Item "--duration-mode=DURATION_MODE" diff --git a/docs/man/pg_activity.pod b/docs/man/pg_activity.pod index 66a95308..fd375f3e 100644 --- a/docs/man/pg_activity.pod +++ b/docs/man/pg_activity.pod @@ -183,9 +183,9 @@ required by another session. It shows following information: Skip total size of DB. -=item B<-w DISPLAY_MODE, --query-display-mode=DISPLAY_MODE> +=item B<-w, --wrap-query> - Queries display mode. Values: 1-TRUNCATED, 2-FULL(default), 3-INDENTED + Wrap query column instead of truncating =item B<--duration-mode=DURATION_MODE> diff --git a/pgactivity/__init__.py b/pgactivity/__init__.py index b19ee4b7..e05c5311 100644 --- a/pgactivity/__init__.py +++ b/pgactivity/__init__.py @@ -1 +1 @@ -__version__ = "2.2.1" +__version__ = "3.0.0.dev0" diff --git a/pgactivity/cli.py b/pgactivity/cli.py index 7a761c74..79aef3ce 100755 --- a/pgactivity/cli.py +++ b/pgactivity/cli.py @@ -98,15 +98,14 @@ def get_parser() -> OptionParser: help="Skip total size of DB", default=False, ) - # --query-display-mode + # --wrap-query parser.add_option( "-w", - "--query-display-mode", - dest="querydisplaymode", - help="Queries display mode. Values: 1-TRUNCATED, 2-FULL(default), 3-INDENTED", - metavar="DISPLAY_MODE", - choices=["1", "2", "3"], - default="2", + "--wrap-query", + dest="wrap_query", + action="store_true", + help="Wrap query column instead of truncating", + default=False, ) # --duration-mode parser.add_option( @@ -138,15 +137,6 @@ def get_parser() -> OptionParser: metavar="FIELD:REGEX", default=[], ) - # --verbose-mode - parser.add_option( - "--verbose-mode", - dest="querydisplaymode", - help="DEPRECATED, use --query-display-mode instead", - metavar="VERBOSE_MODE", - choices=["1", "2", "3"], - default="2", - ) group = OptionGroup( parser, "Display Options, you can exclude some columns by using them " diff --git a/pgactivity/handlers.py b/pgactivity/handlers.py index fab8d9a0..9dedd62c 100644 --- a/pgactivity/handlers.py +++ b/pgactivity/handlers.py @@ -3,7 +3,7 @@ from blessed.keyboard import Keystroke from . import keys -from .types import DurationMode, Flag, QueryDisplayMode, QueryMode, SortKey, enum_next +from .types import DurationMode, Flag, QueryMode, SortKey, enum_next def refresh_time( @@ -50,19 +50,19 @@ def duration_mode(key: Keystroke, mode: DurationMode) -> DurationMode: return mode -def query_display_mode(key: Keystroke, mode: QueryDisplayMode) -> QueryDisplayMode: - """Return the updated query display mode matching input key. +def wrap_query(key: Keystroke, wrap: bool) -> bool: + """Return the updated 'wrap' value depending on input key. >>> from blessed.keyboard import Keystroke as k - >>> query_display_mode(k("42"), QueryDisplayMode.truncate).name - 'truncate' - >>> query_display_mode(k("v"), QueryDisplayMode.wrap_noindent).name - 'wrap' + >>> wrap_query(k("42"), True) + True + >>> wrap_query(k("v"), False) + True """ - if key == keys.CHANGE_DISPLAY_MODE: - return enum_next(mode) - return mode + if key == keys.WRAP_QUERY: + return not wrap + return wrap def query_mode(key: Keystroke) -> Optional[QueryMode]: diff --git a/pgactivity/keys.py b/pgactivity/keys.py index d5c18ba8..12badfa8 100644 --- a/pgactivity/keys.py +++ b/pgactivity/keys.py @@ -22,7 +22,7 @@ def __eq__(self, other: Any) -> bool: CANCEL_SELECTION = "KEY_ESCAPE" CHANGE_DURATION_MODE = "T" -CHANGE_DISPLAY_MODE = "v" +WRAP_QUERY = "v" EXIT = "q" HELP = "h" SPACE = " " @@ -92,7 +92,7 @@ def is_process_last(key: Keystroke) -> bool: Key(SORTBY_TIME, "sort by TIME+ desc. (activities)", local_only=True), Key(REFRESH_TIME_INCREASE, "increase refresh time (max:5s)"), Key(REFRESH_TIME_DECREASE, "decrease refresh time (min:0.5s)"), - Key(CHANGE_DISPLAY_MODE, "change display mode"), + Key(WRAP_QUERY, "toggle query wrap"), Key(CHANGE_DURATION_MODE, "change duration mode"), Key(REFRESH_DB_SIZE, "force refresh database size"), Key("R", "force refresh"), diff --git a/pgactivity/types.py b/pgactivity/types.py index ad20de63..1fcaeb58 100644 --- a/pgactivity/types.py +++ b/pgactivity/types.py @@ -168,17 +168,6 @@ def default(cls) -> "SortKey": return cls.duration -@enum.unique -class QueryDisplayMode(enum.IntEnum): - truncate = 1 - wrap_noindent = 2 - wrap = 3 - - @classmethod - def default(cls) -> "QueryDisplayMode": - return cls.wrap_noindent - - @enum.unique class QueryMode(enum.Enum): activities = "running queries" @@ -292,9 +281,7 @@ class UI: duration_mode: DurationMode = attr.ib( default=DurationMode.query, converter=DurationMode ) - query_display_mode: QueryDisplayMode = attr.ib( - default=QueryDisplayMode.default(), converter=QueryDisplayMode - ) + wrap_query: bool = False sort_key: SortKey = attr.ib(default=SortKey.default(), converter=SortKey) query_mode: QueryMode = attr.ib(default=QueryMode.activities, converter=QueryMode) refresh_time: Union[float, int] = 2 @@ -595,7 +582,7 @@ def evolve(self, **changes: Any) -> None: return forbidden = set(changes) - { "duration_mode", - "query_display_mode", + "wrap_query", "sort_key", "query_mode", "refresh_time", diff --git a/pgactivity/ui.py b/pgactivity/ui.py index 98767087..999011c6 100644 --- a/pgactivity/ui.py +++ b/pgactivity/ui.py @@ -36,7 +36,7 @@ def main( flag=flag, min_duration=options.minduration, duration_mode=int(options.durationmode), - query_display_mode=int(options.querydisplaymode), + wrap_query=options.wrap_query, max_db_length=min(max(int(pg_db_info["max_length"]), 8), 16), filters=data.filters, ) @@ -134,9 +134,7 @@ def main( ui.end_interactive() changes = { "duration_mode": handlers.duration_mode(key, ui.duration_mode), - "query_display_mode": handlers.query_display_mode( - key, ui.query_display_mode - ), + "wrap_query": handlers.wrap_query(key, ui.wrap_query), } if key in (keys.REFRESH_TIME_INCREASE, keys.REFRESH_TIME_DECREASE): changes["refresh_time"] = handlers.refresh_time( diff --git a/pgactivity/views.py b/pgactivity/views.py index ce913858..6cc4fd56 100644 --- a/pgactivity/views.py +++ b/pgactivity/views.py @@ -35,7 +35,6 @@ Host, IOCounter, MemoryInfo, - QueryDisplayMode, SelectableProcesses, SystemInfo, UI, @@ -356,42 +355,16 @@ def cell( cell(getattr(process, field), column) indent = get_indent(ui) + " " - dif = width - len(indent) - - query_display_mode = ui.query_display_mode - if dif < 0: - # Switch to wrap_noindent mode if terminal is too narrow. - query_display_mode = QueryDisplayMode.wrap_noindent + qwidth = width - len(indent) if process.query is not None: query = format_query(process.query, process.is_parallel_worker) - if query_display_mode == QueryDisplayMode.truncate: - query_value = query[:dif] + if not ui.wrap_query: + query_value = query[:qwidth] else: - if query_display_mode == QueryDisplayMode.wrap_noindent: - if term.length(query.split(" ", 1)[0]) >= dif: - # Query too long to even start on the first line, wrap all - # lines. - query_lines = TextWrapper(width).wrap(query) - else: - # Only wrap subsequent lines. - wrapped_lines = TextWrapper(dif, drop_whitespace=False).wrap( - query - ) - if wrapped_lines: - query_lines = [wrapped_lines[0]] + TextWrapper(width).wrap( - "".join(wrapped_lines[1:]).lstrip() - ) - else: - query_lines = [] - query_value = "\n".join(query_lines) - else: - assert ( - query_display_mode == QueryDisplayMode.wrap - ), f"unexpected mode {query_display_mode}" - wrapped_lines = TextWrapper(dif).wrap(query) - query_value = f"\n{indent}".join(wrapped_lines) + wrapped_lines = TextWrapper(qwidth).wrap(query) + query_value = f"\n{indent}".join(wrapped_lines) cell(query_value, ui.column("query")) diff --git a/tests/test_ui.txt b/tests/test_ui.txt index 2b978b7a..98a9c099 100644 --- a/tests/test_ui.txt +++ b/tests/test_ui.txt @@ -40,7 +40,7 @@ Default CLI options, passed to ui.main(): ... "port": f"{postgres.info.port}", ... "rds": False, ... "username": f"{postgres.info.user}", -... "querydisplaymode": "2", +... "wrap_query": False, ... } >>> options = optparse.Values(defaults=defaults) @@ -138,7 +138,7 @@ Released under PostgreSQL License. Space: pause/unpause +: increase refresh time (max:5s) -: decrease refresh time (min:0.5s) - v: change display mode + v: toggle query wrap T: change duration mode D: force refresh database size R: force refresh @@ -570,7 +570,7 @@ Use another set of options: ... defaults=dict( ... defaults, ... durationmode="2", -... querydisplaymode="3", +... wrap_query=True, ... noclient=True, ... nouser=True, ... nodbsize=True, @@ -681,7 +681,7 @@ Interactive mode: ... defaults=dict( ... defaults, ... durationmode="1", -... querydisplaymode="3", +... wrap_query=True, ... nopid=False, ... noclient=True, ... nouser=True, diff --git a/tests/test_views.txt b/tests/test_views.txt index 48e87f67..f75ed503 100644 --- a/tests/test_views.txt +++ b/tests/test_views.txt @@ -22,7 +22,7 @@ Released under PostgreSQL License. t: sort by TIME+ desc. (activities) +: increase refresh time (max:5s) -: decrease refresh time (min:0.5s) - v: change display mode + v: toggle query wrap T: change duration mode D: force refresh database size R: force refresh @@ -42,7 +42,7 @@ Released under PostgreSQL License. Space: pause/unpause +: increase refresh time (max:5s) -: decrease refresh time (min:0.5s) - v: change display mode + v: toggle query wrap T: change duration mode D: force refresh database size R: force refresh @@ -154,31 +154,11 @@ Tests for processes_rows() >>> ui = UI.make(flag=Flag.PID|Flag.CPU|Flag.MEM|Flag.DATABASE) >>> processes_rows(term, ui, SelectableProcesses(processes1), 100) -6239 pgbench 0.1 1.0 idle in trans UPDATE pgbench_accounts -SET abalance = abalance + 141 WHERE aid = 1932841; -6228 pgbench 0.2 1.0 active \_ UPDATE -pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 7289374; -1234 business 2.4 1.0 active SELECT product_id, p.name -FROM products p LEFT JOIN sales s USING (product_id) WHERE s.date > CURRENT_DATE -- INTERVAL '4 weeks' GROUP BY product_id, p.name, p.price, p.cost HAVING -sum(p.price * s.units) > 5000; - -6239 pgbench 0.1 1.0 idle in trans UPDATE pgbench_accounts -SET abalance = abalance + 141 WHERE aid = 1932841; -6228 pgbench 0.2 1.0 active \_ UPDATE -pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 7289374; -1234 business 2.4 1.0 active SELECT product_id, -p.name FROM products p LEFT JOIN sales s USING (product_id) WHERE s.date > -CURRENT_DATE - INTERVAL '4 weeks' GROUP BY product_id, p.name, p.price, p.cost -HAVING sum(p.price * s.units) > 5000; - ->>> ui.evolve(query_display_mode=QueryDisplayMode.truncate) ->>> processes_rows(term, ui, SelectableProcesses(processes1), 100) 6239 pgbench 0.1 1.0 idle in trans UPDATE pgbench_accounts S 6228 pgbench 0.2 1.0 active \_ UPDATE pgbench_account 1234 business 2.4 1.0 active SELECT product_id, p.name ->>> ui.evolve(query_display_mode=QueryDisplayMode.wrap) +>>> ui.evolve(wrap_query=True) >>> processes_rows(term, ui, SelectableProcesses(processes1), 100) 6239 pgbench 0.1 1.0 idle in trans UPDATE pgbench_accounts SET abalance = abalance + @@ -209,16 +189,14 @@ Terminal is too narrow given selected flags, we switch to wrap_noindent mode 6239 pgbench pgbench postgres local 0.1 1.0 7B 12B 0.000000 N N idle in trans UPDATE pgbench_accounts SET abalance = abalance + 141 WHERE aid = 1932841; 6228 pgbench pgbench postgres local 0.2 1.0 0B 1.08M 0.000413 Y active \_ UPDATE pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 7289374; 1234 business accounting bob local 2.4 1.0 9.42M 1.21K 20:34.00 BackendRandomLoc N active SELECT product_id, p.name FROM products p LEFT JOIN sales s USING (product_id) WHERE s.date > -CURRENT_DATE - INTERVAL '4 weeks' GROUP BY product_id, p.name, p.price, p.cost HAVING sum(p.price * s.units) > 5000; ->>> ui = UI.make(flag=Flag.PID|Flag.DATABASE, -... query_display_mode=QueryDisplayMode.truncate) +>>> ui = UI.make(flag=Flag.PID|Flag.DATABASE, wrap_query=False) >>> processes_rows(term, ui, SelectableProcesses(processes1), 100) 6239 pgbench idle in trans UPDATE pgbench_accounts SET abalance 6228 pgbench active \_ UPDATE pgbench_accounts SET abalan 1234 business active SELECT product_id, p.name FROM produc ->>> ui.evolve(query_display_mode=QueryDisplayMode.wrap) +>>> ui.evolve(wrap_query=True) >>> processes_rows(term, ui, SelectableProcesses(processes1), 100) 6239 pgbench idle in trans UPDATE pgbench_accounts SET abalance = abalance + 141 WHERE aid = 1932841; @@ -318,7 +296,6 @@ CURRENT_DATE - INTERVAL '4 weeks' GROUP BY product_id, p.name, p.price, p.cost H 6239 pgbench pgbench postgres local 0.000000 N idle in trans UPDATE pgbench_accounts SET abalance = abalance + 141 WHERE aid = 1932841; 6228 pgbench none postgres local 0.000413 Y active \_ UPDATE pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 7289374; 1234 business accounting bob 192.168.0.47 20:34.00 clog active SELECT product_id, p.name FROM products p LEFT JOIN sales s USING (product_id) -WHERE s.date > CURRENT_DATE - INTERVAL '4 weeks' GROUP BY product_id, p.name, p.price, p.cost HAVING sum(p.price * s.units) > 5000; Tests scrolling in processes_rows() ----------------------------------- @@ -427,7 +404,7 @@ Tests for screen() ... query_mode=QueryMode.activities, ... flag=Flag.PID|Flag.CPU|Flag.MEM|Flag.DATABASE, ... sort_key=SortKey.cpu, -... query_display_mode=QueryDisplayMode.truncate, +... wrap_query=False, ... in_pause=False, ... ) >>> screen(