Skip to content

Commit

Permalink
♻️ refactor: sensors records and checklist records views making them …
Browse files Browse the repository at this point in the history
…to use use cases from factories
  • Loading branch information
JohnPetros committed Jul 4, 2024
1 parent f122331 commit 436b412
Show file tree
Hide file tree
Showing 42 changed files with 270 additions and 227 deletions.
6 changes: 3 additions & 3 deletions src/app/core/errors/forms/invalid_form_data_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

@dataclass
class InvalidFormDataError(BaseError):
ui_message = "Formulário inválido"
internal_message = "Invalid form data"
status_code = 400
ui_message: str = "Formulário inválido"
internal_message: str = "Invalid form data"
status_code: int = 400
6 changes: 3 additions & 3 deletions src/app/core/errors/validation/date_not_valid_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

@dataclass
class DateNotValidError(BaseError):
ui_message = "Valor de data não válido"
internal_message = "Date value is not valid"
status_code = 400
ui_message: str = "Valor de data não válido"
internal_message: str = "Date value is not valid"
status_code: int = 400
6 changes: 3 additions & 3 deletions src/app/core/errors/validation/datetime_not_valid_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

@dataclass
class DatetimeNotValidError(BaseError):
ui_message = "Valor de data ou hora mal formatado"
internal_message = "Datetime value is not valid"
status_code = 400
ui_message: str = "Valor de data ou hora mal formatado"
internal_message: str = "Datetime value is not valid"
status_code: int = 400
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(
):
self._checklist_records_repository = checklist_records_repository

def execute(self, checklist_records_ids: list[str]) -> None:
def execute(self, checklist_records_ids: list[str]):
for id in checklist_records_ids:
if not isinstance(id, str):
raise ChecklistRecordNotValidError()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import date

from core.commons import RecordsFilters, Error
from core.commons import RecordsFilters
from core.interfaces.repositories import SensorRecordsRepositoryInterface
from core.interfaces.providers import DataAnalyserProviderInterface
from core.constants import CSV_FILE_COLUMNS
Expand All @@ -16,21 +16,18 @@ def __init__(
self._checklist_records_repository = checklist_records_repository

def execute(self, plant_id: str, start_date: date, end_date: date, folder: str):
try:
filters = RecordsFilters(
plant_id=plant_id, start_date=start_date, end_date=end_date
)
filters = RecordsFilters(
plant_id=plant_id, start_date=start_date, end_date=end_date
)

data = self.__get_data(filters)
data = self.__get_data(filters)

csv_filename = "registros-checklist.xlsx"
csv_filename = "registros-checklist.xlsx"

self._data_analyser_provider.analyse(data)
self._data_analyser_provider.convert_to_excel(folder, csv_filename)
self._data_analyser_provider.analyse(data)
self._data_analyser_provider.convert_to_excel(folder, csv_filename)

return csv_filename
except Error as error:
raise error
return csv_filename

def __get_data(self, filters: RecordsFilters):
checklist_records = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def execute(
pagination.get_current_and_last_page_numbers()
)

print(checklist_records_count, flush=True)

checklist_records = (
self._checklist_records_repository.get_filtered_checklist_records(
page_number=current_page_number,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, date
from datetime import datetime, date, time

from core.entities.sensors_record import SensorsRecord, Plant
from core.interfaces.repositories import (
Expand All @@ -20,7 +20,7 @@ def __init__(
self._plants_repository = plants_repository

def execute(self, request: dict):
if "time" not in request or not isinstance(request["time"], datetime):
if "time" not in request or not isinstance(request["time"], time):
raise DatetimeNotValidError()

if "date" not in request or not isinstance(request["date"], date):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def __init__(

def execute(self, sensors_records_ids: list[str]) -> None:
for id in sensors_records_ids:
print(isinstance(id, str))
if not isinstance(id, str):
raise SensorsRecordNotValidError()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ def __init__(
self._sensors_records_repository = sensors_records_repository

def execute(self, plant_id: str, start_date: date, end_date: date, folder: str):
filters = RecordsFilters(
plant_id=plant_id, start_date=start_date, end_date=end_date
)
try:
filters = RecordsFilters(
plant_id=plant_id, start_date=start_date, end_date=end_date
)

data = self.__get_data(filters)
data = self.__get_data(filters)

csv_filename = "registros-dos-sensores.xlsx"
csv_filename = "registros-dos-sensores.xlsx"

self._data_analyser_provider.analyse(data)
self._data_analyser_provider.convert_to_excel(folder, csv_filename)
self._data_analyser_provider.analyse(data)
self._data_analyser_provider.convert_to_excel(folder, csv_filename)

return csv_filename
return csv_filename
except Exception as error:
print(error, flush=True)

def __get_data(self, filters: RecordsFilters):
sensors_records = self._sensors_records_repository.get_filtered_sensors_records(
Expand All @@ -41,6 +44,8 @@ def __get_data(self, filters: RecordsFilters):
columns = CSV_FILE_COLUMNS["sensors_records"]
data = {column: [] for column in columns}

print(sensors_records, flush=True)

if len(sensors_records) == 0:
return data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def execute(self):
plants = self._plants_repository.get_plants()

if len(plants) == 0:
raise PlantNotFoundError(ui_message="Nenhuma planta encontrada")
raise PlantNotFoundError("Nenhuma planta encontrada")

active_plant_id = self._users_repository.get_user_active_plant_id(
ADMIN_USER_EMAIL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from core.commons import Pagination, Error, RecordsFilters
from core.commons import Pagination, RecordsFilters
from core.interfaces.repositories import (
PlantsRepositoryInterface,
SensorRecordsRepositoryInterface,
Expand All @@ -23,38 +23,38 @@ def execute(
page_number: int = 1,
should_get_plants: bool = False,
) -> tuple[list[SensorsRecord], int, list[Plant]]:
try:
plants = []
if should_get_plants:
plants = self._plants_repository.get_plants()

filters = RecordsFilters(
plant_id=plant_id, start_date=start_date, end_date=end_date
)

records_count = self._sensors_records_repository.get_sensors_records_count()

pagination = Pagination(page_number, records_count)

current_page_number, last_page_number = (
pagination.get_current_and_last_page_numbers()
)

sensors_records = (
self._sensors_records_repository.get_filtered_sensors_records(
page_number=current_page_number,
plant_id=filters.plant_id,
start_date=filters.start_date,
end_date=filters.end_date,
)
)

return {
"sensors_records": sensors_records,
"plants": plants,
"last_page_number": last_page_number,
"current_page_number": current_page_number,
}

except Error as error:
return error
plants = []
if should_get_plants:
plants = self._plants_repository.get_plants()

filters = RecordsFilters(
plant_id=plant_id, start_date=start_date, end_date=end_date
)

records_count = self._sensors_records_repository.get_sensors_records_count(
plant_id=filters.plant_id,
start_date=filters.start_date,
end_date=filters.end_date,
)

pagination = Pagination(page_number, records_count)

current_page_number, last_page_number = (
pagination.get_current_and_last_page_numbers()
)

print(current_page_number, flush=True)

sensors_records = self._sensors_records_repository.get_filtered_sensors_records(
page_number=current_page_number,
plant_id=filters.plant_id,
start_date=filters.start_date,
end_date=filters.end_date,
)

return {
"sensors_records": sensors_records,
"plants": plants,
"last_page_number": last_page_number,
"current_page_number": current_page_number,
}
13 changes: 10 additions & 3 deletions src/app/core/use_cases/sensors_records/update_sensors_record.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from datetime import datetime, date
from datetime import datetime, date, time

from core.entities import SensorsRecord
from core.commons import Datetime, Weekday
from core.errors.validation import SensorsRecordNotValidError, DatetimeNotValidError
from core.errors.validation import (
SensorsRecordNotValidError,
DatetimeNotValidError,
DateNotValidError,
)
from core.errors.sensors_records import SensorsRecordNotFoundError
from core.errors.plants import PlantNotFoundError
from core.interfaces.repositories import (
Expand Down Expand Up @@ -35,9 +39,12 @@ def execute(self, request: dict) -> None:
if not has_sensors_record:
raise SensorsRecordNotFoundError()

if not isinstance(request["date"], date):
if "time" not in request or not isinstance(request["time"], time):
raise DatetimeNotValidError()

if "date" not in request or not isinstance(request["date"], date):
raise DateNotValidError()

created_at = Datetime(
datetime(
hour=request["time"].hour,
Expand Down
26 changes: 26 additions & 0 deletions src/app/infra/factories/use_cases/checklist_records/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from .create_checklist_records_by_csv_file_factory import (
CreateChecklistRecordsByCsvFileFactory,
)
from .create_checklist_record_by_form_factory import CreateChecklistRecordByFormFactory
from .delete_checklist_records_factory import DeleteChecklistRecordsFactory
from .get_checklist_records_csv_file_factory import GetChecklistRecordsCsvFileFactory
from .get_checklist_records_dashboard_page_data_factory import (
GetChecklistRecordsDashboardPageDataFactory,
)
from .get_checklist_records_table_page_data_factory import (
GetChecklistRecordsTablePageDataFactory,
)
from .update_checklist_records_factory import UpdateChecklistRecordFactory


create_checklist_records_by_csv_file = CreateChecklistRecordsByCsvFileFactory.produce()
create_checklist_record_by_form = CreateChecklistRecordByFormFactory.produce()
delete_checklist_records = DeleteChecklistRecordsFactory.produce()
get_checklist_records_csv_file = GetChecklistRecordsCsvFileFactory.produce()
get_checklist_records_dashboard_page_data = (
GetChecklistRecordsDashboardPageDataFactory.produce()
)
get_checklist_records_table_page_data = (
GetChecklistRecordsTablePageDataFactory.produce()
)
update_checklist_record = UpdateChecklistRecordFactory.produce()

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from core.use_cases.checklist_records import CreateChecklistRecordByForm

from infra.repositories import checklist_records_repository
from infra.repositories import checklist_records_repository, plants_repository


class CreateChecklistRecordByFormFactory:
@staticmethod
def produce():
return CreateChecklistRecordByForm(checklist_records_repository)
return CreateChecklistRecordByForm(
checklist_records_repository=checklist_records_repository,
plants_repository=plants_repository,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from core.use_cases.checklist_records import CreateChecklistRecordsByCsvFile

from infra.repositories import plants_repository, checklist_records_repository
from infra.providers import DataAnalyserProvider


class CreateChecklistRecordsByCsvFileFactory:
@staticmethod
def produce():
data_analyser_provider = DataAnalyserProvider()

return CreateChecklistRecordsByCsvFile(
plants_repository=plants_repository,
checklist_records_repository=checklist_records_repository,
data_analyser_provider=data_analyser_provider,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from core.use_cases.checklist_records import GetChecklistRecordsCsvFile

from infra.repositories import (
sensors_records_repository,
checklist_records_repository,
)
from infra.providers.data_analyser_provider import DataAnalyserProvider

Expand All @@ -12,6 +12,6 @@ def produce():
data_analyser_provider = DataAnalyserProvider()

return GetChecklistRecordsCsvFile(
sensors_records_repository=sensors_records_repository,
checklist_records_repository=checklist_records_repository,
data_analyser_provider=data_analyser_provider,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from infra.repositories import (
plants_repository,
users_repository,
sensors_records_repository,
checklist_records_repository,
)


Expand All @@ -13,5 +13,5 @@ def produce():
return GetChecklistRecordsDashboardPageData(
plants_repository=plants_repository,
users_repository=users_repository,
sensors_records_repository=sensors_records_repository,
checklist_records_repository=checklist_records_repository,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from infra.repositories import (
plants_repository,
users_repository,
sensors_records_repository,
checklist_records_repository,
)


Expand All @@ -12,6 +11,5 @@ class GetChecklistRecordsTablePageDataFactory:
def produce():
return GetChecklistRecordsTablePageData(
plants_repository=plants_repository,
users_repository=users_repository,
sensors_records_repository=sensors_records_repository,
checklist_records_repository=checklist_records_repository,
)
Loading

0 comments on commit 436b412

Please sign in to comment.