diff --git a/.drone.yml b/.drone.yml index 7036ab1..d39867e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -23,13 +23,6 @@ steps: tags: - latest - ${DRONE_BUILD_NUMBER} -- name: sentry - image: tormachris/drone-sentry - settings: - sentry_project: ${DRONE_REPO_NAME} - sentry_domain: sentry.kmlabz.com - sentry_token: - from_secret: SENTRY_TOKEN - name: ms-teams image: kuperiu/drone-teams diff --git a/requirements.txt b/requirements.txt index 0b5f89e..98188e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,7 @@ psycopg2-binary marshmallow~=3.13.0 marshmallow-sqlalchemy~=0.26.1 flask-marshmallow -py-healthcheck \ No newline at end of file +py-healthcheck +Flask-InfluxDB +tzdata +tzlocal \ No newline at end of file diff --git a/src/app.py b/src/app.py index 0fb6236..5e48e67 100644 --- a/src/app.py +++ b/src/app.py @@ -9,6 +9,7 @@ from healthcheck import HealthCheck from config import Config from db import db from marshm import ma +from influxus import influx_db from resources import SampleResource, SampleParameterResource from healthchecks import health_database_status @@ -39,10 +40,12 @@ api = Api(app) health = HealthCheck() db.init_app(app) ma.init_app(app) +influx_db.init_app(app) @app.before_first_request def init_db(): + influx_db.database.create(Config.INFLUXDB_DATABASE) db.create_all() diff --git a/src/config.py b/src/config.py index 466961e..0d603c4 100644 --- a/src/config.py +++ b/src/config.py @@ -14,6 +14,7 @@ _POSTGRES_HOSTNAME = os.getenv("INPUT_POSTGRES_HOSTNAME", "localhost") _POSTGRES_USERNAME = os.getenv("INPUT_POSTGRES_USERNAME", "input-service") _POSTGRES_PASSWORD = os.getenv("INPUT_POSTGRES_PASSWORD", "input-service") _POSTGRES_DB = os.getenv("INPUT_POSTGRES_DB", "input-service") +_POSTGRES_OPTS = os.getenv("INPUT_POSTGRES_OPTS", "") class Config: @@ -35,6 +36,12 @@ class Config: 'virtual_host': '/' } - SQLALCHEMY_DATABASE_URI = f"postgresql://{_POSTGRES_USERNAME}:{_POSTGRES_PASSWORD}@{_POSTGRES_HOSTNAME}:5432/{_POSTGRES_DB}?sslmode=require" + SQLALCHEMY_DATABASE_URI = f"postgresql://{_POSTGRES_USERNAME}:{_POSTGRES_PASSWORD}@{_POSTGRES_HOSTNAME}:5432/{_POSTGRES_DB}{_POSTGRES_OPTS}" STORAGE_HOSTNAME = os.getenv("INPUT_STORAGE_HOSTNAME", "localhost:8042") + + INFLUXDB_HOST = os.getenv("INFLUX_HOST", "input-influx") + INFLUXDB_PORT = os.getenv("INFLUX_PORT", "8086") + INFLUXDB_USER = os.getenv("INFLUX_USERNAME", "input-service") + INFLUXDB_PASSWORD = os.getenv("INFLUX_PASSWORD", "input-service-supersecret") + INFLUXDB_DATABASE = os.getenv("INFLUX_DB", "input-service") diff --git a/src/db.py b/src/db.py index acfa6d2..98196e0 100644 --- a/src/db.py +++ b/src/db.py @@ -2,7 +2,7 @@ from flask_sqlalchemy import SQLAlchemy """ -Flask Restful endpoints +Database api """ __author__ = '@tormakris' diff --git a/src/influxus.py b/src/influxus.py new file mode 100644 index 0000000..6dc116b --- /dev/null +++ b/src/influxus.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +from flask_influxdb import InfluxDB + + +""" +Influx api +""" + +__author__ = '@tormakris' +__copyright__ = "Copyright 2020, Birbnetes Team" +__module_name__ = "influxus" +__version__text__ = "1" + +influx_db = InfluxDB() diff --git a/src/resources.py b/src/resources.py index fc8f32d..69355ea 100644 --- a/src/resources.py +++ b/src/resources.py @@ -1,11 +1,14 @@ #!/usr/bin/env python3 import json +from datetime import datetime +import tzlocal from xeger import Xeger from flask_restful import Resource from flask import request, current_app, abort import requests import pika from db import db +from influxus import influx_db from models import SampleMetadata from schemas import SampleSchema, SampleMetadataSchema @@ -111,11 +114,25 @@ class SampleResource(Resource): routing_key='feature', body=json.dumps({'tag': generated_tag}).encode('UTF-8')) connection.close() - except Exception as e: 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 + } + } + ] + ) + db.session.commit() return {"tag": generated_tag}, 200