Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace --query-display-mode option by --wrap-query flag #264

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/man/pg_activity.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions docs/man/pg_activity.pod
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion pgactivity/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.2.1"
__version__ = "3.0.0.dev0"
22 changes: 6 additions & 16 deletions pgactivity/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 "
Expand Down
20 changes: 10 additions & 10 deletions pgactivity/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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]:
Expand Down
4 changes: 2 additions & 2 deletions pgactivity/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = " "
Expand Down Expand Up @@ -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"),
Expand Down
17 changes: 2 additions & 15 deletions pgactivity/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions pgactivity/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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(
Expand Down
37 changes: 5 additions & 32 deletions pgactivity/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
Host,
IOCounter,
MemoryInfo,
QueryDisplayMode,
SelectableProcesses,
SystemInfo,
UI,
Expand Down Expand Up @@ -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"))

Expand Down
8 changes: 4 additions & 4 deletions tests/test_ui.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -681,7 +681,7 @@ Interactive mode:
... defaults=dict(
... defaults,
... durationmode="1",
... querydisplaymode="3",
... wrap_query=True,
... nopid=False,
... noclient=True,
... nouser=True,
Expand Down
35 changes: 6 additions & 29 deletions tests/test_views.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 +
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
-----------------------------------
Expand Down Expand Up @@ -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(
Expand Down