Skip to content

Commit

Permalink
Get rid of signals for reindexing
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Jun 16, 2024
1 parent 333be5f commit 94bc54e
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 115 deletions.
5 changes: 0 additions & 5 deletions radis/core/management/commands/populate_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from radis.opensearch.utils.document_utils import create_documents as create_opensearch_documents
from radis.reports.factories import LanguageFactory, ReportFactory
from radis.reports.models import Report
from radis.reports.signals import report_signal_processor
from radis.vespa.utils.document_utils import create_documents as create_vespa_documents

fake = Faker()
Expand All @@ -36,8 +35,6 @@ def create_reports(language: Literal["en", "de"], group: Group):
with open(samples_path, "r") as f:
report_bodies = json.load(f)

report_signal_processor.pause()

start = time.time()
reports: list[Report] = []
for report_body in report_bodies:
Expand All @@ -57,8 +54,6 @@ def create_documents() -> None:

transaction.on_commit(create_documents)

report_signal_processor.resume()


class Command(BaseCommand):
help = "Populates the database with example data."
Expand Down
87 changes: 84 additions & 3 deletions radis/reports/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
from django.contrib import admin
import logging

from django.contrib import admin, messages
from django.db import transaction
from django.db.models.query import QuerySet
from django.http.request import HttpRequest
from django.http.response import HttpResponse

from .models import Language, Metadata, Modality, Report, ReportsAppSettings
from .site import reports_created_handlers, reports_deleted_handlers, reports_updated_handlers

logger = logging.getLogger(__name__)

admin.site.register(ReportsAppSettings, admin.ModelAdmin)

admin.site.register(Language, admin.ModelAdmin)

admin.site.register(Modality, admin.ModelAdmin)
def show_reindex_warning(request: HttpRequest) -> None:
messages.warning(request, "Change does not reflect index. Manual reindex required!")


class LanguageAdmin(admin.ModelAdmin):
def delete_model(self, request: HttpRequest, obj: Language) -> None:
show_reindex_warning(request)
return super().delete_model(request, obj)

def delete_queryset(self, request: HttpRequest, queryset: QuerySet[Language]) -> None:
show_reindex_warning(request)
return super().delete_queryset(request, queryset)

def response_change(self, request: HttpRequest, obj: Language) -> HttpResponse:
show_reindex_warning(request)
return super().response_change(request, obj)


admin.site.register(Language, LanguageAdmin)


class ModalityAdmin(admin.ModelAdmin):
def delete_model(self, request: HttpRequest, obj: Modality) -> None:
show_reindex_warning(request)
return super().delete_model(request, obj)

def delete_queryset(self, request: HttpRequest, queryset: QuerySet[Modality]) -> None:
show_reindex_warning(request)
return super().delete_queryset(request, queryset)

def response_change(self, request: HttpRequest, obj: Modality) -> HttpResponse:
show_reindex_warning(request)
return super().response_change(request, obj)


admin.site.register(Modality, ModalityAdmin)


class MetadataInline(admin.TabularInline):
Expand All @@ -18,5 +61,43 @@ class MetadataInline(admin.TabularInline):
class ReportAdmin(admin.ModelAdmin):
inlines = [MetadataInline]

def delete_model(self, request: HttpRequest, obj: Report) -> None:
# Called when deleting a single report (from the admin form view)
super().delete_model(request, obj)
logger.debug("Remove in admin deleted report from index: %s", obj)
transaction.on_commit(
lambda: [handler.handle([obj]) for handler in reports_deleted_handlers]
)

def delete_queryset(self, request: HttpRequest, queryset: QuerySet[Report]) -> None:
# Called when deleting multiple reports (from the admin list view)
reports_to_delete = list(queryset)
print(reports_to_delete)
super().delete_queryset(request, queryset)
logger.debug("Remove in admin deleted reports from index: %s", reports_to_delete)
transaction.on_commit(
lambda: [handler.handle(reports_to_delete) for handler in reports_deleted_handlers]
)

def response_add(
self, request: HttpRequest, obj: Report, post_url_continue: str | None = None
) -> HttpResponse:
# Called after a new report in the admin is saved (the model itself and also
# its relations)
logger.debug("Reindex report added in admin: %s", obj)
transaction.on_commit(
lambda: [handler.handle([obj]) for handler in reports_created_handlers]
)
return super().response_add(request, obj, post_url_continue)

def response_change(self, request: HttpRequest, obj: Report) -> HttpResponse:
# Called after an existing report in the admin is saved (the model itself and also
# its relations)
logger.debug("Reindex report changed in admin: %s", obj)
transaction.on_commit(
lambda: [handler.handle([obj]) for handler in reports_updated_handlers]
)
return super().response_change(request, obj)


admin.site.register(Report, ReportAdmin)
60 changes: 20 additions & 40 deletions radis/reports/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from rest_framework.serializers import BaseSerializer

from ..models import Report
from ..signals import report_signal_processor
from ..site import (
document_fetchers,
reports_created_handlers,
Expand Down Expand Up @@ -66,13 +65,6 @@ def retrieve(self, request: Request, *args: Any, **kwargs: Any) -> Response:

return Response(data)

def create(self, request: Request, *args: Any, **kwargs: Any) -> Response:
try:
report_signal_processor.pause()
return super().create(request, *args, **kwargs)
finally:
report_signal_processor.resume()

def perform_create(self, serializer: BaseSerializer) -> None:
super().perform_create(serializer)
assert serializer.instance
Expand All @@ -82,34 +74,29 @@ def perform_create(self, serializer: BaseSerializer) -> None:

def on_commit():
for handler in reports_created_handlers:
report_ids = [report.id for report in reports]
logger.debug(f"{handler.name} - handle newly created reports: {report_ids}")
document_ids = [report.document_id for report in reports]
logger.debug(f"{handler.name} - handle newly created reports: {document_ids}")
handler.handle(reports)

transaction.on_commit(on_commit)

def update(self, request: Request, *args: Any, **kwargs: Any) -> Response:
try:
report_signal_processor.pause()

# DRF itself does not support upsert.
# Workaround adapted from https://gist.github.com/tomchristie/a2ace4577eff2c603b1b
upsert = request.GET.get("upsert", "").lower() in ["true", "1", "yes"]
if not upsert:
return super().update(request, *args, **kwargs)
else:
instance = self.get_object_or_none()
serializer = self.get_serializer(instance, data=request.data)
serializer.is_valid(raise_exception=True)

if instance is None:
self.perform_create(serializer)
return Response(serializer.data, status=status.HTTP_201_CREATED)

self.perform_update(serializer)
return Response(serializer.data)
finally:
report_signal_processor.resume()
# DRF itself does not support upsert.
# Workaround adapted from https://gist.github.com/tomchristie/a2ace4577eff2c603b1b
upsert = request.GET.get("upsert", "").lower() in ["true", "1", "yes"]
if not upsert:
return super().update(request, *args, **kwargs)
else:
instance = self.get_object_or_none()
serializer = self.get_serializer(instance, data=request.data)
serializer.is_valid(raise_exception=True)

if instance is None:
self.perform_create(serializer)
return Response(serializer.data, status=status.HTTP_201_CREATED)

self.perform_update(serializer)
return Response(serializer.data)

def get_object_or_none(self) -> Report | None:
try:
Expand All @@ -129,8 +116,8 @@ def perform_update(self, serializer: BaseSerializer) -> None:

def on_commit():
for handler in reports_updated_handlers:
report_ids = [report.id for report in reports]
logger.debug(f"{handler.name} - handle updated reports: {report_ids}")
document_ids = [report.document_id for report in reports]
logger.debug(f"{handler.name} - handle updated reports: {document_ids}")
handler.handle(reports)

transaction.on_commit(on_commit)
Expand All @@ -140,13 +127,6 @@ def partial_update(self, request: Request, *args: Any, **kwargs: Any) -> Respons
assert request.method
raise MethodNotAllowed(request.method)

def destroy(self, request: Request, *args: Any, **kwargs: Any) -> Response:
try:
report_signal_processor.pause()
return super().destroy(request, *args, **kwargs)
finally:
report_signal_processor.resume()

def perform_destroy(self, instance: Report) -> None:
super().perform_destroy(instance)

Expand Down
4 changes: 0 additions & 4 deletions radis/reports/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ class ReportsConfig(AppConfig):
name = "radis.reports"

def ready(self):
from .signals import report_signal_processor

# Put calls to db stuff in this signal handler
post_migrate.connect(init_db, sender=self)

report_signal_processor.connect()


def init_db(**kwargs):
create_app_settings()
Expand Down
62 changes: 0 additions & 62 deletions radis/reports/signals.py

This file was deleted.

2 changes: 1 addition & 1 deletion radis/search/templates/search/_search_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% elif total_relation == "approximately" %}
Approximately
{% endif %}
{{ total_count }} report{{ total_count|pluralize }}.
{{ total_count }} report{{ total_count|pluralize }}
found
</small>
</div>
Expand Down

0 comments on commit 94bc54e

Please sign in to comment.