diagnostics: Add option to toggle daily run

The option is stored in kvstore. If no value is set, it is assumed to be
enabled.

Tests:

- Disable daily run. In development mode, diagnostic are not run after several
  minutes.

- Enable daily run. In development mode, diagnostics are run after several
  minutes.

Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
[sunil: Minor refactoring and update messages in UI]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
James Valleroy 2024-01-20 10:24:44 -05:00 committed by Sunil Mohan Adapa
parent 9c5491de7e
commit d7907e0ef3
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
4 changed files with 56 additions and 11 deletions

View File

@ -68,7 +68,7 @@ class DiagnosticsApp(app_module.App):
glib.schedule(3600, _warn_about_low_ram_space)
# Run diagnostics once a day
glib.schedule(24 * 3600, start_diagnostics, in_thread=False)
glib.schedule(24 * 3600, _daily_diagnostics_run, in_thread=False)
def setup(self, old_version):
"""Install and configure the app."""
@ -246,6 +246,15 @@ def _warn_about_low_ram_space(request):
actions=actions, data=data, group='admin')
def _daily_diagnostics_run(data: None = None):
"""Start daily run if enabled."""
if is_daily_run_enabled():
logger.info('Starting daily diagnostics run')
start_diagnostics()
else:
logger.info('Skipping daily diagnostics run (disabled)')
def start_diagnostics(data: None = None):
"""Start full diagnostics as a background operation."""
logger.info('Running full diagnostics')
@ -343,3 +352,13 @@ def get_results():
results['results'][app_id]['name'] = app.info.name or app_id
return results
def is_daily_run_enabled() -> bool:
"""Return whether daily run is enabled."""
return kvstore.get_default('diagnostics_daily_run_enabled', True)
def set_daily_run_enabled(enabled: bool):
"""Enable or disable daily run."""
kvstore.set('diagnostics_daily_run_enabled', enabled)

View File

@ -0,0 +1,12 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Forms for configuring diagnostics."""
from django import forms
from django.utils.translation import gettext_lazy as _
class ConfigureForm(forms.Form):
"""Configuration form to enable/disable daily diagnostics run."""
daily_run_enabled = forms.BooleanField(
label=_('Enable daily run'), required=False,
help_text=_('When enabled, diagnostic checks will run once a day.'))

View File

@ -6,14 +6,15 @@
{% load i18n %}
{% load static %}
{% block configuration %}
{% block extra_content %}
<h3>{% trans "Diagnostics Run" %}</h3>
<div class="btn-toolbar">
<form class="form form-diagnostics-full" method="post"
action="{% url 'diagnostics:full' %}">
{% csrf_token %}
<input type="submit" class="btn btn-primary"
value="{% trans "Run Diagnostics" %}"/>
<input type="submit" class="btn btn-default"
value="{% trans "Run Diagnostics Now" %}"/>
</form>
{% if results_available %}

View File

@ -5,9 +5,9 @@ FreedomBox app for running diagnostics.
import logging
from django.http import Http404, HttpResponseRedirect
from django.contrib import messages
from django.http import Http404
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.decorators.http import require_POST
from django.views.generic import TemplateView
@ -18,6 +18,7 @@ from plinth.modules import diagnostics
from plinth.views import AppView
from .check import Result
from .forms import ConfigureForm
logger = logging.getLogger(__name__)
@ -26,13 +27,9 @@ class DiagnosticsView(AppView):
"""Diagnostics app page."""
app_id = 'diagnostics'
form_class = ConfigureForm
template_name = 'diagnostics.html'
def post(self, request):
"""Start diagnostics."""
diagnostics.start_diagnostics()
return HttpResponseRedirect(reverse('diagnostics:index'))
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
context = super().get_context_data(**kwargs)
@ -40,6 +37,22 @@ class DiagnosticsView(AppView):
context['results_available'] = diagnostics.are_results_available()
return context
def get_initial(self):
"""Return the initial values for the form."""
status = super().get_initial()
status['daily_run_enabled'] = diagnostics.is_daily_run_enabled()
return status
def form_valid(self, form):
"""Apply the form changes."""
old_status = form.initial
new_status = form.cleaned_data
if old_status['daily_run_enabled'] != new_status['daily_run_enabled']:
diagnostics.set_daily_run_enabled(new_status['daily_run_enabled'])
messages.success(self.request, _('Configuration updated.'))
return super().form_valid(form)
class DiagnosticsFullView(TemplateView):
"""View to run full diagnostics."""