From 847679d90c653ffb23d2816e9df239250a22d775 Mon Sep 17 00:00:00 2001 From: Inna Kocherzhuk Date: Fri, 26 Jul 2024 10:01:43 +0200 Subject: [PATCH] [OSDEV-1142] Technical Debt. Remove unused `api_tilecache` and `api_dynamicsetting` tables. (#303) [OSDEV-1142](https://opensupplyhub.atlassian.net/browse/OSDEV-1142) Technical Debt. Remove unused `api_tilecache` and `api_dynamicsetting` tables. * Removed files with models Tilecache and Dynamicsetting * Added 0152 migration with deleting models and tables from db * Removed imports and the file with unused functions * Replaced direct model import with method get_model in 0133 migration [OSDEV-1142]: https://opensupplyhub.atlassian.net/browse/OSDEV-1142?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: Inessa Druzhkova --- doc/release/RELEASE-NOTES.md | 28 +++++++ src/django/api/admin.py | 6 -- .../migrations/0133_introduce_tile_caching.py | 4 +- ...152_delete_tilecache_and_dynamicsetting.py | 20 +++++ src/django/api/models/__init__.py | 2 - src/django/api/models/dynamic_setting.py | 37 --------- src/django/api/models/tile_cache.py | 37 --------- src/django/api/tests/test_tile_caching.py | 79 ------------------- src/django/api/views/tile/utils.py | 23 ------ 9 files changed, 49 insertions(+), 187 deletions(-) create mode 100644 src/django/api/migrations/0152_delete_tilecache_and_dynamicsetting.py delete mode 100644 src/django/api/models/dynamic_setting.py delete mode 100644 src/django/api/models/tile_cache.py delete mode 100644 src/django/api/tests/test_tile_caching.py delete mode 100644 src/django/api/views/tile/utils.py diff --git a/doc/release/RELEASE-NOTES.md b/doc/release/RELEASE-NOTES.md index cfac4f3ce..393b5b2ec 100644 --- a/doc/release/RELEASE-NOTES.md +++ b/doc/release/RELEASE-NOTES.md @@ -3,6 +3,34 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). The format is based on the `RELEASE-NOTES-TEMPLATE.md` file. +## Release 1.18.0 + +## Introduction +* Product name: Open Supply Hub +* Release date: August 10, 2024 + +### Database changes +#### Migrations: +* 0152_delete_tilecache_and_dynamicsetting - removed unused `api_tilecache` and `api_dynamicsetting` tables. + +#### Scheme changes +* [OSDEV-1142](https://opensupplyhub.atlassian.net/browse/OSDEV-1142) - Technical Debt. Remove unused `api_tilecache` and `api_dynamicsetting` tables. Migration has been created, removed related data in the code base. + +### Code/API changes +* *Describe code/API changes here.* + +### Architecture/Environment changes +* *Describe architecture/environment changes here.* + +### Bugfix +* *Describe bugfix here.* + +### What's new +* *Describe what's new here. The changes that can impact user experience should be listed in this section.* + +### Release instructions: +* *Provide release instructions here.* + ## Release 1.17.0 diff --git a/src/django/api/admin.py b/src/django/api/admin.py index 0b8c776b9..097ba8bee 100644 --- a/src/django/api/admin.py +++ b/src/django/api/admin.py @@ -181,10 +181,6 @@ def get_ordering(self, request): return ['name'] -class TileCacheAdmin(SimpleHistoryAdmin): - search_fields = ('path', 'embed') - - admin_site.register(models.Version) admin_site.register(models.User, OarUserAdmin) admin_site.register(models.Contributor, ContributorAdmin) @@ -205,5 +201,3 @@ class TileCacheAdmin(SimpleHistoryAdmin): admin_site.register(models.RequestLog, RequestLogAdmin) admin_site.register(models.ApiLimit, ApiLimitAdmin) admin_site.register(models.Sector, SectorAdmin) -admin_site.register(models.TileCache, TileCacheAdmin) -admin_site.register(models.DynamicSetting) diff --git a/src/django/api/migrations/0133_introduce_tile_caching.py b/src/django/api/migrations/0133_introduce_tile_caching.py index 37fc53839..5b8c4f3eb 100644 --- a/src/django/api/migrations/0133_introduce_tile_caching.py +++ b/src/django/api/migrations/0133_introduce_tile_caching.py @@ -5,16 +5,14 @@ from django.db import models from django.db.migrations import Migration, CreateModel, RunPython -from api.models.dynamic_setting import DynamicSetting - def add_default_dynamic_settings(apps, schema_editor): + DynamicSetting = apps.get_model('api', 'DynamicSetting') dynamic_settings = DynamicSetting() dynamic_settings.save() class Migration(Migration): - dependencies = [ ('api', '0132_add_moderation_mode_field'), ] diff --git a/src/django/api/migrations/0152_delete_tilecache_and_dynamicsetting.py b/src/django/api/migrations/0152_delete_tilecache_and_dynamicsetting.py new file mode 100644 index 000000000..cee4f8809 --- /dev/null +++ b/src/django/api/migrations/0152_delete_tilecache_and_dynamicsetting.py @@ -0,0 +1,20 @@ + +# Generated by Django 3.2.4 on 2024-07-24 12:38 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0151_replace_index_number_of_workers'), + ] + + operations = [ + migrations.DeleteModel( + name='DynamicSetting', + ), + migrations.DeleteModel( + name='TileCache', + ), + ] diff --git a/src/django/api/models/__init__.py b/src/django/api/models/__init__.py index 185e4cea7..e8d4148a8 100644 --- a/src/django/api/models/__init__.py +++ b/src/django/api/models/__init__.py @@ -77,5 +77,3 @@ User, ) from .version import Version -from .tile_cache import TileCache -from .dynamic_setting import DynamicSetting diff --git a/src/django/api/models/dynamic_setting.py b/src/django/api/models/dynamic_setting.py deleted file mode 100644 index b391f64c1..000000000 --- a/src/django/api/models/dynamic_setting.py +++ /dev/null @@ -1,37 +0,0 @@ -from django.db import models - - -class DynamicSetting(models.Model): - """ - This table serves as a dynamic settings. - """ - cached_tile_expiration_time = models.IntegerField( - default=604800, - null=False, - blank=False, - help_text='The expiration time in seconds for the cached tile.') - - def __str__(self): - return ('Dynamic settings, update this entry to configure the app ' - 'dynamically.') - - def save(self, *args, **kwargs): - if DynamicSetting.objects.exists() and self.pk == 1: - # If an entry already exists, allow updates. - super().save(*args, **kwargs) - else: - # If no entry exists, allow creation of the first entry. - if not DynamicSetting.objects.exists() and self.pk is None: - super().save(*args, **kwargs) - else: - raise Exception(('Creating a new entry is not allowed. Only ' - 'updates of the existing one are permitted.')) - - def delete(self, *args, **kwargs): - raise Exception(("The entry in the dynamic settings can't be deleted " - "since it can lead to the outage of the app.")) - - @classmethod - def load(cls): - obj = cls.objects.get(pk=1) - return obj diff --git a/src/django/api/models/tile_cache.py b/src/django/api/models/tile_cache.py deleted file mode 100644 index cabe07e5a..000000000 --- a/src/django/api/models/tile_cache.py +++ /dev/null @@ -1,37 +0,0 @@ -from django.db import models -from django.contrib.postgres import fields - - -class TileCache(models.Model): - """ - This table serves as a cache for the generated tiles. Each unique key has - a date to check whether it is a stale tile that needs to be regenerated. - """ - path = models.TextField( - primary_key=True, - null=False, - help_text='The requested tile URL.' - ) - embed = models.BooleanField( - default=False, - help_text='Whether the tile is generated for an embedded map.' - ) - contributors = fields.ArrayField(models.TextField( - null=False, - blank=False, - help_text='The ID of the contributor passed via query param.'), - default=list) - value = models.BinaryField( - null=False, - help_text='The cached binary tile.' - ) - counter = models.IntegerField( - default=0, - null=False, - help_text='The count of the number of times the cached tile was used.') - - created_at = models.DateTimeField(auto_now_add=True) - updated_at = models.DateTimeField(auto_now=True) - - def __str__(self): - return f'Path - {self.path} embed - {self.embed}' diff --git a/src/django/api/tests/test_tile_caching.py b/src/django/api/tests/test_tile_caching.py deleted file mode 100644 index 725dbc481..000000000 --- a/src/django/api/tests/test_tile_caching.py +++ /dev/null @@ -1,79 +0,0 @@ -import time - -from api.models import DynamicSetting -from api.views.tile.utils import retrieve_cached_tile -from rest_framework.test import APITestCase -from waffle.testutils import override_switch - -from django.test import override_settings -from django.urls import reverse -from unittest import skip - - -@skip("We don't use the tile caching feature anymore.") -class TileCachingTest(APITestCase): - def setUp(self): - self.domain = "http://allowed.org/" - self.tile_path = reverse( - "tile", - kwargs={ - "layer": "facilitygrid", - "cachekey": "1567700347-1-95f951f7", - "z": 6, - "x": 20, - "y": 21, - "ext": "pbf", - }, - ) - - @override_settings(ALLOWED_HOSTS=["testserver", ".allowed.org"]) - @override_switch("vector_tile", active=True) - def make_tile_request(self): - response = self.client.get( - self.tile_path, {}, HTTP_REFERER=self.domain - ) - return response.status_code - - def test_a_requested_tile_is_cached(self): - self.assertEqual(200, self.make_tile_request()) - - cached_tile, created = retrieve_cached_tile(self.tile_path) - self.assertEqual(0, cached_tile.counter) - self.assertFalse(created) - - def test_requested_cached_tile_is_returned_from_cache(self): - self.assertEqual(200, self.make_tile_request()) - self.assertEqual(200, self.make_tile_request()) - - cached_tile, created = retrieve_cached_tile(self.tile_path) - self.assertEqual(1, cached_tile.counter) - self.assertFalse(created) - - def test_expiration_time_of_cached_tile(self): - dynamic_settings = DynamicSetting.load() - dynamic_settings.cached_tile_expiration_time = 20 - dynamic_settings.save() - - self.assertEqual(200, self.make_tile_request()) - cached_tile1, created1 = retrieve_cached_tile(self.tile_path) - self.assertEqual(0, cached_tile1.counter) - self.assertFalse(created1) - - self.assertEqual(200, self.make_tile_request()) - cached_tile2, created2 = retrieve_cached_tile(self.tile_path) - self.assertEqual(cached_tile1.value, cached_tile2.value) - self.assertEqual(1, cached_tile2.counter) - self.assertFalse(created2) - - time.sleep(25) - - # Verify that the cached tile binary data and the associated URL path - # have expired, and consequently, the entry with the previously - # stored path was refreshed with new tile binary data and the counter - # field was reset to zero. - self.assertEqual(200, self.make_tile_request()) - cached_tile3, created3 = retrieve_cached_tile(self.tile_path) - self.assertEqual(cached_tile2.value, cached_tile3.value) - self.assertEqual(0, cached_tile3.counter) - self.assertEqual(cached_tile1.created_at, cached_tile3.created_at) - self.assertFalse(created3) diff --git a/src/django/api/views/tile/utils.py b/src/django/api/views/tile/utils.py deleted file mode 100644 index e2069bfce..000000000 --- a/src/django/api/views/tile/utils.py +++ /dev/null @@ -1,23 +0,0 @@ -from datetime import datetime -from typing import Tuple - -from django.utils import timezone - -from api.models.tile_cache import TileCache -from api.models.dynamic_setting import DynamicSetting - - -def retrieve_cached_tile(request_full_path: str) -> Tuple[TileCache, bool]: - cached_tile, created = TileCache.objects \ - .select_for_update().get_or_create(path=request_full_path) - - return cached_tile, created - - -def is_tile_cache_valid(creation_date: datetime) -> bool: - settings = DynamicSetting.load() - current_datetime = timezone.now() - expiration_date = current_datetime - timezone.timedelta( - seconds=settings.cached_tile_expiration_time) - - return creation_date > expiration_date