diff --git a/app/main/forms.py b/app/main/forms.py index 399705c24..6f6ad8447 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -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()] + ) diff --git a/app/main/views/platform_admin.py b/app/main/views/platform_admin.py index 04ab5ae2b..14a6110cd 100644 --- a/app/main/views/platform_admin.py +++ b/app/main/views/platform_admin.py @@ -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(): diff --git a/app/navigation.py b/app/navigation.py index befb5e046..e83ff3800 100644 --- a/app/navigation.py +++ b/app/navigation.py @@ -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', diff --git a/app/templates/views/platform-admin/_base_template.html b/app/templates/views/platform-admin/_base_template.html index 71d617dfc..90022f895 100644 --- a/app/templates/views/platform-admin/_base_template.html +++ b/app/templates/views/platform-admin/_base_template.html @@ -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')), ] %}