Skip to content

Commit

Permalink
GeoNode#10995 Faceting: lazy initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
etj committed May 4, 2023
1 parent fc03dc2 commit 55650e3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
49 changes: 35 additions & 14 deletions geonode/facets/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

logger = logging.getLogger(__name__)

registered_facets = dict()


class GeoNodeFacetsConfig(AppConfig):
name = "geonode.facets"
Expand All @@ -38,21 +36,44 @@ def ready(self):

urlpatterns += urls.urlpatterns

init_providers()

class FacetsRegistry:
def __init__(self):
self.facet_providers = None

def _load_facets_configuration(self) -> None:
"""
Facet loading is done lazily because some FacetProvider may need to access the DB, which may not have been
initialized/created yet
"""
self.facet_providers = dict()

from geonode.facets.providers.thesaurus import create_thesaurus_providers
from geonode.facets.providers.users import OwnerFacetProvider

logger.info(f"Initializing Facets")

self.register_facet_provider(CategoryFacetProvider())
self.register_facet_provider(OwnerFacetProvider())

# Thesaurus providers initialization should be called at startup and
# whenever records in Thesaurus or ThesaurusLabel change
for provider in create_thesaurus_providers():
self.register_facet_provider(provider)

def init_providers():
from geonode.facets.providers.thesaurus import create_thesaurus_providers
from geonode.facets.providers.users import OwnerFacetProvider
def register_facet_provider(self, provider: FacetProvider):
logger.info(f"Registering {provider}")
self.facet_providers[provider.get_info()["name"]] = provider

register_facet_provider(CategoryFacetProvider())
register_facet_provider(OwnerFacetProvider())
def get_providers(self):
if self.facet_providers is None:
self._load_facets_configuration()
return self.facet_providers.values()

# Thesaurus providers initialiazion should be called at startup and whenever records in Thesaurus or ThesaurusLabel change
for provider in create_thesaurus_providers():
register_facet_provider(provider)
def get_provider(self, name):
if self.facet_providers is None:
self._load_facets_configuration()
return self.facet_providers.get(name, None)


def register_facet_provider(provider: FacetProvider):
logging.info(f"Registering {provider}")
registered_facets[provider.get_info()["name"]] = provider
facet_registry = FacetsRegistry()
6 changes: 3 additions & 3 deletions geonode/facets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.urls import reverse

from geonode.base.models import ResourceBase
from geonode.facets.apps import registered_facets
from geonode.facets.apps import facet_registry
from geonode.facets.models import FacetProvider, DEFAULT_FACET_PAGE_SIZE
from geonode.security.utils import get_visible_resources

Expand All @@ -31,7 +31,7 @@ def list_facets(request, **kwargs):

facets = []

for provider in registered_facets.values():
for provider in facet_registry.get_providers():
logger.debug("Fetching data from provider %r", provider)
info = provider.get_info(lang=lang)
if add_links:
Expand All @@ -55,7 +55,7 @@ def get_facet(request, facet):
logger.debug("get_facet -> %r for user '%r'", facet, request.user.username)

# retrieve provider for the requested facet
provider: FacetProvider = registered_facets.get(facet)
provider: FacetProvider = facet_registry.get_provider(facet)
if not provider:
return HttpResponseNotFound()

Expand Down

0 comments on commit 55650e3

Please sign in to comment.