clickhouse-orm 3.0.1__tar.gz → 3.1.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clickhouse_orm
3
- Version: 3.0.1
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
- while True:
64
- time.sleep(1)
65
- stats = psutil.cpu_percent(percpu=True)
66
- timestamp = datetime.datetime.now()
67
- db.insert([
68
- CPUStats(timestamp=timestamp, cpu_id=cpu_id, cpu_percent=cpu_percent)
69
- for cpu_id, cpu_percent in enumerate(stats)
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:
@@ -33,14 +33,15 @@ Now we can collect usage statistics per CPU, and write them to the database:
33
33
  import psutil, time, datetime
34
34
 
35
35
  psutil.cpu_percent(percpu=True) # first sample should be discarded
36
- while True:
37
- time.sleep(1)
38
- stats = psutil.cpu_percent(percpu=True)
39
- timestamp = datetime.datetime.now()
40
- db.insert([
41
- CPUStats(timestamp=timestamp, cpu_id=cpu_id, cpu_percent=cpu_percent)
42
- for cpu_id, cpu_percent in enumerate(stats)
43
- ])
36
+ with db.session(): # use a requests session for efficiency
37
+ while True:
38
+ time.sleep(1)
39
+ stats = psutil.cpu_percent(percpu=True)
40
+ timestamp = datetime.datetime.now()
41
+ db.insert([
42
+ CPUStats(timestamp=timestamp, cpu_id=cpu_id, cpu_percent=cpu_percent)
43
+ for cpu_id, cpu_percent in enumerate(stats)
44
+ ])
44
45
  ```
45
46
 
46
47
  Querying the table is easy, using either the query builder or raw SQL:
@@ -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.request_session = requests.Session()
128
- self.request_session.verify = verify_ssl_cert
129
- if username:
130
- self.request_session.auth = (username, password or "")
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
- r = self.request_session.post(self.db_url, params=params, data=data, stream=stream, timeout=self.timeout)
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
@@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"
4
4
 
5
5
  [project]
6
6
  name = "clickhouse_orm"
7
- version = "3.0.1"
7
+ version = "3.1.0"
8
8
  readme = "README.md"
9
9
  description = "A simple ORM for working with the Clickhouse database. Maintainance fork of infi.clickhouse_orm."
10
10
  authors = [
File without changes