Merge branch 'influx'

This commit is contained in:
Pünkösd Marcell 2021-07-26 12:45:01 +02:00
commit 3c10a351ba
7 changed files with 49 additions and 11 deletions

View File

@ -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

View File

@ -12,4 +12,7 @@ psycopg2-binary
marshmallow~=3.13.0
marshmallow-sqlalchemy~=0.26.1
flask-marshmallow
py-healthcheck
py-healthcheck
Flask-InfluxDB
tzdata
tzlocal

View File

@ -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()

View File

@ -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")

View File

@ -2,7 +2,7 @@
from flask_sqlalchemy import SQLAlchemy
"""
Flask Restful endpoints
Database api
"""
__author__ = '@tormakris'

15
src/influxus.py Normal file
View File

@ -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()

View File

@ -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