From 599f35e4f079646f2ffe5815f64eed90ee3cb244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 30 Dec 2020 13:49:33 +0200 Subject: [PATCH] Improve the crontab snippet in the "Check Details" page Fixes: #465 --- CHANGELOG.md | 1 + hc/front/templatetags/hc_extras.py | 30 ++++++++++++++++++++++++++ hc/front/tests/test_details.py | 31 +++++++++++++++++++++++++++ templates/front/show_usage_modal.html | 21 +++++++++++++++--- 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65b6507f..020a58d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/hc/front/templatetags/hc_extras.py b/hc/front/templatetags/hc_extras.py index 23c767b7..c440627b 100644 --- a/hc/front/templatetags/hc_extras.py +++ b/hc/front/templatetags/hc_extras.py @@ -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} * * *" diff --git a/hc/front/tests/test_details.py b/hc/front/tests/test_details.py index adb19a8d..7202acd7 100644 --- a/hc/front/tests/test_details.py +++ b/hc/front/tests/test_details.py @@ -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="alice@example.org", 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="alice@example.org", 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 "* * * * *"') diff --git a/templates/front/show_usage_modal.html b/templates/front/show_usage_modal.html index 9a115ea2..e319a942 100644 --- a/templates/front/show_usage_modal.html +++ b/templates/front/show_usage_modal.html @@ -1,9 +1,13 @@ +{% load hc_extras %}