Skip to content

Commit b6a0b04

Browse files
dawoodhqbenflexcompute
authored andcommitted
feat: add tags filtering to Project.get_case_ids, Project.get_project_ids, and metadata (#1341)
* feat: add tag filtering support to Project class + metadata and class property, get_case_ids/get_project_ids methods 499f63b * fix: remove unused imports + dangerous default value * feat: add property tags to AssetBase
1 parent 9dca384 commit b6a0b04

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

flow360/component/case.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ class CaseMetaV2(AssetMetaBaseModelV2):
182182
"""
183183

184184
id: str = pd.Field(alias="caseId")
185-
case_mesh_id: str = pd.Field(alias="caseMeshId")
186185
status: Flow360Status = pd.Field()
187186

188187
def to_case(self) -> Case:
@@ -546,6 +545,13 @@ def info(self) -> CaseMetaV2:
546545
"""
547546
return super().info
548547

548+
@property
549+
def info_v2(self) -> CaseMetaV2:
550+
"""
551+
returns metadata v2 info for case
552+
"""
553+
return self._web_api_v2.info
554+
549555
@property
550556
def project_id(self) -> Optional[str]:
551557
"""Returns the project id of the case if case was run with V2 interface."""
@@ -555,6 +561,13 @@ def project_id(self) -> Optional[str]:
555561
return self.info.project_id
556562
raise ValueError("Case info is not of type CaseMeta or CaseMetaV2")
557563

564+
@property
565+
def tags(self) -> List[str]:
566+
"""
567+
get case tags
568+
"""
569+
return self._web_api_v2.info.tags
570+
558571
@property
559572
def volume_mesh(self) -> "VolumeMeshV2":
560573
"""

flow360/component/project.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
VolumeMeshInterfaceV2,
2626
)
2727
from flow360.component.project_utils import (
28+
get_project_records,
2829
set_up_params_for_uploading,
2930
show_projects_with_keyword_filter,
3031
validate_params_with_context,
@@ -86,6 +87,8 @@ class ProjectMeta(pd.BaseModel, extra="allow"):
8687
The project ID.
8788
name : str
8889
The name of the project.
90+
tags : List[str]
91+
List of tags associated with the project.
8992
root_item_id : str
9093
ID of the root item in the project.
9194
root_item_type : RootType
@@ -95,6 +98,7 @@ class ProjectMeta(pd.BaseModel, extra="allow"):
9598
user_id: str = pd.Field(alias="userId")
9699
id: str = pd.Field()
97100
name: str = pd.Field()
101+
tags: List[str] = pd.Field(default_factory=list)
98102
root_item_id: str = pd.Field(alias="rootItemId")
99103
root_item_type: RootType = pd.Field(alias="rootItemType")
100104

@@ -402,6 +406,18 @@ def id(self) -> str:
402406
"""
403407
return self.metadata.id
404408

409+
@property
410+
def tags(self) -> List[str]:
411+
"""
412+
Returns the tags of the project.
413+
414+
Returns
415+
-------
416+
List[str]
417+
List of the project's tags.
418+
"""
419+
return self.metadata.tags
420+
405421
@property
406422
def length_unit(self) -> LengthType.Positive:
407423
"""
@@ -613,17 +629,54 @@ def get_volume_mesh_ids(self):
613629
# pylint: disable=protected-access
614630
return self.project_tree._get_asset_ids_by_type(asset_type="VolumeMesh")
615631

616-
def get_case_ids(self):
632+
def get_case_ids(self, tags: Optional[List[str]] = None) -> List[str]:
617633
"""
618-
Returns the available IDs of cases in the project
634+
Returns the available IDs of cases in the project, optionally filtered by tags.
635+
636+
Parameters
637+
----------
638+
tags : List[str], optional
639+
List of tags to filter cases by. If None or empty tags list, returns all case IDs.
619640
620641
Returns
621642
-------
622643
Iterable[str]
623-
An iterable of asset IDs.
644+
An iterable of case IDs. If tags are provided, filters to return only
645+
case IDs that have at least one matching tag.
624646
"""
625647
# pylint: disable=protected-access
626-
return self.project_tree._get_asset_ids_by_type(asset_type="Case")
648+
all_case_ids = self.project_tree._get_asset_ids_by_type(asset_type="Case")
649+
650+
if not tags:
651+
return all_case_ids
652+
653+
# Filter cases by tags
654+
filtered_case_ids = []
655+
for case_id in all_case_ids:
656+
case = self.get_case(asset_id=case_id)
657+
if set(tags) & set(case.info_v2.tags):
658+
filtered_case_ids.append(case_id)
659+
660+
return filtered_case_ids
661+
662+
@classmethod
663+
def get_project_ids(cls, tags: Optional[List[str]] = None) -> List[str]:
664+
"""
665+
Returns the available IDs of projects, optionally filtered by tags.
666+
667+
Parameters
668+
----------
669+
tags : List[str], optional
670+
List of tags to filter projects by. If None, returns all project IDs.
671+
672+
Returns
673+
-------
674+
List[str]
675+
A list of project IDs. If tags are provided, filters to return only
676+
project IDs that have at least one matching tag.
677+
"""
678+
project_records, _ = get_project_records("", tags=tags)
679+
return [record.project_id for record in project_records.records]
627680

628681
# pylint: disable=too-many-arguments
629682
@classmethod

flow360/component/project_utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ def __str__(self):
107107
return output_str
108108

109109

110-
def get_project_records(search_keyword: str) -> tuple[ProjectRecords, int]:
110+
def get_project_records(
111+
search_keyword: str, tags: Optional[List[str]] = None
112+
) -> tuple[ProjectRecords, int]:
111113
"""Get all projects with a keyword filter"""
112114
# pylint: disable=invalid-name
113115
MAX_SEARCHABLE_ITEM_COUNT = 1000
@@ -117,6 +119,7 @@ def get_project_records(search_keyword: str) -> tuple[ProjectRecords, int]:
117119
"page": "0",
118120
"size": MAX_SEARCHABLE_ITEM_COUNT,
119121
"filterKeywords": search_keyword,
122+
"filterTags": tags,
120123
"sortFields": ["createdAt"],
121124
"sortDirections": ["asc"],
122125
}

flow360/component/simulation/web/asset_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ def project_id(self):
7676
"""
7777
return self.info.project_id
7878

79+
@property
80+
def tags(self) -> List[str]:
81+
"""
82+
get asset tags
83+
"""
84+
return self.info.tags
85+
7986
@property
8087
def solver_version(self):
8188
"""

0 commit comments

Comments
 (0)