From 738a64840704ebe8afec5a3c2f0fa5ab6d8f1fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 14 Apr 2021 16:18:43 +0300 Subject: [PATCH] Improve project sorting in the "My Projects" page Primary sort key: projects with overall_status=down go first Secondary sort key: project's name --- CHANGELOG.md | 1 + hc/accounts/models.py | 2 ++ hc/front/views.py | 14 ++++++++++++-- templates/front/projects.html | 12 +++++------- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2353c8c5..74b61e33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. - Improve HTML email display in the "Ping Details" dialog - Add a link to check's details page in Slack notifications - Replace details_url with cloaked_url in email and chat notifications +- In the "My Projects" page, show projects with failing checks first ## Bug Fixes - Fix downtime summary to handle months when the check didn't exist yet (#472) diff --git a/hc/accounts/models.py b/hc/accounts/models.py index 1d40c896..1dde19a0 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -1,4 +1,5 @@ from datetime import timedelta +from functools import cached_property from secrets import token_urlsafe from urllib.parse import quote, urlencode import uuid @@ -342,6 +343,7 @@ class Project(models.Model): for profile in q: profile.update_next_nag_date() + @cached_property def overall_status(self): status = "up" for check in self.check_set.all(): diff --git a/hc/front/views.py b/hc/front/views.py index 374f110a..a0163d4c 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -13,7 +13,7 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.core import signing from django.core.exceptions import PermissionDenied -from django.db.models import Count +from django.db.models import Count, F from django.http import ( Http404, HttpResponse, @@ -275,7 +275,17 @@ def switch_channel(request, code, channel_code): def index(request): if request.user.is_authenticated: - projects = list(request.profile.projects()) + project_ids = request.profile.projects().values("id") + + q = Project.objects.filter(id__in=project_ids) + q = q.annotate(n_checks=Count("check", distinct=True)) + q = q.annotate(n_channels=Count("channel", distinct=True)) + q = q.annotate(owner_email=F("owner__email")) + + projects = list(q) + # Primary sort key: projects with overall_status=down go first + # Secondary sort key: project's name + projects.sort(key=lambda p: (p.overall_status != "down", p.name)) ctx = { "page": "projects", diff --git a/templates/front/projects.html b/templates/front/projects.html index eb466774..3969a229 100644 --- a/templates/front/projects.html +++ b/templates/front/projects.html @@ -21,16 +21,14 @@

{{ project }}

- {% with project.check_set.count as n %} - {{ n }} check{{ n|pluralize }}, - {% endwith %} + {{ project.n_checks }} + check{{ project.n_checks|pluralize }}, - {% with project.channel_set.count as n %} - {{ n }} integration{{ n|pluralize }} - {% endwith %} + {{ project.n_channels }} + integration{{ project.n_channels|pluralize }}
- {{ project.owner.email }} + {{ project.owner_email }}