wallaroo.comment

  1import datetime
  2from typing import TYPE_CHECKING, Any, Dict, List, cast
  3
  4from dateutil import parser as dateparse
  5
  6from .model import Model
  7from .object import *
  8from .pipeline import Pipeline
  9
 10if TYPE_CHECKING:
 11    # Imports that happen below in methods to fix circular import dependency
 12    # issues need to also be specified here to satisfy mypy type checking.
 13    from .client import Client
 14
 15
 16class Comment(Object):
 17    """Comment that may be attached to models and pipelines."""
 18
 19    def __init__(
 20        self, client: Optional["Client"], data: Dict[str, Any], standalone=False
 21    ) -> None:
 22        self.client = client
 23        super().__init__(
 24            gql_client=client._gql_client if client is not None else None,
 25            data=data,
 26            standalone=standalone,
 27        )
 28
 29    def _fill(self, data: Dict[str, Any]) -> None:
 30        """Fills an object given a response dictionary from the GraphQL API.
 31
 32        Only the primary key member must be present; other members will be
 33        filled in via rehydration if their corresponding member function is
 34        called.
 35        """
 36        for required_attribute in ["id"]:
 37            if required_attribute not in data:
 38                raise RequiredAttributeMissing(
 39                    self.__class__.__name__, required_attribute
 40                )
 41        # Required
 42        self._id = data["id"]
 43
 44        # Optional
 45        self._user_id = value_if_present(data, "user_id")
 46        self._message = value_if_present(data, "message")
 47        self._updated_at = (
 48            dateparse.isoparse(data["updated_at"])
 49            if "updated_at" in data
 50            else DehydratedValue()
 51        )
 52
 53    def _fetch_attributes(self) -> Dict[str, Any]:
 54        """Fetches all member data from the GraphQL API."""
 55        return self._gql_client.execute(
 56            gql.gql(
 57                """
 58            query CommentById($comment_id: bigint!){
 59            comment_by_pk(id: $comment_id){
 60                id
 61                updated_at
 62                message
 63                user_id
 64                
 65            }
 66            }
 67            """
 68            )
 69        )["comment_by_pk"]
 70
 71    def id(self) -> int:
 72        return self._id
 73
 74    @rehydrate("_user_id")
 75    def user_id(self) -> str:
 76        return cast(str, self._user_id)
 77
 78    @rehydrate("_message")
 79    def message(self) -> str:
 80        return cast(str, self._message)
 81
 82    @rehydrate("_updated_at")
 83    def updated_at(self) -> datetime.datetime:
 84        return cast(datetime.datetime, self._updated_at)
 85
 86    def list_models(self) -> List[Model]:
 87        """Lists the models this comment is on."""
 88        res = self._gql_client.execute(
 89            gql.gql(
 90                """
 91            query ModelsForComment($comment_id: bigint!){
 92            comment_by_pk(id:$comment_id){
 93                    model_variant_comments {
 94                model {
 95                    id
 96                    file_name
 97                    model_id
 98                    sha
 99                    owner_id
100                    visibility
101                    updated_at
102                }
103
104                }
105                
106            }
107            }
108            """
109            )
110        )
111        list_of_models = []
112        for v in res["comment_by_pk"]["model_variant_comments"]:
113            list_of_models.append(Model(client=self.client, data=v["model"]))
114        return list_of_models
115
116    def list_pipelines(self) -> List[Pipeline]:
117        """Lists the models this comment is on."""
118        res = self._gql_client.execute(
119            gql.gql(
120                """
121            query PipelinesForComment($comment_id: bigint!){
122            comment_by_pk(id:$comment_id){
123                pipeline_comments {
124                    pipeline{
125                    id
126                    owner_id
127                    updated_at
128                    visibility
129                    }
130                }
131
132            }
133            }
134            """
135            )
136        )
137        list_of_pipelines = []
138        for v in res["comment_by_pk"]["pipeline_comments"]:
139            list_of_pipelines.append(Pipeline(client=self.client, data=v["pipeline"]))
140        return list_of_pipelines
141
142    def add_to_model(self, model_pk_id: int):
143        data = self._gql_client.execute(
144            gql.gql(
145                """
146            mutation AddCommentToModel($model_pk_id: bigint!, $comment_id: bigint!) {
147            insert_model_variant_comment(objects: {
148                  model_pk_id : $model_pk_id,
149                  comment_id: $comment_id
150            }) {
151                returning {
152                    model_pk_id
153                    comment_id
154                }
155            }
156            }            
157            """
158            ),
159            variable_values={
160                "model_pk_id": model_pk_id,
161                "comment_id": self._id,
162            },
163        )
164        return data["insert_model_variant_comment"]["returning"][0]
165
166    def remove_from_model(self, model_id: int):
167        data = self._gql_client.execute(
168            gql.gql(
169                """
170            mutation RemoveCommentFromModel($model_id: bigint!, $comment_id: bigint!) {
171            delete_model_variant_comment(
172                    where: {
173                        _and: [
174                            { model_id: {_eq: $model_id} } 
175                            { comment_id: {_eq: $comment_id} }
176                        ]
177                    }
178                    ) 
179                {
180                returning {
181                    model_id
182                    comment_id
183                }
184
185                }
186
187            }
188            """
189            ),
190            variable_values={
191                "model_id": model_id,
192                "comment_id": self._id,
193            },
194        )
195        return data["delete_model_variant_comment"]["returning"][0]
196
197    def add_to_pipeline(self, pipeline_id: int):
198        data = self._gql_client.execute(
199            gql.gql(
200                """
201            mutation AddCommentToPipeline($pipeline_pk_id: bigint!, $comment_id: bigint!) {
202            insert_pipeline_comment(objects: {
203                pipeline_pk_id : $pipeline_id,
204                  comment_id: $comment_id
205            }) {
206                returning {
207                    pipeline_pk_id
208                    comment_id
209                }
210            }
211            }            
212            """
213            ),
214            variable_values={
215                "pipeline_pk_id": pipeline_id,
216                "comment_id": self._id,
217            },
218        )
219        return data["insert_pipeline_comment"]["returning"][0]
220
221    def remove_from_pipeline(self, pipeline_pk_id: int):
222        data = self._gql_client.execute(
223            gql.gql(
224                """
225            mutation RemoveCommentFromPipeline($pipeline_pk_id: bigint!, $comment_pk_id: bigint!) {
226            delete_pipeline_comment(
227                    where: {
228                        _and: [
229                            { pipeline_pk_id: {_eq: $pipeline_pk_id} } 
230                            { comment_pk_id: {_eq: $comment_pk_id} }
231                        ]
232                    }
233                    ) 
234                {
235                returning {
236                    pipeline_pk_id
237                    comment_pk_id
238                }
239
240                }
241            }
242            """
243            ),
244            variable_values={
245                "pipeline_pk_id": pipeline_pk_id,
246                "comment_pk_id": self._id,
247            },
248        )
249        return data["delete_pipeline_comment"]["returning"][0]
class Comment(wallaroo.object.Object):
 17class Comment(Object):
 18    """Comment that may be attached to models and pipelines."""
 19
 20    def __init__(
 21        self, client: Optional["Client"], data: Dict[str, Any], standalone=False
 22    ) -> None:
 23        self.client = client
 24        super().__init__(
 25            gql_client=client._gql_client if client is not None else None,
 26            data=data,
 27            standalone=standalone,
 28        )
 29
 30    def _fill(self, data: Dict[str, Any]) -> None:
 31        """Fills an object given a response dictionary from the GraphQL API.
 32
 33        Only the primary key member must be present; other members will be
 34        filled in via rehydration if their corresponding member function is
 35        called.
 36        """
 37        for required_attribute in ["id"]:
 38            if required_attribute not in data:
 39                raise RequiredAttributeMissing(
 40                    self.__class__.__name__, required_attribute
 41                )
 42        # Required
 43        self._id = data["id"]
 44
 45        # Optional
 46        self._user_id = value_if_present(data, "user_id")
 47        self._message = value_if_present(data, "message")
 48        self._updated_at = (
 49            dateparse.isoparse(data["updated_at"])
 50            if "updated_at" in data
 51            else DehydratedValue()
 52        )
 53
 54    def _fetch_attributes(self) -> Dict[str, Any]:
 55        """Fetches all member data from the GraphQL API."""
 56        return self._gql_client.execute(
 57            gql.gql(
 58                """
 59            query CommentById($comment_id: bigint!){
 60            comment_by_pk(id: $comment_id){
 61                id
 62                updated_at
 63                message
 64                user_id
 65                
 66            }
 67            }
 68            """
 69            )
 70        )["comment_by_pk"]
 71
 72    def id(self) -> int:
 73        return self._id
 74
 75    @rehydrate("_user_id")
 76    def user_id(self) -> str:
 77        return cast(str, self._user_id)
 78
 79    @rehydrate("_message")
 80    def message(self) -> str:
 81        return cast(str, self._message)
 82
 83    @rehydrate("_updated_at")
 84    def updated_at(self) -> datetime.datetime:
 85        return cast(datetime.datetime, self._updated_at)
 86
 87    def list_models(self) -> List[Model]:
 88        """Lists the models this comment is on."""
 89        res = self._gql_client.execute(
 90            gql.gql(
 91                """
 92            query ModelsForComment($comment_id: bigint!){
 93            comment_by_pk(id:$comment_id){
 94                    model_variant_comments {
 95                model {
 96                    id
 97                    file_name
 98                    model_id
 99                    sha
100                    owner_id
101                    visibility
102                    updated_at
103                }
104
105                }
106                
107            }
108            }
109            """
110            )
111        )
112        list_of_models = []
113        for v in res["comment_by_pk"]["model_variant_comments"]:
114            list_of_models.append(Model(client=self.client, data=v["model"]))
115        return list_of_models
116
117    def list_pipelines(self) -> List[Pipeline]:
118        """Lists the models this comment is on."""
119        res = self._gql_client.execute(
120            gql.gql(
121                """
122            query PipelinesForComment($comment_id: bigint!){
123            comment_by_pk(id:$comment_id){
124                pipeline_comments {
125                    pipeline{
126                    id
127                    owner_id
128                    updated_at
129                    visibility
130                    }
131                }
132
133            }
134            }
135            """
136            )
137        )
138        list_of_pipelines = []
139        for v in res["comment_by_pk"]["pipeline_comments"]:
140            list_of_pipelines.append(Pipeline(client=self.client, data=v["pipeline"]))
141        return list_of_pipelines
142
143    def add_to_model(self, model_pk_id: int):
144        data = self._gql_client.execute(
145            gql.gql(
146                """
147            mutation AddCommentToModel($model_pk_id: bigint!, $comment_id: bigint!) {
148            insert_model_variant_comment(objects: {
149                  model_pk_id : $model_pk_id,
150                  comment_id: $comment_id
151            }) {
152                returning {
153                    model_pk_id
154                    comment_id
155                }
156            }
157            }            
158            """
159            ),
160            variable_values={
161                "model_pk_id": model_pk_id,
162                "comment_id": self._id,
163            },
164        )
165        return data["insert_model_variant_comment"]["returning"][0]
166
167    def remove_from_model(self, model_id: int):
168        data = self._gql_client.execute(
169            gql.gql(
170                """
171            mutation RemoveCommentFromModel($model_id: bigint!, $comment_id: bigint!) {
172            delete_model_variant_comment(
173                    where: {
174                        _and: [
175                            { model_id: {_eq: $model_id} } 
176                            { comment_id: {_eq: $comment_id} }
177                        ]
178                    }
179                    ) 
180                {
181                returning {
182                    model_id
183                    comment_id
184                }
185
186                }
187
188            }
189            """
190            ),
191            variable_values={
192                "model_id": model_id,
193                "comment_id": self._id,
194            },
195        )
196        return data["delete_model_variant_comment"]["returning"][0]
197
198    def add_to_pipeline(self, pipeline_id: int):
199        data = self._gql_client.execute(
200            gql.gql(
201                """
202            mutation AddCommentToPipeline($pipeline_pk_id: bigint!, $comment_id: bigint!) {
203            insert_pipeline_comment(objects: {
204                pipeline_pk_id : $pipeline_id,
205                  comment_id: $comment_id
206            }) {
207                returning {
208                    pipeline_pk_id
209                    comment_id
210                }
211            }
212            }            
213            """
214            ),
215            variable_values={
216                "pipeline_pk_id": pipeline_id,
217                "comment_id": self._id,
218            },
219        )
220        return data["insert_pipeline_comment"]["returning"][0]
221
222    def remove_from_pipeline(self, pipeline_pk_id: int):
223        data = self._gql_client.execute(
224            gql.gql(
225                """
226            mutation RemoveCommentFromPipeline($pipeline_pk_id: bigint!, $comment_pk_id: bigint!) {
227            delete_pipeline_comment(
228                    where: {
229                        _and: [
230                            { pipeline_pk_id: {_eq: $pipeline_pk_id} } 
231                            { comment_pk_id: {_eq: $comment_pk_id} }
232                        ]
233                    }
234                    ) 
235                {
236                returning {
237                    pipeline_pk_id
238                    comment_pk_id
239                }
240
241                }
242            }
243            """
244            ),
245            variable_values={
246                "pipeline_pk_id": pipeline_pk_id,
247                "comment_pk_id": self._id,
248            },
249        )
250        return data["delete_pipeline_comment"]["returning"][0]

Comment that may be attached to models and pipelines.

Comment( client: Optional[wallaroo.client.Client], data: Dict[str, Any], standalone=False)
20    def __init__(
21        self, client: Optional["Client"], data: Dict[str, Any], standalone=False
22    ) -> None:
23        self.client = client
24        super().__init__(
25            gql_client=client._gql_client if client is not None else None,
26            data=data,
27            standalone=standalone,
28        )

Base constructor.

Each object requires:

  • a GraphQL client - in order to fill its missing members dynamically
  • an initial data blob - typically from unserialized JSON, contains at
  • least the data for required members (typically the object's primary key) and optionally other data members.
def id(self) -> int:
72    def id(self) -> int:
73        return self._id
def user_id(*args, **kwargs):
41        def wrapper(*args, **kwargs):
42            obj = args[0]
43            if not getattr(obj, "_standalone", None):
44                present = getattr(obj, attr) != DehydratedValue()
45                # Uncomment to debug while testing
46                # print(
47                #    "rehydrate: {} -> {}".format(
48                #        attr, "present" if present else "not present"
49                #    )
50                # )
51                if not present:
52                    obj._rehydrate()
53            result = fn(*args, **kwargs)
54            return result
def message(*args, **kwargs):
41        def wrapper(*args, **kwargs):
42            obj = args[0]
43            if not getattr(obj, "_standalone", None):
44                present = getattr(obj, attr) != DehydratedValue()
45                # Uncomment to debug while testing
46                # print(
47                #    "rehydrate: {} -> {}".format(
48                #        attr, "present" if present else "not present"
49                #    )
50                # )
51                if not present:
52                    obj._rehydrate()
53            result = fn(*args, **kwargs)
54            return result
def updated_at(*args, **kwargs):
41        def wrapper(*args, **kwargs):
42            obj = args[0]
43            if not getattr(obj, "_standalone", None):
44                present = getattr(obj, attr) != DehydratedValue()
45                # Uncomment to debug while testing
46                # print(
47                #    "rehydrate: {} -> {}".format(
48                #        attr, "present" if present else "not present"
49                #    )
50                # )
51                if not present:
52                    obj._rehydrate()
53            result = fn(*args, **kwargs)
54            return result
def list_models(self) -> List[wallaroo.model.Model]:
 87    def list_models(self) -> List[Model]:
 88        """Lists the models this comment is on."""
 89        res = self._gql_client.execute(
 90            gql.gql(
 91                """
 92            query ModelsForComment($comment_id: bigint!){
 93            comment_by_pk(id:$comment_id){
 94                    model_variant_comments {
 95                model {
 96                    id
 97                    file_name
 98                    model_id
 99                    sha
100                    owner_id
101                    visibility
102                    updated_at
103                }
104
105                }
106                
107            }
108            }
109            """
110            )
111        )
112        list_of_models = []
113        for v in res["comment_by_pk"]["model_variant_comments"]:
114            list_of_models.append(Model(client=self.client, data=v["model"]))
115        return list_of_models

Lists the models this comment is on.

def list_pipelines(self) -> List[wallaroo.pipeline.Pipeline]:
117    def list_pipelines(self) -> List[Pipeline]:
118        """Lists the models this comment is on."""
119        res = self._gql_client.execute(
120            gql.gql(
121                """
122            query PipelinesForComment($comment_id: bigint!){
123            comment_by_pk(id:$comment_id){
124                pipeline_comments {
125                    pipeline{
126                    id
127                    owner_id
128                    updated_at
129                    visibility
130                    }
131                }
132
133            }
134            }
135            """
136            )
137        )
138        list_of_pipelines = []
139        for v in res["comment_by_pk"]["pipeline_comments"]:
140            list_of_pipelines.append(Pipeline(client=self.client, data=v["pipeline"]))
141        return list_of_pipelines

Lists the models this comment is on.

def add_to_model(self, model_pk_id: int):
143    def add_to_model(self, model_pk_id: int):
144        data = self._gql_client.execute(
145            gql.gql(
146                """
147            mutation AddCommentToModel($model_pk_id: bigint!, $comment_id: bigint!) {
148            insert_model_variant_comment(objects: {
149                  model_pk_id : $model_pk_id,
150                  comment_id: $comment_id
151            }) {
152                returning {
153                    model_pk_id
154                    comment_id
155                }
156            }
157            }            
158            """
159            ),
160            variable_values={
161                "model_pk_id": model_pk_id,
162                "comment_id": self._id,
163            },
164        )
165        return data["insert_model_variant_comment"]["returning"][0]
def remove_from_model(self, model_id: int):
167    def remove_from_model(self, model_id: int):
168        data = self._gql_client.execute(
169            gql.gql(
170                """
171            mutation RemoveCommentFromModel($model_id: bigint!, $comment_id: bigint!) {
172            delete_model_variant_comment(
173                    where: {
174                        _and: [
175                            { model_id: {_eq: $model_id} } 
176                            { comment_id: {_eq: $comment_id} }
177                        ]
178                    }
179                    ) 
180                {
181                returning {
182                    model_id
183                    comment_id
184                }
185
186                }
187
188            }
189            """
190            ),
191            variable_values={
192                "model_id": model_id,
193                "comment_id": self._id,
194            },
195        )
196        return data["delete_model_variant_comment"]["returning"][0]
def add_to_pipeline(self, pipeline_id: int):
198    def add_to_pipeline(self, pipeline_id: int):
199        data = self._gql_client.execute(
200            gql.gql(
201                """
202            mutation AddCommentToPipeline($pipeline_pk_id: bigint!, $comment_id: bigint!) {
203            insert_pipeline_comment(objects: {
204                pipeline_pk_id : $pipeline_id,
205                  comment_id: $comment_id
206            }) {
207                returning {
208                    pipeline_pk_id
209                    comment_id
210                }
211            }
212            }            
213            """
214            ),
215            variable_values={
216                "pipeline_pk_id": pipeline_id,
217                "comment_id": self._id,
218            },
219        )
220        return data["insert_pipeline_comment"]["returning"][0]
def remove_from_pipeline(self, pipeline_pk_id: int):
222    def remove_from_pipeline(self, pipeline_pk_id: int):
223        data = self._gql_client.execute(
224            gql.gql(
225                """
226            mutation RemoveCommentFromPipeline($pipeline_pk_id: bigint!, $comment_pk_id: bigint!) {
227            delete_pipeline_comment(
228                    where: {
229                        _and: [
230                            { pipeline_pk_id: {_eq: $pipeline_pk_id} } 
231                            { comment_pk_id: {_eq: $comment_pk_id} }
232                        ]
233                    }
234                    ) 
235                {
236                returning {
237                    pipeline_pk_id
238                    comment_pk_id
239                }
240
241                }
242            }
243            """
244            ),
245            variable_values={
246                "pipeline_pk_id": pipeline_pk_id,
247                "comment_pk_id": self._id,
248            },
249        )
250        return data["delete_pipeline_comment"]["returning"][0]