mirror of
https://github.com/Dispatcharr/Dispatcharr.git
synced 2026-07-17 16:50:53 +00:00
enhancement(epg): Performance improvements and API enhancements for EPG refreshes.
- Updated EPG import logic to reject dummy sources without loading the large `programme_index`, optimizing memory usage during API calls.
This commit is contained in:
parent
460677aeea
commit
c2ac08fdfd
3 changed files with 114 additions and 8 deletions
|
|
@ -1119,18 +1119,24 @@ class EPGImportAPIView(APIView):
|
|||
epg_id = request.data.get("id", None)
|
||||
force = bool(request.data.get("force", False))
|
||||
|
||||
# Check if this is a dummy EPG source
|
||||
try:
|
||||
# Reject dummy sources without loading programme_index (multi-MB JSON).
|
||||
if epg_id is not None:
|
||||
from .models import EPGSource
|
||||
epg_source = EPGSource.objects.get(id=epg_id)
|
||||
if epg_source.source_type == 'dummy':
|
||||
logger.info(f"EPGImportAPIView: Skipping refresh for dummy EPG source {epg_id}")
|
||||
|
||||
if EPGSource.objects.filter(
|
||||
id=epg_id, source_type="dummy"
|
||||
).exists():
|
||||
logger.info(
|
||||
"EPGImportAPIView: Skipping refresh for dummy EPG source %s",
|
||||
epg_id,
|
||||
)
|
||||
return Response(
|
||||
{"success": False, "message": "Dummy EPG sources do not require refreshing."},
|
||||
{
|
||||
"success": False,
|
||||
"message": "Dummy EPG sources do not require refreshing.",
|
||||
},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
except EPGSource.DoesNotExist:
|
||||
pass # Let the task handle the missing source
|
||||
|
||||
refresh_epg_data.delay(epg_id, force=force) # Trigger Celery task
|
||||
logger.info("EPGImportAPIView: Task dispatched to refresh EPG data.")
|
||||
|
|
|
|||
95
apps/epg/tests/test_epg_import_api.py
Normal file
95
apps/epg/tests/test_epg_import_api.py
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
from unittest.mock import patch
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import connection
|
||||
from django.test.utils import CaptureQueriesContext
|
||||
from django.test import TestCase
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from apps.epg.models import EPGSource
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
IMPORT_URL = "/api/epg/import/"
|
||||
|
||||
|
||||
class EPGImportAPITests(TestCase):
|
||||
def setUp(self):
|
||||
self.user = User.objects.create_user(
|
||||
username="epg_import_admin", password="testpass123"
|
||||
)
|
||||
self.user.user_level = 10
|
||||
self.user.save()
|
||||
self.client = APIClient()
|
||||
self.client.force_authenticate(user=self.user)
|
||||
|
||||
@patch("apps.epg.api_views.refresh_epg_data.delay")
|
||||
def test_import_dummy_source_rejected_without_dispatch(self, mock_delay):
|
||||
source = EPGSource.objects.create(
|
||||
name="Dummy EPG",
|
||||
source_type="dummy",
|
||||
)
|
||||
|
||||
response = self.client.post(
|
||||
IMPORT_URL, {"id": source.id}, format="json"
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
self.assertFalse(response.data["success"])
|
||||
mock_delay.assert_not_called()
|
||||
|
||||
@patch("apps.epg.api_views.refresh_epg_data.delay")
|
||||
def test_import_xmltv_dispatches_without_loading_programme_index(
|
||||
self, mock_delay
|
||||
):
|
||||
source = EPGSource.objects.create(
|
||||
name="Large Index XMLTV",
|
||||
source_type="xmltv",
|
||||
url="http://example.com/epg.xml",
|
||||
programme_index={
|
||||
"channels": {f"ch.{i}": {"offsets": [0, 100]} for i in range(200)},
|
||||
"interleaved_channels": [],
|
||||
},
|
||||
)
|
||||
mock_delay.reset_mock()
|
||||
|
||||
with CaptureQueriesContext(connection) as ctx:
|
||||
response = self.client.post(
|
||||
IMPORT_URL, {"id": source.id}, format="json"
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
|
||||
self.assertTrue(response.data["success"])
|
||||
mock_delay.assert_called_once_with(source.id, force=False)
|
||||
for query in ctx.captured_queries:
|
||||
self.assertNotIn(
|
||||
"programme_index",
|
||||
query["sql"].lower(),
|
||||
"import trigger should not read programme_index",
|
||||
)
|
||||
|
||||
@patch("apps.epg.api_views.refresh_epg_data.delay")
|
||||
def test_import_missing_source_still_dispatches(self, mock_delay):
|
||||
response = self.client.post(IMPORT_URL, {"id": 99999}, format="json")
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
|
||||
mock_delay.assert_called_once_with(99999, force=False)
|
||||
|
||||
@patch("apps.epg.api_views.refresh_epg_data.delay")
|
||||
def test_import_honours_force_flag(self, mock_delay):
|
||||
source = EPGSource.objects.create(
|
||||
name="Force XMLTV",
|
||||
source_type="xmltv",
|
||||
url="http://example.com/epg.xml",
|
||||
)
|
||||
mock_delay.reset_mock()
|
||||
|
||||
response = self.client.post(
|
||||
IMPORT_URL,
|
||||
{"id": source.id, "force": True},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
|
||||
mock_delay.assert_called_once_with(source.id, force=True)
|
||||
Loading…
Add table
Add a link
Reference in a new issue