Loading src/common/mixins/influx.py +18 −31 Original line number Diff line number Diff line """Scratch file for showing how the mixins get used.""" from influxdb_client import InfluxDBClient from influxdb import InfluxDBClient from common.env import check_environment as ce Loading @@ -13,19 +13,23 @@ class InfluxMixin: DEFAULT_BUCKET = ce("INFLUXDB_BUCKET", "my-bucket") DEFAULT_TIMEOUT = ce("INFLUXDB_TIMEOUT", 10000) # In milliseconds DEFAULT_HOST = ce("INFLUXDB_HOST", "localhost") DEFAULT_PORT = int(ce("INFLUXDB_PORT", 8086)) DEFAULT_USER = ce("INFLUXDB_ADMIN_USER", "admin") DEFAULT_PASSWORD = ce("INFLUXDB_ADMIN_PASSWORD", "secret") DEFAULT_DB = ce("INFLUX_DB", "cast") def open(self): """Explicitly create the InfluxDB client instance :return: True if client created successfully, else false """ """Create the InfluxDB client instance.""" self.logger.debug("Creating InfluxDB Client Instance") try: self.client = InfluxDBClient( url=self.DEFAULT_URL, token=self.DEFAULT_TOKEN, org=self.DEFAULT_ORG, timeout=self.DEFAULT_TIMEOUT, host=self.DEFAULT_HOST, port=self.DEFAULT_PORT, username=self.DEFAULT_USER, password=self.DEFAULT_PASSWORD, database=self.DEFAULT_DB, ) self.query_api = self.client.query_api() self.logger.debug("Successfully created InfluxDB client instance") except Exception as error: self.logger.error(f"Error creating InfluxDB Client: {error}") Loading @@ -33,38 +37,21 @@ class InfluxMixin: return True def query(self, query): """Query the InfluxDB. :param query: A valid Flux query statement. :return: Results of the query, or None if an error occurs. """ """Query the InfluxDB.""" if not self.client: self.logger.info("InfluxDB client not created, creating now.") if not self.open(): return None self.logger.debug("Submitting query to InfluxDB.") try: result = self.query_api.query(query) result = self.client.query(query) return result except Exception as error: self.logger.error(f"Error during query execution: {error}") return None def list_buckets(self): """List all buckets in the InfluxDB instance. :return: List of bucket names or None if an error occurs. """ def list_databases(self): if not self.client: self.logger.info('InfluxDB client not created, creating now.') if not self.open(): return None self.logger.debug('Fetching list of buckets.') try: buckets_api = self.client.buckets_api() buckets = buckets_api.find_buckets() return [bucket.name for bucket in buckets.buckets] except Exception as error: self.logger.error(f'Error fetching buckets: {error}') return None return self.client.get_list_database() src/requirements.txt +1 −1 Original line number Diff line number Diff line Loading @@ -3,4 +3,4 @@ pandas==2.1.4 psycopg2-binary==2.9.9 pymssql==2.2.11 SQLAlchemy~=2.0.23 influxdb_client==1.39.0 No newline at end of file influxdb==5.3.1 No newline at end of file Loading
src/common/mixins/influx.py +18 −31 Original line number Diff line number Diff line """Scratch file for showing how the mixins get used.""" from influxdb_client import InfluxDBClient from influxdb import InfluxDBClient from common.env import check_environment as ce Loading @@ -13,19 +13,23 @@ class InfluxMixin: DEFAULT_BUCKET = ce("INFLUXDB_BUCKET", "my-bucket") DEFAULT_TIMEOUT = ce("INFLUXDB_TIMEOUT", 10000) # In milliseconds DEFAULT_HOST = ce("INFLUXDB_HOST", "localhost") DEFAULT_PORT = int(ce("INFLUXDB_PORT", 8086)) DEFAULT_USER = ce("INFLUXDB_ADMIN_USER", "admin") DEFAULT_PASSWORD = ce("INFLUXDB_ADMIN_PASSWORD", "secret") DEFAULT_DB = ce("INFLUX_DB", "cast") def open(self): """Explicitly create the InfluxDB client instance :return: True if client created successfully, else false """ """Create the InfluxDB client instance.""" self.logger.debug("Creating InfluxDB Client Instance") try: self.client = InfluxDBClient( url=self.DEFAULT_URL, token=self.DEFAULT_TOKEN, org=self.DEFAULT_ORG, timeout=self.DEFAULT_TIMEOUT, host=self.DEFAULT_HOST, port=self.DEFAULT_PORT, username=self.DEFAULT_USER, password=self.DEFAULT_PASSWORD, database=self.DEFAULT_DB, ) self.query_api = self.client.query_api() self.logger.debug("Successfully created InfluxDB client instance") except Exception as error: self.logger.error(f"Error creating InfluxDB Client: {error}") Loading @@ -33,38 +37,21 @@ class InfluxMixin: return True def query(self, query): """Query the InfluxDB. :param query: A valid Flux query statement. :return: Results of the query, or None if an error occurs. """ """Query the InfluxDB.""" if not self.client: self.logger.info("InfluxDB client not created, creating now.") if not self.open(): return None self.logger.debug("Submitting query to InfluxDB.") try: result = self.query_api.query(query) result = self.client.query(query) return result except Exception as error: self.logger.error(f"Error during query execution: {error}") return None def list_buckets(self): """List all buckets in the InfluxDB instance. :return: List of bucket names or None if an error occurs. """ def list_databases(self): if not self.client: self.logger.info('InfluxDB client not created, creating now.') if not self.open(): return None self.logger.debug('Fetching list of buckets.') try: buckets_api = self.client.buckets_api() buckets = buckets_api.find_buckets() return [bucket.name for bucket in buckets.buckets] except Exception as error: self.logger.error(f'Error fetching buckets: {error}') return None return self.client.get_list_database()
src/requirements.txt +1 −1 Original line number Diff line number Diff line Loading @@ -3,4 +3,4 @@ pandas==2.1.4 psycopg2-binary==2.9.9 pymssql==2.2.11 SQLAlchemy~=2.0.23 influxdb_client==1.39.0 No newline at end of file influxdb==5.3.1 No newline at end of file