Skip to content

Commit

Permalink
Update configurations and code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
fjpacheco committed Mar 10, 2024
1 parent 4b4ffea commit 34907a0
Show file tree
Hide file tree
Showing 24 changed files with 259 additions and 300 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ POSTGRES_HOST=
POSTGRES_PORT=
POSTGRES_DB=
POSTGRES_SCHEMA=
DATABASE_URL=

# RabbitMQ
CLOUDAMQP_URL=
Expand Down
16 changes: 8 additions & 8 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Linked ticket in Jira
- https://hanagotchi.atlassian.net/jira/software/projects/HAN/boards/1/backlog

## Describe your changes, marking the new features and possible risks
-

## Media: upload photos or videos (if needed)
-
## Linked ticket in Jira
- https://hanagotchi.atlassian.net/jira/software/projects/HAN/boards/1/backlog

## Describe your changes, marking the new features and possible risks
-

## Media: upload photos or videos (if needed)
-
60 changes: 30 additions & 30 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@

name: Linters

on: 'push'

jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8

- name: Install Python dependencies
run: pip install black flake8

- name: Run linters
uses: wearerequired/lint-action@v1
with:
black: true

- name: flake8 Lint
uses: py-actions/flake8@v1.2.0
with:

name: Linters

on: 'push'

jobs:
run-linters:
name: Run linters
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8

- name: Install Python dependencies
run: pip install black flake8

- name: Run linters
uses: wearerequired/lint-action@v1
with:
black: true

- name: flake8 Lint
uses: py-actions/flake8@v1.2.0
with:
max-line-length: "88"
52 changes: 26 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
default: docker-compose-up

all:

create-network:
@if ! docker network inspect common_network >/dev/null 2>&1; then \
docker network create common_network; \
fi
.PHONY: create-network

docker-image: create-network
docker build -f ./Dockerfile -t "app:latest" .
.PHONY: docker-image

docker-compose-up: docker-image
docker-compose -f docker-compose.yaml up -d --build
.PHONY: docker-compose-up

docker-compose-down:
docker-compose -f docker-compose.yaml stop -t 20
docker-compose -f docker-compose.yaml down --remove-orphans
.PHONY: docker-compose-down

docker-compose-logs:
docker-compose -f docker-compose.yaml logs -f
.PHONY: docker-compose-logs
default: docker-compose-up

all:

create-network:
@if ! docker network inspect common_network >/dev/null 2>&1; then \
docker network create common_network; \
fi
.PHONY: create-network

docker-image: create-network
docker build -f ./Dockerfile -t "app:latest" .
.PHONY: docker-image

docker-compose-up: docker-image
docker-compose -f docker-compose.yaml up -d --build
.PHONY: docker-compose-up

docker-compose-down:
docker-compose -f docker-compose.yaml stop -t 20
docker-compose -f docker-compose.yaml down --remove-orphans
.PHONY: docker-compose-down

docker-compose-logs:
docker-compose -f docker-compose.yaml logs -f
.PHONY: docker-compose-logs
28 changes: 14 additions & 14 deletions app/controller/calculator_controller.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from fastapi import status


class CalculatorController:
def __init__(self, service):
self.service = service

def handle_sum(self, request):
sum = request.number1 + request.number2
self.service.add_number(sum)
return {
'result': sum,
'status': status.HTTP_200_OK
}
from fastapi import status


class CalculatorController:
def __init__(self, service):
self.service = service

def handle_sum(self, request):
sum = request.number1 + request.number2
self.service.add_number(sum)
return {
'result': sum,
'status': status.HTTP_200_OK
}
22 changes: 11 additions & 11 deletions app/controller/measurement_controller.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from controller.device_plant_controller import withSQLExceptionsHandle
from fastapi import Request


@withSQLExceptionsHandle
def last_measurement_made_by_plant(req: Request, id_plant: int):
try:
return req.app.database.get_last_measurement(id_plant)
except Exception as err:
req.app.database.rollback()
raise err
from controller.device_plant_controller import withSQLExceptionsHandle
from fastapi import Request


@withSQLExceptionsHandle
def last_measurement_made_by_plant(req: Request, id_plant: int):
try:
return req.app.database.get_last_measurement(id_plant)
except Exception as err:
req.app.database.rollback()
raise err
9 changes: 1 addition & 8 deletions app/database/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@


class SQLAlchemyClient():
db_url = environ.get("DATABASE_URL", engine.URL.create(
"postgres",
database=environ.get("POSTGRES_DB", "dev"),
username=environ.get("POSTGRES_USER", "user"),
password=environ.get("POSTGRES_PASSWORD", "1234"),
host=environ.get("POSTGRES_HOST", "sql"),
port=environ.get("POSTGRES_PORT", "5432")
)).replace("postgres://", "postgresql://", 1)
db_url = environ.get("DATABASE_URL").replace("postgres://", "postgresql://", 1)

engine = create_engine(db_url)

Expand Down
8 changes: 4 additions & 4 deletions app/exceptions/MeasurementsException.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class MeasurementsException(Exception):
def __init__(self, status_code, message):
self.status_code = status_code
self.message = message
class MeasurementsException(Exception):
def __init__(self, status_code, message):
self.status_code = status_code
self.message = message
8 changes: 4 additions & 4 deletions app/exceptions/empty_package.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class EmptyPackageError(Exception):
def __init__(self, empty_folds: list):
self.empty_folds = empty_folds
super().__init__(f"Package received with empty folds: {self.empty_folds}.")
class EmptyPackageError(Exception):
def __init__(self, empty_folds: list):
self.empty_folds = empty_folds
super().__init__(f"Package received with empty folds: {self.empty_folds}.")
12 changes: 6 additions & 6 deletions app/exceptions/invalid_insertion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class InvalidInsertionError(Exception):
def __init__(self, item, name_table):
self.item = item
self.name_table = name_table
super().__init__(
f"Failed to insert {self.item} in {self.name_table.upper()} table.")
class InvalidInsertionError(Exception):
def __init__(self, item, name_table):
self.item = item
self.name_table = name_table
super().__init__(
f"Failed to insert {self.item} in {self.name_table.upper()} table.")
112 changes: 56 additions & 56 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
from fastapi import FastAPI
from database.database import SQLAlchemyClient
import logging
from controller.calculator_controller import CalculatorController
from schemas.schemas import Request as RequestSchema
from service.calculator_service import CalculatorService
from router import api_router

app = FastAPI()
service = CalculatorService()
controller = CalculatorController(service)

logger = logging.getLogger("measurements")
logger.setLevel("DEBUG")


@app.on_event("startup")
async def start_up():
app.logger = logger

try:
app.database = SQLAlchemyClient()
app.logger.info("Postgres connection established")
except Exception as e:
app.logger.error(e)
app.logger.error("Could not connect to Postgres client")


@app.on_event("shutdown")
async def shutdown_db_client():
app.database.shutdown()
app.logger.info("Postgres shutdown succesfully")


@app.get("/")
async def root():
return {"message": "Hello World"}


@app.post("/sum")
async def calculator(request: RequestSchema):
return controller.handle_sum(request)


""" # Endpoint only for DB conection testing.
@app.post("/device-plant")
async def add_new_device_plant(req: Request,
device_plant: DevicePlantSchema = Body(...)):
try:
req.app.database.add_new(DevicePlant.from_pydantic(device_plant))
return req.app.database.find_device_plant(device_plant.id_device)
except Exception as e:
req.app.database.rollback()
raise e """

app.include_router(api_router)
from fastapi import FastAPI
from database.database import SQLAlchemyClient
import logging
from controller.calculator_controller import CalculatorController
from schemas.schemas import Request as RequestSchema
from service.calculator_service import CalculatorService
from router import api_router

app = FastAPI()
service = CalculatorService()
controller = CalculatorController(service)

logger = logging.getLogger("measurements")
logger.setLevel("DEBUG")


@app.on_event("startup")
async def start_up():
app.logger = logger

try:
app.database = SQLAlchemyClient()
app.logger.info("Postgres connection established")
except Exception as e:
app.logger.error(e)
app.logger.error("Could not connect to Postgres client")


@app.on_event("shutdown")
async def shutdown_db_client():
app.database.shutdown()
app.logger.info("Postgres shutdown succesfully")


@app.get("/")
async def root():
return {"message": "Hello World"}


@app.post("/sum")
async def calculator(request: RequestSchema):
return controller.handle_sum(request)


""" # Endpoint only for DB conection testing.
@app.post("/device-plant")
async def add_new_device_plant(req: Request,
device_plant: DevicePlantSchema = Body(...)):
try:
req.app.database.add_new(DevicePlant.from_pydantic(device_plant))
return req.app.database.find_device_plant(device_plant.id_device)
except Exception as e:
req.app.database.rollback()
raise e """

app.include_router(api_router)
Loading

0 comments on commit 34907a0

Please sign in to comment.