Skip to content

[Enhancement]: Refactor code. #2

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
.venv
__pycache__
.mypy_cache
.pytest_cache
37 changes: 37 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
fail_fast: true

repos:
- repo: https://github.com/ambv/black
rev: 22.3.0
hooks:
- id: black
args: [--diff, --check]

- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
require_serial: true
args: ["--min-public-methods=1"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.902
hooks:
- id: mypy
exclude: ^tests/
additional_dependencies: ['types-pytz']
args: [--strict, --ignore-missing-imports]

- repo: local
hooks:
- id: pytest-check
name: pytest-check
stages: [commit]
types: [python]
entry: pytest
language: system
pass_filenames: false
always_run: true
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Subscriber Usage Report Generator
Generate Usage Report for this day. Query data from MongoDB databases and write the report on a MySQL database.
# Required Packages
- Python 3.11
- MongoDB 6.0 Instance
- MySQL 8.0
- Before installing `mysqlclient`, follow instructions [here](https://github.com/PyMySQL/mysqlclient) at the `Install` section.
# How to Install?
- Create a `.env` file from the `sample.env` file.
```bash
cp sample.env .env
nano .env
```
- Create and activate the Virtual Environment.

**Linux/MacOS**
```bash
python3.11 -m venv .venv
source .venv/bin/activate
```
**Windows**
```bash
python3.11 -m venv .venv
.venv/bin/Activate.ps1
```
- Install the packages.
```bash
(.venv) pip install -r requirements.txt
```
# How to Run?
```
python run.py
```
# How to Test/Develop?
- Assuming you're still in the Virtual Environment, install the Development packages.
```bash
(.venv) pip install -r requirements-dev.txt
```
- Install the `pre-commit` hooks.
```bash
(.venv) pre-commit install
```
- Run all linter and unit tests.
```bash
(.venv) pre-commit
```
Empty file added app/__init__.py
Empty file.
75 changes: 75 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Load or add configurations here. All secrets should be on .env"""
import os
from dotenv import load_dotenv


load_dotenv()


def get_env_var(env_var_key: str) -> str:
"""Get the Environment Variable given the key.

Args:
env_var_key (str): Environment Variable key.

Raises:
ValueError: Environment Variable is not found.

Returns:
str: Environment Variable.
"""
env_var = os.environ.get(env_var_key, None)
if env_var is None:
raise ValueError(f"Missing Environment Variable: {env_var_key}")

return env_var


# Reporting MySQL DB
REPORTING_MYSQL_DB = {
"SERVER": get_env_var("REPORTING_SQL_SERVER"),
"PORT": get_env_var("REPORTING_SQL_PORT"),
"DATABASE": get_env_var("REPORTING_SQL_DATABASE"),
"USERNAME": get_env_var("REPORTING_SQL_USERNAME"),
"PASSWORD": get_env_var("REPORTING_SQL_PASSWORD"),
}
REPORTING_AULDATALEAK_TABLENAME = get_env_var("REPORTING_AULDATALEAK_TABLENAME")

# Audit MongoDB
AUDIT_MONGODB = {
"SERVER": get_env_var("AUDIT_MONGO_SERVER"),
"REPLICASET": get_env_var("AUDIT_MONGO_REPLICASET"),
"USERNAME": get_env_var("AUDIT_MONGO_USERNAME"),
"PASSWORD": get_env_var("AUDIT_MONGO_PASSWORD"),
"DATABASE": get_env_var("AUDIT_MONGO_DATABASE"),
"COLLECTION": get_env_var("AUDIT_MONGO_DATABASE"),
}

# Usage MongoDB's
ARC_MONGODB_NODES = {
"A": {
"SERVER": get_env_var("ARC_MONGO_SERVER_A"),
"REPLICASET": get_env_var("ARC_MONGO_REPLICASET_A"),
},
"B": {
"SERVER": get_env_var("ARC_MONGO_SERVER_B"),
"REPLICASET": get_env_var("ARC_MONGO_REPLICASET_B"),
},
"C": {
"SERVER": get_env_var("ARC_MONGO_SERVER_C"),
"REPLICASET": get_env_var("ARC_MONGO_REPLICASET_C"),
},
}
ARC_MONGODB_DETAILS = {
"USERNAME": get_env_var("ARC_MONGO_USERNAME"),
"PASSWORD": get_env_var("ARC_MONGO_PASSWORD"),
"DATABASE": get_env_var("ARC_MONGO_DATABASE"),
"COLLECTION": get_env_var("ARC_MONGO_COLLECTION"),
}

# General MongoDB Settings
GENERAL_MONGODB_SETTINGS = {
"AUTHSOURCE": get_env_var("MONGO_AUTHSOURCE"),
"AUTHMECHANISM": "SCRAM-SHA-1",
"READ_PREFERENCE": "secondary",
}
Loading