clickhouse-orm 3.0.1__py2.py3-none-any.whl → 3.1.0__py2.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.
- clickhouse_orm/database.py +38 -6
- {clickhouse_orm-3.0.1.dist-info → clickhouse_orm-3.1.0.dist-info}/METADATA +10 -9
- {clickhouse_orm-3.0.1.dist-info → clickhouse_orm-3.1.0.dist-info}/RECORD +5 -5
- {clickhouse_orm-3.0.1.dist-info → clickhouse_orm-3.1.0.dist-info}/WHEEL +0 -0
- {clickhouse_orm-3.0.1.dist-info → clickhouse_orm-3.1.0.dist-info}/licenses/LICENSE +0 -0
clickhouse_orm/database.py
CHANGED
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
import datetime
|
|
4
4
|
import logging
|
|
5
5
|
import re
|
|
6
|
+
from contextlib import contextmanager
|
|
6
7
|
from math import ceil
|
|
7
8
|
from string import Template
|
|
8
9
|
|
|
@@ -105,6 +106,7 @@ class Database:
|
|
|
105
106
|
timeout=60,
|
|
106
107
|
verify_ssl_cert=True,
|
|
107
108
|
log_statements=False,
|
|
109
|
+
session=None,
|
|
108
110
|
):
|
|
109
111
|
"""
|
|
110
112
|
Initializes a database instance. Unless it's readonly, the database will be
|
|
@@ -122,12 +124,13 @@ class Database:
|
|
|
122
124
|
"""
|
|
123
125
|
self.db_name = db_name
|
|
124
126
|
self.db_url = db_url or self._default_url
|
|
125
|
-
self.readonly = False
|
|
127
|
+
self.readonly = self.connection_readonly = False
|
|
126
128
|
self.timeout = timeout
|
|
127
|
-
self.
|
|
128
|
-
self.request_session
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
self.verify_ssl_cert = verify_ssl_cert
|
|
130
|
+
self.request_session = None
|
|
131
|
+
self.__username = username
|
|
132
|
+
self.__password = password
|
|
133
|
+
|
|
131
134
|
self.log_statements = log_statements
|
|
132
135
|
self.settings = {}
|
|
133
136
|
self.db_exists = False # this is required before running _is_existing_database
|
|
@@ -147,6 +150,22 @@ class Database:
|
|
|
147
150
|
# Version 19.0 and above support LowCardinality
|
|
148
151
|
self.has_low_cardinality_support = self.server_version >= (19, 0)
|
|
149
152
|
|
|
153
|
+
@contextmanager
|
|
154
|
+
def session(self):
|
|
155
|
+
"""Contextmanager to use a persistent session for requests.
|
|
156
|
+
|
|
157
|
+
This can be quicker if making lots of small queries.
|
|
158
|
+
"""
|
|
159
|
+
with requests.Session() as session:
|
|
160
|
+
session.verify = self.verify_ssl_cert
|
|
161
|
+
if self.__username:
|
|
162
|
+
session.auth = (self.__username, self.__password or "")
|
|
163
|
+
self.request_session = session
|
|
164
|
+
try:
|
|
165
|
+
yield self
|
|
166
|
+
finally:
|
|
167
|
+
self.request_session = None
|
|
168
|
+
|
|
150
169
|
def create_database(self):
|
|
151
170
|
"""
|
|
152
171
|
Creates the database on the ClickHouse server if it does not already exist.
|
|
@@ -398,7 +417,20 @@ class Database:
|
|
|
398
417
|
if self.log_statements:
|
|
399
418
|
logger.info(data)
|
|
400
419
|
params = self._build_params(settings)
|
|
401
|
-
|
|
420
|
+
|
|
421
|
+
if self.request_session:
|
|
422
|
+
r = self.request_session.post(self.db_url, params=params, data=data, stream=stream, timeout=self.timeout)
|
|
423
|
+
else:
|
|
424
|
+
r = requests.post(
|
|
425
|
+
self.db_url,
|
|
426
|
+
params=params,
|
|
427
|
+
data=data,
|
|
428
|
+
stream=stream,
|
|
429
|
+
timeout=self.timeout,
|
|
430
|
+
verify=self.verify_ssl_cert,
|
|
431
|
+
auth=(self.__username, self.__password or "") if self.__username else None,
|
|
432
|
+
)
|
|
433
|
+
|
|
402
434
|
if r.status_code != 200:
|
|
403
435
|
raise ServerError(r.text)
|
|
404
436
|
return r
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: clickhouse_orm
|
|
3
|
-
Version: 3.0
|
|
3
|
+
Version: 3.1.0
|
|
4
4
|
Summary: A simple ORM for working with the Clickhouse database. Maintainance fork of infi.clickhouse_orm.
|
|
5
5
|
Author-email: Oliver Margetts <oliver.margetts@gmail.com>
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
@@ -60,14 +60,15 @@ Now we can collect usage statistics per CPU, and write them to the database:
|
|
|
60
60
|
import psutil, time, datetime
|
|
61
61
|
|
|
62
62
|
psutil.cpu_percent(percpu=True) # first sample should be discarded
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
with db.session(): # use a requests session for efficiency
|
|
64
|
+
while True:
|
|
65
|
+
time.sleep(1)
|
|
66
|
+
stats = psutil.cpu_percent(percpu=True)
|
|
67
|
+
timestamp = datetime.datetime.now()
|
|
68
|
+
db.insert([
|
|
69
|
+
CPUStats(timestamp=timestamp, cpu_id=cpu_id, cpu_percent=cpu_percent)
|
|
70
|
+
for cpu_id, cpu_percent in enumerate(stats)
|
|
71
|
+
])
|
|
71
72
|
```
|
|
72
73
|
|
|
73
74
|
Querying the table is easy, using either the query builder or raw SQL:
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
clickhouse_orm/__init__.py,sha256=ksYW9scTnD6VVCj8Rzv7B41VkvHDn-JWpyIh1Tjserc,478
|
|
2
|
-
clickhouse_orm/database.py,sha256=
|
|
2
|
+
clickhouse_orm/database.py,sha256=JdQhviLvK9sABb7WRrAqMaxgA1nJzuDznfH6VuGRZtg,18804
|
|
3
3
|
clickhouse_orm/engines.py,sha256=AV80AmLRM3hlEXj3o6GPjjKDPk8YyRdTEns9SZCppA0,11166
|
|
4
4
|
clickhouse_orm/fields.py,sha256=Wfm3NkctRhnaw2bc9KqlncV04FCvBEb_74ky9ZF2aq4,24039
|
|
5
5
|
clickhouse_orm/funcs.py,sha256=8S1kdwCjIJ45imvA2XkxHjDhGOYlNg4aS1vMU2CQ224,42108
|
|
@@ -8,7 +8,7 @@ clickhouse_orm/models.py,sha256=pUkLBClnfyXPg5xuHTpobNhFqHtKuHlXTNwWv5PnV_U,2308
|
|
|
8
8
|
clickhouse_orm/query.py,sha256=3-v1Z5G1xO1c8ywVhmZiz0tpAvBmH19xzeyxVpxc6uU,24103
|
|
9
9
|
clickhouse_orm/system_models.py,sha256=pU-Pvq69GH6QD4iKIrSRxw3Q6n-qlgQzGLqpYwAF12U,6447
|
|
10
10
|
clickhouse_orm/utils.py,sha256=2fEmTrBSDNpPzbbnUOZeCQ2NX5httkOKrJKzgdTezH4,5179
|
|
11
|
-
clickhouse_orm-3.0.
|
|
12
|
-
clickhouse_orm-3.0.
|
|
13
|
-
clickhouse_orm-3.0.
|
|
14
|
-
clickhouse_orm-3.0.
|
|
11
|
+
clickhouse_orm-3.1.0.dist-info/licenses/LICENSE,sha256=MP0KDNu_tFQJrr35YkSFnwwp2F8p2a7Q1Mi2lOs8FLw,1503
|
|
12
|
+
clickhouse_orm-3.1.0.dist-info/WHEEL,sha256=Dyt6SBfaasWElUrURkknVFAZDHSTwxg3PaTza7RSbkY,100
|
|
13
|
+
clickhouse_orm-3.1.0.dist-info/METADATA,sha256=2Pa9QHoZsuXV_9tEoXVtZgwxk6Xcr_XZmHggSjhzURE,3492
|
|
14
|
+
clickhouse_orm-3.1.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|