Skip to content

Commit

Permalink
Remove class SumoConnection (#93)
Browse files Browse the repository at this point in the history
* Get rid of class uploader.SumoConnection and use sumo-wrapper.SumoClient directly, instead.

* Create temporary stub for SumoConnection, for clients that expects that rather than sumo.wrapper.SumoClient.

---------

Co-authored-by: Raymond Wiker <[email protected]>
  • Loading branch information
rwiker and rwiker authored Sep 25, 2024
1 parent 49093d4 commit 068d18f
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 156 deletions.
4 changes: 3 additions & 1 deletion src/fmu/sumo/uploader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

from fmu.sumo.uploader.caseondisk import CaseOnDisk
from fmu.sumo.uploader.caseonjob import CaseOnJob
from fmu.sumo.uploader._connection import SumoConnection
import sumo.wrapper

SumoConnection = sumo.wrapper.SumoClient

# from fmu.sumo.uploader._fileondisk import FileOnDisk
# from fmu.sumo.uploader._fileonjob import FileOnJob
37 changes: 0 additions & 37 deletions src/fmu/sumo/uploader/_connection.py

This file was deleted.

6 changes: 3 additions & 3 deletions src/fmu/sumo/uploader/_sumocase.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class SumoCase:
def __init__(
self,
case_metadata: str,
sumo_connection,
sumoclient,
verbosity="WARNING",
sumo_mode="copy",
config_path="fmuconfig/output/global_variables.yml",
parameters_path="parameters.txt",
):
logger.setLevel(verbosity)
self.sumo_connection = sumo_connection
self.sumoclient = sumoclient
self.case_metadata = _sanitize_datetimes(case_metadata)
self._fmu_case_uuid = self._get_fmu_case_uuid()
logger.debug("self._fmu_case_uuid is %s", self._fmu_case_uuid)
Expand Down Expand Up @@ -91,7 +91,7 @@ def upload(self, threads=4):
upload_results = upload_files(
files_to_upload,
self._sumo_parent_id,
self.sumo_connection,
self.sumoclient,
threads,
self.sumo_mode,
self.config_path,
Expand Down
18 changes: 9 additions & 9 deletions src/fmu/sumo/uploader/_sumofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,26 @@ class SumoFile:
def __init__(self):
return

def _upload_metadata(self, sumo_connection, sumo_parent_id):
def _upload_metadata(self, sumoclient, sumo_parent_id):
path = f"/objects('{sumo_parent_id}')"
response = sumo_connection.api.post(path=path, json=self.metadata)
response = sumoclient.post(path=path, json=self.metadata)
return response

def _upload_byte_string(self, sumo_connection, object_id, blob_url):
def _upload_byte_string(self, sumoclient, object_id, blob_url):
blobclient = BlobClient.from_blob_url(blob_url)
content_settings = ContentSettings(content_type="application/octet-stream")
response = blobclient.upload_blob(self.byte_string, blob_type="BlockBlob", length=len(self.byte_string), overwrite=True, content_settings=content_settings)
# response has the form {'etag': '"0x8DCDC8EED1510CC"', 'last_modified': datetime.datetime(2024, 9, 24, 11, 49, 20, tzinfo=datetime.timezone.utc), 'content_md5': bytearray(b'\x1bPM3(\xe1o\xdf(\x1d\x1f\xb9Qm\xd9\x0b'), 'client_request_id': '08c962a4-7a6b-11ef-8710-acde48001122', 'request_id': 'f459ad2b-801e-007d-1977-0ef6ee000000', 'version': '2024-11-04', 'version_id': None, 'date': datetime.datetime(2024, 9, 24, 11, 49, 19, tzinfo=datetime.timezone.utc), 'request_server_encrypted': True, 'encryption_key_sha256': None, 'encryption_scope': None}
# ... which is not what the caller expects, so we return something reasonable.
return httpx.Response(201)

def _delete_metadata(self, sumo_connection, object_id):
def _delete_metadata(self, sumoclient, object_id):
logger.warning("Deleting metadata object: %s", object_id)
path = f"/objects('{object_id}')"
response = sumo_connection.api.delete(path=path)
response = sumoclient.delete(path=path)
return response

def upload_to_sumo(self, sumo_parent_id, sumo_connection, sumo_mode):
def upload_to_sumo(self, sumo_parent_id, sumoclient, sumo_mode):
"""Upload this file to Sumo"""

logger.debug("Starting upload_to_sumo()")
Expand Down Expand Up @@ -128,7 +128,7 @@ def upload_to_sumo(self, sumo_parent_id, sumo_connection, sumo_mode):

try:
response = self._upload_metadata(
sumo_connection=sumo_connection, sumo_parent_id=sumo_parent_id
sumoclient=sumoclient, sumo_parent_id=sumo_parent_id
)

_t1_metadata = time.perf_counter()
Expand Down Expand Up @@ -274,7 +274,7 @@ def upload_to_sumo(self, sumo_parent_id, sumo_connection, sumo_mode):
else: # non-seismic blob
try:
response = self._upload_byte_string(
sumo_connection=sumo_connection,
sumoclient=sumoclient,
object_id=self.sumo_object_id,
blob_url=blob_url,
)
Expand Down Expand Up @@ -352,7 +352,7 @@ def upload_to_sumo(self, sumo_parent_id, sumo_connection, sumo_mode):
+ self.sumo_object_id
)
result["status"] = "failed"
self._delete_metadata(sumo_connection, self.sumo_object_id)
self._delete_metadata(sumoclient, self.sumo_object_id)
else:
result["status"] = "ok"
file_path = self.path
Expand Down
30 changes: 15 additions & 15 deletions src/fmu/sumo/uploader/_upload_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def create_parameter_file(
realization_id,
parameters_path,
config_path,
sumo_connection,
sumoclient,
):
"""If not already stored, generate a parameters object from the parameters.txt file
Expand All @@ -33,7 +33,7 @@ def create_parameter_file(
realization_id (str): the id of the realization
parameters_path (str): path to the parameters.txt file
config_path (str): path to the fmu config file
sumo_connection (SumoClient): Initialized sumo client for performing query
sumoclient (SumoClient): Initialized sumo client for performing query
Returns:
SumoFile: parameters ready for upload, or None
Expand All @@ -44,7 +44,7 @@ def create_parameter_file(

query = f"fmu.case.uuid:{case_uuid} AND fmu.realization.uuid:{realization_id} AND data.content:parameters"

search_res = sumo_connection.api.get("/search", {"$query": query}).json()
search_res = sumoclient.get("/search", {"$query": query}).json()

if search_res["hits"]["total"]["value"] > 0:
logger.info("Parameters already uploaded")
Expand Down Expand Up @@ -87,11 +87,11 @@ def create_parameter_file(
return paramfile


def maybe_upload_realization_and_iteration(sumo_connection, base_metadata):
def maybe_upload_realization_and_iteration(sumoclient, base_metadata):
realization_uuid = base_metadata["fmu"]["realization"]["uuid"]
iteration_uuid = base_metadata["fmu"]["iteration"]["uuid"]

hits = sumo_connection.api.post(
hits = sumoclient.post(
"/search",
json={
"query": {"ids": {"values": [realization_uuid, iteration_uuid]}},
Expand All @@ -116,18 +116,18 @@ def maybe_upload_realization_and_iteration(sumo_connection, base_metadata):
del iteration_metadata["fmu"]["realization"]
iteration_metadata["class"] = "iteration"
iteration_metadata["fmu"]["context"]["stage"] = "iteration"
sumo_connection.api.post(
sumoclient.post(
f"/objects('{case_uuid}')", json=iteration_metadata
)

sumo_connection.api.post(
sumoclient.post(
f"/objects('{case_uuid}')", json=realization_metadata
)


def _upload_files(
files,
sumo_connection,
sumoclient,
sumo_parent_id,
threads=4,
sumo_mode="copy",
Expand All @@ -144,7 +144,7 @@ def _upload_files(

try:
maybe_upload_realization_and_iteration(
sumo_connection, file.metadata
sumoclient, file.metadata
)
except Exception as e:
logger.error(
Expand All @@ -157,7 +157,7 @@ def _upload_files(
realization_id,
parameters_path,
config_path,
sumo_connection,
sumoclient,
)
if paramfile is not None:
files.append(paramfile)
Expand All @@ -168,7 +168,7 @@ def _upload_files(
results = executor.map(
_upload_file,
[
(file, sumo_connection, sumo_parent_id, sumo_mode)
(file, sumoclient, sumo_parent_id, sumo_mode)
for file in files
],
)
Expand All @@ -179,10 +179,10 @@ def _upload_files(
def _upload_file(args):
"""Upload a file"""

file, sumo_connection, sumo_parent_id, sumo_mode = args
file, sumoclient, sumo_parent_id, sumo_mode = args

result = file.upload_to_sumo(
sumo_connection=sumo_connection,
sumoclient=sumoclient,
sumo_parent_id=sumo_parent_id,
sumo_mode=sumo_mode,
)
Expand All @@ -195,7 +195,7 @@ def _upload_file(args):
def upload_files(
files: list,
sumo_parent_id: str,
sumo_connection,
sumoclient,
threads=4,
sumo_mode="copy",
config_path="fmuconfig/output/global_variables.yml",
Expand All @@ -212,7 +212,7 @@ def upload_files(

results = _upload_files(
files,
sumo_connection,
sumoclient,
sumo_parent_id,
threads,
sumo_mode,
Expand Down
16 changes: 8 additions & 8 deletions src/fmu/sumo/uploader/caseondisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class CaseOnDisk(SumoCase):
>>> case_metadata_path = 'path/to/case_metadata.yaml'
>>> search_path = 'path/to/search_path/'
>>> sumo_connection = sumo.SumoConnection(env=env)
>>> sumoclient = sumo.wrapper.SumoClient(env=env)
>>> case = sumo.CaseOnDisk(
case_metadata_path=case_metadata_path,
sumo_connection=sumo_connection)
sumoclient=sumoclient)
After initialization, files must be explicitly indexed into the CaseOnDisk object:
Expand All @@ -53,15 +53,15 @@ class CaseOnDisk(SumoCase):
Args:
case_metadata_path (str): Path to the case_metadata file for the case
sumo_connection (fmu.sumo.SumoConnection): SumoConnection object
sumoclient (sumo.wrapper.SumoClient): SumoConnection object
"""

def __init__(
self,
case_metadata_path: str,
sumo_connection,
sumoclient,
verbosity=logging.WARNING,
sumo_mode="copy",
config_path="fmuconfig/output/global_variables.yml",
Expand All @@ -71,7 +71,7 @@ def __init__(
Args:
case_metadata_path (str): Path to case_metadata for case
sumo_connection (fmu.sumo.SumoConnection): Connection to Sumo.
sumoclient (sumo.wrapper.SumoClient): Connection to Sumo.
verbosity (str): Python logging level.
"""

Expand All @@ -83,14 +83,14 @@ def __init__(
case_metadata = _load_case_metadata(case_metadata_path)
super().__init__(
case_metadata,
sumo_connection,
sumoclient,
verbosity,
sumo_mode,
config_path,
parameters_path,
)

self._sumo_logger = sumo_connection.api.getLogger("fmu-sumo-uploader")
self._sumo_logger = sumoclient.getLogger("fmu-sumo-uploader")
self._sumo_logger.setLevel(logging.INFO)
# Avoid that logging to sumo-server also is visible in local logging:
self._sumo_logger.propagate = False
Expand Down Expand Up @@ -194,7 +194,7 @@ def register(self):
def _upload_case_metadata(self, case_metadata: dict):
"""Upload case metadata to Sumo."""

response = self.sumo_connection.api.post(
response = self.sumoclient.post(
path="/objects", json=case_metadata
)

Expand Down
6 changes: 3 additions & 3 deletions src/fmu/sumo/uploader/caseonjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class CaseOnJob(SumoCase):
"""Initialize the CaseOnJob object."""

def __init__(
self, case_metadata: str, sumo_connection, verbosity=logging.DEBUG
self, case_metadata: str, sumoclient, verbosity=logging.DEBUG
):
super().__init__(case_metadata, sumo_connection)
super().__init__(case_metadata, sumoclient)
logger.setLevel(level=verbosity)

self.sumo_connection = sumo_connection
self.sumoclient = sumoclient

@property
def sumo_parent_id(self):
Expand Down
7 changes: 4 additions & 3 deletions src/fmu/sumo/uploader/scripts/sumo_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
except ModuleNotFoundError:
from res.job_queue import ErtScript # type: ignore

from sumo.wrapper import SumoClient
from fmu.sumo import uploader
from fmu.sumo.uploader._logger import get_uploader_logger

Expand Down Expand Up @@ -121,7 +122,7 @@ def sumo_upload_main(

try:
# establish the connection to Sumo
sumo_connection = uploader.SumoConnection(env=env)
sumoclient = SumoClient(env=env)
logger.info("Connection to Sumo established, env=%s", env)

# initiate the case on disk object
Expand All @@ -133,7 +134,7 @@ def sumo_upload_main(

e = uploader.CaseOnDisk(
case_metadata_path,
sumo_connection,
sumoclient,
verbosity,
sumo_mode,
config_path,
Expand All @@ -156,7 +157,7 @@ def sumo_upload_main(
except Exception as err:
err = err.with_traceback(None)
logger.warning(f"Problem related to Sumo upload: {err} {type(err)}")
_sumo_logger = sumo_connection.api.getLogger("fmu-sumo-uploader")
_sumo_logger = sumoclient.getLogger("fmu-sumo-uploader")
_sumo_logger.propagate = False
_sumo_logger.warning(
"Problem related to Sumo upload for case: %s; %s %s",
Expand Down
Loading

0 comments on commit 068d18f

Please sign in to comment.