From 3adf95f5c31696c7959d76c94505b4cccc8f6916 Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Mon, 13 Jan 2020 10:27:37 +0100 Subject: [PATCH 01/13] Add support for default tags (#50) --- CHANGELOG.md | 3 + README.rst | 28 ++++ influxdb_client/client/influxdb_client.py | 7 +- influxdb_client/client/write_api.py | 44 ++++++- tests/test_WriteApi.py | 150 +++++++++++++++++++++- 5 files changed, 225 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d4e1a0f..befcac94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 1.3.0 [unreleased] +### Features +1. [#49](https://github.com/influxdata/influxdb-client-python/issues/50): Implemented default tags + ### API 1. [#47](https://github.com/influxdata/influxdb-client-python/pull/47): Updated swagger to latest version diff --git a/README.rst b/README.rst index 845f1591..7efaa295 100644 --- a/README.rst +++ b/README.rst @@ -252,6 +252,34 @@ The batching is configurable by ``write_options``\ : .. marker-batching-end +Default Tags +"""""""""""" +.. marker-default-tags-start + +Sometimes is useful to store same information in every measurement e.g. ``hostname``, ``location``, ``customer``. +The client is able to use static value or env property as a tag value. + +The expressions: + +- ``California Miner`` - static value +- ``${env.hostname}`` - environment property + +.. code-block:: python + + point_settings = PointSettings() + point_settings.add_default_tag("id", "132-987-655") + point_settings.add_default_tag("customer", "California Miner") + point_settings.add_default_tag("data_center", "${env.data_center}") + + self.write_client = self.client.write_api(write_options=SYNCHRONOUS, point_settings=point_settings) + +.. code-block:: python + + self.write_client = self.client.write_api(write_options=SYNCHRONOUS, + point_settings=PointSettings(**{"id": "132-987-655", + "customer": "California Miner"})) +.. marker-default-tags-end + Asynchronous client """"""""""""""""""" diff --git a/influxdb_client/client/influxdb_client.py b/influxdb_client/client/influxdb_client.py index ea582149..b0fb64c2 100644 --- a/influxdb_client/client/influxdb_client.py +++ b/influxdb_client/client/influxdb_client.py @@ -9,7 +9,7 @@ from influxdb_client.client.query_api import QueryApi from influxdb_client.client.tasks_api import TasksApi from influxdb_client.client.users_api import UsersApi -from influxdb_client.client.write_api import WriteApi, WriteOptions +from influxdb_client.client.write_api import WriteApi, WriteOptions, PointSettings class InfluxDBClient(object): @@ -45,14 +45,15 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org self.api_client = ApiClient(configuration=conf, header_name=auth_header_name, header_value=auth_header_value) - def write_api(self, write_options=WriteOptions()) -> WriteApi: + def write_api(self, write_options=WriteOptions(), point_settings=PointSettings()) -> WriteApi: """ Creates a Write API instance + :param point_settings: :param write_options: write api configuration :return: write api instance """ - return WriteApi(influxdb_client=self, write_options=write_options) + return WriteApi(influxdb_client=self, write_options=write_options, point_settings=point_settings) def query_api(self) -> QueryApi: """ diff --git a/influxdb_client/client/write_api.py b/influxdb_client/client/write_api.py index 8bc988e6..b69e0646 100644 --- a/influxdb_client/client/write_api.py +++ b/influxdb_client/client/write_api.py @@ -6,6 +6,8 @@ from time import sleep from typing import Union, List +import os + import rx from rx import operators as ops, Observable from rx.scheduler import ThreadPoolScheduler @@ -55,6 +57,33 @@ def __init__(self, write_type: WriteType = WriteType.batching, ASYNCHRONOUS = WriteOptions(write_type=WriteType.asynchronous) +class PointSettings(object): + + def __init__(self, **default_tags) -> None: + + """ + Creates point settings for write api. + + :param default_tags: Default tags which will be added to each point written by api. + """ + + self.defaultTags = dict() + + for key, val in default_tags.items(): + self.add_default_tag(key, val) + + @staticmethod + def _get_value(value): + + if value.startswith("${env."): + return os.environ.get(value[6:-1]) + + return value + + def add_default_tag(self, key, value) -> None: + self.defaultTags[key] = self._get_value(value) + + class _BatchItem(object): def __init__(self, key, data, size=1) -> None: self.key = key @@ -103,10 +132,12 @@ def _body_reduce(batch_items): class WriteApi(AbstractClient): - def __init__(self, influxdb_client, write_options: WriteOptions = WriteOptions()) -> None: + def __init__(self, influxdb_client, write_options: WriteOptions = WriteOptions(), + point_settings: PointSettings = PointSettings()) -> None: self._influxdb_client = influxdb_client self._write_service = WriteService(influxdb_client.api_client) self._write_options = write_options + self._point_settings = point_settings if self._write_options.write_type is WriteType.batching: # Define Subject that listen incoming data and produces writes into InfluxDB self._subject = Subject() @@ -153,6 +184,17 @@ def write(self, bucket: str, org: str = None, if self._write_options.write_type is WriteType.batching: return self._write_batching(bucket, org, record, write_precision) + if self._point_settings.defaultTags and record: + for key, val in self._point_settings.defaultTags.items(): + if isinstance(record, dict): + record.get("tags")[key] = val + else: + for r in record: + if isinstance(r, dict): + r.get("tags")[key] = val + elif isinstance(r, Point): + r.tag(key, val) + final_string = self._serialize(record, write_precision) _async_req = True if self._write_options.write_type == WriteType.asynchronous else False diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index cb4f0c9b..cfdf8e65 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -3,12 +3,13 @@ from __future__ import absolute_import import datetime +import os import unittest import time from multiprocessing.pool import ApplyResult from influxdb_client import Point, WritePrecision -from influxdb_client.client.write_api import SYNCHRONOUS, ASYNCHRONOUS +from influxdb_client.client.write_api import SYNCHRONOUS, ASYNCHRONOUS, PointSettings from influxdb_client.rest import ApiException from tests.base_test import BaseTest @@ -17,7 +18,18 @@ class SynchronousWriteTest(BaseTest): def setUp(self) -> None: super().setUp() - self.write_client = self.client.write_api(write_options=SYNCHRONOUS) + + os.environ['data_center'] = "LA" + + self.id_tag = "132-987-655" + self.customer_tag = "California Miner" + self.data_center_key = "data_center" + + self.write_client = self.client.write_api(write_options=SYNCHRONOUS, + point_settings=PointSettings(**{"id": self.id_tag, + "customer": self.customer_tag, + self.data_center_key: + '${env.data_center}'})) def tearDown(self) -> None: self.write_client.__del__() @@ -97,6 +109,7 @@ def test_write_points_unicode(self): p.field(field_name, utf8_val) p.tag(tag, tag_value) record_list = [p] + print(record_list) self.write_client.write(bucket.name, self.org, record_list) @@ -105,10 +118,55 @@ def test_write_points_unicode(self): self.assertEqual(1, len(flux_result)) rec = flux_result[0].records[0] + self.assertEqual(self.id_tag, rec["id"]) + self.assertEqual(self.customer_tag, rec["customer"]) + self.assertEqual("LA", rec[self.data_center_key]) + self.assertEqual(measurement, rec.get_measurement()) self.assertEqual(utf8_val, rec.get_value()) self.assertEqual(field_name, rec.get_field()) + def test_write_using_default_tags(self): + bucket = self.create_test_bucket() + + measurement = "h2o_feet" + field_name = "water_level" + val = "1.0" + val2 = "2.0" + tag = "location" + tag_value = "creek level" + + p = Point(measurement) + p.field(field_name, val) + p.tag(tag, tag_value) + p.time(1) + + p2 = Point(measurement) + p2.field(field_name, val2) + p2.tag(tag, tag_value) + p2.time(2) + + record_list = [p, p2] + print(record_list) + + self.write_client.write(bucket.name, self.org, record_list) + + query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)' + flux_result = self.client.query_api().query(query) + self.assertEqual(1, len(flux_result)) + rec = flux_result[0].records[0] + rec2 = flux_result[0].records[1] + + self.assertEqual(self.id_tag, rec["id"]) + self.assertEqual(self.customer_tag, rec["customer"]) + self.assertEqual("LA", rec[self.data_center_key]) + + self.assertEqual(self.id_tag, rec2["id"]) + self.assertEqual(self.customer_tag, rec2["customer"]) + self.assertEqual("LA", rec2[self.data_center_key]) + + self.delete_test_bucket(bucket) + def test_write_result(self): _bucket = self.create_test_bucket() @@ -205,7 +263,18 @@ class AsynchronousWriteTest(BaseTest): def setUp(self) -> None: super().setUp() - self.write_client = self.client.write_api(write_options=ASYNCHRONOUS) + + os.environ['data_center'] = "LA" + + self.id_tag = "132-987-655" + self.customer_tag = "California Miner" + self.data_center_key = "data_center" + + self.write_client = self.client.write_api(write_options=ASYNCHRONOUS, + point_settings=PointSettings(**{"id": self.id_tag, + "customer": self.customer_tag, + self.data_center_key: + '${env.data_center}'})) def tearDown(self) -> None: self.write_client.__del__() @@ -261,6 +330,43 @@ def test_write_dictionaries(self): self.delete_test_bucket(bucket) + def test_use_default_tags_with_dictionaries(self): + bucket = self.create_test_bucket() + + _point1 = {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"}, + "time": "2009-11-10T22:00:00Z", "fields": {"water_level": 1.0}} + _point2 = {"measurement": "h2o_feet", "tags": {"location": "coyote_creek"}, + "time": "2009-11-10T23:00:00Z", "fields": {"water_level": 2.0}} + + _point_list = [_point1, _point2] + + self.write_client.write(bucket.name, self.org, _point_list) + time.sleep(1) + + query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)' + print(query) + + flux_result = self.client.query_api().query(query) + + self.assertEqual(1, len(flux_result)) + + records = flux_result[0].records + + self.assertEqual(2, len(records)) + + rec = records[0] + rec2 = records[1] + + self.assertEqual(self.id_tag, rec["id"]) + self.assertEqual(self.customer_tag, rec["customer"]) + self.assertEqual("LA", rec[self.data_center_key]) + + self.assertEqual(self.id_tag, rec2["id"]) + self.assertEqual(self.customer_tag, rec2["customer"]) + self.assertEqual("LA", rec2[self.data_center_key]) + + self.delete_test_bucket(bucket) + def test_write_bytes(self): bucket = self.create_test_bucket() @@ -300,5 +406,43 @@ def test_write_bytes(self): self.delete_test_bucket(bucket) +class PointSettingTest(BaseTest): + + def setUp(self) -> None: + super().setUp() + self.id_tag = "132-987-655" + self.customer_tag = "California Miner" + + def tearDown(self) -> None: + self.write_client.__del__() + super().tearDown() + + def test_point_settings(self): + self.write_client = self.client.write_api(write_options=SYNCHRONOUS, + point_settings=PointSettings(**{"id": self.id_tag, + "customer": self.customer_tag})) + + default_tags = self.write_client._point_settings.defaultTags + + self.assertEqual(self.id_tag, default_tags.get("id")) + self.assertEqual(self.customer_tag, default_tags.get("customer")) + + def test_point_settings_with_add(self): + os.environ['data_center'] = "LA" + + point_settings = PointSettings() + point_settings.add_default_tag("id", self.id_tag) + point_settings.add_default_tag("customer", self.customer_tag) + point_settings.add_default_tag("data_center", "${env.data_center}") + + self.write_client = self.client.write_api(write_options=SYNCHRONOUS, point_settings=point_settings) + + default_tags = self.write_client._point_settings.defaultTags + + self.assertEqual(self.id_tag, default_tags.get("id")) + self.assertEqual(self.customer_tag, default_tags.get("customer")) + self.assertEqual("LA", default_tags.get("data_center")) + + if __name__ == '__main__': unittest.main() From 5fb2e2fb9ed7cb167ebac68ca858a13c376f9d0a Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Wed, 15 Jan 2020 13:08:44 +0100 Subject: [PATCH 02/13] Initialize client library from config file and environmental properties (#52) --- CHANGELOG.md | 3 +- README.rst | 81 ++++++++++++++++++++ influxdb_client/client/influxdb_client.py | 52 ++++++++++++- influxdb_client/client/write_api.py | 6 ++ tests/config.ini | 10 +++ tests/test_WriteApi.py | 90 ++++++++++++++++++++++- 6 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 tests/config.ini diff --git a/CHANGELOG.md b/CHANGELOG.md index befcac94..f52776f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ ## 1.3.0 [unreleased] ### Features -1. [#49](https://github.com/influxdata/influxdb-client-python/issues/50): Implemented default tags +1. [#50](https://github.com/influxdata/influxdb-client-python/issues/50): Implemented default tags +2. [#52](https://github.com/influxdata/influxdb-client-python/issues/52): Initialize client library from config file and environmental properties ### API 1. [#47](https://github.com/influxdata/influxdb-client-python/pull/47): Updated swagger to latest version diff --git a/README.rst b/README.rst index 7efaa295..97d46b21 100644 --- a/README.rst +++ b/README.rst @@ -136,7 +136,49 @@ Please follow the `Installation`_ and then run the following: for cell in row: val_count += 1 + .. marker-query-end + +Client configuration +-------------------- + +Via File +^^^^^^^^ +A client can be configured via ``*.ini`` file in segment ``influx2``. + +The following options are supported: + +- ``url`` - the url to connect to InfluxDB +- ``org`` - default destination organization for writes and queries +- ``token`` - the token to use for the authorization +- ``timeout`` - socket timeout in ms (default value is 10000) + +.. code-block:: python + + self.client = InfluxDBClient.from_config_file("config.ini") + +.. code-block:: + + [influx2] + url=http://localhost:9999 + org=my-org + token=my-token + timeout=6000 + +Via Environment Properties +^^^^^^^^^^^^^^^^^^^^^^^^^^ +A client can be configured via environment properties. + +Supported properties are: + +- ``INFLUXDB_V2_URL`` - the url to connect to InfluxDB +- ``INFLUXDB_V2_ORG`` - default destination organization for writes and queries +- ``INFLUXDB_V2_TOKEN`` - the token to use for the authorization +- ``INFLUXDB_V2_TIMEOUT`` - socket timeout in ms (default value is 10000) + +.. code-block:: python + self.client = InfluxDBClient.from_env_properties() + .. marker-index-end @@ -264,6 +306,9 @@ The expressions: - ``California Miner`` - static value - ``${env.hostname}`` - environment property +Via API +_______ + .. code-block:: python point_settings = PointSettings() @@ -278,6 +323,42 @@ The expressions: self.write_client = self.client.write_api(write_options=SYNCHRONOUS, point_settings=PointSettings(**{"id": "132-987-655", "customer": "California Miner"})) + +Via Configuration file +______________________ + +In a ini configuration file you are able to specify default tags by ``tags`` segment. + +.. code-block:: python + + self.client = InfluxDBClient.from_config_file("config.ini") + +.. code-block:: + + [influx2] + url=http://localhost:9999 + org=my-org + token=my-token + timeout=6000 + + [tags] + id = 132-987-655 + customer = California Miner + data_center = ${env.data_center} + +Via Environment Properties +__________________________ +You are able to specify default tags by environment properties with prefix ``INFLUXDB_V2_TAG_``. + +Examples: + +- ``INFLUXDB_V2_TAG_ID`` +- ``INFLUXDB_V2_TAG_HOSTNAME`` + +.. code-block:: python + + self.client = InfluxDBClient.from_env_properties() + .. marker-default-tags-end Asynchronous client diff --git a/influxdb_client/client/influxdb_client.py b/influxdb_client/client/influxdb_client.py index b0fb64c2..a8ca4eea 100644 --- a/influxdb_client/client/influxdb_client.py +++ b/influxdb_client/client/influxdb_client.py @@ -1,5 +1,8 @@ from __future__ import absolute_import +import configparser +import os + from influxdb_client import Configuration, ApiClient, HealthCheck, HealthService, Ready, ReadyService from influxdb_client.client.authorizations_api import AuthorizationsApi from influxdb_client.client.bucket_api import BucketsApi @@ -14,7 +17,8 @@ class InfluxDBClient(object): - def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org: str = None) -> None: + def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org: str = None, + default_tags: dict = None) -> None: """ :class:`influxdb_client.InfluxDBClient` is client for HTTP API defined in https://github.com/influxdata/influxdb/blob/master/http/swagger.yml. @@ -33,6 +37,8 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org self.timeout = timeout self.org = org + self.default_tags = default_tags + conf = _Configuration() conf.host = self.url conf.enable_gzip = enable_gzip @@ -45,6 +51,50 @@ def __init__(self, url, token, debug=None, timeout=10000, enable_gzip=False, org self.api_client = ApiClient(configuration=conf, header_name=auth_header_name, header_value=auth_header_value) + @classmethod + def from_config_file(cls, config_file: str = "config.ini", debug=None, enable_gzip=False): + config = configparser.ConfigParser() + config.read(config_file) + + url = config['influx2']['url'] + token = config['influx2']['token'] + + timeout = None + + if config.has_option('influx2', 'timeout'): + timeout = config['influx2']['timeout'] + + org = None + + if config.has_option('influx2', 'org'): + org = config['influx2']['org'] + + default_tags = None + + if config.has_section('tags'): + default_tags = dict(config.items('tags')) + + if timeout: + return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags, + enable_gzip=enable_gzip) + + return cls(url, token, debug=debug, org=org, default_tags=default_tags, enable_gzip=enable_gzip) + + @classmethod + def from_env_properties(cls, debug=None, enable_gzip=False): + url = os.getenv('INFLUXDB_V2_URL', "http://localhost:9999") + token = os.getenv('INFLUXDB_V2_TOKEN', "my-token") + timeout = os.getenv('INFLUXDB_V2_TIMEOUT', "10000") + org = os.getenv('INFLUXDB_V2_ORG', "my-org") + + default_tags = dict() + + for key, value in os.environ.items(): + if key.startswith("INFLUXDB_V2_TAG_"): + default_tags[key[16:].lower()] = value + + return cls(url, token, debug=debug, timeout=int(timeout), org=org, default_tags=default_tags, enable_gzip=enable_gzip) + def write_api(self, write_options=WriteOptions(), point_settings=PointSettings()) -> WriteApi: """ Creates a Write API instance diff --git a/influxdb_client/client/write_api.py b/influxdb_client/client/write_api.py index b69e0646..0e5feead 100644 --- a/influxdb_client/client/write_api.py +++ b/influxdb_client/client/write_api.py @@ -6,6 +6,7 @@ from time import sleep from typing import Union, List +import configparser import os import rx @@ -138,6 +139,11 @@ def __init__(self, influxdb_client, write_options: WriteOptions = WriteOptions() self._write_service = WriteService(influxdb_client.api_client) self._write_options = write_options self._point_settings = point_settings + + if influxdb_client.default_tags: + for key, value in influxdb_client.default_tags.items(): + self._point_settings.add_default_tag(key, value) + if self._write_options.write_type is WriteType.batching: # Define Subject that listen incoming data and produces writes into InfluxDB self._subject = Subject() diff --git a/tests/config.ini b/tests/config.ini new file mode 100644 index 00000000..02e24397 --- /dev/null +++ b/tests/config.ini @@ -0,0 +1,10 @@ +[influx2] +url=http://localhost:9999 +org=my-org +token=my-token +timeout=6000 + +[tags] +id = 132-987-655 +customer = California Miner +data_center = ${env.data_center} \ No newline at end of file diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index cfdf8e65..06cfa629 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -8,7 +8,7 @@ import time from multiprocessing.pool import ApplyResult -from influxdb_client import Point, WritePrecision +from influxdb_client import Point, WritePrecision, InfluxDBClient from influxdb_client.client.write_api import SYNCHRONOUS, ASYNCHRONOUS, PointSettings from influxdb_client.rest import ApiException from tests.base_test import BaseTest @@ -444,5 +444,93 @@ def test_point_settings_with_add(self): self.assertEqual("LA", default_tags.get("data_center")) +class DefaultTagsConfiguration(BaseTest): + + def setUp(self) -> None: + super().setUp() + + os.environ['data_center'] = "LA" + + self.id_tag = "132-987-655" + self.customer_tag = "California Miner" + self.data_center_key = "data_center" + + os.environ['INFLUXDB_V2_URL'] = "http://localhost:9999" + os.environ['INFLUXDB_V2_TOKEN'] = "my-token" + os.environ['INFLUXDB_V2_TIMEOUT'] = "6000" + os.environ['INFLUXDB_V2_ORG'] = "my-org" + + os.environ['INFLUXDB_V2_TAG_ID'] = self.id_tag + os.environ['INFLUXDB_V2_TAG_CUSTOMER'] = self.customer_tag + os.environ['INFLUXDB_V2_TAG_DATA_CENTER'] = "${env.data_center}" + + def tearDown(self) -> None: + self.write_client.__del__() + super().tearDown() + + def test_connection_option_from_conf_file(self): + self.client.close() + self.client = InfluxDBClient.from_config_file("config.ini", self.debug) + + self._check_connection_settings() + + def test_connection_option_from_env(self): + self.client.close() + self.client = InfluxDBClient.from_env_properties(self.debug) + + self._check_connection_settings() + + def _check_connection_settings(self): + self.write_client = self.client.write_api(write_options=SYNCHRONOUS) + + self.assertEqual("http://localhost:9999", self.client.url) + self.assertEqual("my-org", self.client.org) + self.assertEqual("my-token", self.client.token) + self.assertEqual(6000, self.client.timeout) + + def test_default_tags_from_conf_file(self): + self.client.close() + self.client = InfluxDBClient.from_config_file("config.ini", self.debug) + + self._write_point() + + def test_default_tags_from_env(self): + self.client.close() + self.client = InfluxDBClient.from_env_properties(self.debug) + + self._write_point() + + def _write_point(self): + self.write_client = self.client.write_api(write_options=SYNCHRONOUS) + + bucket = self.create_test_bucket() + + measurement = "h2o_feet" + field_name = "water_level" + val = "1.0" + tag = "location" + tag_value = "creek level" + + p = Point(measurement) + p.field(field_name, val) + p.tag(tag, tag_value) + + record_list = [p] + print(record_list) + + self.write_client.write(bucket.name, self.org, record_list) + + query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)' + flux_result = self.client.query_api().query(query) + self.assertEqual(1, len(flux_result)) + rec = flux_result[0].records[0] + + self.assertEqual(self.id_tag, rec["id"]) + self.assertEqual(self.customer_tag, rec["customer"]) + self.assertEqual("LA", rec[self.data_center_key]) + + self.delete_test_bucket(bucket) + + if __name__ == '__main__': unittest.main() From 0641ea7ef333816096ae0fd972130398fdd62ea8 Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Wed, 15 Jan 2020 13:15:41 +0100 Subject: [PATCH 03/13] Initialize client library from config file and environmental properties (#52) --- README.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/README.rst b/README.rst index 97d46b21..cd38b71b 100644 --- a/README.rst +++ b/README.rst @@ -177,6 +177,7 @@ Supported properties are: - ``INFLUXDB_V2_TIMEOUT`` - socket timeout in ms (default value is 10000) .. code-block:: python + self.client = InfluxDBClient.from_env_properties() .. marker-index-end From 806b86bbd24e54853d234e1119f725614dbadc19 Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Wed, 15 Jan 2020 14:20:39 +0100 Subject: [PATCH 04/13] Initialize client library from config file and environmental properties (#52) --- tests/test_WriteApi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index 06cfa629..f1b62e71 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -470,7 +470,7 @@ def tearDown(self) -> None: def test_connection_option_from_conf_file(self): self.client.close() - self.client = InfluxDBClient.from_config_file("config.ini", self.debug) + self.client = InfluxDBClient.from_config_file(os.getcwd() + "/config.ini", self.debug) self._check_connection_settings() @@ -490,7 +490,7 @@ def _check_connection_settings(self): def test_default_tags_from_conf_file(self): self.client.close() - self.client = InfluxDBClient.from_config_file("config.ini", self.debug) + self.client = InfluxDBClient.from_config_file(os.getcwd() + "/config.ini", self.debug) self._write_point() From 0bf42b06a145e6b2b505272ca1ebc1b87bd0198b Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Wed, 15 Jan 2020 14:29:42 +0100 Subject: [PATCH 05/13] Initialize client library from config file and environmental properties (#52) --- tests/test_WriteApi.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index f1b62e71..acccf468 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -470,6 +470,7 @@ def tearDown(self) -> None: def test_connection_option_from_conf_file(self): self.client.close() + os.chdir("../tests") self.client = InfluxDBClient.from_config_file(os.getcwd() + "/config.ini", self.debug) self._check_connection_settings() @@ -490,6 +491,7 @@ def _check_connection_settings(self): def test_default_tags_from_conf_file(self): self.client.close() + os.chdir("../tests") self.client = InfluxDBClient.from_config_file(os.getcwd() + "/config.ini", self.debug) self._write_point() From 352482223b300381b5ce25f1c06726031353b818 Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Wed, 15 Jan 2020 14:34:30 +0100 Subject: [PATCH 06/13] Initialize client library from config file and environmental properties (#52) --- tests/test_WriteApi.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index acccf468..ba6e004e 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -470,6 +470,7 @@ def tearDown(self) -> None: def test_connection_option_from_conf_file(self): self.client.close() + print(os.getcwd()) os.chdir("../tests") self.client = InfluxDBClient.from_config_file(os.getcwd() + "/config.ini", self.debug) From 88e0384340a04427c445bad34d6297c355ed4002 Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Wed, 15 Jan 2020 14:41:17 +0100 Subject: [PATCH 07/13] Initialize client library from config file and environmental properties (#52) --- tests/test_WriteApi.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index ba6e004e..49d740f9 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -470,9 +470,7 @@ def tearDown(self) -> None: def test_connection_option_from_conf_file(self): self.client.close() - print(os.getcwd()) - os.chdir("../tests") - self.client = InfluxDBClient.from_config_file(os.getcwd() + "/config.ini", self.debug) + self.client = InfluxDBClient.from_config_file(os.getcwd() + "/tests/config.ini", self.debug) self._check_connection_settings() @@ -492,8 +490,7 @@ def _check_connection_settings(self): def test_default_tags_from_conf_file(self): self.client.close() - os.chdir("../tests") - self.client = InfluxDBClient.from_config_file(os.getcwd() + "/config.ini", self.debug) + self.client = InfluxDBClient.from_config_file(os.getcwd() + "/tests/config.ini", self.debug) self._write_point() From 7b96a6d8db09eddc8ecb4652de3edf777e77c22f Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Thu, 16 Jan 2020 08:30:58 +0100 Subject: [PATCH 08/13] Initialize client library from config file and environmental properties (#52) --- tests/config.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/config.ini b/tests/config.ini index 02e24397..e650ca8f 100644 --- a/tests/config.ini +++ b/tests/config.ini @@ -1,5 +1,5 @@ [influx2] -url=http://localhost:9999 +url=${env.INFLUXDB_V2_URL} org=my-org token=my-token timeout=6000 From ec6958d92e9da3aa8241aa73c71949636a30e02d Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Thu, 16 Jan 2020 08:42:06 +0100 Subject: [PATCH 09/13] Initialize client library from config file and environmental properties (#52) --- tests/config.ini | 2 +- tests/test_WriteApi.py | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/config.ini b/tests/config.ini index e650ca8f..54ab60f7 100644 --- a/tests/config.ini +++ b/tests/config.ini @@ -1,5 +1,5 @@ [influx2] -url=${env.INFLUXDB_V2_URL} +url=http://192.168.0.2:9999 org=my-org token=my-token timeout=6000 diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index 49d740f9..d05d5487 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -77,7 +77,6 @@ def test_write_records_list(self): self.write_client.write(bucket.name, self.org, record_list) query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)' - print(query) flux_result = self.client.query_api().query(query) @@ -109,7 +108,6 @@ def test_write_points_unicode(self): p.field(field_name, utf8_val) p.tag(tag, tag_value) record_list = [p] - print(record_list) self.write_client.write(bucket.name, self.org, record_list) @@ -147,7 +145,6 @@ def test_write_using_default_tags(self): p2.time(2) record_list = [p, p2] - print(record_list) self.write_client.write(bucket.name, self.org, record_list) @@ -304,7 +301,6 @@ def test_write_dictionaries(self): time.sleep(1) query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)' - print(query) flux_result = self.client.query_api().query(query) @@ -344,7 +340,6 @@ def test_use_default_tags_with_dictionaries(self): time.sleep(1) query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)' - print(query) flux_result = self.client.query_api().query(query) @@ -379,7 +374,6 @@ def test_write_bytes(self): time.sleep(1) query = 'from(bucket:"' + bucket.name + '") |> range(start: 1970-01-01T00:00:00.000000001Z)' - print(query) flux_result = self.client.query_api().query(query) @@ -516,7 +510,6 @@ def _write_point(self): p.tag(tag, tag_value) record_list = [p] - print(record_list) self.write_client.write(bucket.name, self.org, record_list) From bbbe164cdbc0236cf146502dafdeea1d17c2e6ff Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Thu, 16 Jan 2020 08:50:11 +0100 Subject: [PATCH 10/13] Initialize client library from config file and environmental properties (#52) --- tests/test_WriteApi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index d05d5487..cfbc3004 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -477,7 +477,7 @@ def test_connection_option_from_env(self): def _check_connection_settings(self): self.write_client = self.client.write_api(write_options=SYNCHRONOUS) - self.assertEqual("http://localhost:9999", self.client.url) + self.assertEqual(os.environ("INFLUXDB_V2_URL"), self.client.url) self.assertEqual("my-org", self.client.org) self.assertEqual("my-token", self.client.token) self.assertEqual(6000, self.client.timeout) From f2f87ceb8933d11ce56ad1c5a82707aefe3d84f5 Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Thu, 16 Jan 2020 08:50:44 +0100 Subject: [PATCH 11/13] Initialize client library from config file and environmental properties (#52) --- tests/test_WriteApi.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index cfbc3004..6544afcb 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -449,7 +449,6 @@ def setUp(self) -> None: self.customer_tag = "California Miner" self.data_center_key = "data_center" - os.environ['INFLUXDB_V2_URL'] = "http://localhost:9999" os.environ['INFLUXDB_V2_TOKEN'] = "my-token" os.environ['INFLUXDB_V2_TIMEOUT'] = "6000" os.environ['INFLUXDB_V2_ORG'] = "my-org" From bee20df33aba4d706895aa043be024f8d1ed2cdd Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Thu, 16 Jan 2020 08:55:44 +0100 Subject: [PATCH 12/13] Initialize client library from config file and environmental properties (#52) --- tests/test_WriteApi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_WriteApi.py b/tests/test_WriteApi.py index 6544afcb..805385a9 100644 --- a/tests/test_WriteApi.py +++ b/tests/test_WriteApi.py @@ -476,7 +476,7 @@ def test_connection_option_from_env(self): def _check_connection_settings(self): self.write_client = self.client.write_api(write_options=SYNCHRONOUS) - self.assertEqual(os.environ("INFLUXDB_V2_URL"), self.client.url) + self.assertEqual(os.getenv("INFLUXDB_V2_URL"), self.client.url) self.assertEqual("my-org", self.client.org) self.assertEqual("my-token", self.client.token) self.assertEqual(6000, self.client.timeout) From d954d369294c56b116442b280e96eeb2a02c4632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Mon, 20 Jan 2020 08:40:19 +0100 Subject: [PATCH 13/13] Update CHANGELOG.md --- CHANGELOG.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6467442c..29df4801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ -## 1.3.0 [unreleased] +## 1.4.0 [unreleased] + +### Features +1. [#52](https://github.com/influxdata/influxdb-client-python/issues/52): Initialize client library from config file and environmental properties + +## 1.3.0 [2020-01-17] ### Features 1. [#50](https://github.com/influxdata/influxdb-client-python/issues/50): Implemented default tags -2. [#52](https://github.com/influxdata/influxdb-client-python/issues/52): Initialize client library from config file and environmental properties ### API 1. [#47](https://github.com/influxdata/influxdb-client-python/pull/47): Updated swagger to latest version