corvic-engine 0.3.0rc77__cp38-abi3-win_amd64.whl → 0.3.0rc78__cp38-abi3-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
corvic/orm/base.py DELETED
@@ -1,256 +0,0 @@
1
- """Base models for corvic RDBMS backed orm tables."""
2
-
3
- from __future__ import annotations
4
-
5
- import uuid
6
- from datetime import UTC, datetime
7
- from typing import Any, ClassVar, Protocol, Self, runtime_checkable
8
-
9
- import sqlalchemy as sa
10
- import sqlalchemy.orm as sa_orm
11
- from google.protobuf import timestamp_pb2
12
- from sqlalchemy.ext import hybrid
13
-
14
- from corvic.orm._proto_columns import ProtoMessageDecorator
15
- from corvic.orm.func import utc_now
16
- from corvic.orm.ids import (
17
- AgentID,
18
- AgentMessageID,
19
- CompletionModelID,
20
- FeatureViewID,
21
- FeatureViewSourceID,
22
- IntIDDecorator,
23
- MessageEntryID,
24
- OrgID,
25
- PipelineID,
26
- ResourceID,
27
- RoomID,
28
- SourceID,
29
- SpaceID,
30
- SpaceParametersID,
31
- SpaceRunID,
32
- StrIDDecorator,
33
- UserMessageID,
34
- )
35
- from corvic.orm.keys import (
36
- INT_PK_TYPE,
37
- primary_key_identity_column,
38
- primary_key_uuid_column,
39
- )
40
- from corvic_generated.orm.v1 import (
41
- agent_pb2,
42
- common_pb2,
43
- completion_model_pb2,
44
- feature_view_pb2,
45
- pipeline_pb2,
46
- space_pb2,
47
- table_pb2,
48
- )
49
- from corvic_generated.status.v1 import event_pb2
50
-
51
- # A quick primer on SQLAlchemy (sa) hybrid methods:
52
- #
53
- # Hybrid just means functionality that is different at the class-level versus
54
- # the instance-level, and in the sa documentation, the authors really
55
- # want to stress that class-versus-instance (decorators) is orthgonal to an
56
- # ORM.
57
- #
58
- # However, this distinction is not particularly helpful for users of sa.
59
- # It is best to have a working model of instance-level means Python-world and
60
- # class-level means SQL-world. So, a hybrid method is something that has
61
- # a different Python representation from its SQL representation.
62
- #
63
- # Since sa already handles conversions like "Python str" to "SQL text",
64
- # certain representation differences between Python and SQL are already handled
65
- # (not considered differences at all).
66
- #
67
- # Hybrid methods are for cases where we want to do non-trivial transformations
68
- # between SQL and Python representations.
69
- #
70
- # The recipe is:
71
- #
72
- # 1. Define a hybrid_method / hybrid_property (wlog property) that produces the Python
73
- # object you want.
74
- # 2. If the property doesn't need to be used in any sa query again, you are done.
75
- # 3. If the property is simple enough for sa to also use it to produce the SQL
76
- # representation, you are also done. E.g., comparisons and bitwise operations
77
- # on columns.
78
- # 4. Otherwise, you need to define a class-level function, hybrid_property.expression,
79
- # which gives the SQL representation of your property when it is passed to
80
- # a sa query.
81
- # 5. Because of how redefining decorators is supposed to work in Python [1], you
82
- # should use @<property_method_name>.inplace.expression to define your
83
- # class-level function that describes how the property should be represented
84
- # in SQL.
85
- #
86
- # [1] https://docs.sqlalchemy.org/en/20/orm/extensions/hybrid.html#using-inplace-to-create-pep-484-compliant-hybrid-properties
87
-
88
-
89
- class Base(sa_orm.MappedAsDataclass, sa_orm.DeclarativeBase):
90
- """Base class for all DB mapped classes."""
91
-
92
- type_annotation_map: ClassVar = {
93
- # proto message column types
94
- common_pb2.BlobUrlList: ProtoMessageDecorator(common_pb2.BlobUrlList()),
95
- feature_view_pb2.FeatureViewOutput: ProtoMessageDecorator(
96
- feature_view_pb2.FeatureViewOutput()
97
- ),
98
- common_pb2.EmbeddingMetrics: ProtoMessageDecorator(
99
- common_pb2.EmbeddingMetrics()
100
- ),
101
- common_pb2.AgentMessageMetadata: ProtoMessageDecorator(
102
- common_pb2.AgentMessageMetadata()
103
- ),
104
- space_pb2.SpaceParameters: ProtoMessageDecorator(space_pb2.SpaceParameters()),
105
- table_pb2.TableComputeOp: ProtoMessageDecorator(table_pb2.TableComputeOp()),
106
- agent_pb2.AgentParameters: ProtoMessageDecorator(agent_pb2.AgentParameters()),
107
- table_pb2.NamedTables: ProtoMessageDecorator(table_pb2.NamedTables()),
108
- common_pb2.RetrievedEntities: ProtoMessageDecorator(
109
- common_pb2.RetrievedEntities()
110
- ),
111
- pipeline_pb2.PipelineTransformation: ProtoMessageDecorator(
112
- pipeline_pb2.PipelineTransformation()
113
- ),
114
- event_pb2.Event: ProtoMessageDecorator(event_pb2.Event()),
115
- completion_model_pb2.CompletionModelParameters: ProtoMessageDecorator(
116
- completion_model_pb2.CompletionModelParameters()
117
- ),
118
- # ID types
119
- OrgID: StrIDDecorator(OrgID()),
120
- RoomID: IntIDDecorator(RoomID()),
121
- ResourceID: IntIDDecorator(ResourceID()),
122
- SourceID: IntIDDecorator(SourceID()),
123
- PipelineID: IntIDDecorator(PipelineID()),
124
- FeatureViewID: IntIDDecorator(FeatureViewID()),
125
- FeatureViewSourceID: IntIDDecorator(FeatureViewSourceID()),
126
- SpaceID: IntIDDecorator(SpaceID()),
127
- SpaceRunID: IntIDDecorator(SpaceRunID()),
128
- SpaceParametersID: IntIDDecorator(SpaceParametersID()),
129
- AgentID: IntIDDecorator(AgentID()),
130
- AgentMessageID: IntIDDecorator(AgentMessageID()),
131
- UserMessageID: IntIDDecorator(UserMessageID()),
132
- MessageEntryID: IntIDDecorator(MessageEntryID()),
133
- CompletionModelID: IntIDDecorator(CompletionModelID()),
134
- }
135
-
136
- _created_at: sa_orm.Mapped[datetime] = sa_orm.mapped_column(
137
- "created_at",
138
- sa.DateTime(timezone=True),
139
- server_default=utc_now(),
140
- init=False,
141
- index=True,
142
- )
143
-
144
- _updated_at: sa_orm.Mapped[datetime] = sa_orm.mapped_column(
145
- "updated_at",
146
- sa.DateTime(timezone=True),
147
- onupdate=utc_now(),
148
- server_default=utc_now(),
149
- init=False,
150
- nullable=True,
151
- )
152
-
153
- @hybrid.hybrid_property
154
- def created_at(self) -> datetime | None:
155
- if not self._created_at:
156
- return None
157
- return self._created_at.replace(tzinfo=UTC)
158
-
159
- @created_at.inplace.expression
160
- @classmethod
161
- def _created_at_expression(cls):
162
- return cls._created_at
163
-
164
- @hybrid.hybrid_property
165
- def updated_at(self) -> datetime | None:
166
- if not self._updated_at:
167
- return None
168
- return self._updated_at.replace(tzinfo=UTC)
169
-
170
- @updated_at.inplace.expression
171
- @classmethod
172
- def _updated_at_expression(cls):
173
- return cls._updated_at
174
-
175
-
176
- class OrgBase(Base):
177
- """An organization it a top level grouping of resources."""
178
-
179
- __tablename__ = "org"
180
-
181
- # overriding table_args is the recommending way of defining these base model types
182
- __table_args__: ClassVar[Any] = ({"extend_existing": True},)
183
-
184
- id: sa_orm.Mapped[OrgID | None] = primary_key_uuid_column()
185
-
186
- @property
187
- def name(self) -> str:
188
- return str(self.id)
189
-
190
-
191
- class EventKey:
192
- """An event key."""
193
-
194
- @runtime_checkable
195
- class Provider(Protocol):
196
- """Type which can provide an event key."""
197
-
198
- @property
199
- def event_key(self) -> EventKey: ...
200
-
201
- def __init__(self, id: str):
202
- self._id = id
203
-
204
- def __str__(self):
205
- return self._id
206
-
207
- @classmethod
208
- def from_str(cls, id: str) -> Self:
209
- return cls(id=id)
210
-
211
- @classmethod
212
- def from_uuid(cls, uuid: uuid.UUID) -> Self:
213
- return cls(id=str(uuid))
214
-
215
- @property
216
- def event_key(self):
217
- return self
218
-
219
-
220
- class EventBase(Base):
221
- """Events from corvic orm objects."""
222
-
223
- __tablename__ = "event"
224
-
225
- # overriding table_args is the recommending way of defining these base model types
226
- __table_args__: ClassVar[Any] = {"extend_existing": True}
227
-
228
- event: sa_orm.Mapped[int] = sa_orm.mapped_column(sa.Integer)
229
- reason: sa_orm.Mapped[str] = sa_orm.mapped_column(sa.Text)
230
- regarding: sa_orm.Mapped[str] = sa_orm.mapped_column(sa.Text)
231
- event_key: sa_orm.Mapped[str] = sa_orm.mapped_column(sa.Text)
232
- timestamp: sa_orm.Mapped[datetime] = sa_orm.mapped_column(
233
- sa.DateTime(timezone=True)
234
- )
235
- id: sa_orm.Mapped[int | None] = primary_key_identity_column(type_=INT_PK_TYPE)
236
-
237
- @classmethod
238
- def select_latest_by_event_key(cls, event_key: EventKey, limit: int | None = None):
239
- query = (
240
- sa.select(cls)
241
- .where(cls.event_key == str(event_key))
242
- .order_by(cls.timestamp.desc())
243
- )
244
- if limit:
245
- query = query.limit(limit)
246
- return query
247
-
248
- def as_event(self) -> event_pb2.Event:
249
- timestamp = timestamp_pb2.Timestamp()
250
- timestamp.FromDatetime(dt=self.timestamp)
251
- return event_pb2.Event(
252
- reason=self.reason,
253
- regarding=self.regarding,
254
- event_type=event_pb2.EventType.Name(self.event),
255
- timestamp=timestamp,
256
- )