mlrun 1.7.0rc17__py3-none-any.whl → 1.7.0rc18__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/alerts/alert.py +1 -1
- mlrun/artifacts/manager.py +5 -1
- mlrun/common/runtimes/constants.py +3 -0
- mlrun/common/schemas/__init__.py +1 -1
- mlrun/common/schemas/alert.py +31 -9
- mlrun/common/schemas/client_spec.py +1 -0
- mlrun/common/schemas/function.py +4 -0
- mlrun/common/schemas/model_monitoring/__init__.py +3 -1
- mlrun/common/schemas/model_monitoring/constants.py +20 -1
- mlrun/common/schemas/model_monitoring/grafana.py +9 -5
- mlrun/common/schemas/model_monitoring/model_endpoints.py +17 -6
- mlrun/config.py +2 -0
- mlrun/data_types/to_pandas.py +5 -5
- mlrun/datastore/datastore.py +6 -2
- mlrun/datastore/redis.py +2 -2
- mlrun/datastore/s3.py +5 -0
- mlrun/datastore/sources.py +111 -6
- mlrun/datastore/targets.py +2 -2
- mlrun/db/base.py +5 -1
- mlrun/db/httpdb.py +22 -3
- mlrun/db/nopdb.py +5 -1
- mlrun/errors.py +6 -0
- mlrun/feature_store/retrieval/conversion.py +5 -5
- mlrun/feature_store/retrieval/job.py +3 -2
- mlrun/feature_store/retrieval/spark_merger.py +2 -1
- mlrun/frameworks/_dl_common/loggers/tensorboard_logger.py +2 -2
- mlrun/model_monitoring/db/stores/base/store.py +16 -3
- mlrun/model_monitoring/db/stores/sqldb/sql_store.py +44 -43
- mlrun/model_monitoring/db/stores/v3io_kv/kv_store.py +190 -91
- mlrun/model_monitoring/db/tsdb/__init__.py +35 -6
- mlrun/model_monitoring/db/tsdb/base.py +25 -18
- mlrun/model_monitoring/db/tsdb/tdengine/__init__.py +15 -0
- mlrun/model_monitoring/db/tsdb/tdengine/schemas.py +207 -0
- mlrun/model_monitoring/db/tsdb/tdengine/stream_graph_steps.py +45 -0
- mlrun/model_monitoring/db/tsdb/tdengine/tdengine_connector.py +231 -0
- mlrun/model_monitoring/db/tsdb/v3io/v3io_connector.py +73 -72
- mlrun/model_monitoring/db/v3io_tsdb_reader.py +217 -16
- mlrun/model_monitoring/helpers.py +32 -0
- mlrun/model_monitoring/stream_processing.py +7 -4
- mlrun/model_monitoring/writer.py +18 -13
- mlrun/package/utils/_formatter.py +2 -2
- mlrun/projects/project.py +33 -8
- mlrun/render.py +8 -5
- mlrun/runtimes/databricks_job/databricks_wrapper.py +1 -1
- mlrun/utils/async_http.py +25 -5
- mlrun/utils/helpers.py +20 -1
- mlrun/utils/notifications/notification/slack.py +27 -7
- mlrun/utils/notifications/notification_pusher.py +38 -40
- mlrun/utils/version/version.json +2 -2
- {mlrun-1.7.0rc17.dist-info → mlrun-1.7.0rc18.dist-info}/METADATA +7 -2
- {mlrun-1.7.0rc17.dist-info → mlrun-1.7.0rc18.dist-info}/RECORD +55 -51
- {mlrun-1.7.0rc17.dist-info → mlrun-1.7.0rc18.dist-info}/LICENSE +0 -0
- {mlrun-1.7.0rc17.dist-info → mlrun-1.7.0rc18.dist-info}/WHEEL +0 -0
- {mlrun-1.7.0rc17.dist-info → mlrun-1.7.0rc18.dist-info}/entry_points.txt +0 -0
- {mlrun-1.7.0rc17.dist-info → mlrun-1.7.0rc18.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,207 @@
|
|
|
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 datetime
|
|
16
|
+
from dataclasses import dataclass
|
|
17
|
+
from io import StringIO
|
|
18
|
+
from typing import Union
|
|
19
|
+
|
|
20
|
+
import mlrun.common.schemas.model_monitoring as mm_schemas
|
|
21
|
+
import mlrun.common.types
|
|
22
|
+
|
|
23
|
+
_MODEL_MONITORING_DATABASE = "mlrun_model_monitoring"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class _TDEngineColumnType:
|
|
27
|
+
def __init__(self, data_type: str, length: int = None):
|
|
28
|
+
self.data_type = data_type
|
|
29
|
+
self.length = length
|
|
30
|
+
|
|
31
|
+
def __str__(self):
|
|
32
|
+
if self.length is not None:
|
|
33
|
+
return f"{self.data_type}({self.length})"
|
|
34
|
+
else:
|
|
35
|
+
return self.data_type
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class _TDEngineColumn(mlrun.common.types.StrEnum):
|
|
39
|
+
TIMESTAMP = _TDEngineColumnType("TIMESTAMP")
|
|
40
|
+
FLOAT = _TDEngineColumnType("FLOAT")
|
|
41
|
+
INT = _TDEngineColumnType("INT")
|
|
42
|
+
BINARY_40 = _TDEngineColumnType("BINARY", 40)
|
|
43
|
+
BINARY_64 = _TDEngineColumnType("BINARY", 64)
|
|
44
|
+
BINARY_10000 = _TDEngineColumnType("BINARY", 10000)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class TDEngineSchema:
|
|
49
|
+
"""
|
|
50
|
+
A class to represent a supertable schema in TDengine. Using this schema, you can generate the relevant queries to
|
|
51
|
+
create, insert, delete and query data from TDengine. At the moment, there are 3 schemas: AppResultTable,
|
|
52
|
+
Metrics, and Predictions.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(
|
|
56
|
+
self,
|
|
57
|
+
super_table: str,
|
|
58
|
+
columns: dict[str, str],
|
|
59
|
+
tags: dict[str, str],
|
|
60
|
+
):
|
|
61
|
+
self.super_table = super_table
|
|
62
|
+
self.columns = columns
|
|
63
|
+
self.tags = tags
|
|
64
|
+
self.database = _MODEL_MONITORING_DATABASE
|
|
65
|
+
|
|
66
|
+
def _create_super_table_query(self) -> str:
|
|
67
|
+
columns = ", ".join(f"{col} {val}" for col, val in self.columns.items())
|
|
68
|
+
tags = ", ".join(f"{col} {val}" for col, val in self.tags.items())
|
|
69
|
+
return f"CREATE STABLE if NOT EXISTS {self.database}.{self.super_table} ({columns}) TAGS ({tags});"
|
|
70
|
+
|
|
71
|
+
def _create_subtable_query(
|
|
72
|
+
self,
|
|
73
|
+
subtable: str,
|
|
74
|
+
values: dict[str, Union[str, int, float, datetime.datetime]],
|
|
75
|
+
) -> str:
|
|
76
|
+
try:
|
|
77
|
+
values = ", ".join(f"'{values[val]}'" for val in self.tags)
|
|
78
|
+
except KeyError:
|
|
79
|
+
raise mlrun.errors.MLRunInvalidArgumentError(
|
|
80
|
+
f"values must contain all tags: {self.tags.keys()}"
|
|
81
|
+
)
|
|
82
|
+
return f"CREATE TABLE if NOT EXISTS {self.database}.{subtable} USING {self.super_table} TAGS ({values});"
|
|
83
|
+
|
|
84
|
+
def _insert_subtable_query(
|
|
85
|
+
self,
|
|
86
|
+
subtable: str,
|
|
87
|
+
values: dict[str, Union[str, int, float, datetime.datetime]],
|
|
88
|
+
) -> str:
|
|
89
|
+
values = ", ".join(f"'{values[val]}'" for val in self.columns)
|
|
90
|
+
return f"INSERT INTO {self.database}.{subtable} VALUES ({values});"
|
|
91
|
+
|
|
92
|
+
def _delete_subtable_query(
|
|
93
|
+
self,
|
|
94
|
+
subtable: str,
|
|
95
|
+
values: dict[str, Union[str, int, float, datetime.datetime]],
|
|
96
|
+
) -> str:
|
|
97
|
+
values = " AND ".join(
|
|
98
|
+
f"{val} like '{values[val]}'" for val in self.tags if val in values
|
|
99
|
+
)
|
|
100
|
+
if not values:
|
|
101
|
+
raise mlrun.errors.MLRunInvalidArgumentError(
|
|
102
|
+
f"values must contain at least one tag: {self.tags.keys()}"
|
|
103
|
+
)
|
|
104
|
+
return f"DELETE FROM {self.database}.{subtable} WHERE {values};"
|
|
105
|
+
|
|
106
|
+
def _drop_subtable_query(
|
|
107
|
+
self,
|
|
108
|
+
subtable: str,
|
|
109
|
+
) -> str:
|
|
110
|
+
return f"DROP TABLE if EXISTS {self.database}.{subtable};"
|
|
111
|
+
|
|
112
|
+
def _get_subtables_query(
|
|
113
|
+
self,
|
|
114
|
+
values: dict[str, Union[str, int, float, datetime.datetime]],
|
|
115
|
+
) -> str:
|
|
116
|
+
values = " AND ".join(
|
|
117
|
+
f"{val} like '{values[val]}'" for val in self.tags if val in values
|
|
118
|
+
)
|
|
119
|
+
if not values:
|
|
120
|
+
raise mlrun.errors.MLRunInvalidArgumentError(
|
|
121
|
+
f"values must contain at least one tag: {self.tags.keys()}"
|
|
122
|
+
)
|
|
123
|
+
return f"SELECT tbname FROM {self.database}.{self.super_table} WHERE {values};"
|
|
124
|
+
|
|
125
|
+
@staticmethod
|
|
126
|
+
def _get_records_query(
|
|
127
|
+
table: str,
|
|
128
|
+
start: str,
|
|
129
|
+
end: str,
|
|
130
|
+
columns_to_filter: list[str] = None,
|
|
131
|
+
filter_query: str = "",
|
|
132
|
+
timestamp_column: str = "time",
|
|
133
|
+
database: str = _MODEL_MONITORING_DATABASE,
|
|
134
|
+
) -> str:
|
|
135
|
+
with StringIO() as query:
|
|
136
|
+
query.write("SELECT ")
|
|
137
|
+
if columns_to_filter:
|
|
138
|
+
query.write(", ".join(columns_to_filter))
|
|
139
|
+
else:
|
|
140
|
+
query.write("*")
|
|
141
|
+
query.write(f" from {database}.{table}")
|
|
142
|
+
|
|
143
|
+
if any([filter_query, start, end]):
|
|
144
|
+
query.write(" where ")
|
|
145
|
+
if filter_query:
|
|
146
|
+
query.write(f"{filter_query} and ")
|
|
147
|
+
if start:
|
|
148
|
+
query.write(f"{timestamp_column} >= '{start}'" + " and ")
|
|
149
|
+
if end:
|
|
150
|
+
query.write(f"{timestamp_column} <= '{end}'")
|
|
151
|
+
full_query = query.getvalue()
|
|
152
|
+
if full_query.endswith(" and "):
|
|
153
|
+
full_query = full_query[:-5]
|
|
154
|
+
return full_query + ";"
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
@dataclass
|
|
158
|
+
class AppResultTable(TDEngineSchema):
|
|
159
|
+
super_table = mm_schemas.TDEngineSuperTables.APP_RESULTS
|
|
160
|
+
columns = {
|
|
161
|
+
mm_schemas.WriterEvent.END_INFER_TIME: _TDEngineColumn.TIMESTAMP,
|
|
162
|
+
mm_schemas.WriterEvent.START_INFER_TIME: _TDEngineColumn.TIMESTAMP,
|
|
163
|
+
mm_schemas.ResultData.RESULT_VALUE: _TDEngineColumn.FLOAT,
|
|
164
|
+
mm_schemas.ResultData.RESULT_STATUS: _TDEngineColumn.INT,
|
|
165
|
+
mm_schemas.ResultData.CURRENT_STATS: _TDEngineColumn.BINARY_10000,
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
tags = {
|
|
169
|
+
mm_schemas.EventFieldType.PROJECT: _TDEngineColumn.BINARY_64,
|
|
170
|
+
mm_schemas.WriterEvent.ENDPOINT_ID: _TDEngineColumn.BINARY_64,
|
|
171
|
+
mm_schemas.WriterEvent.APPLICATION_NAME: _TDEngineColumn.BINARY_64,
|
|
172
|
+
mm_schemas.ResultData.RESULT_NAME: _TDEngineColumn.BINARY_64,
|
|
173
|
+
}
|
|
174
|
+
database = _MODEL_MONITORING_DATABASE
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
@dataclass
|
|
178
|
+
class Metrics(TDEngineSchema):
|
|
179
|
+
super_table = mm_schemas.TDEngineSuperTables.METRICS
|
|
180
|
+
columns = {
|
|
181
|
+
mm_schemas.WriterEvent.END_INFER_TIME: _TDEngineColumn.TIMESTAMP,
|
|
182
|
+
mm_schemas.WriterEvent.START_INFER_TIME: _TDEngineColumn.TIMESTAMP,
|
|
183
|
+
mm_schemas.MetricData.METRIC_VALUE: _TDEngineColumn.FLOAT,
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
tags = {
|
|
187
|
+
mm_schemas.EventFieldType.PROJECT: _TDEngineColumn.BINARY_64,
|
|
188
|
+
mm_schemas.WriterEvent.ENDPOINT_ID: _TDEngineColumn.BINARY_64,
|
|
189
|
+
mm_schemas.WriterEvent.APPLICATION_NAME: _TDEngineColumn.BINARY_64,
|
|
190
|
+
mm_schemas.MetricData.METRIC_NAME: _TDEngineColumn.BINARY_64,
|
|
191
|
+
}
|
|
192
|
+
database = _MODEL_MONITORING_DATABASE
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
@dataclass
|
|
196
|
+
class Predictions(TDEngineSchema):
|
|
197
|
+
super_table = mm_schemas.TDEngineSuperTables.PREDICTIONS
|
|
198
|
+
columns = {
|
|
199
|
+
mm_schemas.EventFieldType.TIME: _TDEngineColumn.TIMESTAMP,
|
|
200
|
+
mm_schemas.EventFieldType.LATENCY: _TDEngineColumn.FLOAT,
|
|
201
|
+
mm_schemas.EventKeyMetrics.CUSTOM_METRICS: _TDEngineColumn.BINARY_10000,
|
|
202
|
+
}
|
|
203
|
+
tags = {
|
|
204
|
+
mm_schemas.EventFieldType.PROJECT: _TDEngineColumn.BINARY_64,
|
|
205
|
+
mm_schemas.WriterEvent.ENDPOINT_ID: _TDEngineColumn.BINARY_64,
|
|
206
|
+
}
|
|
207
|
+
database = _MODEL_MONITORING_DATABASE
|
|
@@ -0,0 +1,45 @@
|
|
|
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 json
|
|
17
|
+
|
|
18
|
+
import mlrun.feature_store.steps
|
|
19
|
+
from mlrun.common.schemas.model_monitoring import (
|
|
20
|
+
EventFieldType,
|
|
21
|
+
EventKeyMetrics,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
_TABLE_COLUMN = "table_column"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class ProcessBeforeTDEngine(mlrun.feature_store.steps.MapClass):
|
|
28
|
+
def __init__(self, **kwargs):
|
|
29
|
+
"""
|
|
30
|
+
Process the data before writing to TDEngine. This step create the relevant keys for the TDEngine table,
|
|
31
|
+
including project name, custom metrics, time column, and table name column.
|
|
32
|
+
|
|
33
|
+
:returns: Event as a dictionary which will be written into the TDEngine Predictions table.
|
|
34
|
+
"""
|
|
35
|
+
super().__init__(**kwargs)
|
|
36
|
+
|
|
37
|
+
def do(self, event):
|
|
38
|
+
event[EventFieldType.PROJECT] = event[EventFieldType.FUNCTION_URI].split("/")[0]
|
|
39
|
+
event[EventKeyMetrics.CUSTOM_METRICS] = json.dumps(
|
|
40
|
+
event.get(EventFieldType.METRICS, {})
|
|
41
|
+
)
|
|
42
|
+
event[EventFieldType.TIME] = event.get(EventFieldType.TIMESTAMP)
|
|
43
|
+
event[EventFieldType.TABLE_COLUMN] = "_" + event.get(EventFieldType.ENDPOINT_ID)
|
|
44
|
+
|
|
45
|
+
return event
|
|
@@ -0,0 +1,231 @@
|
|
|
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 typing
|
|
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
|
|
23
|
+
import mlrun.model_monitoring.db.tsdb.tdengine.schemas as tdengine_schemas
|
|
24
|
+
import mlrun.model_monitoring.db.tsdb.tdengine.stream_graph_steps
|
|
25
|
+
from mlrun.model_monitoring.db import TSDBConnector
|
|
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: str,
|
|
191
|
+
end: str,
|
|
192
|
+
columns: typing.Optional[list[str]] = None,
|
|
193
|
+
filter_query: str = "",
|
|
194
|
+
timestamp_column: str = mm_schemas.EventFieldType.TIME,
|
|
195
|
+
) -> pd.DataFrame:
|
|
196
|
+
"""
|
|
197
|
+
Getting records from TSDB data collection.
|
|
198
|
+
:param table: Either a supertable or a subtable name.
|
|
199
|
+
:param columns: Columns to include in the result.
|
|
200
|
+
:param filter_query: Optional filter expression as a string. The filter structure depends on the TSDB
|
|
201
|
+
connector type.
|
|
202
|
+
:param start: The start time of the metrics.
|
|
203
|
+
:param end: The end time of the metrics.
|
|
204
|
+
:param timestamp_column: The column name that holds the timestamp.
|
|
205
|
+
|
|
206
|
+
:return: DataFrame with the provided attributes from the data collection.
|
|
207
|
+
:raise: MLRunInvalidArgumentError if query the provided table failed.
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
filter_query += f" project = '{self.project}'"
|
|
211
|
+
|
|
212
|
+
full_query = tdengine_schemas.TDEngineSchema._get_records_query(
|
|
213
|
+
table=table,
|
|
214
|
+
columns_to_filter=columns,
|
|
215
|
+
filter_query=filter_query,
|
|
216
|
+
start=start,
|
|
217
|
+
end=end,
|
|
218
|
+
timestamp_column=timestamp_column,
|
|
219
|
+
database=self.database,
|
|
220
|
+
)
|
|
221
|
+
try:
|
|
222
|
+
query_result = self._connection.query(full_query)
|
|
223
|
+
except taosws.QueryError as e:
|
|
224
|
+
raise mlrun.errors.MLRunInvalidArgumentError(
|
|
225
|
+
f"Failed to query table {table} in database {self.database}, {str(e)}"
|
|
226
|
+
)
|
|
227
|
+
columns = []
|
|
228
|
+
for column in query_result.fields:
|
|
229
|
+
columns.append(column.name())
|
|
230
|
+
|
|
231
|
+
return pd.DataFrame(query_result, columns=columns)
|