mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
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:
parent
9c5491de7e
commit
d7907e0ef3
@ -68,7 +68,7 @@ class DiagnosticsApp(app_module.App):
|
|||||||
glib.schedule(3600, _warn_about_low_ram_space)
|
glib.schedule(3600, _warn_about_low_ram_space)
|
||||||
|
|
||||||
# Run diagnostics once a day
|
# 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):
|
def setup(self, old_version):
|
||||||
"""Install and configure the app."""
|
"""Install and configure the app."""
|
||||||
@ -246,6 +246,15 @@ def _warn_about_low_ram_space(request):
|
|||||||
actions=actions, data=data, group='admin')
|
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):
|
def start_diagnostics(data: None = None):
|
||||||
"""Start full diagnostics as a background operation."""
|
"""Start full diagnostics as a background operation."""
|
||||||
logger.info('Running full diagnostics')
|
logger.info('Running full diagnostics')
|
||||||
@ -343,3 +352,13 @@ def get_results():
|
|||||||
results['results'][app_id]['name'] = app.info.name or app_id
|
results['results'][app_id]['name'] = app.info.name or app_id
|
||||||
|
|
||||||
return results
|
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)
|
||||||
|
|||||||
12
plinth/modules/diagnostics/forms.py
Normal file
12
plinth/modules/diagnostics/forms.py
Normal 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.'))
|
||||||
@ -6,14 +6,15 @@
|
|||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
{% load static %}
|
{% load static %}
|
||||||
|
|
||||||
{% block configuration %}
|
{% block extra_content %}
|
||||||
|
|
||||||
|
<h3>{% trans "Diagnostics Run" %}</h3>
|
||||||
<div class="btn-toolbar">
|
<div class="btn-toolbar">
|
||||||
<form class="form form-diagnostics-full" method="post"
|
<form class="form form-diagnostics-full" method="post"
|
||||||
action="{% url 'diagnostics:full' %}">
|
action="{% url 'diagnostics:full' %}">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<input type="submit" class="btn btn-primary"
|
<input type="submit" class="btn btn-default"
|
||||||
value="{% trans "Run Diagnostics" %}"/>
|
value="{% trans "Run Diagnostics Now" %}"/>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% if results_available %}
|
{% if results_available %}
|
||||||
|
|||||||
@ -5,9 +5,9 @@ FreedomBox app for running diagnostics.
|
|||||||
|
|
||||||
import logging
|
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.template.response import TemplateResponse
|
||||||
from django.urls import reverse
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.views.decorators.http import require_POST
|
from django.views.decorators.http import require_POST
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
@ -18,6 +18,7 @@ from plinth.modules import diagnostics
|
|||||||
from plinth.views import AppView
|
from plinth.views import AppView
|
||||||
|
|
||||||
from .check import Result
|
from .check import Result
|
||||||
|
from .forms import ConfigureForm
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -26,13 +27,9 @@ class DiagnosticsView(AppView):
|
|||||||
"""Diagnostics app page."""
|
"""Diagnostics app page."""
|
||||||
|
|
||||||
app_id = 'diagnostics'
|
app_id = 'diagnostics'
|
||||||
|
form_class = ConfigureForm
|
||||||
template_name = 'diagnostics.html'
|
template_name = 'diagnostics.html'
|
||||||
|
|
||||||
def post(self, request):
|
|
||||||
"""Start diagnostics."""
|
|
||||||
diagnostics.start_diagnostics()
|
|
||||||
return HttpResponseRedirect(reverse('diagnostics:index'))
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
"""Return additional context for rendering the template."""
|
"""Return additional context for rendering the template."""
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
@ -40,6 +37,22 @@ class DiagnosticsView(AppView):
|
|||||||
context['results_available'] = diagnostics.are_results_available()
|
context['results_available'] = diagnostics.are_results_available()
|
||||||
return context
|
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):
|
class DiagnosticsFullView(TemplateView):
|
||||||
"""View to run full diagnostics."""
|
"""View to run full diagnostics."""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user