Commit a1fa2fbb authored by Gregory Harris's avatar Gregory Harris
Browse files

Adding search before load support.

parent 84254789
import json
from pathlib import Path
from ntpath import basename
from knack.log import get_logger
......@@ -5,7 +6,7 @@ from lasloader.file_loader import LasParser, LocalFileLoader, FileUtilities
from lasloader.record_mapper import LasToRecordMapper
from lasloader.osdu_client import OsduClient
from lasloader.configuration import Configuration
from lasloader.well_service import WellBoreService, WellLogService
from lasloader.well_service import WellBoreService, WellLogService, LasLoaderConflictError
logger = get_logger(__name__)
......@@ -39,9 +40,12 @@ def wellbore(
las_data = las_parser.load_las_file(file_path)
las_mapper = LasToRecordMapper(las_data, config)
service.file_ingest(las_mapper, config.data_partition_id, no_recognize)
logger.warning(f"Ingest complete: {basename(file_path)}")
except LasLoaderConflictError as e:
logger.error(f"Ingest failed: {basename(file_path)} (see summary for details)")
ids = json.dumps(e.ids, indent=4, sort_keys=True)
logger.debug(f"{basename(file_path)} ({str(e)} Matching wellbore ids {ids})")
failed_ingests.append(f"{basename(file_path)} ({str(e)} Perform a search to list the conflicted wellbore ids.)")
except Exception as e:
failed_ingests.append(f"{basename(file_path)} ({str(e)})")
logger.error(f"Ingest failed: {basename(file_path)} (see summary for details)")
......
......@@ -3,7 +3,6 @@ from knack.log import get_logger
from lasloader.osdu_client import OsduClient
from lasloader.configuration import Configuration
from lasloader.file_loader import LocalFileLoader
from lasloader.well_service import WellBoreService, WellLogService
logger = get_logger(__name__)
......
......@@ -2,6 +2,7 @@ import json
from typing import Tuple
import urllib
from knack.log import get_logger
from lasio import read
from lasio.las import LASFile
from lasloader.file_loader import FileValidationError, LasParser, LocalFileLoader
from lasloader.osdu_client import OsduClient, LasLoaderWebResponseError
......@@ -11,6 +12,27 @@ from lasloader.record_mapper import LasToRecordMapper, Record, MappingUtilities,
logger = get_logger(__name__)
class LasLoaderConflictError(Exception):
"""
Exception class for data conflict errors that occur in the service layer
"""
def __init__(self, message: str, ids):
"""
Create a new instance of a LasLoaderConflictError
:param str message: An error message.
"""
self._ids = ids
super().__init__(message)
@property
def ids(self):
"""
Get the ids of the conflicted objects
"""
return self._ids
class WellLogService:
def __init__(self, client: OsduClient):
"""
......@@ -206,10 +228,15 @@ class WellBoreService:
wellbore_record = mapper.map_to_wellbore_record()
ids = self._client.create_wellbore(wellbore_record)
logger.warning(f"New wellbore IDs: {ids}")
wellbore_id = self._get_wellbore_by_name(wellbore_record.data.get("FacilityName"))
if wellbore_id is None:
ids = self._client.create_wellbore(wellbore_record)
logger.warning(f"New wellbore IDs: {ids}")
wellbore_id = self._safe_get_first_record(ids)
else:
logger.warning(f"Adding new welllog data to the existing well bore with id: {wellbore_id}")
wellbore_id = self._safe_get_first_record(ids)
logger.info(json.dumps(self._client.get_wellbore_record(wellbore_id).get_raw_data(), indent=4, sort_keys=True))
welllog_record = mapper.map_to_well_log_record(wellbore_id)
......@@ -231,6 +258,26 @@ class WellBoreService:
welllog_data = mapper.extract_log_data()
self._client.add_welllog_data(welllog_data, welllog_id)
def _get_wellbore_by_name(self, wellbore_name: str) -> str:
"""
Get the first element from a list or return None if the list is None or empty
:param list array: An OSDU client wrapper
:return: The id of the matching wellbore
:rtype: str
"""
if wellbore_name is None:
return None
wellbore_ids = self.search_for_wellbore(wellbore_name)
if wellbore_ids is None or len(wellbore_ids) < 1:
return None
elif len(wellbore_ids) > 1:
message = f"More than one matching wellbore found for '{wellbore_name}'."
raise LasLoaderConflictError(message, wellbore_ids)
else:
return self._safe_get_first_record(wellbore_ids)
def _safe_get_first_record(self, array: list[any]) -> any:
"""
Get the first element from a list or return None if the list is None or empty
......@@ -240,11 +287,11 @@ class WellBoreService:
"""
return array[0] if array is not None and len(array) > 0 else None
def search_for_wellbore(self, wellbore_name: str):
def search_for_wellbore(self, wellbore_name: str) -> list[str]:
"""
Get the first element from a list or return None if the list is None or empty
:param list array: An OSDU client wrapper
:return: The first element of the list or None
:rtype: any
:rtype: list[str]
"""
return self._client.search_for_wellbore(wellbore_name)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment