From 292af663b0b91f065853ad2bc3157d1d174987b1 Mon Sep 17 00:00:00 2001 From: femalves Date: Fri, 24 Nov 2023 14:08:57 -0500 Subject: [PATCH] adding debug logs --- scan_explorer_service/manifest_factory.py | 24 ++++++++++++++++++----- scan_explorer_service/utils/utils.py | 4 ++++ scan_explorer_service/views/manifest.py | 9 +++++++++ scan_explorer_service/views/view_utils.py | 10 ++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/scan_explorer_service/manifest_factory.py b/scan_explorer_service/manifest_factory.py index 82ffb77..a8dc039 100644 --- a/scan_explorer_service/manifest_factory.py +++ b/scan_explorer_service/manifest_factory.py @@ -3,6 +3,7 @@ from scan_explorer_service.models import Article, Page, Collection from typing import Union from itertools import chain +from scan_explorer_service.views.view_utils import logger class ManifestFactoryExtended(ManifestFactory): """ Extended manifest factory. @@ -12,30 +13,37 @@ class ManifestFactoryExtended(ManifestFactory): """ def create_manifest(self, item: Union[Article, Collection]): + logger.debug(f"Creating manifest for item: {item}") manifest = self.manifest( ident=f'{item.id}/manifest.json', label=item.id) manifest.description = item.id manifest.add_sequence(self.create_sequence(item)) for range in self.create_range(item): manifest.add_range(range) - + logger.debug(f"Manifest created: {manifest}") return manifest def create_sequence(self, item: Union[Article, Collection]): + logger.debug(f"Creating sequence for item: {item}") sequence: Sequence = self.sequence() + logger.debug(f"Sequence is: {sequence}. Adding canvases to sequence.") for page in item.pages: + logger.debug(f"Adding canvas to sequence: {page}.") sequence.add_canvas(self.get_or_create_canvas(page)) - + + logger.debug(f"Final sequence created: {sequence}") return sequence def create_range(self, item: Union[Article, Collection]): + logger.debug(f"Creating range for item: {item}") if isinstance(item, Collection): return list(chain(*[self.create_range(article) for article in item.articles])) range: Range = self.range(ident=item.bibcode, label=item.bibcode) for page in item.pages: + logger.debug(f"Adding canvas to range: {page}.") range.add_canvas(self.get_or_create_canvas(page)) - + logger.debug(f"Range created: {[range]}") return [range] def get_canvas_dict(self) -> Dict[str, Canvas]: @@ -44,6 +52,7 @@ def get_canvas_dict(self) -> Dict[str, Canvas]: return self.canvas_dict def get_or_create_canvas(self, page: Page): + logger.debug(f"Getting or creating canvas for page: {page}") canvas_dict = self.get_canvas_dict() if(page.id in canvas_dict.keys()): return canvas_dict[page.id] @@ -61,24 +70,29 @@ def get_or_create_canvas(self, page: Page): annotation.on = canvas.id canvas.add_annotation(annotation) canvas_dict[page.id] = canvas + + logger.debug(f"Canvas created: {canvas}") return canvas def create_image_annotation(self, page: Page): + logger.debug(f"Creating image annotation for page: {page}") annotation: Annotation = self.annotation(ident=str(page.id)) image: Image = annotation.image( ident=page.image_path, label=f'p. {page.label}', iiif=True) # Override default image quality and format set by prezi image.id = image.id.replace(f'/default.jpg', f'/{page.image_color_quality}.tif') - + logger.debug(f"Image id: {image.id}") image.format = page.format image.height = page.height image.width = page.width - + logger.debug(f"Image annotation created: {annotation}") return annotation def add_search_service(self, manifest: Manifest, search_url: str): + logger.debug(f"Adding search services for manifest {manifest} and search url {search_url}") context = 'http://iiif.io/api/search/1/context.json' profile = 'http://iiif.io/api/search/1/search' manifest.add_service(ident=search_url, context=context, profile=profile) + logger.debug(f"Adding search services for manifest {manifest} and search url {search_url}") diff --git a/scan_explorer_service/utils/utils.py b/scan_explorer_service/utils/utils.py index 72f0d0f..2ebc0a4 100644 --- a/scan_explorer_service/utils/utils.py +++ b/scan_explorer_service/utils/utils.py @@ -1,12 +1,16 @@ from flask import current_app, url_for +from scan_explorer_service.views.view_utils import logger + def url_for_proxy(endpoint: str, **values): values['_external'] = False server, prefix = proxy_url() + logger.debug(f"Server is {server} and prefix is {prefix}.") path = url_for(endpoint, **values).lstrip('/') + logger.debug(f"Url is {server}/{prefix}/{path}.") return f'{server}/{prefix}/{path}' def proxy_url(): diff --git a/scan_explorer_service/views/manifest.py b/scan_explorer_service/views/manifest.py index cafb3d3..5efbeb0 100644 --- a/scan_explorer_service/views/manifest.py +++ b/scan_explorer_service/views/manifest.py @@ -7,6 +7,7 @@ from scan_explorer_service.open_search import EsFields, text_search_highlight from scan_explorer_service.utils.utils import proxy_url, url_for_proxy from typing import Union +from scan_explorer_service.views.view_utils import logger bp_manifest = Blueprint('manifest', __name__, url_prefix='/manifest') @@ -26,18 +27,26 @@ def before_request(): @bp_manifest.route('//manifest.json', methods=['GET']) def get_manifest(id: str): """ Creates an IIIF manifest from an article or Collection""" + + logger.debug(f"Fetching manifest for item with id : {id}") with current_app.session_scope() as session: + logger.debug(f"Fetching item (Article/Collection).") item: Union[Article, Collection] = ( session.query(Article).filter(Article.id == id).one_or_none() or session.query(Collection).filter(Collection.id == id).one_or_none()) if item: + logger.debug(f"Item successfully fetched. Creating manifest for item: {item}") manifest = manifest_factory.create_manifest(item) + logger.debug(f"Getting url for proxy for endpoint manifest.search and id {id}") search_url = url_for_proxy('manifest.search', id=id) + logger.debug(f"Getting url for proxy for endpoint manifest.search and id {id}") manifest_factory.add_search_service(manifest, search_url) + logger.debug(f"Manifest generated successfully for ID: {id}") return manifest.toJSON(top=True) else: + logger.debug(f"Item not found for article with id: {id}") return jsonify(exception='Article not found'), 404 diff --git a/scan_explorer_service/views/view_utils.py b/scan_explorer_service/views/view_utils.py index 8bf006c..022abd8 100644 --- a/scan_explorer_service/views/view_utils.py +++ b/scan_explorer_service/views/view_utils.py @@ -1,4 +1,14 @@ import enum +import os +from adsmutils import setup_logging, load_config + +# ============================= INITIALIZATION ==================================== # + +proj_home = os.path.realpath(os.path.dirname(__file__)) +config = load_config(proj_home=proj_home) +logger = setup_logging('view_utils.py', proj_home=proj_home, + level=config.get('LOGGING_LEVEL', 'DEBUG'), + attach_stdout=config.get('LOG_STDOUT', False)) class ApiErrors(enum.Enum): SearchError = 1 \ No newline at end of file