Skip to content

Commit

Permalink
Merge pull request #2 from datasci4health/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
matheusmota authored Mar 18, 2019
2 parents f2fa5da + c21fca2 commit 2fa6328
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

#mongodb volume
data/
103 changes: 103 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
version: '3'

services:

broker:
image: eclipse-mosquitto
ports:
- 1883:1883
# - 9001:9001
# volumes:
# - broker_config:/mosquitto/config/
restart: always

relayer:
build: ./modules/relayer
environment:
- HARENA_LOGGER_BROKER_HOST=broker
- HARENA_LOGGER_BROKER_PORT=1883
- HARENA_LOGGER_FLASK_HOST=0.0.0.0
- HARENA_LOGGER_FLASK_PORT=5000
- HARENA_LOGGER_FLASK_DEBUG=False
- HARENA_LOGGER_MONGODB_HOST=mongodb
- HARENA_LOGGER_MONGODB_PORT=27017
- HARENA_LOGGER_MONGODB_DB=harena_logger
- HARENA_LOGGER_MONGODB_COLLECTION=executions
ports:
- 5000:5000
depends_on:
- broker
- mongodb
restart: always

mongodb:
image: mongo:latest
container_name: "mongodb"
environment:
- MONGO_DATA_DIR=/data/db
- MONGO_LOG_DIR=/dev/null
volumes:
- harena_logger_mongodb:/data/db
ports:
- 27017:27017
# command: mongod --smallfiles --logpath=/dev/null # --quiet

volumes:
harena_logger_mongodb:


# database:
# image: mysql:5.7
# ports:
# - "3306:3306"
# volumes:
# - database:/var/lib/mysql
# environment: # will be replaced to .env vars, as in ${DB_PASSWORD}
# - MYSQL_DATABASE=jacinto-casemanager
# - MYSQL_USER=jacinto-casemanager
# - MYSQL_ALLOW_EMPTY_PASSWORD=yes
# - MYSQL_PASSWORD=jacinto
# - MYSQL_ROOT_PASSWORD=root
# restart: always


# database-ui:
# image: phpmyadmin/phpmyadmin:latest
# links:
# - database
# ports:
# - 80:80
# environment:
# - PMA_ARBITRARY=0 # connection to any server
# - PMA_HOST=database
# - PMA_PORT=3306
# depends_on:
# - database


# jacinto-casemanager:
# image: datasci4health/jacinto-casemanager
# ports:
# - "3333:3333"
# environment:
# - HOST=0.0.0.0
# - PORT=3333
# - NODE_ENV=production
# - APP_NAME=jacinto-casemanager
# - APP_URL=http://${HOST}:${PORT}
# - CACHE_VIEWS=false
# - APP_KEY=Xw8Ctuhsasew77qDx4GSnUlyTY3a6x
# - DB_CONNECTION=mysql
# - DB_HOST=127.0.0.1
# - DB_PORT=3306
# - DB_DATABASE=jacinto-casemanager
# - DB_USER=jacinto-casemanager
# - DB_PASSWORD=jacinto
# - HASH_DRIVER=bcrypt
# depends_on:
# - database
# restart: always

# volumes:
# database:
# driver: "local"
11 changes: 11 additions & 0 deletions modules/relayer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM python:3.7-alpine

WORKDIR /app

COPY requirements.txt .

RUN pip3 install -r requirements.txt

ADD . .

CMD ["python3", "src/server.py"]
7 changes: 7 additions & 0 deletions modules/relayer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#python-dotenv
flask
flask_restful
#Flask-MongoAlchemy
#flask-migrate
paho-mqtt
pymongo
93 changes: 93 additions & 0 deletions modules/relayer/src/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import os
import json
from flask import Flask, request, jsonify
from flask_restful import Resource, Api
import paho.mqtt.client as paho
import random
import pymongo
import time


class IndexResource(Resource):

def __init__(self,broker,mongodb_client):
self.broker = broker

def get(self):
message = {"message": "Welcome to the Harena Logger module",
"broker_status" : broker.__repr__(),
"database_status":mongodb_client.server_info()['ok']

}
return message



class HarenaMessageResource(Resource):

def __init__(self, broker, mongodb_collection):
self.broker = broker
self.mongodb_collection = mongodb_collection

def post(self):
message = request.get_json()
print(json.dumps(message))
topic = message['topic']
payload = message['payload']

message['timestamp'] = "{}".format(int(round(time.time() * 1000)))

broker_publishing_flag = self.broker.publish(topic,json.dumps(payload))
mongodb_insertion_flag = self.mongodb_collection.insert(message)

return 'Message published successfully',201

def get(self):
docs = self.mongodb_collection.find().sort([("timestamp", pymongo.DESCENDING)])

items = []

for doc in docs:
doc['_id'] = str(doc['_id'])
items.append(doc)

return jsonify({'execution_stream':items})

def delete(self):
self.mongodb_collection.delete_many({})

return 'Messages in the execution stream deleted successfully'




if __name__ == '__main__':

web_app = Flask(__name__)
api = Api(web_app)

config = {}
config['broker_host'] = os.environ.get('HARENA_LOGGER_BROKER_HOST', 'localhost')
config['broker_port'] = int(os.environ.get('HARENA_LOGGER_BROKER_PORT', 1883))

config['flask_host'] = os.environ.get('HARENA_LOGGER_FLASK_HOST', '0.0.0.0')
config['flask_port'] = int(os.environ.get('HARENA_LOGGER_FLASK_PORT', 5000))
config['flask_debug'] = bool(os.environ.get('HARENA_LOGGER_FLASK_DEBUG', False))

config['mongodb_host'] = os.environ.get('HARENA_LOGGER_MONGODB_HOST', 'localhost')
config['mongodb_port'] = int(os.environ.get('HARENA_LOGGER_MONGODB_PORT', 27017))
config['mongodb_db'] = os.environ.get('HARENA_LOGGER_MONGODB_DB', 'harena_logger')
config['mongodb_collection'] = os.environ.get('HARENA_LOGGER_MONGODB_COLLECTION', 'executions')

mongodb_client = pymongo.MongoClient("mongodb://{0}:{1}/".format(config['mongodb_host'],config['mongodb_port']))
mongodb_db = mongodb_client[config['mongodb_db']]
mongodb_collection = mongodb_db[ config['mongodb_collection']]

broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) )
broker.connect(config['broker_host'],config['broker_port'])
broker.reconnect_delay_set(min_delay=1, max_delay=20)

api.add_resource(IndexResource, '/', resource_class_args=[broker,mongodb_client])
api.add_resource(HarenaMessageResource, '/message',resource_class_args=[broker,mongodb_collection])

web_app.run(host=config['flask_host'], port=config['flask_port'],debug=config['flask_debug'])

0 comments on commit 2fa6328

Please sign in to comment.