mlrun 1.7.0rc4__py3-none-any.whl → 1.7.0rc20__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.
- mlrun/__init__.py +11 -1
- mlrun/__main__.py +25 -111
- mlrun/{datastore/helpers.py → alerts/__init__.py} +2 -5
- mlrun/alerts/alert.py +144 -0
- mlrun/api/schemas/__init__.py +4 -3
- mlrun/artifacts/__init__.py +8 -3
- mlrun/artifacts/base.py +38 -254
- mlrun/artifacts/dataset.py +9 -190
- mlrun/artifacts/manager.py +41 -47
- mlrun/artifacts/model.py +30 -158
- mlrun/artifacts/plots.py +23 -380
- mlrun/common/constants.py +68 -0
- mlrun/common/formatters/__init__.py +19 -0
- mlrun/{model_monitoring/stores/models/sqlite.py → common/formatters/artifact.py} +6 -8
- mlrun/common/formatters/base.py +78 -0
- mlrun/common/formatters/function.py +41 -0
- mlrun/common/formatters/pipeline.py +53 -0
- mlrun/common/formatters/project.py +51 -0
- mlrun/{runtimes → common/runtimes}/constants.py +32 -4
- mlrun/common/schemas/__init__.py +25 -4
- mlrun/common/schemas/alert.py +203 -0
- mlrun/common/schemas/api_gateway.py +148 -0
- mlrun/common/schemas/artifact.py +15 -5
- mlrun/common/schemas/auth.py +8 -2
- mlrun/common/schemas/client_spec.py +2 -0
- mlrun/common/schemas/frontend_spec.py +1 -0
- mlrun/common/schemas/function.py +4 -0
- mlrun/common/schemas/hub.py +7 -9
- mlrun/common/schemas/model_monitoring/__init__.py +19 -3
- mlrun/common/schemas/model_monitoring/constants.py +96 -26
- mlrun/common/schemas/model_monitoring/grafana.py +9 -5
- mlrun/common/schemas/model_monitoring/model_endpoints.py +86 -2
- mlrun/{runtimes/mpijob/v1alpha1.py → common/schemas/pagination.py} +10 -13
- mlrun/common/schemas/pipeline.py +0 -9
- mlrun/common/schemas/project.py +22 -21
- mlrun/common/types.py +7 -1
- mlrun/config.py +87 -19
- mlrun/data_types/data_types.py +4 -0
- mlrun/data_types/to_pandas.py +9 -9
- mlrun/datastore/__init__.py +5 -8
- mlrun/datastore/alibaba_oss.py +130 -0
- mlrun/datastore/azure_blob.py +4 -5
- mlrun/datastore/base.py +69 -30
- mlrun/datastore/datastore.py +10 -2
- mlrun/datastore/datastore_profile.py +90 -6
- mlrun/datastore/google_cloud_storage.py +1 -1
- mlrun/datastore/hdfs.py +5 -0
- mlrun/datastore/inmem.py +2 -2
- mlrun/datastore/redis.py +2 -2
- mlrun/datastore/s3.py +5 -0
- mlrun/datastore/snowflake_utils.py +43 -0
- mlrun/datastore/sources.py +172 -44
- mlrun/datastore/store_resources.py +7 -7
- mlrun/datastore/targets.py +285 -41
- mlrun/datastore/utils.py +68 -5
- mlrun/datastore/v3io.py +27 -50
- mlrun/db/auth_utils.py +152 -0
- mlrun/db/base.py +149 -14
- mlrun/db/factory.py +1 -1
- mlrun/db/httpdb.py +608 -178
- mlrun/db/nopdb.py +191 -7
- mlrun/errors.py +11 -0
- mlrun/execution.py +37 -20
- mlrun/feature_store/__init__.py +0 -2
- mlrun/feature_store/api.py +21 -52
- mlrun/feature_store/feature_set.py +48 -23
- mlrun/feature_store/feature_vector.py +2 -1
- mlrun/feature_store/ingestion.py +7 -6
- mlrun/feature_store/retrieval/base.py +9 -4
- mlrun/feature_store/retrieval/conversion.py +9 -9
- mlrun/feature_store/retrieval/dask_merger.py +2 -0
- mlrun/feature_store/retrieval/job.py +9 -3
- mlrun/feature_store/retrieval/local_merger.py +2 -0
- mlrun/feature_store/retrieval/spark_merger.py +34 -24
- mlrun/feature_store/steps.py +30 -19
- mlrun/features.py +4 -13
- mlrun/frameworks/_dl_common/loggers/tensorboard_logger.py +7 -12
- mlrun/frameworks/auto_mlrun/auto_mlrun.py +2 -2
- mlrun/frameworks/lgbm/__init__.py +1 -1
- mlrun/frameworks/lgbm/callbacks/callback.py +2 -4
- mlrun/frameworks/lgbm/model_handler.py +1 -1
- mlrun/frameworks/parallel_coordinates.py +2 -1
- mlrun/frameworks/pytorch/__init__.py +2 -2
- mlrun/frameworks/sklearn/__init__.py +1 -1
- mlrun/frameworks/tf_keras/__init__.py +5 -2
- mlrun/frameworks/tf_keras/callbacks/logging_callback.py +1 -1
- mlrun/frameworks/tf_keras/mlrun_interface.py +2 -2
- mlrun/frameworks/xgboost/__init__.py +1 -1
- mlrun/k8s_utils.py +10 -11
- mlrun/launcher/__init__.py +1 -1
- mlrun/launcher/base.py +6 -5
- mlrun/launcher/client.py +8 -6
- mlrun/launcher/factory.py +1 -1
- mlrun/launcher/local.py +9 -3
- mlrun/launcher/remote.py +9 -3
- mlrun/lists.py +6 -2
- mlrun/model.py +58 -19
- mlrun/model_monitoring/__init__.py +1 -1
- mlrun/model_monitoring/api.py +127 -301
- mlrun/model_monitoring/application.py +5 -296
- mlrun/model_monitoring/applications/__init__.py +11 -0
- mlrun/model_monitoring/applications/_application_steps.py +157 -0
- mlrun/model_monitoring/applications/base.py +282 -0
- mlrun/model_monitoring/applications/context.py +214 -0
- mlrun/model_monitoring/applications/evidently_base.py +211 -0
- mlrun/model_monitoring/applications/histogram_data_drift.py +224 -93
- mlrun/model_monitoring/applications/results.py +99 -0
- mlrun/model_monitoring/controller.py +30 -36
- mlrun/model_monitoring/db/__init__.py +18 -0
- mlrun/model_monitoring/{stores → db/stores}/__init__.py +43 -36
- mlrun/model_monitoring/db/stores/base/__init__.py +15 -0
- mlrun/model_monitoring/{stores/model_endpoint_store.py → db/stores/base/store.py} +58 -32
- mlrun/model_monitoring/db/stores/sqldb/__init__.py +13 -0
- mlrun/model_monitoring/db/stores/sqldb/models/__init__.py +71 -0
- mlrun/model_monitoring/{stores → db/stores/sqldb}/models/base.py +109 -5
- mlrun/model_monitoring/db/stores/sqldb/models/mysql.py +88 -0
- mlrun/model_monitoring/{stores/models/mysql.py → db/stores/sqldb/models/sqlite.py} +19 -13
- mlrun/model_monitoring/db/stores/sqldb/sql_store.py +684 -0
- mlrun/model_monitoring/db/stores/v3io_kv/__init__.py +13 -0
- mlrun/model_monitoring/{stores/kv_model_endpoint_store.py → db/stores/v3io_kv/kv_store.py} +302 -155
- mlrun/model_monitoring/db/tsdb/__init__.py +100 -0
- mlrun/model_monitoring/db/tsdb/base.py +329 -0
- mlrun/model_monitoring/db/tsdb/helpers.py +30 -0
- mlrun/model_monitoring/db/tsdb/tdengine/__init__.py +15 -0
- mlrun/model_monitoring/db/tsdb/tdengine/schemas.py +240 -0
- mlrun/model_monitoring/db/tsdb/tdengine/stream_graph_steps.py +45 -0
- mlrun/model_monitoring/db/tsdb/tdengine/tdengine_connector.py +397 -0
- mlrun/model_monitoring/db/tsdb/v3io/__init__.py +15 -0
- mlrun/model_monitoring/db/tsdb/v3io/stream_graph_steps.py +117 -0
- mlrun/model_monitoring/db/tsdb/v3io/v3io_connector.py +630 -0
- mlrun/model_monitoring/evidently_application.py +6 -118
- mlrun/model_monitoring/features_drift_table.py +34 -22
- mlrun/model_monitoring/helpers.py +100 -7
- mlrun/model_monitoring/model_endpoint.py +3 -2
- mlrun/model_monitoring/stream_processing.py +93 -228
- mlrun/model_monitoring/tracking_policy.py +7 -1
- mlrun/model_monitoring/writer.py +152 -124
- mlrun/package/packagers_manager.py +1 -0
- mlrun/package/utils/_formatter.py +2 -2
- mlrun/platforms/__init__.py +11 -10
- mlrun/platforms/iguazio.py +21 -202
- mlrun/projects/operations.py +30 -16
- mlrun/projects/pipelines.py +92 -99
- mlrun/projects/project.py +757 -268
- mlrun/render.py +15 -14
- mlrun/run.py +160 -162
- mlrun/runtimes/__init__.py +55 -3
- mlrun/runtimes/base.py +33 -19
- mlrun/runtimes/databricks_job/databricks_wrapper.py +1 -1
- mlrun/runtimes/funcdoc.py +0 -28
- mlrun/runtimes/kubejob.py +28 -122
- mlrun/runtimes/local.py +5 -2
- mlrun/runtimes/mpijob/__init__.py +0 -20
- mlrun/runtimes/mpijob/abstract.py +8 -8
- mlrun/runtimes/mpijob/v1.py +1 -1
- mlrun/runtimes/nuclio/__init__.py +1 -0
- mlrun/runtimes/nuclio/api_gateway.py +709 -0
- mlrun/runtimes/nuclio/application/__init__.py +15 -0
- mlrun/runtimes/nuclio/application/application.py +523 -0
- mlrun/runtimes/nuclio/application/reverse_proxy.go +95 -0
- mlrun/runtimes/nuclio/function.py +98 -58
- mlrun/runtimes/nuclio/serving.py +36 -42
- mlrun/runtimes/pod.py +196 -45
- mlrun/runtimes/remotesparkjob.py +1 -1
- mlrun/runtimes/sparkjob/spark3job.py +1 -1
- mlrun/runtimes/utils.py +6 -73
- mlrun/secrets.py +6 -2
- mlrun/serving/remote.py +2 -3
- mlrun/serving/routers.py +7 -4
- mlrun/serving/server.py +7 -8
- mlrun/serving/states.py +73 -43
- mlrun/serving/v2_serving.py +8 -7
- mlrun/track/tracker.py +2 -1
- mlrun/utils/async_http.py +25 -5
- mlrun/utils/helpers.py +141 -75
- mlrun/utils/http.py +1 -1
- mlrun/utils/logger.py +39 -7
- mlrun/utils/notifications/notification/__init__.py +14 -9
- mlrun/utils/notifications/notification/base.py +12 -0
- mlrun/utils/notifications/notification/console.py +2 -0
- mlrun/utils/notifications/notification/git.py +3 -1
- mlrun/utils/notifications/notification/ipython.py +2 -0
- mlrun/utils/notifications/notification/slack.py +101 -21
- mlrun/utils/notifications/notification/webhook.py +11 -1
- mlrun/utils/notifications/notification_pusher.py +147 -16
- mlrun/utils/retryer.py +3 -2
- mlrun/utils/v3io_clients.py +0 -1
- mlrun/utils/version/version.json +2 -2
- {mlrun-1.7.0rc4.dist-info → mlrun-1.7.0rc20.dist-info}/METADATA +33 -18
- mlrun-1.7.0rc20.dist-info/RECORD +353 -0
- {mlrun-1.7.0rc4.dist-info → mlrun-1.7.0rc20.dist-info}/WHEEL +1 -1
- mlrun/kfpops.py +0 -868
- mlrun/model_monitoring/batch.py +0 -974
- mlrun/model_monitoring/stores/models/__init__.py +0 -27
- mlrun/model_monitoring/stores/sql_model_endpoint_store.py +0 -382
- mlrun/platforms/other.py +0 -305
- mlrun-1.7.0rc4.dist-info/RECORD +0 -321
- {mlrun-1.7.0rc4.dist-info → mlrun-1.7.0rc20.dist-info}/LICENSE +0 -0
- {mlrun-1.7.0rc4.dist-info → mlrun-1.7.0rc20.dist-info}/entry_points.txt +0 -0
- {mlrun-1.7.0rc4.dist-info → mlrun-1.7.0rc20.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
# Copyright 2024 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
|
+
import typing
|
|
16
|
+
from datetime import datetime
|
|
17
|
+
|
|
18
|
+
import pandas as pd
|
|
19
|
+
import taosws
|
|
20
|
+
|
|
21
|
+
import mlrun.common.schemas.model_monitoring as mm_schemas
|
|
22
|
+
import mlrun.model_monitoring.db.tsdb.tdengine.schemas as tdengine_schemas
|
|
23
|
+
import mlrun.model_monitoring.db.tsdb.tdengine.stream_graph_steps
|
|
24
|
+
from mlrun.model_monitoring.db import TSDBConnector
|
|
25
|
+
from mlrun.model_monitoring.helpers import get_invocations_fqn
|
|
26
|
+
from mlrun.utils import logger
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class TDEngineConnector(TSDBConnector):
|
|
30
|
+
"""
|
|
31
|
+
Handles the TSDB operations when the TSDB connector is of type TDEngine.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
type: str = mm_schemas.TSDBTarget.TDEngine
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
project: str,
|
|
39
|
+
database: str = tdengine_schemas._MODEL_MONITORING_DATABASE,
|
|
40
|
+
**kwargs,
|
|
41
|
+
):
|
|
42
|
+
super().__init__(project=project)
|
|
43
|
+
if "connection_string" not in kwargs:
|
|
44
|
+
raise mlrun.errors.MLRunInvalidArgumentError(
|
|
45
|
+
"connection_string is a required parameter for TDEngineConnector."
|
|
46
|
+
)
|
|
47
|
+
self._tdengine_connection_string = kwargs.get("connection_string")
|
|
48
|
+
self.database = database
|
|
49
|
+
self._connection = self._create_connection()
|
|
50
|
+
self._init_super_tables()
|
|
51
|
+
|
|
52
|
+
def _create_connection(self):
|
|
53
|
+
"""Establish a connection to the TSDB server."""
|
|
54
|
+
conn = taosws.connect(self._tdengine_connection_string)
|
|
55
|
+
try:
|
|
56
|
+
conn.execute(f"CREATE DATABASE {self.database}")
|
|
57
|
+
except taosws.QueryError:
|
|
58
|
+
# Database already exists
|
|
59
|
+
pass
|
|
60
|
+
conn.execute(f"USE {self.database}")
|
|
61
|
+
return conn
|
|
62
|
+
|
|
63
|
+
def _init_super_tables(self):
|
|
64
|
+
"""Initialize the super tables for the TSDB."""
|
|
65
|
+
self.tables = {
|
|
66
|
+
mm_schemas.TDEngineSuperTables.APP_RESULTS: tdengine_schemas.AppResultTable(),
|
|
67
|
+
mm_schemas.TDEngineSuperTables.METRICS: tdengine_schemas.Metrics(),
|
|
68
|
+
mm_schemas.TDEngineSuperTables.PREDICTIONS: tdengine_schemas.Predictions(),
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
def create_tables(self):
|
|
72
|
+
"""Create TDEngine supertables."""
|
|
73
|
+
for table in self.tables:
|
|
74
|
+
create_table_query = self.tables[table]._create_super_table_query()
|
|
75
|
+
self._connection.execute(create_table_query)
|
|
76
|
+
|
|
77
|
+
def write_application_event(
|
|
78
|
+
self,
|
|
79
|
+
event: dict,
|
|
80
|
+
kind: mm_schemas.WriterEventKind = mm_schemas.WriterEventKind.RESULT,
|
|
81
|
+
):
|
|
82
|
+
"""
|
|
83
|
+
Write a single result or metric to TSDB.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
table_name = (
|
|
87
|
+
f"{self.project}_"
|
|
88
|
+
f"{event[mm_schemas.WriterEvent.ENDPOINT_ID]}_"
|
|
89
|
+
f"{event[mm_schemas.WriterEvent.APPLICATION_NAME]}_"
|
|
90
|
+
)
|
|
91
|
+
event[mm_schemas.EventFieldType.PROJECT] = self.project
|
|
92
|
+
|
|
93
|
+
if kind == mm_schemas.WriterEventKind.RESULT:
|
|
94
|
+
# Write a new result
|
|
95
|
+
table = self.tables[mm_schemas.TDEngineSuperTables.APP_RESULTS]
|
|
96
|
+
table_name = (
|
|
97
|
+
f"{table_name}_" f"{event[mm_schemas.ResultData.RESULT_NAME]}"
|
|
98
|
+
).replace("-", "_")
|
|
99
|
+
|
|
100
|
+
else:
|
|
101
|
+
# Write a new metric
|
|
102
|
+
table = self.tables[mm_schemas.TDEngineSuperTables.METRICS]
|
|
103
|
+
table_name = (
|
|
104
|
+
f"{table_name}_" f"{event[mm_schemas.MetricData.METRIC_NAME]}"
|
|
105
|
+
).replace("-", "_")
|
|
106
|
+
|
|
107
|
+
create_table_query = table._create_subtable_query(
|
|
108
|
+
subtable=table_name, values=event
|
|
109
|
+
)
|
|
110
|
+
self._connection.execute(create_table_query)
|
|
111
|
+
insert_table_query = table._insert_subtable_query(
|
|
112
|
+
subtable=table_name, values=event
|
|
113
|
+
)
|
|
114
|
+
self._connection.execute(insert_table_query)
|
|
115
|
+
|
|
116
|
+
def apply_monitoring_stream_steps(self, graph):
|
|
117
|
+
"""
|
|
118
|
+
Apply TSDB steps on the provided monitoring graph. Throughout these steps, the graph stores live data of
|
|
119
|
+
different key metric dictionaries. This data is being used by the monitoring dashboards in
|
|
120
|
+
grafana. At the moment, we store two types of data:
|
|
121
|
+
- prediction latency.
|
|
122
|
+
- custom metrics.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
def apply_process_before_tsdb():
|
|
126
|
+
graph.add_step(
|
|
127
|
+
"mlrun.model_monitoring.db.tsdb.tdengine.stream_graph_steps.ProcessBeforeTDEngine",
|
|
128
|
+
name="ProcessBeforeTDEngine",
|
|
129
|
+
after="MapFeatureNames",
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
def apply_tdengine_target(name, after):
|
|
133
|
+
graph.add_step(
|
|
134
|
+
"storey.TDEngineTarget",
|
|
135
|
+
name=name,
|
|
136
|
+
after=after,
|
|
137
|
+
url=self._tdengine_connection_string,
|
|
138
|
+
supertable=mm_schemas.TDEngineSuperTables.PREDICTIONS,
|
|
139
|
+
table_col=mm_schemas.EventFieldType.TABLE_COLUMN,
|
|
140
|
+
time_col=mm_schemas.EventFieldType.TIME,
|
|
141
|
+
database=self.database,
|
|
142
|
+
columns=[
|
|
143
|
+
mm_schemas.EventFieldType.LATENCY,
|
|
144
|
+
mm_schemas.EventKeyMetrics.CUSTOM_METRICS,
|
|
145
|
+
],
|
|
146
|
+
tag_cols=[
|
|
147
|
+
mm_schemas.EventFieldType.PROJECT,
|
|
148
|
+
mm_schemas.EventFieldType.ENDPOINT_ID,
|
|
149
|
+
],
|
|
150
|
+
max_events=10,
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
apply_process_before_tsdb()
|
|
154
|
+
apply_tdengine_target(
|
|
155
|
+
name="TDEngineTarget",
|
|
156
|
+
after="ProcessBeforeTDEngine",
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
def delete_tsdb_resources(self):
|
|
160
|
+
"""
|
|
161
|
+
Delete all project resources in the TSDB connector, such as model endpoints data and drift results.
|
|
162
|
+
"""
|
|
163
|
+
for table in self.tables:
|
|
164
|
+
get_subtable_names_query = self.tables[table]._get_subtables_query(
|
|
165
|
+
values={mm_schemas.EventFieldType.PROJECT: self.project}
|
|
166
|
+
)
|
|
167
|
+
subtables = self._connection.query(get_subtable_names_query)
|
|
168
|
+
for subtable in subtables:
|
|
169
|
+
drop_query = self.tables[table]._drop_subtable_query(
|
|
170
|
+
subtable=subtable[0]
|
|
171
|
+
)
|
|
172
|
+
self._connection.execute(drop_query)
|
|
173
|
+
logger.info(
|
|
174
|
+
f"Deleted all project resources in the TSDB connector for project {self.project}"
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
def get_model_endpoint_real_time_metrics(
|
|
178
|
+
self,
|
|
179
|
+
endpoint_id: str,
|
|
180
|
+
metrics: list[str],
|
|
181
|
+
start: str,
|
|
182
|
+
end: str,
|
|
183
|
+
) -> dict[str, list[tuple[str, float]]]:
|
|
184
|
+
# Not implemented, use get_records() instead
|
|
185
|
+
pass
|
|
186
|
+
|
|
187
|
+
def _get_records(
|
|
188
|
+
self,
|
|
189
|
+
table: str,
|
|
190
|
+
start: datetime,
|
|
191
|
+
end: datetime,
|
|
192
|
+
columns: typing.Optional[list[str]] = None,
|
|
193
|
+
filter_query: typing.Optional[str] = None,
|
|
194
|
+
interval: typing.Optional[str] = None,
|
|
195
|
+
agg_funcs: typing.Optional[list] = None,
|
|
196
|
+
limit: typing.Optional[int] = None,
|
|
197
|
+
sliding_window_step: typing.Optional[str] = None,
|
|
198
|
+
timestamp_column: str = mm_schemas.EventFieldType.TIME,
|
|
199
|
+
) -> pd.DataFrame:
|
|
200
|
+
"""
|
|
201
|
+
Getting records from TSDB data collection.
|
|
202
|
+
:param table: Either a supertable or a subtable name.
|
|
203
|
+
:param start: The start time of the metrics.
|
|
204
|
+
:param end: The end time of the metrics.
|
|
205
|
+
:param columns: Columns to include in the result.
|
|
206
|
+
:param filter_query: Optional filter expression as a string. TDengine supports SQL-like syntax.
|
|
207
|
+
:param interval: The interval to aggregate the data by. Note that if interval is provided,
|
|
208
|
+
`agg_funcs` must bg provided as well. Provided as a string in the format of '1m',
|
|
209
|
+
'1h', etc.
|
|
210
|
+
:param agg_funcs: The aggregation functions to apply on the columns. Note that if `agg_funcs` is
|
|
211
|
+
provided, `interval` must bg provided as well. Provided as a list of strings in
|
|
212
|
+
the format of ['sum', 'avg', 'count', ...].
|
|
213
|
+
:param limit: The maximum number of records to return.
|
|
214
|
+
:param sliding_window_step: The time step for which the time window moves forward. Note that if
|
|
215
|
+
`sliding_window_step` is provided, interval must be provided as well. Provided
|
|
216
|
+
as a string in the format of '1m', '1h', etc.
|
|
217
|
+
:param timestamp_column: The column name that holds the timestamp index.
|
|
218
|
+
|
|
219
|
+
:return: DataFrame with the provided attributes from the data collection.
|
|
220
|
+
:raise: MLRunInvalidArgumentError if query the provided table failed.
|
|
221
|
+
"""
|
|
222
|
+
|
|
223
|
+
project_condition = f"project = '{self.project}'"
|
|
224
|
+
filter_query = (
|
|
225
|
+
f"{filter_query} AND {project_condition}"
|
|
226
|
+
if filter_query
|
|
227
|
+
else project_condition
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
full_query = tdengine_schemas.TDEngineSchema._get_records_query(
|
|
231
|
+
table=table,
|
|
232
|
+
start=start,
|
|
233
|
+
end=end,
|
|
234
|
+
columns_to_filter=columns,
|
|
235
|
+
filter_query=filter_query,
|
|
236
|
+
interval=interval,
|
|
237
|
+
limit=limit,
|
|
238
|
+
agg_funcs=agg_funcs,
|
|
239
|
+
sliding_window_step=sliding_window_step,
|
|
240
|
+
timestamp_column=timestamp_column,
|
|
241
|
+
database=self.database,
|
|
242
|
+
)
|
|
243
|
+
try:
|
|
244
|
+
query_result = self._connection.query(full_query)
|
|
245
|
+
except taosws.QueryError as e:
|
|
246
|
+
raise mlrun.errors.MLRunInvalidArgumentError(
|
|
247
|
+
f"Failed to query table {table} in database {self.database}, {str(e)}"
|
|
248
|
+
)
|
|
249
|
+
columns = []
|
|
250
|
+
for column in query_result.fields:
|
|
251
|
+
columns.append(column.name())
|
|
252
|
+
|
|
253
|
+
return pd.DataFrame(query_result, columns=columns)
|
|
254
|
+
|
|
255
|
+
def read_metrics_data(
|
|
256
|
+
self,
|
|
257
|
+
*,
|
|
258
|
+
endpoint_id: str,
|
|
259
|
+
start: datetime,
|
|
260
|
+
end: datetime,
|
|
261
|
+
metrics: list[mm_schemas.ModelEndpointMonitoringMetric],
|
|
262
|
+
type: typing.Literal["metrics", "results"],
|
|
263
|
+
) -> typing.Union[
|
|
264
|
+
list[
|
|
265
|
+
typing.Union[
|
|
266
|
+
mm_schemas.ModelEndpointMonitoringResultValues,
|
|
267
|
+
mm_schemas.ModelEndpointMonitoringMetricNoData,
|
|
268
|
+
],
|
|
269
|
+
],
|
|
270
|
+
list[
|
|
271
|
+
typing.Union[
|
|
272
|
+
mm_schemas.ModelEndpointMonitoringMetricValues,
|
|
273
|
+
mm_schemas.ModelEndpointMonitoringMetricNoData,
|
|
274
|
+
],
|
|
275
|
+
],
|
|
276
|
+
]:
|
|
277
|
+
if type == "metrics":
|
|
278
|
+
table = mm_schemas.TDEngineSuperTables.METRICS
|
|
279
|
+
name = mm_schemas.MetricData.METRIC_NAME
|
|
280
|
+
df_handler = self.df_to_metrics_values
|
|
281
|
+
elif type == "results":
|
|
282
|
+
table = mm_schemas.TDEngineSuperTables.APP_RESULTS
|
|
283
|
+
name = mm_schemas.ResultData.RESULT_NAME
|
|
284
|
+
df_handler = self.df_to_results_values
|
|
285
|
+
else:
|
|
286
|
+
raise mlrun.errors.MLRunInvalidArgumentError(
|
|
287
|
+
f"Invalid type {type}, must be either 'metrics' or 'results'."
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
metrics_condition = " OR ".join(
|
|
291
|
+
[
|
|
292
|
+
f"({mm_schemas.WriterEvent.APPLICATION_NAME} = '{metric.app}' AND {name} = '{metric.name}')"
|
|
293
|
+
for metric in metrics
|
|
294
|
+
]
|
|
295
|
+
)
|
|
296
|
+
filter_query = f"endpoint_id='{endpoint_id}' AND ({metrics_condition})"
|
|
297
|
+
|
|
298
|
+
df = self._get_records(
|
|
299
|
+
table=table,
|
|
300
|
+
start=start,
|
|
301
|
+
end=end,
|
|
302
|
+
filter_query=filter_query,
|
|
303
|
+
timestamp_column=mm_schemas.WriterEvent.END_INFER_TIME,
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
df[mm_schemas.WriterEvent.END_INFER_TIME] = pd.to_datetime(
|
|
307
|
+
df[mm_schemas.WriterEvent.END_INFER_TIME]
|
|
308
|
+
)
|
|
309
|
+
df.set_index(mm_schemas.WriterEvent.END_INFER_TIME, inplace=True)
|
|
310
|
+
|
|
311
|
+
logger.debug(
|
|
312
|
+
"Converting a DataFrame to a list of metrics or results values",
|
|
313
|
+
table=table,
|
|
314
|
+
project=self.project,
|
|
315
|
+
endpoint_id=endpoint_id,
|
|
316
|
+
is_empty=df.empty,
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
return df_handler(df=df, metrics=metrics, project=self.project)
|
|
320
|
+
|
|
321
|
+
def read_predictions(
|
|
322
|
+
self,
|
|
323
|
+
*,
|
|
324
|
+
endpoint_id: str,
|
|
325
|
+
start: datetime,
|
|
326
|
+
end: datetime,
|
|
327
|
+
aggregation_window: typing.Optional[str] = None,
|
|
328
|
+
agg_funcs: typing.Optional[list] = None,
|
|
329
|
+
limit: typing.Optional[int] = None,
|
|
330
|
+
) -> typing.Union[
|
|
331
|
+
mm_schemas.ModelEndpointMonitoringMetricValues,
|
|
332
|
+
mm_schemas.ModelEndpointMonitoringMetricNoData,
|
|
333
|
+
]:
|
|
334
|
+
if (agg_funcs and not aggregation_window) or (
|
|
335
|
+
aggregation_window and not agg_funcs
|
|
336
|
+
):
|
|
337
|
+
raise mlrun.errors.MLRunInvalidArgumentError(
|
|
338
|
+
"both or neither of `aggregation_window` and `agg_funcs` must be provided"
|
|
339
|
+
)
|
|
340
|
+
df = self._get_records(
|
|
341
|
+
table=mm_schemas.TDEngineSuperTables.PREDICTIONS,
|
|
342
|
+
start=start,
|
|
343
|
+
end=end,
|
|
344
|
+
columns=[mm_schemas.EventFieldType.LATENCY],
|
|
345
|
+
filter_query=f"endpoint_id='{endpoint_id}'",
|
|
346
|
+
agg_funcs=agg_funcs,
|
|
347
|
+
interval=aggregation_window,
|
|
348
|
+
limit=limit,
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
full_name = get_invocations_fqn(self.project)
|
|
352
|
+
|
|
353
|
+
if df.empty:
|
|
354
|
+
return mm_schemas.ModelEndpointMonitoringMetricNoData(
|
|
355
|
+
full_name=full_name,
|
|
356
|
+
type=mm_schemas.ModelEndpointMonitoringMetricType.METRIC,
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
if aggregation_window:
|
|
360
|
+
# _wend column, which represents the end time of each window, will be used as the time index
|
|
361
|
+
df["_wend"] = pd.to_datetime(df["_wend"])
|
|
362
|
+
df.set_index("_wend", inplace=True)
|
|
363
|
+
|
|
364
|
+
latency_column = (
|
|
365
|
+
f"{agg_funcs[0]}({mm_schemas.EventFieldType.LATENCY})"
|
|
366
|
+
if agg_funcs
|
|
367
|
+
else mm_schemas.EventFieldType.LATENCY
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
return mm_schemas.ModelEndpointMonitoringMetricValues(
|
|
371
|
+
full_name=full_name,
|
|
372
|
+
values=list(
|
|
373
|
+
zip(
|
|
374
|
+
df.index,
|
|
375
|
+
df[latency_column],
|
|
376
|
+
)
|
|
377
|
+
), # pyright: ignore[reportArgumentType]
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
def read_prediction_metric_for_endpoint_if_exists(
|
|
381
|
+
self, endpoint_id: str
|
|
382
|
+
) -> typing.Optional[mm_schemas.ModelEndpointMonitoringMetric]:
|
|
383
|
+
# Read just one record, because we just want to check if there is any data for this endpoint_id
|
|
384
|
+
predictions = self.read_predictions(
|
|
385
|
+
endpoint_id=endpoint_id,
|
|
386
|
+
start=datetime.min,
|
|
387
|
+
end=mlrun.utils.now_date(),
|
|
388
|
+
limit=1,
|
|
389
|
+
)
|
|
390
|
+
if predictions:
|
|
391
|
+
return mm_schemas.ModelEndpointMonitoringMetric(
|
|
392
|
+
project=self.project,
|
|
393
|
+
app=mm_schemas.SpecialApps.MLRUN_INFRA,
|
|
394
|
+
type=mm_schemas.ModelEndpointMonitoringMetricType.METRIC,
|
|
395
|
+
name=mm_schemas.PredictionsQueryConstants.INVOCATIONS,
|
|
396
|
+
full_name=get_invocations_fqn(self.project),
|
|
397
|
+
)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Copyright 2024 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 .v3io_connector import V3IOTSDBConnector
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# Copyright 2024 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 mlrun.feature_store.steps
|
|
17
|
+
from mlrun.common.schemas.model_monitoring import (
|
|
18
|
+
EventFieldType,
|
|
19
|
+
EventKeyMetrics,
|
|
20
|
+
EventLiveStats,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class ProcessBeforeTSDB(mlrun.feature_store.steps.MapClass):
|
|
25
|
+
def __init__(self, **kwargs):
|
|
26
|
+
"""
|
|
27
|
+
Process the data before writing to TSDB. This step creates a dictionary that includes 3 different dictionaries
|
|
28
|
+
that each one of them contains important details and stats about the events:
|
|
29
|
+
1. base_metrics: stats about the average latency and the amount of predictions over time. It is based on
|
|
30
|
+
storey.AggregateByKey which was executed in step 5.
|
|
31
|
+
2. endpoint_features: feature names and values along with the prediction names and value.
|
|
32
|
+
3. custom_metric (opt): optional metrics provided by the user.
|
|
33
|
+
:returns: Dictionary of 2-3 dictionaries that contains stats and details about the events.
|
|
34
|
+
"""
|
|
35
|
+
super().__init__(**kwargs)
|
|
36
|
+
|
|
37
|
+
def do(self, event):
|
|
38
|
+
# Compute prediction per second
|
|
39
|
+
event[EventLiveStats.PREDICTIONS_PER_SECOND] = (
|
|
40
|
+
float(event[EventLiveStats.PREDICTIONS_COUNT_5M]) / 300
|
|
41
|
+
)
|
|
42
|
+
base_fields = [
|
|
43
|
+
EventFieldType.TIMESTAMP,
|
|
44
|
+
EventFieldType.ENDPOINT_ID,
|
|
45
|
+
EventFieldType.ENDPOINT_TYPE,
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
# Getting event timestamp and endpoint_id
|
|
49
|
+
base_event = {k: event[k] for k in base_fields}
|
|
50
|
+
|
|
51
|
+
# base_metrics includes the stats about the average latency and the amount of predictions over time
|
|
52
|
+
base_metrics = {
|
|
53
|
+
EventFieldType.RECORD_TYPE: EventKeyMetrics.BASE_METRICS,
|
|
54
|
+
EventLiveStats.PREDICTIONS_PER_SECOND: event[
|
|
55
|
+
EventLiveStats.PREDICTIONS_PER_SECOND
|
|
56
|
+
],
|
|
57
|
+
EventLiveStats.PREDICTIONS_COUNT_5M: event[
|
|
58
|
+
EventLiveStats.PREDICTIONS_COUNT_5M
|
|
59
|
+
],
|
|
60
|
+
EventLiveStats.PREDICTIONS_COUNT_1H: event[
|
|
61
|
+
EventLiveStats.PREDICTIONS_COUNT_1H
|
|
62
|
+
],
|
|
63
|
+
EventLiveStats.LATENCY_AVG_5M: event[EventLiveStats.LATENCY_AVG_5M],
|
|
64
|
+
EventLiveStats.LATENCY_AVG_1H: event[EventLiveStats.LATENCY_AVG_1H],
|
|
65
|
+
**base_event,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
# endpoint_features includes the event values of each feature and prediction
|
|
69
|
+
endpoint_features = {
|
|
70
|
+
EventFieldType.RECORD_TYPE: EventKeyMetrics.ENDPOINT_FEATURES,
|
|
71
|
+
**event[EventFieldType.NAMED_PREDICTIONS],
|
|
72
|
+
**event[EventFieldType.NAMED_FEATURES],
|
|
73
|
+
**base_event,
|
|
74
|
+
}
|
|
75
|
+
# Create a dictionary that includes both base_metrics and endpoint_features
|
|
76
|
+
processed = {
|
|
77
|
+
EventKeyMetrics.BASE_METRICS: base_metrics,
|
|
78
|
+
EventKeyMetrics.ENDPOINT_FEATURES: endpoint_features,
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
# If metrics provided, add another dictionary if custom_metrics values
|
|
82
|
+
if event[EventFieldType.METRICS]:
|
|
83
|
+
processed[EventKeyMetrics.CUSTOM_METRICS] = {
|
|
84
|
+
EventFieldType.RECORD_TYPE: EventKeyMetrics.CUSTOM_METRICS,
|
|
85
|
+
**event[EventFieldType.METRICS],
|
|
86
|
+
**base_event,
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return processed
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class FilterAndUnpackKeys(mlrun.feature_store.steps.MapClass):
|
|
93
|
+
def __init__(self, keys, **kwargs):
|
|
94
|
+
"""
|
|
95
|
+
Create unpacked event dictionary based on provided key metrics (base_metrics, endpoint_features,
|
|
96
|
+
or custom_metric). Please note that the next step of the TSDB target requires an unpacked dictionary.
|
|
97
|
+
:param keys: list of key metrics.
|
|
98
|
+
:returns: An unpacked dictionary of event filtered by the provided key metrics.
|
|
99
|
+
"""
|
|
100
|
+
super().__init__(**kwargs)
|
|
101
|
+
self.keys = keys
|
|
102
|
+
|
|
103
|
+
def do(self, event):
|
|
104
|
+
# Keep only the relevant dictionary based on the provided keys
|
|
105
|
+
new_event = {}
|
|
106
|
+
for key in self.keys:
|
|
107
|
+
if key in event:
|
|
108
|
+
new_event[key] = event[key]
|
|
109
|
+
|
|
110
|
+
# Create unpacked dictionary
|
|
111
|
+
unpacked = {}
|
|
112
|
+
for key in new_event.keys():
|
|
113
|
+
if key in self.keys:
|
|
114
|
+
unpacked = {**unpacked, **new_event[key]}
|
|
115
|
+
else:
|
|
116
|
+
unpacked[key] = new_event[key]
|
|
117
|
+
return unpacked if unpacked else None
|