Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

started implementing ECG monitorin app #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
161 changes: 161 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env.ecg
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
.DS_Store
57 changes: 57 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
version: '3.8'

services:
ecg:
build:
dockerfile: Dockerfile
context: ecg
args:
- environment=${ENVIRONMENT}
image: ecg:latest
ports:
- "8000:8000"
env_file:
- .env.ecg
environment:
- VIRTUAL_HOST=ecg.localhost
- VIRTUAL_PORT=8000
depends_on:
- ecg-db
- redis
volumes:
- ./ecg/app:/local
command: bash -c "gunicorn ecg.wsgi --workers 1 -b 0.0.0.0:8000 --reload"

redis-server:
image: redis:5.0.4
expose:
- "6379"
command: [ "redis-server", "--appendonly", "yes" ]
volumes:
- ./certs:/certs

redis:
depends_on:
- redis-server
image: runnable/redis-stunnel
volumes:
- ./certs/rediscert.pem:/stunnel/private.pem:ro
expose:
- "6380"
environment:
- REDIS_PORT_6379_TCP_ADDR=redis-server
- REDIS_PORT_6379_TCP_PORT=6379

ecg-db:
# platform: linux/x86_64 # Necessary for MacOS M1 Chip
image: mysql:5.7.37
env_file:
- .env.ecg
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
command: --default-authentication-plugin=mysql_native_password

volumes:
mysql_data:
21 changes: 21 additions & 0 deletions ecg/.env.ecg.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

# Database configuration
DATABASE_HOST=ecg-db
DATABASE_PORT=3306
DATABASE_NAME=ecg_docker
DATABASE_USER=docker
DATABASE_PASSWORD=docker

# MySQL docker
MYSQL_ALLOW_EMPTY_PASSWORD=True
MYSQL_DATABASE=ecg_docker
MYSQL_USER=docker
MYSQL_PASSWORD=docker

DEBUG=True

DJANGO_LOG_LEVEL=INFO
LOG_LEVEL=INFO

ENVIRONMENT=local
DJANGO_SETTINGS_MODULE=ecg.settings
51 changes: 51 additions & 0 deletions ecg/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
#
# You must run next command first time:
# pre-commit install
#
default_language_version:
python: python3.10 # TODO: upgrade to newer Python version if it's required.
default_stages: [ commit ]
repos:
# black and docformatter always at top of the list.
- repo: https://github.com/ambv/black
rev: 22.12.0
hooks:
- id: black
description: Python code formatter.
args: [ --line-length=79 ]
- repo: https://github.com/PyCQA/docformatter
rev: v1.5.1
hooks:
- id: docformatter
description: Formats docstrings to follow PEP 257.
args: [ --config=./pyproject.toml ]
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
description: Command-line utility for enforcing style consistency across Python projects.
entry: pflake8
additional_dependencies: [ pyproject-flake8 ]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
description: Trims trailing whitespace.
types: [ python ]
- id: double-quote-string-fixer
description: Replaces double quoted strings with single quoted strings.
types: [ python ]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
description: Reorders imports in python files.
args: [ --settings-path=ecg/pyproject.toml, --filter-files, --line-length=79 ]
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
description: Automatically upgrade syntax for newer versions.
args: [ --py36-plus ] # TODO: upgrade to newer Python version if it's required.
19 changes: 19 additions & 0 deletions ecg/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ARG environment
FROM python:3.10.9
# noqa: E501
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update \
&& apt-get install -y libxml2-dev libxmlsec1-dev \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir /local
ADD /app /local
ADD /config /local/config
RUN pip install -r /local/config/requirements.txt

EXPOSE 8000
CMD bash -c "gunicorn uservice.wsgi --workers 3 --threads 100 --max-requests 1000 --max-requests-jitter 15 -b 0.0.0.0:8000"
WORKDIR /local
77 changes: 77 additions & 0 deletions ecg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# ECG Monitoring Microservice

The ECG Monitoring Microservice is designed to handle electrocardiograms (ECG) and provide various insights about them.
It offers endpoints for performing CRUD operations on ECG records, as well as calculating the number of zero crossings
of the ECG signal. Additionally, the microservice includes user login and registration endpoints.

## Usage

To run the microservice, use the following Docker Compose command:

```bash
docker-compose up ecg --build
```

## Access

- The service will be accessible at *http://ecg.localhost:8000/swagger/*
- To access the admin panel, visit *http://ecg.localhost:8000/admin/*

## Authentication

To interact with the microservice endpoints, a user token is required.
After a user logs in, the token is generated and should be included in the request header as follows:

**Authorization: Token {*generated_token*}**

## User Access

Users can only access ECG data they have created.
Admin users, registered in the admin panel,
have the authority to register new users but cannot create new ECG data.
Other than admin registration, users can also be registerd with **registration** endpoint

## Running Tests

To run tests and generate coverage reports, use the following command:

```bash
docker-compose run --no-deps ecg bash -c "coverage run manage.py test connector; coverage report -m; coverage html; coverage xml"
```

## Pre-commit Checks

Ensure code quality and formatting by running pre-commit checks:

```bash
pre-commit run --all-files
```

## Environment Configuration

### Setting up Environment Variables

For local development, setting up environment variables is necessary. Copy the provided `.env.ecg.sample` file to
create your own `.env.ecg` file. Update the values according to your configuration.

#### Step 1: Copy the Sample Environment File

Copy the contents of `.env.ecg.sample`:

```bash
cp .env.ecg.sample .env.ecg
```

#### Step 2: Update Environment Variables

Open the newly created .env.ecg file and update the values based on your specific configuration.
This file contains essential settings for the database, Django, and other environment-specific variables.

## Files

- **docker-compose.yml**: Docker Compose configuration file.
- **Dockerfile**: Docker configuration file for building the microservice image.
- **config/requirements.txt**: List of Python dependencies.
- **pre-commit.yml**: Configuration file for pre-commit hooks.
- **pyproject.toml**: TOML configuration file for the project.
- **.env.ecg.sample**: sample for environment variables
Empty file added ecg/__init__.py
Empty file.
Empty file added ecg/app/connector/__init__.py
Empty file.
Loading