feat: refactor services folder/module structure
Refactor services folder/module structure.
**Motivation**
While working on our services I've repeatedly encountered circular imports and a general lack of clarity regarding where to put things. The structure introduced goes a long way towards resolving those issues, setting us up for a clean structure going forward.
**Services**
Services are now in their own folder with a few files:
- `services/{service_name}/__init__.py`: init as needed, mostly empty now
- `services/{service_name}/{service_name}_base.py`: the base class for the service
- `services/{service_name}/{service_name}_{impl_type}.py`: the default concrete implementation of the service - typically one of `sqlite`, `default`, or `memory`
- `services/{service_name}/{service_name}_common.py`: any common items - models, exceptions, utilities, etc
Though it's a bit verbose to have the service name both as the folder name and the prefix for files, I found it is _extremely_ confusing to have all of the base classes just be named `base.py`. So, at the cost of some verbosity when importing things, I've included the service name in the filename.
There are some minor logic changes. For example, in `InvocationProcessor`, instead of assigning the model manager service to a variable to be used later in the file, the service is used directly via the `Invoker`.
**Shared**
Things that are used across disparate services are in `services/shared/`:
- `default_graphs.py`: previously in `services/`
- `graphs.py`: previously in `services/`
- `paginatation`: generic pagination models used in a few services
- `sqlite`: the `SqliteDatabase` class, other sqlite-specific things
2023-09-24 08:11:07 +00:00
|
|
|
from invokeai.app.services.board_records.board_records_common import BoardChanges
|
|
|
|
from invokeai.app.services.boards.boards_common import BoardDTO
|
2023-09-24 05:12:51 +00:00
|
|
|
from invokeai.app.services.invoker import Invoker
|
2023-09-24 05:19:57 +00:00
|
|
|
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
|
2023-06-14 14:07:20 +00:00
|
|
|
|
feat: refactor services folder/module structure
Refactor services folder/module structure.
**Motivation**
While working on our services I've repeatedly encountered circular imports and a general lack of clarity regarding where to put things. The structure introduced goes a long way towards resolving those issues, setting us up for a clean structure going forward.
**Services**
Services are now in their own folder with a few files:
- `services/{service_name}/__init__.py`: init as needed, mostly empty now
- `services/{service_name}/{service_name}_base.py`: the base class for the service
- `services/{service_name}/{service_name}_{impl_type}.py`: the default concrete implementation of the service - typically one of `sqlite`, `default`, or `memory`
- `services/{service_name}/{service_name}_common.py`: any common items - models, exceptions, utilities, etc
Though it's a bit verbose to have the service name both as the folder name and the prefix for files, I found it is _extremely_ confusing to have all of the base classes just be named `base.py`. So, at the cost of some verbosity when importing things, I've included the service name in the filename.
There are some minor logic changes. For example, in `InvocationProcessor`, instead of assigning the model manager service to a variable to be used later in the file, the service is used directly via the `Invoker`.
**Shared**
Things that are used across disparate services are in `services/shared/`:
- `default_graphs.py`: previously in `services/`
- `graphs.py`: previously in `services/`
- `paginatation`: generic pagination models used in a few services
- `sqlite`: the `SqliteDatabase` class, other sqlite-specific things
2023-09-24 08:11:07 +00:00
|
|
|
from .boards_base import BoardServiceABC
|
|
|
|
from .boards_common import board_record_to_dto
|
2023-06-20 12:51:05 +00:00
|
|
|
|
2023-06-13 18:51:20 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
class BoardService(BoardServiceABC):
|
2023-09-24 05:12:51 +00:00
|
|
|
__invoker: Invoker
|
2023-06-14 14:07:20 +00:00
|
|
|
|
2023-09-24 05:12:51 +00:00
|
|
|
def start(self, invoker: Invoker) -> None:
|
|
|
|
self.__invoker = invoker
|
2023-06-14 14:07:20 +00:00
|
|
|
|
|
|
|
def create(
|
2023-06-13 18:51:20 +00:00
|
|
|
self,
|
|
|
|
board_name: str,
|
2023-06-14 14:07:20 +00:00
|
|
|
) -> BoardDTO:
|
2023-09-24 05:12:51 +00:00
|
|
|
board_record = self.__invoker.services.board_records.save(board_name)
|
2023-06-14 14:07:20 +00:00
|
|
|
return board_record_to_dto(board_record, None, 0)
|
|
|
|
|
|
|
|
def get_dto(self, board_id: str) -> BoardDTO:
|
2023-09-24 05:12:51 +00:00
|
|
|
board_record = self.__invoker.services.board_records.get(board_id)
|
|
|
|
cover_image = self.__invoker.services.image_records.get_most_recent_image_for_board(board_record.board_id)
|
2023-06-20 12:51:05 +00:00
|
|
|
if cover_image:
|
2023-06-16 17:21:55 +00:00
|
|
|
cover_image_name = cover_image.image_name
|
|
|
|
else:
|
|
|
|
cover_image_name = None
|
2023-09-24 05:12:51 +00:00
|
|
|
image_count = self.__invoker.services.board_image_records.get_image_count_for_board(board_id)
|
2023-06-16 17:21:55 +00:00
|
|
|
return board_record_to_dto(board_record, cover_image_name, image_count)
|
2023-06-13 19:56:09 +00:00
|
|
|
|
2023-06-14 14:07:20 +00:00
|
|
|
def update(
|
|
|
|
self,
|
|
|
|
board_id: str,
|
|
|
|
changes: BoardChanges,
|
|
|
|
) -> BoardDTO:
|
2023-09-24 05:12:51 +00:00
|
|
|
board_record = self.__invoker.services.board_records.update(board_id, changes)
|
|
|
|
cover_image = self.__invoker.services.image_records.get_most_recent_image_for_board(board_record.board_id)
|
2023-06-20 12:51:05 +00:00
|
|
|
if cover_image:
|
2023-06-16 17:21:55 +00:00
|
|
|
cover_image_name = cover_image.image_name
|
|
|
|
else:
|
|
|
|
cover_image_name = None
|
2023-06-20 12:51:05 +00:00
|
|
|
|
2023-09-24 05:12:51 +00:00
|
|
|
image_count = self.__invoker.services.board_image_records.get_image_count_for_board(board_id)
|
2023-06-16 17:21:55 +00:00
|
|
|
return board_record_to_dto(board_record, cover_image_name, image_count)
|
2023-06-14 14:07:20 +00:00
|
|
|
|
|
|
|
def delete(self, board_id: str) -> None:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.board_records.delete(board_id)
|
2023-06-13 21:08:04 +00:00
|
|
|
|
2024-06-26 18:03:03 +00:00
|
|
|
def get_many(self, offset: int = 0, limit: int = 10, archived: bool = False) -> OffsetPaginatedResults[BoardDTO]:
|
|
|
|
board_records = self.__invoker.services.board_records.get_many(offset, limit, archived)
|
2023-06-14 14:07:20 +00:00
|
|
|
board_dtos = []
|
|
|
|
for r in board_records.items:
|
2023-09-24 05:12:51 +00:00
|
|
|
cover_image = self.__invoker.services.image_records.get_most_recent_image_for_board(r.board_id)
|
2023-06-20 12:51:05 +00:00
|
|
|
if cover_image:
|
2023-06-16 17:21:55 +00:00
|
|
|
cover_image_name = cover_image.image_name
|
2023-06-15 15:31:14 +00:00
|
|
|
else:
|
2023-06-16 17:21:55 +00:00
|
|
|
cover_image_name = None
|
2023-06-15 15:31:14 +00:00
|
|
|
|
2023-09-24 05:12:51 +00:00
|
|
|
image_count = self.__invoker.services.board_image_records.get_image_count_for_board(r.board_id)
|
2023-06-16 17:21:55 +00:00
|
|
|
board_dtos.append(board_record_to_dto(r, cover_image_name, image_count))
|
2023-06-14 14:07:20 +00:00
|
|
|
|
|
|
|
return OffsetPaginatedResults[BoardDTO](items=board_dtos, offset=offset, limit=limit, total=len(board_dtos))
|
2023-06-20 12:51:05 +00:00
|
|
|
|
2024-06-26 18:03:03 +00:00
|
|
|
def get_all(self, archived: bool = False) -> list[BoardDTO]:
|
|
|
|
board_records = self.__invoker.services.board_records.get_all(archived)
|
2023-06-20 12:51:05 +00:00
|
|
|
board_dtos = []
|
|
|
|
for r in board_records:
|
2023-09-24 05:12:51 +00:00
|
|
|
cover_image = self.__invoker.services.image_records.get_most_recent_image_for_board(r.board_id)
|
2023-06-20 12:51:05 +00:00
|
|
|
if cover_image:
|
|
|
|
cover_image_name = cover_image.image_name
|
|
|
|
else:
|
|
|
|
cover_image_name = None
|
|
|
|
|
2023-09-24 05:12:51 +00:00
|
|
|
image_count = self.__invoker.services.board_image_records.get_image_count_for_board(r.board_id)
|
2023-06-20 12:51:05 +00:00
|
|
|
board_dtos.append(board_record_to_dto(r, cover_image_name, image_count))
|
|
|
|
|
|
|
|
return board_dtos
|