Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter component by user name #3126

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/zenml/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,7 @@ def list_stack_components(
user_id: Optional[Union[str, UUID]] = None,
connector_id: Optional[Union[str, UUID]] = None,
stack_id: Optional[Union[str, UUID]] = None,
user: Optional[Union[UUID, str]] = None,
hydrate: bool = False,
) -> Page[ComponentResponse]:
"""Lists all registered stack components.
Expand All @@ -1925,6 +1926,7 @@ def list_stack_components(
connector_id: The id of the connector to filter by.
stack_id: The id of the stack to filter by.
name: The name of the component to filter by.
user: The ID of name of the user to filter by.
hydrate: Flag deciding whether to hydrate the output model(s)
by including metadata fields in the response.

Expand All @@ -1946,6 +1948,7 @@ def list_stack_components(
id=id,
created=created,
updated=updated,
user=user,
)
component_filter_model.set_scope_workspace(self.active_workspace.id)

Expand Down
9 changes: 9 additions & 0 deletions src/zenml/models/v2/base/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,12 +604,15 @@ def generate_name_or_id_query_conditions(
self,
value: Union[UUID, str],
table: Type["NamedSchema"],
additional_columns: Optional[List[str]] = None,
) -> "ColumnElement[bool]":
"""Generate filter conditions for name or id of a table.

Args:
value: The filter value.
table: The table to filter.
additional_columns: Additional table columns that should also
filtered for the given value as part of the or condition.

Returns:
The query conditions.
Expand Down Expand Up @@ -637,6 +640,12 @@ def generate_name_or_id_query_conditions(
)
conditions.append(filter_.generate_query_conditions(table=table))

for column in additional_columns or []:
filter_ = FilterGenerator(table).define_filter(
column=column, value=value, operator=operator
)
conditions.append(filter_.generate_query_conditions(table=table))

return or_(*conditions)

def generate_custom_query_conditions_for_column(
Expand Down
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/artifact_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,9 @@ def get_custom_filters(self) -> List[Union["ColumnElement[bool]"]]:
user_filter = and_(
ArtifactVersionSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
33 changes: 33 additions & 0 deletions src/zenml/models/v2/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ class ComponentFilter(WorkspaceScopedFilter):
*WorkspaceScopedFilter.FILTER_EXCLUDE_FIELDS,
"scope_type",
"stack_id",
"user",
]
CLI_EXCLUDE_FIELDS: ClassVar[List[str]] = [
*WorkspaceScopedFilter.CLI_EXCLUDE_FIELDS,
Expand Down Expand Up @@ -418,6 +419,10 @@ class ComponentFilter(WorkspaceScopedFilter):
description="Stack of the stack component",
union_mode="left_to_right",
)
user: Optional[Union[UUID, str]] = Field(
default=None,
description="Name/ID of the user that created the component.",
)

def set_scope_type(self, component_type: str) -> None:
"""Set the type of component on which to perform the filtering to scope the response.
Expand Down Expand Up @@ -464,3 +469,31 @@ def generate_filter(
base_filter = operator(base_filter, stack_filter)

return base_filter

def get_custom_filters(self) -> List["ColumnElement[bool]"]:
"""Get custom filters.

Returns:
A list of custom filters.
"""
from sqlmodel import and_

from zenml.zen_stores.schemas import (
StackComponentSchema,
UserSchema,
)

custom_filters = super().get_custom_filters()

if self.user:
user_filter = and_(
StackComponentSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)

return custom_filters
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,9 @@ def get_custom_filters(
user_filter = and_(
ModelSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/model_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,9 @@ def get_custom_filters(
user_filter = and_(
ModelVersionSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/model_version_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ def get_custom_filters(self) -> List[Union["ColumnElement[bool]"]]:
== ArtifactVersionSchema.id,
ArtifactVersionSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/model_version_pipeline_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ def get_custom_filters(self) -> List["ColumnElement[bool]"]:
== PipelineRunSchema.id,
PipelineRunSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,9 @@ def get_custom_filters(
user_filter = and_(
PipelineSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/pipeline_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,9 @@ def get_custom_filters(
user_filter = and_(
PipelineRunSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/run_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,9 @@ def get_custom_filters(
user_filter = and_(
RunTemplateSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
4 changes: 3 additions & 1 deletion src/zenml/models/v2/core/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ def get_custom_filters(self) -> List["ColumnElement[bool]"]:
user_filter = and_(
StackSchema.user_id == UserSchema.id,
self.generate_name_or_id_query_conditions(
value=self.user, table=UserSchema
value=self.user,
table=UserSchema,
additional_columns=["full_name"],
),
)
custom_filters.append(user_filter)
Expand Down
Loading