Skip to content

Commit

Permalink
Merge pull request #914 from ImageMarkup/defer-imports
Browse files Browse the repository at this point in the history
Defer slower imports until runtime
  • Loading branch information
danlamanna committed Jun 25, 2024
2 parents f7385e8 + 6555a91 commit 7c8ab9a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 13 additions & 5 deletions isic/core/search.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from copy import deepcopy
from functools import lru_cache, partial
import logging
from typing import Any
from typing import TYPE_CHECKING, Any

from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.query import QuerySet
from isic_metadata import FIELD_REGISTRY
from isic_metadata.fields import FitzpatrickSkinType, ImageTypeEnum
from opensearchpy import NotFoundError, OpenSearch
from opensearchpy.helpers import parallel_bulk
from opensearchpy.transport import Transport

if TYPE_CHECKING:
from opensearchpy import OpenSearch


from isic.core.models import Image
from isic.core.models.collection import Collection
Expand Down Expand Up @@ -66,13 +67,18 @@


@lru_cache
def get_elasticsearch_client() -> OpenSearch:
def get_elasticsearch_client() -> "OpenSearch":
from opensearchpy import OpenSearch
from opensearchpy.transport import Transport

# TODO: investigate using retryable requests with transport_class
RetryOnTimeoutTransport = partial(Transport, retry_on_timeout=True) # noqa: N806
return OpenSearch(settings.ISIC_ELASTICSEARCH_URI, transport_class=RetryOnTimeoutTransport)


def maybe_create_index() -> None:
from opensearchpy import NotFoundError

try:
indices = get_elasticsearch_client().indices.get(settings.ISIC_ELASTICSEARCH_INDEX)
except NotFoundError:
Expand Down Expand Up @@ -103,6 +109,8 @@ def add_to_search_index(image: Image) -> None:


def bulk_add_to_search_index(qs: QuerySet[Image], chunk_size: int = 2000) -> None:
from opensearchpy.helpers import parallel_bulk

# qs must be generated with with_elasticsearch_properties
# Use a generator for lazy evaluation
image_documents = (image.to_elasticsearch_document() for image in qs)
Expand Down
3 changes: 2 additions & 1 deletion isic/ingest/models/metadata_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import re

from deepdiff import DeepDiff
from django.contrib.auth.models import User
from django.db import models

Expand Down Expand Up @@ -47,6 +46,8 @@ def _strip_root(key: str) -> str:
return re.sub(r"^root\['(.*)'\]$", r"\1", key)

def _diff(a, b):
from deepdiff import DeepDiff

result = DeepDiff(
a,
b,
Expand Down

0 comments on commit 7c8ab9a

Please sign in to comment.