Skip to content

Commit

Permalink
Handle None value when sorting processes
Browse files Browse the repository at this point in the history
The duration field may be None in some situations (e.g. when viewing
blocking queries in transaction duration mode). We compare to 0 in such
cases in activities.sorted().

Fix #168.
  • Loading branch information
dlax committed Jan 27, 2021
1 parent 98510a3 commit 3888edd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions pgactivity/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def sorted(processes: List[T], *, key: SortKey, reverse: bool = False) -> List[T
... write=0.282_725_318_098_656_75,
... state="idle in transaction",
... query="UPDATE pgbench_accounts SET abalance = abalance + 141 WHERE aid = 1932841;",
... duration=0,
... duration=0.1,
... wait=False,
... io_wait=False,
... is_parallel_worker=False,
Expand All @@ -177,7 +177,7 @@ def sorted(processes: List[T], *, key: SortKey, reverse: bool = False) -> List[T
... write=0.113_090_128_201_154_74,
... state="active",
... query="UPDATE pgbench_accounts SET abalance = abalance + 3062 WHERE aid = 7289374;",
... duration=0,
... duration=None,
... wait=False,
... io_wait=False,
... is_parallel_worker=True,
Expand All @@ -190,10 +190,13 @@ def sorted(processes: List[T], *, key: SortKey, reverse: bool = False) -> List[T
>>> processes = sorted(processes, key=SortKey.mem)
>>> [p.pid for p in processes]
['6239', '6228']
>>> processes = sorted(processes, key=SortKey.duration, reverse=True)
>>> [p.pid for p in processes]
['6239', '6228']
"""
return builtins.sorted(
processes,
key=lambda p: getattr(p, key.name), # type: ignore # TODO: avoid getattr()
key=lambda p: getattr(p, key.name) or 0, # TODO: avoid getattr()
reverse=reverse,
)

Expand Down
2 changes: 1 addition & 1 deletion pgactivity/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ class BaseProcess:
database: str
user: str
client: str
duration: float
duration: Optional[float]
state: str
query: str
is_parallel_worker: bool
Expand Down

0 comments on commit 3888edd

Please sign in to comment.