diff --git a/README.md b/README.md index fa16c3e..fc53903 100644 --- a/README.md +++ b/README.md @@ -1 +1,119 @@ -# harena-logger \ No newline at end of file +[![GitHub license](https://img.shields.io/github/license/Naereen/StrapDown.js.svg)](https://github.com/datasci4health/harena-manager/blob/master/LICENSE) +[![Docker Automated](https://img.shields.io/docker/cloud/automated/datasci4health/harena-manager.svg?style=flat)](https://cloud.docker.com/u/datasci4health/repository/registry-1.docker.io/datasci4health/harena-manager) +[![Docker Build](https://img.shields.io/docker/cloud/build/datasci4health/harena-manager.svg?style=flat)](https://cloud.docker.com/u/datasci4health/repository/registry-1.docker.io/datasci4health/harena-manager) +[![Docker Pulls](https://img.shields.io/docker/pulls/datasci4health/harena-manager.svg?style=flat)](https://cloud.docker.com/u/datasci4health/repository/registry-1.docker.io/datasci4health/harena-manager) +[![Docker Stars](https://img.shields.io/docker/stars/datasci4health/harena-manager.svg?style=flat)](https://cloud.docker.com/u/datasci4health/repository/registry-1.docker.io/datasci4health/harena-manager) + +# herena-logger + +[Harena](https://github.com/datasci4health/harena)'s API for managing users and clinical cases. + +## Table of Contents + + * [herena-manager](#herena-manager) + * [Table of Contents](#table-of-contents) + * [Getting Started](#getting-started) + * [Running as Docker containers - Linux](#running-as-docker-containers---linux) + * [Running as Docker containers - Windows](#running-as-docker-containers---windows) + * [Running locally - Linux](#running-locally---linux) + * [Running locally - Windows](#running-locally---windows) + * [System Requirements](#system-requirements) + * [For running as Docker containers](#for-running-as-linuxwindows-docker-containers) + * [For running locally](#for-running-locally) + * [Configuration](#configuration) + * [Virtualenvs: AdonisJS](#virtualenvs-adonisjs) + * [Virtualenvs: Database](#virtualenvs-database) + * [Contributing](#contributing) + * [Project organization](#project-organization) + * [Branch organization (future CI/CD)](#branch-organization-future-cicd) + +## Getting Started + +### Running as Docker containers - Linux + +```bash +sudo apt-get install -y wget +wget https://github.com/datasci4health/harena-manager/blob/master/docker-compose.yml +sudo docker-compose up +``` + + Make sure you have **node.js** and **npm** already installed (see [system requirements](#system-requirements) for more details). + + +### Running as Docker containers - Windows + +//to do + +### Running as Docker containers - Linux´ +```bash +sudo docker run datasci4health/harena-logger:latest +``` + +### Running locally - Linux + +First, clone this repository and enter the folder: + +```bash +git clone https://github.com/datasci4health/harena-logger +cd harena-logger +``` +```bash +cd modules/relayer # entering the source folder +pip3 install -r requirements.txt # installing requirements packages for python +export FLASK_APP=server.py # defining flask application +flask run # running the application +``` + +### Running locally - Windows + +//to do + +## System Requirements + +### For running as Docker containers + +* [docker]() +* [docker-compose]() + +### For running locally + +##### System dependencies + +* flask +* flask-restful +* flask-cors +* paho-mqtt +* pymongo + +## Configuration + +### Virtualenvs + +* HARENA_LOGGER_BROKER_HOST = mqtt host +* HARENA_LOGGER_BROKER_PORT = mqtt host port + +* HARENA_LOGGER_FLASK_HOST = Flask host +* HARENA_LOGGER_FLASK_PORT = Flask port +* HARENA_LOGGER_FLASK_DEBUG = Flask debug + +* HARENA_LOGGER_MONGODB_HOST = mongo host +* HARENA_LOGGER_MONGODB_PORT = mongo port +* HARENA_LOGGER_MONGODB_DB = mongo database name +* HARENA_LOGGER_MONGODB_COLLECTION = mongo current document + +## Contributing + +### Project organization + +//to do + +### Branch organization (future CI/CD) +* **feature/< label >:** + * new features. +* **development:** + * Protected. Must use _pull request_ to merge new features. +* **master:** + * Version running at http://cloud.lis.ic.unicamp.br/harena/latest . + * Protected. Must use _pull request_ to merge evolutions of the _development_ branch. +* **tags:** + * Are used for creating Dockerhub image versions at https://cloud.docker.com/u/datasci4health/repository/docker/datasci4health/harena-logger . diff --git a/modules/relayer/Dockerfile b/modules/relayer/Dockerfile index 3da05b9..e70742b 100644 --- a/modules/relayer/Dockerfile +++ b/modules/relayer/Dockerfile @@ -4,7 +4,7 @@ WORKDIR /app COPY requirements.txt . -RUN pip3 install -r requirements.txt +RUN pip3 install -U -r requirements.txt ADD . . diff --git a/modules/relayer/requirements.txt b/modules/relayer/requirements.txt index 3f47e1b..4667007 100644 --- a/modules/relayer/requirements.txt +++ b/modules/relayer/requirements.txt @@ -1,6 +1,7 @@ #python-dotenv flask flask_restful +flask-cors #Flask-MongoAlchemy #flask-migrate paho-mqtt diff --git a/modules/relayer/src/config.py b/modules/relayer/src/config.py new file mode 100644 index 0000000..935b27e --- /dev/null +++ b/modules/relayer/src/config.py @@ -0,0 +1,15 @@ +import os + +class Config(object): + + HARENA_LOGGER_BROKER_HOST = os.environ.get('HARENA_LOGGER_BROKER_HOST', 'localhost') + HARENA_LOGGER_BROKER_PORT = int(os.environ.get('HARENA_LOGGER_BROKER_PORT', 1883)) + + HARENA_LOGGER_FLASK_HOST = os.environ.get('HARENA_LOGGER_FLASK_HOST', '0.0.0.0') + HARENA_LOGGER_FLASK_PORT = int(os.environ.get('HARENA_LOGGER_FLASK_PORT', 5000)) + HARENA_LOGGER_FLASK_DEBUG = bool(os.environ.get('HARENA_LOGGER_FLASK_DEBUG', False)) + + HARENA_LOGGER_MONGODB_HOST = os.environ.get('HARENA_LOGGER_MONGODB_HOST', 'localhost') + HARENA_LOGGER_MONGODB_PORT = int(os.environ.get('HARENA_LOGGER_MONGODB_PORT', 27017)) + HARENA_LOGGER_MONGODB_DB = os.environ.get('HARENA_LOGGER_MONGODB_DB', 'harena_logger') + HARENA_LOGGER_MONGODB_COLLECTION = os.environ.get('HARENA_LOGGER_MONGODB_COLLECTION', 'executions') diff --git a/modules/relayer/src/server.py b/modules/relayer/src/server.py index 0a5679a..3c09fcd 100644 --- a/modules/relayer/src/server.py +++ b/modules/relayer/src/server.py @@ -1,11 +1,13 @@ 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 +from flask import Flask, request, jsonify +from flask_restful import Resource, Api +from config import Config +from flask_cors import CORS class IndexResource(Resource): @@ -22,72 +24,65 @@ def get(self): return message - class HarenaMessageResource(Resource): def __init__(self, broker, mongodb_collection): - self.broker = broker - self.mongodb_collection = 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 = request.get_json() + print(json.dumps(message)) + topic = message['topic'] + payload = message['payload'] - message['timestamp'] = "{}".format(int(round(time.time() * 1000))) + 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) + broker_publishing_flag = self.broker.publish(topic,json.dumps(payload)) + mongodb_insertion_flag = self.mongodb_collection.insert_one(message) - return 'Message published successfully',201 + return 'Message published successfully', 201 def get(self): - docs = self.mongodb_collection.find().sort([("timestamp", pymongo.DESCENDING)]) + docs = self.mongodb_collection.find().sort([("timestamp", pymongo.DESCENDING)]) - items = [] + items = [] - for doc in docs: - doc['_id'] = str(doc['_id']) - items.append(doc) + for doc in docs: + doc['_id'] = str(doc['_id']) + items.append(doc) - return jsonify({'execution_stream':items}) + return jsonify({'execution_stream':items}) def delete(self): - self.mongodb_collection.delete_many({}) - - return 'Messages in the execution stream deleted successfully' - + self.mongodb_collection.delete_many({}) + data = {"message":'Messages in the execution stream deleted successfully'} + return jsonify(data) 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)) + web_app = Flask(__name__) + web_app.config.from_object(Config) + CORS(web_app) + api = Api(web_app) - 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(web_app.config['HARENA_LOGGER_MONGODB_HOST'], \ + web_app.config['HARENA_LOGGER_MONGODB_PORT'])) - 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']] + mongodb_db = mongodb_client[web_app.config['HARENA_LOGGER_MONGODB_DB']] + mongodb_collection = mongodb_db[web_app.config['HARENA_LOGGER_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) + broker = paho.Client("publisher{0}".format(random.randint(0,99999999)) ) + broker.connect(web_app.config['HARENA_LOGGER_BROKER_HOST'], + web_app.config['HARENA_LOGGER_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]) + 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']) + web_app.run(host=web_app.config['HARENA_LOGGER_FLASK_HOST'], + port=web_app.config['HARENA_LOGGER_FLASK_PORT'], + debug=web_app.config['HARENA_LOGGER_FLASK_DEBUG'])