From f15517af626bfde67c8a3a5578509a79415199c8 Mon Sep 17 00:00:00 2001 From: marcsello Date: Mon, 26 Jul 2021 12:51:52 +0200 Subject: [PATCH] made InfluxDB optional --- src/app.py | 6 ++++-- src/config.py | 1 + src/resources.py | 27 ++++++++++++++------------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/app.py b/src/app.py index 5e48e67..0132fe0 100644 --- a/src/app.py +++ b/src/app.py @@ -40,12 +40,14 @@ api = Api(app) health = HealthCheck() db.init_app(app) ma.init_app(app) -influx_db.init_app(app) +if Config.ENABLE_INFLUXDB: + influx_db.init_app(app) @app.before_first_request def init_db(): - influx_db.database.create(Config.INFLUXDB_DATABASE) + if Config.ENABLE_INFLUXDB: + influx_db.database.create(Config.INFLUXDB_DATABASE) db.create_all() diff --git a/src/config.py b/src/config.py index 0d603c4..d5a7738 100644 --- a/src/config.py +++ b/src/config.py @@ -40,6 +40,7 @@ class Config: STORAGE_HOSTNAME = os.getenv("INPUT_STORAGE_HOSTNAME", "localhost:8042") + ENABLE_INFLUXDB = os.environ.get("INPUT_ENABLE_INFLUX", "true").lower() in ["true", "yes", "1"] INFLUXDB_HOST = os.getenv("INFLUX_HOST", "input-influx") INFLUXDB_PORT = os.getenv("INFLUX_PORT", "8086") INFLUXDB_USER = os.getenv("INFLUX_USERNAME", "input-service") diff --git a/src/resources.py b/src/resources.py index 69355ea..9200e12 100644 --- a/src/resources.py +++ b/src/resources.py @@ -118,20 +118,21 @@ class SampleResource(Resource): current_app.logger.exception(e) return abort(569, "AMPQ Publish error") - influx_db.write_points( - [ - { - 'time': datetime.now(tz=tzlocal.get_localzone()), - 'measurement': 'cloudinput', - 'tags': { - 'device': desc['device_id'] - }, - 'fields': { - 'bruh': 1.0 + if current_app.config['ENABLE_INFLUXDB']: + influx_db.write_points( + [ + { + 'time': datetime.now(tz=tzlocal.get_localzone()), + 'measurement': 'cloudinput', + 'tags': { + 'device': desc['device_id'] + }, + 'fields': { + 'bruh': 1.0 + } } - } - ] - ) + ] + ) db.session.commit() return {"tag": generated_tag}, 200