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

Adding flag to sort incident list by field name (with reverse option and multiple fields support) #197

Merged
merged 4 commits into from
Sep 26, 2024
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,26 @@ Any other incident currently outstanding:
pdh inc ls -e
```

### Sorting incident by field

```bash
pdh inc ls --sort assignee --reverse
```

In case the field is not found the cli will notice you and print the list of available fields

```bash
pdh inc ls -e --sort unkn --reverse
Invalid sort field: unkn
Available fields: id, assignee, title, status, created_at, last_status_change_at, url
```

You can always sort by multiple fields using comma as separator:

```bash
pdh inc ls --sort assignee,status
```

### Auto ACK incoming incidents

Watch for new incidents every 10s and automatically set them to `Acknowledged`
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ignore = [

[tool.poetry]
name = "pdh"
version = "0.4.3"
version = "0.4.4"
description = "Pagerduty CLI for Humans"
authors = ["Manuel Bovo <manuel.bovo@gmail.com>"]
license = "GPL-3.0-or-later"
Expand Down
2 changes: 1 addition & 1 deletion rules/examples.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/pdh/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/pdh/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/pdh/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
4 changes: 2 additions & 2 deletions src/pdh/filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -132,7 +132,7 @@ def f(item: dict) -> bool:

return f

def do(objects: list, transformations: dict = None, filters: list = [], preserve : bool = False) -> list:
def do(objects: list, transformations: dict|None = None, filters: list = [], preserve : bool = False) -> list:
"""Given a list of objects, apply every transformations and filters on it, return the new filtered list
Transformations is a dict of "key": func(item) where key is the destination key and func(item) the
function to used to extract values from the original list (see Transformation class)
Expand Down
20 changes: 18 additions & 2 deletions src/pdh/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -241,7 +241,9 @@ def apply(ctx, incident, path, output, script):
@click.option("--alert-fields", "alert_fields", required=False, help="Show these alert fields only, comma separated", default=None)
@click.option("-S","--service-re", "service_re", required=False, help="Show only incidents for this service (regexp)", default=None)
@click.option("--excluded-service-re", "excluded_service_re", required=False, help="Exclude incident of these services (regexp)", default=None)
def inc_list(ctx, everything, user, new, ack, output, snooze, resolve, high, low, watch, timeout, regexp, apply, rules_path, fields, alerts, alert_fields, service_re, excluded_service_re):
@click.option("--sort", "sort_by", required=False, help="Sort by field name", default=None)
@click.option("--reverse", "reverse_sort", required=False, help="Reverse the sort", is_flag=True, default=False)
def inc_list(ctx, everything, user, new, ack, output, snooze, resolve, high, low, watch, timeout, regexp, apply, rules_path, fields, alerts, alert_fields, service_re, excluded_service_re, sort_by, reverse_sort):

# Prepare defaults
status = [STATUS_TRIGGERED]
Expand Down Expand Up @@ -329,6 +331,20 @@ def plain_print_f(i):
s += f"{i[f]}\t"
print(s)


if sort_by:
try:
sort_fields: str|list[str] = sort_by.split(",") if ',' in sort_by else sort_by

if isinstance(sort_fields, list) and len(sort_fields) > 1:
filtered = sorted(filtered, key=lambda x: [x[k] for k in sort_fields], reverse=reverse_sort)
else:
filtered = sorted(filtered, key=lambda x: x[sort_fields], reverse=reverse_sort)
except KeyError:
print(f"[red]Invalid sort field: {sort_by}[/red]")
print(f"[yellow]Available fields: {', '.join(fields)}[/yellow]")
sys.exit(-2)

print_items(filtered, output, plain_print_f=plain_print_f)

# now apply actions like snooze, resolve, ack...
Expand Down
2 changes: 1 addition & 1 deletion src/pdh/output.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/pdh/pd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/pdh/rules.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion src/pdh/transformations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion tests/test_rules.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
2 changes: 1 addition & 1 deletion tests/test_transformations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# This file is part of the pdh (https://github.com/mbovo/pdh).
# Copyright (c) 2020-2023 Manuel Bovo.
# Copyright (c) 2020-2024 Manuel Bovo.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down
Loading