Browse Source

Fix N+1 queries issue in "My Checks" and clean up Check.url()

pull/563/head
Pēteris Caune 3 years ago
parent
commit
9517035501
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 28 additions and 17 deletions
  1. +11
    -11
      hc/api/models.py
  2. +14
    -0
      hc/front/templatetags/hc_extras.py
  3. +1
    -0
      hc/front/views.py
  4. +2
    -6
      templates/front/my_checks_desktop.html

+ 11
- 11
hc/api/models.py View File

@ -115,23 +115,23 @@ class Check(models.Model):
return str(self.code)
def relative_url(self):
def url(self):
""" Return check's ping url in user's preferred style.
Note: this method reads self.project. If project is not loaded already,
this causes a SQL query.
"""
if self.project_id and self.project.show_slugs:
if not self.slug:
return None
key = self.project.ping_key
# If ping_key is not set, use dummy placeholder
if key is None:
key = "{ping_key}"
return key + "/" + self.slug
return str(self.code)
key = self.project.ping_key or "{ping_key}"
return settings.PING_ENDPOINT + key + "/" + self.slug
def url(self):
s = self.relative_url()
if s:
return settings.PING_ENDPOINT + s
return settings.PING_ENDPOINT + str(self.code)
def details_url(self):
return settings.SITE_ROOT + reverse("hc-details", args=[self.code])


+ 14
- 0
hc/front/templatetags/hc_extras.py View File

@ -213,3 +213,17 @@ def guess_schedule(check):
hours, seconds = divmod(v, 3600)
if hours in (2, 3, 4, 6, 8, 12) and seconds == 0:
return f"0 */{hours} * * *"
FORMATTED_PING_ENDPOINT = (
f"""<span class="base hidden-md">{settings.PING_ENDPOINT}</span>"""
)
@register.filter
def format_ping_endpoint(ping_url):
""" Wrap the ping endpoint in span tags for styling with CSS. """
assert ping_url.startswith(settings.PING_ENDPOINT)
tail = ping_url[len(settings.PING_ENDPOINT) :]
return mark_safe(FORMATTED_PING_ENDPOINT + escape(tail))

+ 1
- 0
hc/front/views.py View File

@ -182,6 +182,7 @@ def my_checks(request, code):
request.session["last_project_id"] = project.id
q = Check.objects.filter(project=project)
q = q.select_related("project")
checks = list(q.prefetch_related("channel_set"))
sortchecks(checks, request.profile.sort)


+ 2
- 6
templates/front/my_checks_desktop.html View File

@ -84,12 +84,8 @@
{% if project.show_slugs and not check.slug %}
<span class="unavailable">unavailable, set name first</span>
{% else %}
<span class="my-checks-url">
<span class="base hidden-md">{{ ping_endpoint }}</span>{{ check.relative_url }}
</span>
<button class="copy-link" data-clipboard-text="{{ check.url }}">
copy
</button>
<span class="my-checks-url">{{ check.url|format_ping_endpoint }}</span>
<button class="copy-link" data-clipboard-text="{{ check.url }}">copy</button>
{% endif %}
</td>
<td class="hidden-xs">


Loading…
Cancel
Save