Skip to content

Commit

Permalink
Async support (#192)
Browse files Browse the repository at this point in the history
* add async options

* update tests

* small fix
  • Loading branch information
equinor-ruaj authored Jul 11, 2023
1 parent 1290233 commit 25b020a
Show file tree
Hide file tree
Showing 17 changed files with 684 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def parse_requirements(fname):
from sphinx.setup_command import BuildDoc

CMDCLASS.update({"build_sphinx": BuildDoc})
except ImportError as e:
except ImportError:
# sphinx not installed - do not provide build_sphinx cmd
pass

Expand Down
82 changes: 82 additions & 0 deletions src/fmu/sumo/explorer/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ def get_buckets(

return buckets

async def get_buckets_async(
self,
field: str,
query: Dict,
sort: List = None,
) -> List[Dict]:
"""Get a List of buckets
Arguments:
- field (str): a field in the metadata
- query (List[Dict] or None): filter options
- sort (List or None): sorting options
Returns:
A List of unique values for a given field
"""
query = {
"size": 0,
"aggs": {f"{field}": {"terms": {"field": field, "size": 2000}}},
"query": query,
}

if sort is not None:
query["sort"] = sort

res = await self._sumo.post_async("/search", json=query)
buckets = res.json()["aggregations"][field]["buckets"]

return buckets

def get_objects(
self,
size: int,
Expand All @@ -65,6 +95,31 @@ def get_objects(

return res.json()["hits"]["hits"]

async def get_objects_async(
self,
size: int,
query: Dict,
select: List[str] = None,
) -> List[Dict]:
"""Get objects
Args:
size (int): number of objects to return
query (List[Dict] or None): filter options
select (List[str] or None): list of metadata fields to return
Returns:
List[Dict]: A List of metadata
"""
query = {"size": size, "query": query}

if select is not None:
query["_source"] = select

res = await self._sumo.post_async("/search", json=query)

return res.json()["hits"]["hits"]

def get_object(self, uuid: str, select: List[str] = None) -> Dict:
"""Get metadata object by uuid
Expand Down Expand Up @@ -92,6 +147,33 @@ def get_object(self, uuid: str, select: List[str] = None) -> Dict:

return hits[0]

async def get_object_async(self, uuid: str, select: List[str] = None) -> Dict:
"""Get metadata object by uuid
Args:
uuid (str): uuid of metadata object
select (List[str]): list of metadata fields to return
Returns:
Dict: a metadata object
"""

query = {
"query": {"term": {"_id": uuid}},
"size": 1,
}

if select is not None:
query["_source"] = select

res = await self._sumo.post_async("/search", json=query)
hits = res.json()["hits"]["hits"]

if len(hits) == 0:
raise Exception(f"Document not found: {uuid}")

return hits[0]

def extend_query_object(self, old: Dict, new: Dict) -> Dict:
"""Extend query object
Expand Down
65 changes: 65 additions & 0 deletions src/fmu/sumo/explorer/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ def get_permissions(self, asset: str = None):

return res

async def get_permissions_async(self, asset: str = None):
"""Get permissions
Args:
asset (str): asset in Sumo
Returns:
dict: Dictionary of user permissions
"""
res = await self._sumo.get_async("/userpermissions")

if asset is not None:
if asset not in res:
raise PermissionError(f"No permissions for asset: {asset}")

return res

def get_case_by_uuid(self, uuid: str) -> Case:
"""Get case object by uuid
Expand All @@ -99,6 +116,18 @@ def get_case_by_uuid(self, uuid: str) -> Case:
metadata = self._utils.get_object(uuid, _CASE_FIELDS)
return Case(self._sumo, metadata)

async def get_case_by_uuid_async(self, uuid: str) -> Case:
"""Get case object by uuid
Args:
uuid (str): case uuid
Returns:
Case: case object
"""
metadata = await self._utils.get_object_async(uuid, _CASE_FIELDS)
return Case(self._sumo, metadata)

def get_surface_by_uuid(self, uuid: str) -> Surface:
"""Get surface object by uuid
Expand All @@ -111,6 +140,18 @@ def get_surface_by_uuid(self, uuid: str) -> Surface:
metadata = self._utils.get_object(uuid, _CHILD_FIELDS)
return Surface(self._sumo, metadata)

async def get_surface_by_uuid_async(self, uuid: str) -> Surface:
"""Get surface object by uuid
Args:
uuid (str): surface uuid
Returns:
Surface: surface object
"""
metadata = await self._utils.get_object_async(uuid, _CHILD_FIELDS)
return Surface(self._sumo, metadata)

def get_polygons_by_uuid(self, uuid: str) -> Polygons:
"""Get polygons object by uuid
Expand All @@ -123,6 +164,18 @@ def get_polygons_by_uuid(self, uuid: str) -> Polygons:
metadata = self._utils.get_object(uuid, _CHILD_FIELDS)
return Polygons(self._sumo, metadata)

async def get_polygons_by_uuid_async(self, uuid: str) -> Polygons:
"""Get polygons object by uuid
Args:
uuid (str): polygons uuid
Returns:
Polygons: polygons object
"""
metadata = await self._utils.get_object_async(uuid, _CHILD_FIELDS)
return Polygons(self._sumo, metadata)

def get_table_by_uuid(self, uuid: str) -> Table:
"""Get table object by uuid
Expand All @@ -134,3 +187,15 @@ def get_table_by_uuid(self, uuid: str) -> Table:
"""
metadata = self._utils.get_object(uuid, _CHILD_FIELDS)
return Table(self._sumo, metadata)

async def get_table_by_uuid_async(self, uuid: str) -> Table:
"""Get table object by uuid
Args:
uuid (str): table uuid
Returns:
Table: table object
"""
metadata = await self._utils.get_object_async(uuid, _CHILD_FIELDS)
return Table(self._sumo, metadata)
9 changes: 9 additions & 0 deletions src/fmu/sumo/explorer/objects/_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,12 @@ def blob(self) -> BytesIO:
self._blob = BytesIO(res)

return self._blob

@property
async def blob_async(self) -> BytesIO:
"""Object blob"""
if self._blob is None:
res = await self._sumo.get_async(f"/objects('{self.uuid}')/blob")
self._blob = BytesIO(res)

return self._blob
40 changes: 40 additions & 0 deletions src/fmu/sumo/explorer/objects/_child_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,81 @@ def names(self) -> List[str]:
"""List of unique object names"""
return self._get_field_values("data.name.keyword")

@property
async def names_async(self) -> List[str]:
"""List of unique object names"""
return await self._get_field_values_async("data.name.keyword")

@property
def tagnames(self) -> List[str]:
"""List of unqiue object tagnames"""
return self._get_field_values("data.tagname.keyword")

@property
async def tagnames_async(self) -> List[str]:
"""List of unqiue object tagnames"""
return await self._get_field_values_async("data.tagname.keyword")

@property
def iterations(self) -> List[int]:
"""List of unique object iteration names"""
return self._get_field_values("fmu.iteration.name.keyword")

@property
async def iterations_async(self) -> List[int]:
"""List of unique object iteration names"""
return await self._get_field_values_async("fmu.iteration.name.keyword")

@property
def realizations(self) -> List[int]:
"""List of unique object realization ids"""
return self._get_field_values("fmu.realization.id")

@property
async def realizations_async(self) -> List[int]:
"""List of unique object realization ids"""
return await self._get_field_values_async("fmu.realization.id")

@property
def aggregations(self) -> List[str]:
"""List of unique object aggregation operations"""
return self._get_field_values("fmu.aggregation.operation.keyword")

@property
async def aggregations_async(self) -> List[str]:
"""List of unique object aggregation operations"""
return await self._get_field_values_async("fmu.aggregation.operation.keyword")

@property
def stages(self) -> List[str]:
"""List of unique stages"""
return self._get_field_values("fmu.context.stage.keyword")

@property
async def stages_async(self) -> List[str]:
"""List of unique stages"""
return await self._get_field_values_async("fmu.context.stage.keyword")

@property
def stratigraphic(self) -> List[str]:
"""List of unqiue object stratigraphic"""
return self._get_field_values("data.stratigraphic")

@property
async def stratigraphic_async(self) -> List[str]:
"""List of unqiue object stratigraphic"""
return await self._get_field_values_async("data.stratigraphic")

@property
def vertical_domain(self) -> List[str]:
"""List of unqiue object vertical domain"""
return self._get_field_values("data.vertical_domain")

@property
async def vertical_domain_async(self) -> List[str]:
"""List of unqiue object vertical domain"""
return await self._get_field_values_async("data.vertical_domain")

def _init_query(self, doc_type: str, query: Dict = None) -> Dict:
new_query = super()._init_query(doc_type, query)
case_filter = {
Expand Down
Loading

0 comments on commit 25b020a

Please sign in to comment.