add clear cache platform admin page

a form that allows you to clear entries from the cache for all of
either users, services or templates. It'll tell you the largest amount
of keys deleted, since there are multiple keys associated with each
model.
This commit is contained in:
Leo Hemsted
2019-02-15 10:27:38 +00:00
parent f6513613d3
commit 1dcba53daf
5 changed files with 85 additions and 2 deletions

View File

@@ -1281,3 +1281,15 @@ class TemplateAndFoldersSelectionForm(Form):
required_for_ops('add-new-template'),
Optional(),
], required_message='Select the type of template you want to add')
class ClearCacheForm(StripWhitespaceForm):
model_type = RadioField(
'What do you want to clear today',
choices=[
('user', 'Users'),
('service', 'Services'),
('template', 'Templates')
],
validators=[DataRequired()]
)

View File

@@ -13,9 +13,9 @@ from app import (
platform_stats_api_client,
service_api_client,
)
from app.extensions import antivirus_client
from app.extensions import antivirus_client, redis_client
from app.main import main
from app.main.forms import DateFilterForm, PDFUploadForm, ReturnedLettersForm
from app.main.forms import DateFilterForm, PDFUploadForm, ReturnedLettersForm, ClearCacheForm
from app.statistics_utils import (
get_formatted_percentage,
get_formatted_percentage_two_dp,
@@ -281,6 +281,50 @@ def platform_admin_letter_validation_preview():
)
@main.route("/platform-admin/clear-cache", methods=['GET', 'POST'])
@login_required
@user_is_platform_admin
def clear_cache():
# note: `service-{uuid}-templates` cache is cleared for both services and templates.
CACHE_KEYS = {
'user': [
'user-????????-????-????-????-????????????',
],
'service': [
'has_jobs-????????-????-????-????-????????????',
'service-????????-????-????-????-????????????',
'service-????????-????-????-????-????????????-templates',
'service-????????-????-????-????-????????????-data-retention',
'service-????????-????-????-????-????????????-template-folders',
],
'template': [
'service-????????-????-????-????-????????????-templates',
'template-????????-????-????-????-????????????-version-*',
'template-????????-????-????-????-????????????-versions',
],
'email_branding': [
'email_branding',
'email_branding-????????-????-????-????-????????????',
]
}
form = ClearCacheForm()
if form.validate_on_submit():
to_delete = form.model_type.data
num_deleted = max(
redis_client.delete_cache_keys_by_pattern(pattern)
for pattern in CACHE_KEYS[to_delete]
)
flash('Removed {} {} objects from redis'.format(num_deleted, to_delete))
return render_template(
'views/platform-admin/clear-cache.html',
form=form
)
def sum_service_usage(service):
total = 0
for notification_type in service['statistics'].keys():

View File

@@ -74,6 +74,7 @@ class HeaderNavigation(Navigation):
},
'platform-admin': {
'add_organisation',
'clear_cache',
'create_email_branding',
'create_letter_branding',
'email_branding',
@@ -302,6 +303,7 @@ class MainNavigation(Navigation):
'check_notification',
'choose_template',
'choose_template_to_copy',
'clear_cache',
'confirm_redact_template',
'conversation_reply',
'copy_template',
@@ -399,6 +401,7 @@ class MainNavigation(Navigation):
'check_notification_preview',
'choose_account',
'choose_service',
'clear_cache',
'confirm_edit_organisation_name',
'conversation_reply_with_template',
'conversation_updates',
@@ -573,6 +576,7 @@ class CaseworkNavigation(Navigation):
'choose_account',
'choose_service',
'choose_template_to_copy',
'clear_cache',
'confirm_edit_organisation_name',
'confirm_redact_template',
'conversation',
@@ -809,6 +813,7 @@ class OrgNavigation(Navigation):
'choose_service',
'choose_template',
'choose_template_to_copy',
'clear_cache',
'confirm_redact_template',
'conversation',
'conversation_reply',

View File

@@ -24,6 +24,7 @@
('Email Complaints', url_for('main.platform_admin_list_complaints')),
('Returned letters', url_for('main.platform_admin_returned_letters')),
('Letter validation preview', url_for('main.platform_admin_letter_validation_preview')),
('Clear cache', url_for('main.clear_cache')),
] %}
<li>
<a href="{{ url }}">

View File

@@ -0,0 +1,21 @@
{% extends "views/platform-admin/_base_template.html" %}
{% from "components/form.html" import form_wrapper %}
{% from "components/radios.html" import radios %}
{% from "components/page-footer.html" import page_footer %}
{% block per_page_title %}
Clear Cache
{% endblock %}
{% block platform_admin_content %}
<h1 class="heading-large">
Clear Redis Cache
</h1>
{% call form_wrapper() %}
{{ radios(form.model_type) }}
{{ page_footer('Clear') }}
{% endcall %}
{% endblock %}