Browse Source

Improve the crontab snippet in the "Check Details" page

Fixes: #465
pull/468/head
Pēteris Caune 4 years ago
parent
commit
599f35e4f0
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
4 changed files with 80 additions and 3 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +30
    -0
      hc/front/templatetags/hc_extras.py
  3. +31
    -0
      hc/front/tests/test_details.py
  4. +18
    -3
      templates/front/show_usage_modal.html

+ 1
- 0
CHANGELOG.md View File

@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
- Add tighter parameter checks in hc.front.views.serve_doc
- Update OpsGenie instructions (#450)
- Update the email notification template to include more check and last ping details
- Improve the crontab snippet in the "Check Details" page (#465)
## v1.18.0 - 2020-12-09


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

@ -157,3 +157,33 @@ def format_headers(headers):
@register.simple_tag
def now_isoformat():
return now().replace(microsecond=0).isoformat()
@register.filter
def guess_schedule(check):
if check.kind == "cron":
return check.schedule
v = int(check.timeout.total_seconds())
# every minute
if v == 60:
return "* * * * *"
# every hour
if v == 3600:
return "0 * * * *"
# every day
if v == 3600 * 24:
return "0 0 * * *"
# every X minutes, if 60 is divisible by X
minutes, seconds = divmod(v, 60)
if minutes in (2, 3, 4, 5, 6, 10, 12, 15, 20, 30) and seconds == 0:
return f"*/{minutes} * * * *"
# every X hours, if 24 is divisible by X
hours, seconds = divmod(v, 3600)
if hours in (2, 3, 4, 6, 8, 12) and seconds == 0:
return f"0 */{hours} * * *"

+ 31
- 0
hc/front/tests/test_details.py View File

@ -1,3 +1,5 @@
from datetime import timedelta as td
from hc.api.models import Check, Ping
from hc.test import BaseTestCase
@ -74,3 +76,32 @@ class DetailsTestCase(BaseTestCase):
r = self.client.get(self.url)
self.assertNotContains(r, "resume-btn", status_code=200)
def test_crontab_example_guesses_schedules(self):
self.client.login(username="[email protected]", password="password")
pairs = [
(td(minutes=1), "* * * * *"),
(td(minutes=12), "*/12 * * * *"),
(td(hours=1), "0 * * * *"),
(td(hours=6), "0 */6 * * *"),
(td(days=1), "0 0 * * *"),
]
for timeout, expression in pairs:
self.check.timeout = timeout
self.check.save()
r = self.client.get(self.url)
self.assertContains(r, f"{expression} /your/command.sh")
self.assertNotContains(r, 'FIXME: replace "* * * * *"')
def test_crontab_example_handles_unsupported_timeout_values(self):
self.client.login(username="[email protected]", password="password")
self.check.timeout = td(minutes=13)
self.check.save()
r = self.client.get(self.url)
self.assertContains(r, f"* * * * * /your/command.sh")
self.assertContains(r, 'FIXME: replace "* * * * *"')

+ 18
- 3
templates/front/show_usage_modal.html View File

@ -1,9 +1,13 @@
{% load hc_extras %}
<div id="show-usage-modal" class="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 id="usage-examples-title">Usage Examples</h4>
<h4 id="usage-examples-title">
{% if check.name %}"{{ check.name }}"{% endif %}
Usage Examples
</h4>
<ul class="nav nav-pills" role="tablist">
<li class="active">
<a href="#crontab" data-toggle="tab">Crontab</a>
@ -42,9 +46,20 @@
</div>
<div class="modal-body">
<div class="tab-content">
{% with ping_url=check.url|default:'<span class="ex"></span>' %}
{% with ping_url=check.url %}
<div role="tabpanel" class="tab-pane active" id="crontab">
{% include "front/snippets/crontab.html" %}
{% with check|guess_schedule as schedule %}
<div class="highlight">
<pre><span class="c1"># A sample crontab entry. Note the curl call appended after the command.</span>{% if not schedule %}
<span class="c1"># FIXME: replace "* * * * *" below with the correct cron expression!</span>{% endif %}
<span class="c1"># FIXME: replace "/your/command.sh" below with the correct command!</span>
{{ schedule|default:"* * * * *" }} /your/command.sh && curl -fsS -m 10 --retry 5 -o /dev/null {{ ping_url }}</pre>
</div>
<div class="highlight">
<pre><span class="c1"># Here's the part you need to append, provided here separately for easy copy/pasting:</span>
&& curl -fsS -m 10 --retry 5 -o /dev/null {{ ping_url }}</pre>
</div>
{% endwith %}
</div>
<div role="tabpanel" class="tab-pane" id="bash">
{% include "front/snippets/bash_curl.html" %}


Loading…
Cancel
Save