mlrun 1.7.2rc4__py3-none-any.whl → 1.8.0__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 +26 -22
- mlrun/__main__.py +15 -16
- mlrun/alerts/alert.py +150 -15
- mlrun/api/schemas/__init__.py +1 -9
- mlrun/artifacts/__init__.py +2 -3
- mlrun/artifacts/base.py +62 -19
- mlrun/artifacts/dataset.py +17 -17
- mlrun/artifacts/document.py +454 -0
- mlrun/artifacts/manager.py +28 -18
- mlrun/artifacts/model.py +91 -59
- mlrun/artifacts/plots.py +2 -2
- mlrun/common/constants.py +8 -0
- mlrun/common/formatters/__init__.py +1 -0
- mlrun/common/formatters/artifact.py +1 -1
- mlrun/common/formatters/feature_set.py +2 -0
- mlrun/common/formatters/function.py +1 -0
- mlrun/{model_monitoring/db/stores/v3io_kv/__init__.py → common/formatters/model_endpoint.py} +17 -0
- mlrun/common/formatters/pipeline.py +1 -2
- mlrun/common/formatters/project.py +9 -0
- mlrun/common/model_monitoring/__init__.py +0 -5
- mlrun/common/model_monitoring/helpers.py +12 -62
- mlrun/common/runtimes/constants.py +25 -4
- mlrun/common/schemas/__init__.py +9 -5
- mlrun/common/schemas/alert.py +114 -19
- mlrun/common/schemas/api_gateway.py +3 -3
- mlrun/common/schemas/artifact.py +22 -9
- mlrun/common/schemas/auth.py +8 -4
- mlrun/common/schemas/background_task.py +7 -7
- mlrun/common/schemas/client_spec.py +4 -4
- mlrun/common/schemas/clusterization_spec.py +2 -2
- mlrun/common/schemas/common.py +53 -3
- mlrun/common/schemas/constants.py +15 -0
- mlrun/common/schemas/datastore_profile.py +1 -1
- mlrun/common/schemas/feature_store.py +9 -9
- mlrun/common/schemas/frontend_spec.py +4 -4
- mlrun/common/schemas/function.py +10 -10
- mlrun/common/schemas/hub.py +1 -1
- mlrun/common/schemas/k8s.py +3 -3
- mlrun/common/schemas/memory_reports.py +3 -3
- mlrun/common/schemas/model_monitoring/__init__.py +4 -8
- mlrun/common/schemas/model_monitoring/constants.py +127 -46
- mlrun/common/schemas/model_monitoring/grafana.py +18 -12
- mlrun/common/schemas/model_monitoring/model_endpoints.py +154 -160
- mlrun/common/schemas/notification.py +24 -3
- mlrun/common/schemas/object.py +1 -1
- mlrun/common/schemas/pagination.py +4 -4
- mlrun/common/schemas/partition.py +142 -0
- mlrun/common/schemas/pipeline.py +3 -3
- mlrun/common/schemas/project.py +26 -18
- mlrun/common/schemas/runs.py +3 -3
- mlrun/common/schemas/runtime_resource.py +5 -5
- mlrun/common/schemas/schedule.py +1 -1
- mlrun/common/schemas/secret.py +1 -1
- mlrun/{model_monitoring/db/stores/sqldb/__init__.py → common/schemas/serving.py} +10 -1
- mlrun/common/schemas/tag.py +3 -3
- mlrun/common/schemas/workflow.py +6 -5
- mlrun/common/types.py +1 -0
- mlrun/config.py +157 -89
- mlrun/data_types/__init__.py +5 -3
- mlrun/data_types/infer.py +13 -3
- mlrun/data_types/spark.py +2 -1
- mlrun/datastore/__init__.py +59 -18
- mlrun/datastore/alibaba_oss.py +4 -1
- mlrun/datastore/azure_blob.py +4 -1
- mlrun/datastore/base.py +19 -24
- mlrun/datastore/datastore.py +10 -4
- mlrun/datastore/datastore_profile.py +178 -45
- mlrun/datastore/dbfs_store.py +4 -1
- mlrun/datastore/filestore.py +4 -1
- mlrun/datastore/google_cloud_storage.py +4 -1
- mlrun/datastore/hdfs.py +4 -1
- mlrun/datastore/inmem.py +4 -1
- mlrun/datastore/redis.py +4 -1
- mlrun/datastore/s3.py +14 -3
- mlrun/datastore/sources.py +89 -92
- mlrun/datastore/store_resources.py +7 -4
- mlrun/datastore/storeytargets.py +51 -16
- mlrun/datastore/targets.py +38 -31
- mlrun/datastore/utils.py +87 -4
- mlrun/datastore/v3io.py +4 -1
- mlrun/datastore/vectorstore.py +291 -0
- mlrun/datastore/wasbfs/fs.py +13 -12
- mlrun/db/base.py +286 -100
- mlrun/db/httpdb.py +1562 -490
- mlrun/db/nopdb.py +250 -83
- mlrun/errors.py +6 -2
- mlrun/execution.py +194 -50
- mlrun/feature_store/__init__.py +2 -10
- mlrun/feature_store/api.py +20 -458
- mlrun/feature_store/common.py +9 -9
- mlrun/feature_store/feature_set.py +20 -18
- mlrun/feature_store/feature_vector.py +105 -479
- mlrun/feature_store/feature_vector_utils.py +466 -0
- mlrun/feature_store/retrieval/base.py +15 -11
- mlrun/feature_store/retrieval/job.py +2 -1
- mlrun/feature_store/retrieval/storey_merger.py +1 -1
- mlrun/feature_store/steps.py +3 -3
- mlrun/features.py +30 -13
- mlrun/frameworks/__init__.py +1 -2
- mlrun/frameworks/_common/__init__.py +1 -2
- mlrun/frameworks/_common/artifacts_library.py +2 -2
- mlrun/frameworks/_common/mlrun_interface.py +10 -6
- mlrun/frameworks/_common/model_handler.py +31 -31
- mlrun/frameworks/_common/producer.py +3 -1
- mlrun/frameworks/_dl_common/__init__.py +1 -2
- mlrun/frameworks/_dl_common/loggers/__init__.py +1 -2
- mlrun/frameworks/_dl_common/loggers/mlrun_logger.py +4 -4
- mlrun/frameworks/_dl_common/loggers/tensorboard_logger.py +3 -3
- mlrun/frameworks/_ml_common/__init__.py +1 -2
- mlrun/frameworks/_ml_common/loggers/__init__.py +1 -2
- mlrun/frameworks/_ml_common/model_handler.py +21 -21
- mlrun/frameworks/_ml_common/plans/__init__.py +1 -2
- mlrun/frameworks/_ml_common/plans/confusion_matrix_plan.py +3 -1
- mlrun/frameworks/_ml_common/plans/dataset_plan.py +3 -3
- mlrun/frameworks/_ml_common/plans/roc_curve_plan.py +4 -4
- mlrun/frameworks/auto_mlrun/__init__.py +1 -2
- mlrun/frameworks/auto_mlrun/auto_mlrun.py +22 -15
- mlrun/frameworks/huggingface/__init__.py +1 -2
- mlrun/frameworks/huggingface/model_server.py +9 -9
- mlrun/frameworks/lgbm/__init__.py +47 -44
- mlrun/frameworks/lgbm/callbacks/__init__.py +1 -2
- mlrun/frameworks/lgbm/callbacks/logging_callback.py +4 -2
- mlrun/frameworks/lgbm/callbacks/mlrun_logging_callback.py +4 -2
- mlrun/frameworks/lgbm/mlrun_interfaces/__init__.py +1 -2
- mlrun/frameworks/lgbm/mlrun_interfaces/mlrun_interface.py +5 -5
- mlrun/frameworks/lgbm/model_handler.py +15 -11
- mlrun/frameworks/lgbm/model_server.py +11 -7
- mlrun/frameworks/lgbm/utils.py +2 -2
- mlrun/frameworks/onnx/__init__.py +1 -2
- mlrun/frameworks/onnx/dataset.py +3 -3
- mlrun/frameworks/onnx/mlrun_interface.py +2 -2
- mlrun/frameworks/onnx/model_handler.py +7 -5
- mlrun/frameworks/onnx/model_server.py +8 -6
- mlrun/frameworks/parallel_coordinates.py +11 -11
- mlrun/frameworks/pytorch/__init__.py +22 -23
- mlrun/frameworks/pytorch/callbacks/__init__.py +1 -2
- mlrun/frameworks/pytorch/callbacks/callback.py +2 -1
- mlrun/frameworks/pytorch/callbacks/logging_callback.py +15 -8
- mlrun/frameworks/pytorch/callbacks/mlrun_logging_callback.py +19 -12
- mlrun/frameworks/pytorch/callbacks/tensorboard_logging_callback.py +22 -15
- mlrun/frameworks/pytorch/callbacks_handler.py +36 -30
- mlrun/frameworks/pytorch/mlrun_interface.py +17 -17
- mlrun/frameworks/pytorch/model_handler.py +21 -17
- mlrun/frameworks/pytorch/model_server.py +13 -9
- mlrun/frameworks/sklearn/__init__.py +19 -18
- mlrun/frameworks/sklearn/estimator.py +2 -2
- mlrun/frameworks/sklearn/metric.py +3 -3
- mlrun/frameworks/sklearn/metrics_library.py +8 -6
- mlrun/frameworks/sklearn/mlrun_interface.py +3 -2
- mlrun/frameworks/sklearn/model_handler.py +4 -3
- mlrun/frameworks/tf_keras/__init__.py +11 -12
- mlrun/frameworks/tf_keras/callbacks/__init__.py +1 -2
- mlrun/frameworks/tf_keras/callbacks/logging_callback.py +17 -14
- mlrun/frameworks/tf_keras/callbacks/mlrun_logging_callback.py +15 -12
- mlrun/frameworks/tf_keras/callbacks/tensorboard_logging_callback.py +21 -18
- mlrun/frameworks/tf_keras/model_handler.py +17 -13
- mlrun/frameworks/tf_keras/model_server.py +12 -8
- mlrun/frameworks/xgboost/__init__.py +19 -18
- mlrun/frameworks/xgboost/model_handler.py +13 -9
- mlrun/k8s_utils.py +2 -5
- mlrun/launcher/base.py +3 -4
- mlrun/launcher/client.py +2 -2
- mlrun/launcher/local.py +6 -2
- mlrun/launcher/remote.py +1 -1
- mlrun/lists.py +8 -4
- mlrun/model.py +132 -46
- mlrun/model_monitoring/__init__.py +3 -5
- mlrun/model_monitoring/api.py +113 -98
- mlrun/model_monitoring/applications/__init__.py +0 -5
- mlrun/model_monitoring/applications/_application_steps.py +81 -50
- mlrun/model_monitoring/applications/base.py +467 -14
- mlrun/model_monitoring/applications/context.py +212 -134
- mlrun/model_monitoring/{db/stores/base → applications/evidently}/__init__.py +6 -2
- mlrun/model_monitoring/applications/evidently/base.py +146 -0
- mlrun/model_monitoring/applications/histogram_data_drift.py +89 -56
- mlrun/model_monitoring/applications/results.py +67 -15
- mlrun/model_monitoring/controller.py +701 -315
- mlrun/model_monitoring/db/__init__.py +0 -2
- mlrun/model_monitoring/db/_schedules.py +242 -0
- mlrun/model_monitoring/db/_stats.py +189 -0
- mlrun/model_monitoring/db/tsdb/__init__.py +33 -22
- mlrun/model_monitoring/db/tsdb/base.py +243 -49
- mlrun/model_monitoring/db/tsdb/tdengine/schemas.py +76 -36
- mlrun/model_monitoring/db/tsdb/tdengine/stream_graph_steps.py +33 -0
- mlrun/model_monitoring/db/tsdb/tdengine/tdengine_connection.py +213 -0
- mlrun/model_monitoring/db/tsdb/tdengine/tdengine_connector.py +534 -88
- mlrun/model_monitoring/db/tsdb/v3io/stream_graph_steps.py +1 -0
- mlrun/model_monitoring/db/tsdb/v3io/v3io_connector.py +436 -106
- mlrun/model_monitoring/helpers.py +356 -114
- mlrun/model_monitoring/stream_processing.py +190 -345
- mlrun/model_monitoring/tracking_policy.py +11 -4
- mlrun/model_monitoring/writer.py +49 -90
- mlrun/package/__init__.py +3 -6
- mlrun/package/context_handler.py +2 -2
- mlrun/package/packager.py +12 -9
- mlrun/package/packagers/__init__.py +0 -2
- mlrun/package/packagers/default_packager.py +14 -11
- mlrun/package/packagers/numpy_packagers.py +16 -7
- mlrun/package/packagers/pandas_packagers.py +18 -18
- mlrun/package/packagers/python_standard_library_packagers.py +25 -11
- mlrun/package/packagers_manager.py +35 -32
- mlrun/package/utils/__init__.py +0 -3
- mlrun/package/utils/_pickler.py +6 -6
- mlrun/platforms/__init__.py +47 -16
- mlrun/platforms/iguazio.py +4 -1
- mlrun/projects/operations.py +30 -30
- mlrun/projects/pipelines.py +116 -47
- mlrun/projects/project.py +1292 -329
- mlrun/render.py +5 -9
- mlrun/run.py +57 -14
- mlrun/runtimes/__init__.py +1 -3
- mlrun/runtimes/base.py +30 -22
- mlrun/runtimes/daskjob.py +9 -9
- mlrun/runtimes/databricks_job/databricks_runtime.py +6 -5
- mlrun/runtimes/function_reference.py +5 -2
- mlrun/runtimes/generators.py +3 -2
- mlrun/runtimes/kubejob.py +6 -7
- mlrun/runtimes/mounts.py +574 -0
- mlrun/runtimes/mpijob/__init__.py +0 -2
- mlrun/runtimes/mpijob/abstract.py +7 -6
- mlrun/runtimes/nuclio/api_gateway.py +7 -7
- mlrun/runtimes/nuclio/application/application.py +11 -13
- mlrun/runtimes/nuclio/application/reverse_proxy.go +66 -64
- mlrun/runtimes/nuclio/function.py +127 -70
- mlrun/runtimes/nuclio/serving.py +105 -37
- mlrun/runtimes/pod.py +159 -54
- mlrun/runtimes/remotesparkjob.py +3 -2
- mlrun/runtimes/sparkjob/__init__.py +0 -2
- mlrun/runtimes/sparkjob/spark3job.py +22 -12
- mlrun/runtimes/utils.py +7 -6
- mlrun/secrets.py +2 -2
- mlrun/serving/__init__.py +8 -0
- mlrun/serving/merger.py +7 -5
- mlrun/serving/remote.py +35 -22
- mlrun/serving/routers.py +186 -240
- mlrun/serving/server.py +41 -10
- mlrun/serving/states.py +432 -118
- mlrun/serving/utils.py +13 -2
- mlrun/serving/v1_serving.py +3 -2
- mlrun/serving/v2_serving.py +161 -203
- mlrun/track/__init__.py +1 -1
- mlrun/track/tracker.py +2 -2
- mlrun/track/trackers/mlflow_tracker.py +6 -5
- mlrun/utils/async_http.py +35 -22
- mlrun/utils/clones.py +7 -4
- mlrun/utils/helpers.py +511 -58
- mlrun/utils/logger.py +119 -13
- mlrun/utils/notifications/notification/__init__.py +22 -19
- mlrun/utils/notifications/notification/base.py +39 -15
- mlrun/utils/notifications/notification/console.py +6 -6
- mlrun/utils/notifications/notification/git.py +11 -11
- mlrun/utils/notifications/notification/ipython.py +10 -9
- mlrun/utils/notifications/notification/mail.py +176 -0
- mlrun/utils/notifications/notification/slack.py +16 -8
- mlrun/utils/notifications/notification/webhook.py +24 -8
- mlrun/utils/notifications/notification_pusher.py +191 -200
- mlrun/utils/regex.py +12 -2
- mlrun/utils/version/version.json +2 -2
- {mlrun-1.7.2rc4.dist-info → mlrun-1.8.0.dist-info}/METADATA +69 -54
- mlrun-1.8.0.dist-info/RECORD +351 -0
- {mlrun-1.7.2rc4.dist-info → mlrun-1.8.0.dist-info}/WHEEL +1 -1
- mlrun/model_monitoring/applications/evidently_base.py +0 -137
- mlrun/model_monitoring/db/stores/__init__.py +0 -136
- mlrun/model_monitoring/db/stores/base/store.py +0 -213
- mlrun/model_monitoring/db/stores/sqldb/models/__init__.py +0 -71
- mlrun/model_monitoring/db/stores/sqldb/models/base.py +0 -190
- mlrun/model_monitoring/db/stores/sqldb/models/mysql.py +0 -103
- mlrun/model_monitoring/db/stores/sqldb/models/sqlite.py +0 -40
- mlrun/model_monitoring/db/stores/sqldb/sql_store.py +0 -659
- mlrun/model_monitoring/db/stores/v3io_kv/kv_store.py +0 -726
- mlrun/model_monitoring/model_endpoint.py +0 -118
- mlrun-1.7.2rc4.dist-info/RECORD +0 -351
- {mlrun-1.7.2rc4.dist-info → mlrun-1.8.0.dist-info}/entry_points.txt +0 -0
- {mlrun-1.7.2rc4.dist-info → mlrun-1.8.0.dist-info/licenses}/LICENSE +0 -0
- {mlrun-1.7.2rc4.dist-info → mlrun-1.8.0.dist-info}/top_level.txt +0 -0
|
@@ -13,12 +13,14 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
import json
|
|
16
|
+
from datetime import datetime
|
|
16
17
|
|
|
17
18
|
import mlrun.feature_store.steps
|
|
18
19
|
from mlrun.common.schemas.model_monitoring import (
|
|
19
20
|
EventFieldType,
|
|
20
21
|
EventKeyMetrics,
|
|
21
22
|
)
|
|
23
|
+
from mlrun.utils import logger
|
|
22
24
|
|
|
23
25
|
|
|
24
26
|
class ProcessBeforeTDEngine(mlrun.feature_store.steps.MapClass):
|
|
@@ -40,3 +42,34 @@ class ProcessBeforeTDEngine(mlrun.feature_store.steps.MapClass):
|
|
|
40
42
|
event[EventFieldType.TABLE_COLUMN] = "_" + event.get(EventFieldType.ENDPOINT_ID)
|
|
41
43
|
|
|
42
44
|
return event
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ErrorExtractor(mlrun.feature_store.steps.MapClass):
|
|
48
|
+
def __init__(self, **kwargs):
|
|
49
|
+
"""
|
|
50
|
+
Prepare the event for insertion into the TDEngine error table
|
|
51
|
+
"""
|
|
52
|
+
super().__init__(**kwargs)
|
|
53
|
+
|
|
54
|
+
def do(self, event):
|
|
55
|
+
error = str(event.get("error"))
|
|
56
|
+
if len(error) > 1000:
|
|
57
|
+
error = error[-1000:]
|
|
58
|
+
logger.warning(
|
|
59
|
+
f"Error message exceeds 1000 chars: The error message writen to TSDB will be it last "
|
|
60
|
+
f"1000 chars, Error: {error}",
|
|
61
|
+
event=event,
|
|
62
|
+
)
|
|
63
|
+
timestamp = datetime.fromisoformat(event.get("when"))
|
|
64
|
+
endpoint_id = event[EventFieldType.ENDPOINT_ID]
|
|
65
|
+
event = {
|
|
66
|
+
EventFieldType.MODEL_ERROR: error,
|
|
67
|
+
EventFieldType.ERROR_TYPE: EventFieldType.INFER_ERROR,
|
|
68
|
+
EventFieldType.ENDPOINT_ID: endpoint_id,
|
|
69
|
+
EventFieldType.TIME: timestamp,
|
|
70
|
+
EventFieldType.PROJECT: event[EventFieldType.FUNCTION_URI].split("/")[0],
|
|
71
|
+
EventFieldType.TABLE_COLUMN: "_err_"
|
|
72
|
+
+ event.get(EventFieldType.ENDPOINT_ID),
|
|
73
|
+
}
|
|
74
|
+
logger.info("Write error to errors TSDB table", event=event)
|
|
75
|
+
return event
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# Copyright 2025 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 traceback
|
|
16
|
+
from collections.abc import Callable
|
|
17
|
+
from enum import Enum
|
|
18
|
+
from typing import Any, Final, Optional, Union
|
|
19
|
+
|
|
20
|
+
import taosws
|
|
21
|
+
from taosws import TaosStmt
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _StrEnum(str, Enum):
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class TimestampPrecision(_StrEnum):
|
|
29
|
+
ms = "ms" # milliseconds
|
|
30
|
+
us = "us" # microseconds
|
|
31
|
+
ns = "ns" # nanoseconds
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
_TS_PRECISION_TO_FACTOR_AND_FUNC: Final[
|
|
35
|
+
dict[TimestampPrecision, tuple[int, Callable[[list[int]], taosws.PyColumnView]]]
|
|
36
|
+
] = {
|
|
37
|
+
TimestampPrecision.ms: (10**3, taosws.millis_timestamps_to_column),
|
|
38
|
+
TimestampPrecision.us: (10**6, taosws.micros_timestamps_to_column),
|
|
39
|
+
TimestampPrecision.ns: (10**9, taosws.nanos_timestamps_to_column),
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class QueryResult:
|
|
44
|
+
def __init__(self, data, fields):
|
|
45
|
+
self.data = data
|
|
46
|
+
self.fields = fields
|
|
47
|
+
|
|
48
|
+
def __eq__(self, other):
|
|
49
|
+
return self.data == other.data and self.fields == other.fields
|
|
50
|
+
|
|
51
|
+
def __repr__(self):
|
|
52
|
+
return f"QueryResult({self.data}, {self.fields})"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Field:
|
|
56
|
+
def __init__(self, name, type, bytes):
|
|
57
|
+
self.name = name
|
|
58
|
+
self.type = type
|
|
59
|
+
self.bytes = bytes
|
|
60
|
+
|
|
61
|
+
def __eq__(self, other):
|
|
62
|
+
return (
|
|
63
|
+
self.name == other.name
|
|
64
|
+
and self.type == other.type
|
|
65
|
+
and self.bytes == other.bytes
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def __repr__(self):
|
|
69
|
+
return f"Field({self.name}, {self.type}, {self.bytes})"
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class TDEngineError(Exception):
|
|
73
|
+
pass
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class ErrorResult:
|
|
77
|
+
def __init__(self, tb, err):
|
|
78
|
+
self.tb = tb
|
|
79
|
+
self.err = err
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _get_timestamp_column(
|
|
83
|
+
values: list, timestamp_precision: TimestampPrecision
|
|
84
|
+
) -> taosws.PyColumnView:
|
|
85
|
+
factor, to_col_func = _TS_PRECISION_TO_FACTOR_AND_FUNC[timestamp_precision]
|
|
86
|
+
timestamps = [round(timestamp.timestamp() * factor) for timestamp in values]
|
|
87
|
+
return to_col_func(timestamps)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def values_to_column(
|
|
91
|
+
values: list,
|
|
92
|
+
column_type: str,
|
|
93
|
+
timestamp_precision: TimestampPrecision = TimestampPrecision.ms,
|
|
94
|
+
) -> taosws.PyColumnView:
|
|
95
|
+
if column_type == "TIMESTAMP":
|
|
96
|
+
return _get_timestamp_column(values, timestamp_precision)
|
|
97
|
+
if column_type == "FLOAT":
|
|
98
|
+
return taosws.floats_to_column(values)
|
|
99
|
+
if column_type == "INT":
|
|
100
|
+
return taosws.ints_to_column(values)
|
|
101
|
+
if column_type.startswith("BINARY"):
|
|
102
|
+
return taosws.binary_to_column(values)
|
|
103
|
+
|
|
104
|
+
raise NotImplementedError(f"Unsupported column type '{column_type}'")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class Statement:
|
|
108
|
+
def __init__(
|
|
109
|
+
self,
|
|
110
|
+
columns: dict[str, str],
|
|
111
|
+
subtable: str,
|
|
112
|
+
values: dict[str, Any],
|
|
113
|
+
timestamp_precision: str = TimestampPrecision.ms,
|
|
114
|
+
) -> None:
|
|
115
|
+
self.columns = columns
|
|
116
|
+
self.subtable = subtable
|
|
117
|
+
self.values = values
|
|
118
|
+
self.timestamp_precision = TimestampPrecision[timestamp_precision]
|
|
119
|
+
|
|
120
|
+
def prepare(self, statement: TaosStmt) -> TaosStmt:
|
|
121
|
+
question_marks = ", ".join("?" * len(self.columns))
|
|
122
|
+
statement.prepare(f"INSERT INTO ? VALUES ({question_marks});")
|
|
123
|
+
statement.set_tbname(self.subtable)
|
|
124
|
+
|
|
125
|
+
bind_params = []
|
|
126
|
+
|
|
127
|
+
for col_name, col_type in self.columns.items():
|
|
128
|
+
val = self.values[col_name]
|
|
129
|
+
bind_params.append(
|
|
130
|
+
values_to_column(
|
|
131
|
+
[val], col_type, timestamp_precision=self.timestamp_precision
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
statement.bind_param(bind_params)
|
|
136
|
+
statement.add_batch()
|
|
137
|
+
return statement
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _run(connection_string, prefix_statements, q, statements, query):
|
|
141
|
+
try:
|
|
142
|
+
conn = taosws.connect(connection_string)
|
|
143
|
+
|
|
144
|
+
for statement in prefix_statements + statements:
|
|
145
|
+
if isinstance(statement, Statement):
|
|
146
|
+
prepared_statement = statement.prepare(conn.statement())
|
|
147
|
+
prepared_statement.execute()
|
|
148
|
+
else:
|
|
149
|
+
conn.execute(statement)
|
|
150
|
+
|
|
151
|
+
if not query:
|
|
152
|
+
q.put(None)
|
|
153
|
+
return
|
|
154
|
+
|
|
155
|
+
res = conn.query(query)
|
|
156
|
+
|
|
157
|
+
# taosws.TaosField is not serializable
|
|
158
|
+
fields = [
|
|
159
|
+
Field(field.name(), field.type(), field.bytes()) for field in res.fields
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
q.put(QueryResult(list(res), fields))
|
|
163
|
+
except Exception as e:
|
|
164
|
+
tb = traceback.format_exc()
|
|
165
|
+
q.put(ErrorResult(tb, e))
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class TDEngineConnection:
|
|
169
|
+
def __init__(self, connection_string):
|
|
170
|
+
self._connection_string = connection_string
|
|
171
|
+
self.prefix_statements = []
|
|
172
|
+
|
|
173
|
+
self._conn = taosws.connect(self._connection_string)
|
|
174
|
+
|
|
175
|
+
def run(
|
|
176
|
+
self,
|
|
177
|
+
statements: Optional[Union[str, Statement, list[Union[str, Statement]]]] = None,
|
|
178
|
+
query: Optional[str] = None,
|
|
179
|
+
) -> Optional[QueryResult]:
|
|
180
|
+
statements = statements or []
|
|
181
|
+
if not isinstance(statements, list):
|
|
182
|
+
statements = [statements]
|
|
183
|
+
|
|
184
|
+
for statement in self.prefix_statements + statements:
|
|
185
|
+
if isinstance(statement, Statement):
|
|
186
|
+
try:
|
|
187
|
+
prepared_statement = statement.prepare(self._conn.statement())
|
|
188
|
+
prepared_statement.execute()
|
|
189
|
+
except taosws.Error as e:
|
|
190
|
+
raise TDEngineError(
|
|
191
|
+
f"Failed to run prepared statement `{self._conn.statement()}`: {e}"
|
|
192
|
+
) from e
|
|
193
|
+
else:
|
|
194
|
+
try:
|
|
195
|
+
self._conn.execute(statement)
|
|
196
|
+
except taosws.Error as e:
|
|
197
|
+
raise TDEngineError(
|
|
198
|
+
f"Failed to run statement `{statement}`: {e}"
|
|
199
|
+
) from e
|
|
200
|
+
|
|
201
|
+
if not query:
|
|
202
|
+
return None
|
|
203
|
+
|
|
204
|
+
try:
|
|
205
|
+
res = self._conn.query(query)
|
|
206
|
+
except taosws.Error as e:
|
|
207
|
+
raise TDEngineError(f"Failed to run query `{query}`: {e}") from e
|
|
208
|
+
|
|
209
|
+
fields = [
|
|
210
|
+
Field(field.name(), field.type(), field.bytes()) for field in res.fields
|
|
211
|
+
]
|
|
212
|
+
|
|
213
|
+
return QueryResult(list(res), fields)
|