mlrun 1.7.0rc4__py3-none-any.whl → 1.7.2__py3-none-any.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.

Potentially problematic release.


This version of mlrun might be problematic. Click here for more details.

Files changed (235) hide show
  1. mlrun/__init__.py +11 -1
  2. mlrun/__main__.py +39 -121
  3. mlrun/{datastore/helpers.py → alerts/__init__.py} +2 -5
  4. mlrun/alerts/alert.py +248 -0
  5. mlrun/api/schemas/__init__.py +4 -3
  6. mlrun/artifacts/__init__.py +8 -3
  7. mlrun/artifacts/base.py +39 -254
  8. mlrun/artifacts/dataset.py +9 -190
  9. mlrun/artifacts/manager.py +73 -46
  10. mlrun/artifacts/model.py +30 -158
  11. mlrun/artifacts/plots.py +23 -380
  12. mlrun/common/constants.py +73 -1
  13. mlrun/common/db/sql_session.py +3 -2
  14. mlrun/common/formatters/__init__.py +21 -0
  15. mlrun/common/formatters/artifact.py +46 -0
  16. mlrun/common/formatters/base.py +113 -0
  17. mlrun/common/formatters/feature_set.py +44 -0
  18. mlrun/common/formatters/function.py +46 -0
  19. mlrun/common/formatters/pipeline.py +53 -0
  20. mlrun/common/formatters/project.py +51 -0
  21. mlrun/common/formatters/run.py +29 -0
  22. mlrun/common/helpers.py +11 -1
  23. mlrun/{runtimes → common/runtimes}/constants.py +32 -4
  24. mlrun/common/schemas/__init__.py +31 -4
  25. mlrun/common/schemas/alert.py +202 -0
  26. mlrun/common/schemas/api_gateway.py +196 -0
  27. mlrun/common/schemas/artifact.py +28 -1
  28. mlrun/common/schemas/auth.py +13 -2
  29. mlrun/common/schemas/client_spec.py +2 -1
  30. mlrun/common/schemas/common.py +7 -4
  31. mlrun/common/schemas/constants.py +3 -0
  32. mlrun/common/schemas/feature_store.py +58 -28
  33. mlrun/common/schemas/frontend_spec.py +8 -0
  34. mlrun/common/schemas/function.py +11 -0
  35. mlrun/common/schemas/hub.py +7 -9
  36. mlrun/common/schemas/model_monitoring/__init__.py +21 -4
  37. mlrun/common/schemas/model_monitoring/constants.py +136 -42
  38. mlrun/common/schemas/model_monitoring/grafana.py +9 -5
  39. mlrun/common/schemas/model_monitoring/model_endpoints.py +89 -41
  40. mlrun/common/schemas/notification.py +69 -12
  41. mlrun/{runtimes/mpijob/v1alpha1.py → common/schemas/pagination.py} +10 -13
  42. mlrun/common/schemas/pipeline.py +7 -0
  43. mlrun/common/schemas/project.py +67 -16
  44. mlrun/common/schemas/runs.py +17 -0
  45. mlrun/common/schemas/schedule.py +1 -1
  46. mlrun/common/schemas/workflow.py +10 -2
  47. mlrun/common/types.py +14 -1
  48. mlrun/config.py +233 -58
  49. mlrun/data_types/data_types.py +11 -1
  50. mlrun/data_types/spark.py +5 -4
  51. mlrun/data_types/to_pandas.py +75 -34
  52. mlrun/datastore/__init__.py +8 -10
  53. mlrun/datastore/alibaba_oss.py +131 -0
  54. mlrun/datastore/azure_blob.py +131 -43
  55. mlrun/datastore/base.py +107 -47
  56. mlrun/datastore/datastore.py +17 -7
  57. mlrun/datastore/datastore_profile.py +91 -7
  58. mlrun/datastore/dbfs_store.py +3 -7
  59. mlrun/datastore/filestore.py +1 -3
  60. mlrun/datastore/google_cloud_storage.py +92 -32
  61. mlrun/datastore/hdfs.py +5 -0
  62. mlrun/datastore/inmem.py +6 -3
  63. mlrun/datastore/redis.py +3 -2
  64. mlrun/datastore/s3.py +30 -12
  65. mlrun/datastore/snowflake_utils.py +45 -0
  66. mlrun/datastore/sources.py +274 -59
  67. mlrun/datastore/spark_utils.py +30 -0
  68. mlrun/datastore/store_resources.py +9 -7
  69. mlrun/datastore/storeytargets.py +151 -0
  70. mlrun/datastore/targets.py +387 -119
  71. mlrun/datastore/utils.py +68 -5
  72. mlrun/datastore/v3io.py +28 -50
  73. mlrun/db/auth_utils.py +152 -0
  74. mlrun/db/base.py +245 -20
  75. mlrun/db/factory.py +1 -4
  76. mlrun/db/httpdb.py +909 -231
  77. mlrun/db/nopdb.py +279 -14
  78. mlrun/errors.py +35 -5
  79. mlrun/execution.py +111 -38
  80. mlrun/feature_store/__init__.py +0 -2
  81. mlrun/feature_store/api.py +46 -53
  82. mlrun/feature_store/common.py +6 -11
  83. mlrun/feature_store/feature_set.py +48 -23
  84. mlrun/feature_store/feature_vector.py +13 -2
  85. mlrun/feature_store/ingestion.py +7 -6
  86. mlrun/feature_store/retrieval/base.py +9 -4
  87. mlrun/feature_store/retrieval/dask_merger.py +2 -0
  88. mlrun/feature_store/retrieval/job.py +13 -4
  89. mlrun/feature_store/retrieval/local_merger.py +2 -0
  90. mlrun/feature_store/retrieval/spark_merger.py +24 -32
  91. mlrun/feature_store/steps.py +38 -19
  92. mlrun/features.py +6 -14
  93. mlrun/frameworks/_common/plan.py +3 -3
  94. mlrun/frameworks/_dl_common/loggers/tensorboard_logger.py +7 -12
  95. mlrun/frameworks/_ml_common/plan.py +1 -1
  96. mlrun/frameworks/auto_mlrun/auto_mlrun.py +2 -2
  97. mlrun/frameworks/lgbm/__init__.py +1 -1
  98. mlrun/frameworks/lgbm/callbacks/callback.py +2 -4
  99. mlrun/frameworks/lgbm/model_handler.py +1 -1
  100. mlrun/frameworks/parallel_coordinates.py +4 -4
  101. mlrun/frameworks/pytorch/__init__.py +2 -2
  102. mlrun/frameworks/sklearn/__init__.py +1 -1
  103. mlrun/frameworks/sklearn/mlrun_interface.py +13 -3
  104. mlrun/frameworks/tf_keras/__init__.py +5 -2
  105. mlrun/frameworks/tf_keras/callbacks/logging_callback.py +1 -1
  106. mlrun/frameworks/tf_keras/mlrun_interface.py +2 -2
  107. mlrun/frameworks/xgboost/__init__.py +1 -1
  108. mlrun/k8s_utils.py +57 -12
  109. mlrun/launcher/__init__.py +1 -1
  110. mlrun/launcher/base.py +6 -5
  111. mlrun/launcher/client.py +13 -11
  112. mlrun/launcher/factory.py +1 -1
  113. mlrun/launcher/local.py +15 -5
  114. mlrun/launcher/remote.py +10 -3
  115. mlrun/lists.py +6 -2
  116. mlrun/model.py +297 -48
  117. mlrun/model_monitoring/__init__.py +1 -1
  118. mlrun/model_monitoring/api.py +152 -357
  119. mlrun/model_monitoring/applications/__init__.py +10 -0
  120. mlrun/model_monitoring/applications/_application_steps.py +190 -0
  121. mlrun/model_monitoring/applications/base.py +108 -0
  122. mlrun/model_monitoring/applications/context.py +341 -0
  123. mlrun/model_monitoring/{evidently_application.py → applications/evidently_base.py} +27 -22
  124. mlrun/model_monitoring/applications/histogram_data_drift.py +227 -91
  125. mlrun/model_monitoring/applications/results.py +99 -0
  126. mlrun/model_monitoring/controller.py +130 -303
  127. mlrun/model_monitoring/{stores/models/sqlite.py → db/__init__.py} +5 -10
  128. mlrun/model_monitoring/db/stores/__init__.py +136 -0
  129. mlrun/model_monitoring/db/stores/base/__init__.py +15 -0
  130. mlrun/model_monitoring/db/stores/base/store.py +213 -0
  131. mlrun/model_monitoring/db/stores/sqldb/__init__.py +13 -0
  132. mlrun/model_monitoring/db/stores/sqldb/models/__init__.py +71 -0
  133. mlrun/model_monitoring/db/stores/sqldb/models/base.py +190 -0
  134. mlrun/model_monitoring/db/stores/sqldb/models/mysql.py +103 -0
  135. mlrun/model_monitoring/{stores/models/mysql.py → db/stores/sqldb/models/sqlite.py} +19 -13
  136. mlrun/model_monitoring/db/stores/sqldb/sql_store.py +659 -0
  137. mlrun/model_monitoring/db/stores/v3io_kv/__init__.py +13 -0
  138. mlrun/model_monitoring/db/stores/v3io_kv/kv_store.py +726 -0
  139. mlrun/model_monitoring/db/tsdb/__init__.py +105 -0
  140. mlrun/model_monitoring/db/tsdb/base.py +448 -0
  141. mlrun/model_monitoring/db/tsdb/helpers.py +30 -0
  142. mlrun/model_monitoring/db/tsdb/tdengine/__init__.py +15 -0
  143. mlrun/model_monitoring/db/tsdb/tdengine/schemas.py +298 -0
  144. mlrun/model_monitoring/db/tsdb/tdengine/stream_graph_steps.py +42 -0
  145. mlrun/model_monitoring/db/tsdb/tdengine/tdengine_connector.py +522 -0
  146. mlrun/model_monitoring/db/tsdb/v3io/__init__.py +15 -0
  147. mlrun/model_monitoring/db/tsdb/v3io/stream_graph_steps.py +158 -0
  148. mlrun/model_monitoring/db/tsdb/v3io/v3io_connector.py +849 -0
  149. mlrun/model_monitoring/features_drift_table.py +34 -22
  150. mlrun/model_monitoring/helpers.py +177 -39
  151. mlrun/model_monitoring/model_endpoint.py +3 -2
  152. mlrun/model_monitoring/stream_processing.py +165 -398
  153. mlrun/model_monitoring/tracking_policy.py +7 -1
  154. mlrun/model_monitoring/writer.py +161 -125
  155. mlrun/package/packagers/default_packager.py +2 -2
  156. mlrun/package/packagers_manager.py +1 -0
  157. mlrun/package/utils/_formatter.py +2 -2
  158. mlrun/platforms/__init__.py +11 -10
  159. mlrun/platforms/iguazio.py +67 -228
  160. mlrun/projects/__init__.py +6 -1
  161. mlrun/projects/operations.py +47 -20
  162. mlrun/projects/pipelines.py +396 -249
  163. mlrun/projects/project.py +1176 -406
  164. mlrun/render.py +28 -22
  165. mlrun/run.py +208 -181
  166. mlrun/runtimes/__init__.py +76 -11
  167. mlrun/runtimes/base.py +54 -24
  168. mlrun/runtimes/daskjob.py +9 -2
  169. mlrun/runtimes/databricks_job/databricks_runtime.py +1 -0
  170. mlrun/runtimes/databricks_job/databricks_wrapper.py +1 -1
  171. mlrun/runtimes/funcdoc.py +1 -29
  172. mlrun/runtimes/kubejob.py +34 -128
  173. mlrun/runtimes/local.py +39 -10
  174. mlrun/runtimes/mpijob/__init__.py +0 -20
  175. mlrun/runtimes/mpijob/abstract.py +8 -8
  176. mlrun/runtimes/mpijob/v1.py +1 -1
  177. mlrun/runtimes/nuclio/__init__.py +1 -0
  178. mlrun/runtimes/nuclio/api_gateway.py +769 -0
  179. mlrun/runtimes/nuclio/application/__init__.py +15 -0
  180. mlrun/runtimes/nuclio/application/application.py +758 -0
  181. mlrun/runtimes/nuclio/application/reverse_proxy.go +95 -0
  182. mlrun/runtimes/nuclio/function.py +188 -68
  183. mlrun/runtimes/nuclio/serving.py +57 -60
  184. mlrun/runtimes/pod.py +191 -58
  185. mlrun/runtimes/remotesparkjob.py +11 -8
  186. mlrun/runtimes/sparkjob/spark3job.py +17 -18
  187. mlrun/runtimes/utils.py +40 -73
  188. mlrun/secrets.py +6 -2
  189. mlrun/serving/__init__.py +8 -1
  190. mlrun/serving/remote.py +2 -3
  191. mlrun/serving/routers.py +89 -64
  192. mlrun/serving/server.py +54 -26
  193. mlrun/serving/states.py +187 -56
  194. mlrun/serving/utils.py +19 -11
  195. mlrun/serving/v2_serving.py +136 -63
  196. mlrun/track/tracker.py +2 -1
  197. mlrun/track/trackers/mlflow_tracker.py +5 -0
  198. mlrun/utils/async_http.py +26 -6
  199. mlrun/utils/db.py +18 -0
  200. mlrun/utils/helpers.py +375 -105
  201. mlrun/utils/http.py +2 -2
  202. mlrun/utils/logger.py +75 -9
  203. mlrun/utils/notifications/notification/__init__.py +14 -10
  204. mlrun/utils/notifications/notification/base.py +48 -0
  205. mlrun/utils/notifications/notification/console.py +2 -0
  206. mlrun/utils/notifications/notification/git.py +24 -1
  207. mlrun/utils/notifications/notification/ipython.py +2 -0
  208. mlrun/utils/notifications/notification/slack.py +96 -21
  209. mlrun/utils/notifications/notification/webhook.py +63 -2
  210. mlrun/utils/notifications/notification_pusher.py +146 -16
  211. mlrun/utils/regex.py +9 -0
  212. mlrun/utils/retryer.py +3 -2
  213. mlrun/utils/v3io_clients.py +2 -3
  214. mlrun/utils/version/version.json +2 -2
  215. mlrun-1.7.2.dist-info/METADATA +390 -0
  216. mlrun-1.7.2.dist-info/RECORD +351 -0
  217. {mlrun-1.7.0rc4.dist-info → mlrun-1.7.2.dist-info}/WHEEL +1 -1
  218. mlrun/feature_store/retrieval/conversion.py +0 -271
  219. mlrun/kfpops.py +0 -868
  220. mlrun/model_monitoring/application.py +0 -310
  221. mlrun/model_monitoring/batch.py +0 -974
  222. mlrun/model_monitoring/controller_handler.py +0 -37
  223. mlrun/model_monitoring/prometheus.py +0 -216
  224. mlrun/model_monitoring/stores/__init__.py +0 -111
  225. mlrun/model_monitoring/stores/kv_model_endpoint_store.py +0 -574
  226. mlrun/model_monitoring/stores/model_endpoint_store.py +0 -145
  227. mlrun/model_monitoring/stores/models/__init__.py +0 -27
  228. mlrun/model_monitoring/stores/models/base.py +0 -84
  229. mlrun/model_monitoring/stores/sql_model_endpoint_store.py +0 -382
  230. mlrun/platforms/other.py +0 -305
  231. mlrun-1.7.0rc4.dist-info/METADATA +0 -269
  232. mlrun-1.7.0rc4.dist-info/RECORD +0 -321
  233. {mlrun-1.7.0rc4.dist-info → mlrun-1.7.2.dist-info}/LICENSE +0 -0
  234. {mlrun-1.7.0rc4.dist-info → mlrun-1.7.2.dist-info}/entry_points.txt +0 -0
  235. {mlrun-1.7.0rc4.dist-info → mlrun-1.7.2.dist-info}/top_level.txt +0 -0
@@ -1,145 +0,0 @@
1
- # Copyright 2023 Iguazio
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- #
15
-
16
- import typing
17
- from abc import ABC, abstractmethod
18
-
19
-
20
- class ModelEndpointStore(ABC):
21
- """
22
- An abstract class to handle the model endpoint in the DB target.
23
- """
24
-
25
- def __init__(self, project: str):
26
- """
27
- Initialize a new model endpoint target.
28
-
29
- :param project: The name of the project.
30
- """
31
- self.project = project
32
-
33
- @abstractmethod
34
- def write_model_endpoint(self, endpoint: dict[str, typing.Any]):
35
- """
36
- Create a new endpoint record in the DB table.
37
-
38
- :param endpoint: model endpoint dictionary that will be written into the DB.
39
- """
40
- pass
41
-
42
- @abstractmethod
43
- def update_model_endpoint(
44
- self, endpoint_id: str, attributes: dict[str, typing.Any]
45
- ):
46
- """
47
- Update a model endpoint record with a given attributes.
48
-
49
- :param endpoint_id: The unique id of the model endpoint.
50
- :param attributes: Dictionary of attributes that will be used for update the model endpoint. Note that the keys
51
- of the attributes dictionary should exist in the DB table.
52
-
53
- """
54
- pass
55
-
56
- @abstractmethod
57
- def delete_model_endpoint(self, endpoint_id: str):
58
- """
59
- Deletes the record of a given model endpoint id.
60
-
61
- :param endpoint_id: The unique id of the model endpoint.
62
- """
63
- pass
64
-
65
- @abstractmethod
66
- def delete_model_endpoints_resources(self, endpoints: list[dict[str, typing.Any]]):
67
- """
68
- Delete all model endpoints resources.
69
-
70
- :param endpoints: A list of model endpoints flattened dictionaries.
71
-
72
- """
73
- pass
74
-
75
- @abstractmethod
76
- def get_model_endpoint(
77
- self,
78
- endpoint_id: str,
79
- ) -> dict[str, typing.Any]:
80
- """
81
- Get a single model endpoint record.
82
-
83
- :param endpoint_id: The unique id of the model endpoint.
84
-
85
- :return: A model endpoint record as a dictionary.
86
- """
87
- pass
88
-
89
- @abstractmethod
90
- def list_model_endpoints(
91
- self,
92
- model: str = None,
93
- function: str = None,
94
- labels: list[str] = None,
95
- top_level: bool = None,
96
- uids: list = None,
97
- ) -> list[dict[str, typing.Any]]:
98
- """
99
- Returns a list of model endpoint dictionaries, supports filtering by model, function, labels or top level.
100
- By default, when no filters are applied, all available model endpoints for the given project will
101
- be listed.
102
-
103
- :param model: The name of the model to filter by.
104
- :param function: The name of the function to filter by.
105
- :param labels: A list of labels to filter by. Label filters work by either filtering a specific value
106
- of a label (i.e. list("key=value")) or by looking for the existence of a given
107
- key (i.e. "key").
108
- :param top_level: If True will return only routers and endpoint that are NOT children of any router.
109
- :param uids: List of model endpoint unique ids to include in the result.
110
-
111
- :return: A list of model endpoint dictionaries.
112
- """
113
- pass
114
-
115
- @abstractmethod
116
- def get_endpoint_real_time_metrics(
117
- self,
118
- endpoint_id: str,
119
- metrics: list[str],
120
- start: str = "now-1h",
121
- end: str = "now",
122
- access_key: str = None,
123
- ) -> dict[str, list[tuple[str, float]]]:
124
- """
125
- Getting metrics from the time series DB. There are pre-defined metrics for model endpoints such as
126
- `predictions_per_second` and `latency_avg_5m` but also custom metrics defined by the user.
127
-
128
- :param endpoint_id: The unique id of the model endpoint.
129
- :param metrics: A list of real-time metrics to return for the model endpoint.
130
- :param start: The start time of the metrics. Can be represented by a string containing an RFC 3339
131
- time, a Unix timestamp in milliseconds, a relative time (`'now'` or
132
- `'now-[0-9]+[mhd]'`, where `m` = minutes, `h` = hours, and `'d'` = days), or 0 for the
133
- earliest time.
134
- :param end: The end time of the metrics. Can be represented by a string containing an RFC 3339
135
- time, a Unix timestamp in milliseconds, a relative time (`'now'` or
136
- `'now-[0-9]+[mhd]'`, where `m` = minutes, `h` = hours, and `'d'` = days), or 0 for the
137
- earliest time.
138
- :param access_key: V3IO access key that will be used for generating Frames client object. If not
139
- provided, the access key will be retrieved from the environment variables.
140
-
141
- :return: A dictionary of metrics in which the key is a metric name and the value is a list of tuples that
142
- includes timestamps and the values.
143
- """
144
-
145
- pass
@@ -1,27 +0,0 @@
1
- # Copyright 2023 Iguazio
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from typing import Optional, Union
16
-
17
- from .mysql import ModelEndpointsTable as MySQLModelEndpointsTable
18
- from .sqlite import ModelEndpointsTable as SQLiteModelEndpointsTable
19
-
20
-
21
- def get_model_endpoints_table(
22
- connection_string: Optional[str] = None,
23
- ) -> Union[type[MySQLModelEndpointsTable], type[SQLiteModelEndpointsTable]]:
24
- """Return ModelEndpointsTable based on the provided connection string"""
25
- if connection_string and "mysql:" in connection_string:
26
- return MySQLModelEndpointsTable
27
- return SQLiteModelEndpointsTable
@@ -1,84 +0,0 @@
1
- # Copyright 2023 Iguazio
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from sqlalchemy import TIMESTAMP, Boolean, Column, Integer, String, Text
16
-
17
- from mlrun.common.schemas.model_monitoring import EventFieldType
18
- from mlrun.utils.db import BaseModel
19
-
20
-
21
- class ModelEndpointsBaseTable(BaseModel):
22
- __tablename__ = EventFieldType.MODEL_ENDPOINTS
23
-
24
- uid = Column(
25
- EventFieldType.UID,
26
- String(40),
27
- primary_key=True,
28
- )
29
- state = Column(EventFieldType.STATE, String(10))
30
- project = Column(EventFieldType.PROJECT, String(40))
31
- function_uri = Column(
32
- EventFieldType.FUNCTION_URI,
33
- String(255),
34
- )
35
- model = Column(EventFieldType.MODEL, String(255))
36
- model_class = Column(
37
- EventFieldType.MODEL_CLASS,
38
- String(255),
39
- )
40
- labels = Column(EventFieldType.LABELS, Text)
41
- model_uri = Column(EventFieldType.MODEL_URI, String(255))
42
- stream_path = Column(EventFieldType.STREAM_PATH, Text)
43
- algorithm = Column(
44
- EventFieldType.ALGORITHM,
45
- String(255),
46
- )
47
- active = Column(EventFieldType.ACTIVE, Boolean)
48
- monitoring_mode = Column(
49
- EventFieldType.MONITORING_MODE,
50
- String(10),
51
- )
52
- feature_stats = Column(EventFieldType.FEATURE_STATS, Text)
53
- current_stats = Column(EventFieldType.CURRENT_STATS, Text)
54
- feature_names = Column(EventFieldType.FEATURE_NAMES, Text)
55
- children = Column(EventFieldType.CHILDREN, Text)
56
- label_names = Column(EventFieldType.LABEL_NAMES, Text)
57
- endpoint_type = Column(
58
- EventFieldType.ENDPOINT_TYPE,
59
- String(10),
60
- )
61
- children_uids = Column(EventFieldType.CHILDREN_UIDS, Text)
62
- drift_measures = Column(EventFieldType.DRIFT_MEASURES, Text)
63
- drift_status = Column(
64
- EventFieldType.DRIFT_STATUS,
65
- String(40),
66
- )
67
- monitor_configuration = Column(
68
- EventFieldType.MONITOR_CONFIGURATION,
69
- Text,
70
- )
71
- monitoring_feature_set_uri = Column(
72
- EventFieldType.FEATURE_SET_URI,
73
- String(255),
74
- )
75
- error_count = Column(EventFieldType.ERROR_COUNT, Integer)
76
- metrics = Column(EventFieldType.METRICS, Text)
77
- first_request = Column(
78
- EventFieldType.FIRST_REQUEST,
79
- TIMESTAMP,
80
- )
81
- last_request = Column(
82
- EventFieldType.LAST_REQUEST,
83
- TIMESTAMP,
84
- )
@@ -1,382 +0,0 @@
1
- # Copyright 2023 Iguazio
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- #
15
-
16
- import json
17
- import typing
18
- from datetime import datetime, timezone
19
-
20
- import pandas as pd
21
- import sqlalchemy as db
22
-
23
- import mlrun.common.model_monitoring.helpers
24
- import mlrun.common.schemas.model_monitoring
25
- import mlrun.model_monitoring.helpers
26
- from mlrun.common.db.sql_session import create_session, get_engine
27
- from mlrun.utils import logger
28
-
29
- from .model_endpoint_store import ModelEndpointStore
30
- from .models import get_model_endpoints_table
31
-
32
-
33
- class SQLModelEndpointStore(ModelEndpointStore):
34
- """
35
- Handles the DB operations when the DB target is from type SQL. For the SQL operations, we use SQLAlchemy, a Python
36
- SQL toolkit that handles the communication with the database. When using SQL for storing the model endpoints
37
- record, the user needs to provide a valid connection string for the database.
38
- """
39
-
40
- _engine = None
41
-
42
- def __init__(
43
- self,
44
- project: str,
45
- sql_connection_string: str = None,
46
- secret_provider: typing.Callable = None,
47
- ):
48
- """
49
- Initialize SQL store target object.
50
-
51
- :param project: The name of the project.
52
- :param sql_connection_string: Valid connection string or a path to SQL database with model endpoints table.
53
- :param secret_provider: An optional secret provider to get the connection string secret.
54
- """
55
-
56
- super().__init__(project=project)
57
-
58
- self.sql_connection_string = (
59
- sql_connection_string
60
- or mlrun.model_monitoring.helpers.get_connection_string(
61
- secret_provider=secret_provider
62
- )
63
- )
64
-
65
- self.table_name = (
66
- mlrun.common.schemas.model_monitoring.EventFieldType.MODEL_ENDPOINTS
67
- )
68
-
69
- self._engine = get_engine(dsn=self.sql_connection_string)
70
- self.ModelEndpointsTable = get_model_endpoints_table(
71
- connection_string=self.sql_connection_string
72
- )
73
- # Create table if not exist. The `metadata` contains the `ModelEndpointsTable`
74
- if not self._engine.has_table(self.table_name):
75
- self.ModelEndpointsTable.metadata.create_all( # pyright: ignore[reportGeneralTypeIssues]
76
- bind=self._engine
77
- )
78
- self.model_endpoints_table = (
79
- self.ModelEndpointsTable.__table__ # pyright: ignore[reportGeneralTypeIssues]
80
- )
81
-
82
- def write_model_endpoint(self, endpoint: dict[str, typing.Any]):
83
- """
84
- Create a new endpoint record in the SQL table. This method also creates the model endpoints table within the
85
- SQL database if not exist.
86
-
87
- :param endpoint: model endpoint dictionary that will be written into the DB.
88
- """
89
-
90
- with self._engine.connect() as connection:
91
- # Adjust timestamps fields
92
- endpoint[
93
- mlrun.common.schemas.model_monitoring.EventFieldType.FIRST_REQUEST
94
- ] = datetime.now(timezone.utc)
95
- endpoint[
96
- mlrun.common.schemas.model_monitoring.EventFieldType.LAST_REQUEST
97
- ] = datetime.now(timezone.utc)
98
-
99
- # Convert the result into a pandas Dataframe and write it into the database
100
- endpoint_df = pd.DataFrame([endpoint])
101
-
102
- endpoint_df.to_sql(
103
- self.table_name, con=connection, index=False, if_exists="append"
104
- )
105
-
106
- def update_model_endpoint(
107
- self, endpoint_id: str, attributes: dict[str, typing.Any]
108
- ):
109
- """
110
- Update a model endpoint record with a given attributes.
111
-
112
- :param endpoint_id: The unique id of the model endpoint.
113
- :param attributes: Dictionary of attributes that will be used for update the model endpoint. Note that the keys
114
- of the attributes dictionary should exist in the SQL table.
115
-
116
- """
117
-
118
- # Update the model endpoint record using sqlalchemy ORM
119
- with create_session(dsn=self.sql_connection_string) as session:
120
- # Remove endpoint id (foreign key) from the update query
121
- attributes.pop(
122
- mlrun.common.schemas.model_monitoring.EventFieldType.ENDPOINT_ID, None
123
- )
124
-
125
- # Generate and commit the update session query
126
- session.query(self.ModelEndpointsTable).filter(
127
- self.ModelEndpointsTable.uid == endpoint_id
128
- ).update(attributes)
129
- session.commit()
130
-
131
- def delete_model_endpoint(self, endpoint_id: str):
132
- """
133
- Deletes the SQL record of a given model endpoint id.
134
-
135
- :param endpoint_id: The unique id of the model endpoint.
136
- """
137
-
138
- # Delete the model endpoint record using sqlalchemy ORM
139
- with create_session(dsn=self.sql_connection_string) as session:
140
- # Generate and commit the delete query
141
- session.query(self.ModelEndpointsTable).filter_by(uid=endpoint_id).delete()
142
- session.commit()
143
-
144
- def get_model_endpoint(
145
- self,
146
- endpoint_id: str,
147
- ) -> dict[str, typing.Any]:
148
- """
149
- Get a single model endpoint record.
150
-
151
- :param endpoint_id: The unique id of the model endpoint.
152
-
153
- :return: A model endpoint record as a dictionary.
154
-
155
- :raise MLRunNotFoundError: If the model endpoints table was not found or the model endpoint id was not found.
156
- """
157
-
158
- # Get the model endpoint record using sqlalchemy ORM
159
- with create_session(dsn=self.sql_connection_string) as session:
160
- # Generate the get query
161
- endpoint_record = (
162
- session.query(self.ModelEndpointsTable)
163
- .filter_by(uid=endpoint_id)
164
- .one_or_none()
165
- )
166
-
167
- if not endpoint_record:
168
- raise mlrun.errors.MLRunNotFoundError(f"Endpoint {endpoint_id} not found")
169
-
170
- # Convert the database values and the table columns into a python dictionary
171
- return endpoint_record.to_dict()
172
-
173
- def list_model_endpoints(
174
- self,
175
- model: str = None,
176
- function: str = None,
177
- labels: list[str] = None,
178
- top_level: bool = None,
179
- uids: list = None,
180
- ) -> list[dict[str, typing.Any]]:
181
- """
182
- Returns a list of model endpoint dictionaries, supports filtering by model, function, labels or top level.
183
- By default, when no filters are applied, all available model endpoints for the given project will
184
- be listed.
185
-
186
- :param model: The name of the model to filter by.
187
- :param function: The name of the function to filter by.
188
- :param labels: A list of labels to filter by. Label filters work by either filtering a specific value
189
- of a label (i.e. list("key=value")) or by looking for the existence of a given
190
- key (i.e. "key").
191
- :param top_level: If True will return only routers and endpoint that are NOT children of any router.
192
- :param uids: List of model endpoint unique ids to include in the result.
193
-
194
- :return: A list of model endpoint dictionaries.
195
- """
196
-
197
- # Generate an empty model endpoints that will be filled afterwards with model endpoint dictionaries
198
- endpoint_list = []
199
-
200
- # Get the model endpoints records using sqlalchemy ORM
201
- with create_session(dsn=self.sql_connection_string) as session:
202
- # Generate the list query
203
- query = session.query(self.ModelEndpointsTable).filter_by(
204
- project=self.project
205
- )
206
-
207
- # Apply filters
208
- if model:
209
- query = self._filter_values(
210
- query=query,
211
- model_endpoints_table=self.model_endpoints_table,
212
- key_filter=mlrun.common.schemas.model_monitoring.EventFieldType.MODEL,
213
- filtered_values=[model],
214
- )
215
- if function:
216
- query = self._filter_values(
217
- query=query,
218
- model_endpoints_table=self.model_endpoints_table,
219
- key_filter=mlrun.common.schemas.model_monitoring.EventFieldType.FUNCTION,
220
- filtered_values=[function],
221
- )
222
- if uids:
223
- query = self._filter_values(
224
- query=query,
225
- model_endpoints_table=self.model_endpoints_table,
226
- key_filter=mlrun.common.schemas.model_monitoring.EventFieldType.UID,
227
- filtered_values=uids,
228
- combined=False,
229
- )
230
- if top_level:
231
- node_ep = str(
232
- mlrun.common.schemas.model_monitoring.EndpointType.NODE_EP.value
233
- )
234
- router_ep = str(
235
- mlrun.common.schemas.model_monitoring.EndpointType.ROUTER.value
236
- )
237
- endpoint_types = [node_ep, router_ep]
238
- query = self._filter_values(
239
- query=query,
240
- model_endpoints_table=self.model_endpoints_table,
241
- key_filter=mlrun.common.schemas.model_monitoring.EventFieldType.ENDPOINT_TYPE,
242
- filtered_values=endpoint_types,
243
- combined=False,
244
- )
245
- # Convert the results from the DB into a ModelEndpoint object and append it to the model endpoints list
246
- for endpoint_record in query.all():
247
- endpoint_dict = endpoint_record.to_dict()
248
-
249
- # Filter labels
250
- if labels and not self._validate_labels(
251
- endpoint_dict=endpoint_dict, labels=labels
252
- ):
253
- continue
254
-
255
- endpoint_list.append(endpoint_dict)
256
-
257
- return endpoint_list
258
-
259
- @staticmethod
260
- def _filter_values(
261
- query: db.orm.query.Query,
262
- model_endpoints_table: db.Table,
263
- key_filter: str,
264
- filtered_values: list,
265
- combined=True,
266
- ) -> db.orm.query.Query:
267
- """Filtering the SQL query object according to the provided filters.
268
-
269
- :param query: SQLAlchemy ORM query object. Includes the SELECT statements generated by the ORM
270
- for getting the model endpoint data from the SQL table.
271
- :param model_endpoints_table: SQLAlchemy table object that represents the model endpoints table.
272
- :param key_filter: Key column to filter by.
273
- :param filtered_values: List of values to filter the query the result.
274
- :param combined: If true, then apply AND operator on the filtered values list. Otherwise, apply OR
275
- operator.
276
-
277
- return: SQLAlchemy ORM query object that represents the updated query with the provided
278
- filters.
279
- """
280
-
281
- if combined and len(filtered_values) > 1:
282
- raise mlrun.errors.MLRunInvalidArgumentError(
283
- "Can't apply combined policy with multiple values"
284
- )
285
-
286
- if not combined:
287
- return query.filter(
288
- model_endpoints_table.c[key_filter].in_(filtered_values)
289
- )
290
-
291
- # Generating a tuple with the relevant filters
292
- filter_query = []
293
- for _filter in filtered_values:
294
- filter_query.append(model_endpoints_table.c[key_filter] == _filter)
295
-
296
- # Apply AND operator on the SQL query object with the filters tuple
297
- return query.filter(db.and_(*filter_query))
298
-
299
- @staticmethod
300
- def _validate_labels(
301
- endpoint_dict: dict,
302
- labels: list,
303
- ) -> bool:
304
- """Validate that the model endpoint dictionary has the provided labels. There are 2 possible cases:
305
- 1 - Labels were provided as a list of key-values pairs (e.g. ['label_1=value_1', 'label_2=value_2']): Validate
306
- that each pair exist in the endpoint dictionary.
307
- 2 - Labels were provided as a list of key labels (e.g. ['label_1', 'label_2']): Validate that each key exist in
308
- the endpoint labels dictionary.
309
-
310
- :param endpoint_dict: Dictionary of the model endpoint records.
311
- :param labels: List of dictionary of required labels.
312
-
313
- :return: True if the labels exist in the endpoint labels dictionary, otherwise False.
314
- """
315
-
316
- # Convert endpoint labels into dictionary
317
- endpoint_labels = json.loads(
318
- endpoint_dict.get(
319
- mlrun.common.schemas.model_monitoring.EventFieldType.LABELS
320
- )
321
- )
322
-
323
- for label in labels:
324
- # Case 1 - label is a key=value pair
325
- if "=" in label:
326
- lbl, value = list(map(lambda x: x.strip(), label.split("=")))
327
- if lbl not in endpoint_labels or str(endpoint_labels[lbl]) != value:
328
- return False
329
- # Case 2 - label is just a key
330
- else:
331
- if label not in endpoint_labels:
332
- return False
333
-
334
- return True
335
-
336
- def delete_model_endpoints_resources(self, endpoints: list[dict[str, typing.Any]]):
337
- """
338
- Delete all model endpoints resources in both SQL and the time series DB.
339
-
340
- :param endpoints: A list of model endpoints flattened dictionaries.
341
- """
342
-
343
- for endpoint_dict in endpoints:
344
- # Delete model endpoint record from SQL table
345
- self.delete_model_endpoint(
346
- endpoint_dict[mlrun.common.schemas.model_monitoring.EventFieldType.UID],
347
- )
348
-
349
- def get_endpoint_real_time_metrics(
350
- self,
351
- endpoint_id: str,
352
- metrics: list[str],
353
- start: str = "now-1h",
354
- end: str = "now",
355
- access_key: str = None,
356
- ) -> dict[str, list[tuple[str, float]]]:
357
- """
358
- Getting metrics from the time series DB. There are pre-defined metrics for model endpoints such as
359
- `predictions_per_second` and `latency_avg_5m` but also custom metrics defined by the user.
360
-
361
- :param endpoint_id: The unique id of the model endpoint.
362
- :param metrics: A list of real-time metrics to return for the model endpoint.
363
- :param start: The start time of the metrics. Can be represented by a string containing an RFC 3339
364
- time, a Unix timestamp in milliseconds, a relative time (`'now'` or
365
- `'now-[0-9]+[mhd]'`, where `m` = minutes, `h` = hours, and `'d'` = days), or 0 for the
366
- earliest time.
367
- :param end: The end time of the metrics. Can be represented by a string containing an RFC 3339
368
- time, a Unix timestamp in milliseconds, a relative time (`'now'` or
369
- `'now-[0-9]+[mhd]'`, where `m` = minutes, `h` = hours, and `'d'` = days), or 0 for the
370
- earliest time.
371
- :param access_key: V3IO access key that will be used for generating Frames client object. If not
372
- provided, the access key will be retrieved from the environment variables.
373
-
374
- :return: A dictionary of metrics in which the key is a metric name and the value is a list of tuples that
375
- includes timestamps and the values.
376
- """
377
- # # TODO : Implement this method once Perometheus is supported
378
- logger.warning(
379
- "Real time metrics service using Prometheus will be implemented in 1.4.0"
380
- )
381
-
382
- return {}