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

Filter alerts #242

Merged
merged 9 commits into from
Feb 26, 2020
Merged

Filter alerts #242

merged 9 commits into from
Feb 26, 2020

Conversation

shokada
Copy link
Contributor

@shokada shokada commented Feb 25, 2020

No description provided.

@codecov-io
Copy link

codecov-io commented Feb 25, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@c6f1ad8). Click here to learn what that means.
The diff coverage is 57.14%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #242   +/-   ##
=========================================
  Coverage          ?   46.62%           
=========================================
  Files             ?       53           
  Lines             ?     2623           
  Branches          ?        0           
=========================================
  Hits              ?     1223           
  Misses            ?     1400           
  Partials          ?        0
Impacted Files Coverage Δ
promgen/models.py 28.72% <0%> (ø)
promgen/migrations/0016_alertlabel.py 100% <100%> (ø)
promgen/views.py 48.54% <42.85%> (ø)
promgen/tasks.py 45.45% <80%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c6f1ad8...9a1307e. Read the comment docs.

promgen/tasks.py Outdated
@shared_task
def index_alert(alert_pk):
alert = models.Alert.objects.get(pk=alert_pk)
labels = json.loads(alert.body)["commonLabels"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be written as labels = alert.json.get('commonLabels') since Alert model defines a .json property

Copy link
Contributor Author

@shokada shokada Feb 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 9a55893

{% include 'promgen/pagination_short.html' %}
<form class="navbar-form navbar-left" action="/alert">
<div class="form-group">
<input name="search" type="text" value="{{ search }}" class="form-control" placeholder="Service, Project">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can write this as value="{{ request.GET.search }}" and then you do not have to implement get_context_data in the view

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

promgen/views.py Outdated

def get_context_data(self, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would probably leave the queryset at the top, and then implement it something like

class AlertList(LoginRequiredMixin, ListView):
    paginate_by = 20
    queryset = models.Alert.objects.order_by("-created")

    def get_queryset(self):
        search = self.request.GET.get("search")
        if search:
            return self.queryset.filter(
                Q(alertlabel__name="Service", alertlabel__value__icontains=search)
                | Q(alertlabel__name="Project", alertlabel__value__icontains=search)
            )
        return self.queryset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@kfdm
Copy link
Collaborator

kfdm commented Feb 26, 2020

May also be worthwhile to implement a management command 1 to allow to queue alerts to be re-indexed.

for alert in Alert.objects.all():
    task. index_alert.delay(alert.pk)

@shokada
Copy link
Contributor Author

shokada commented Feb 26, 2020

May also be worthwhile to implement a management command 1 to allow to queue alerts to be re-indexed.

I will implement that.

@shokada
Copy link
Contributor Author

shokada commented Feb 26, 2020

@kfdm How about this? 9a1307e

@shokada shokada changed the title [WIP] Filter alerts Filter alerts Feb 26, 2020
promgen/views.py Outdated

qs = self.queryset
for key, value in self.request.GET.items():
if key == "page" or key == "search":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably write this as if key in ["page", "search"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

promgen/views.py Outdated
@@ -1033,8 +1033,15 @@ def get_queryset(self):
return self.queryset.filter(
Q(alertlabel__name="Service", alertlabel__value__contains=search)
| Q(alertlabel__name="Project", alertlabel__value__contains=search)
| Q(alertlabel__name="Job", alertlabel__value__contains=search)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be better to do alertlabel__value__icontains so that upper/lower case do not matter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

@kfdm kfdm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few minor things I think we can fix up in a future PR, but looks good for now :)

@kfdm kfdm merged commit 6f104d4 into line:master Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants