feat(backend): move pagination models to own file

This commit is contained in:
psychedelicious 2023-09-24 15:19:57 +10:00 committed by Kent Keirsey
parent 2a35d93a4d
commit 5048fc7c9e
15 changed files with 60 additions and 56 deletions

View File

@ -5,8 +5,8 @@ from fastapi.routing import APIRouter
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from invokeai.app.services.board_record_storage import BoardChanges from invokeai.app.services.board_record_storage import BoardChanges
from invokeai.app.services.image_record_storage import OffsetPaginatedResults
from invokeai.app.services.models.board_record import BoardDTO from invokeai.app.services.models.board_record import BoardDTO
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
from ..dependencies import ApiDependencies from ..dependencies import ApiDependencies

View File

@ -9,8 +9,8 @@ from pydantic import BaseModel, Field
from invokeai.app.invocations.metadata import ImageMetadata from invokeai.app.invocations.metadata import ImageMetadata
from invokeai.app.models.image import ImageCategory, ResourceOrigin from invokeai.app.models.image import ImageCategory, ResourceOrigin
from invokeai.app.services.image_record_storage import OffsetPaginatedResults
from invokeai.app.services.models.image_record import ImageDTO, ImageRecordChanges, ImageUrlsDTO from invokeai.app.services.models.image_record import ImageDTO, ImageRecordChanges, ImageUrlsDTO
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
from ..dependencies import ApiDependencies from ..dependencies import ApiDependencies

View File

@ -18,7 +18,7 @@ from invokeai.app.services.session_queue.session_queue_common import (
SessionQueueItemDTO, SessionQueueItemDTO,
SessionQueueStatus, SessionQueueStatus,
) )
from invokeai.app.services.shared.models import CursorPaginatedResults from invokeai.app.services.shared.pagination import CursorPaginatedResults
from ...services.graph import Graph from ...services.graph import Graph
from ..dependencies import ApiDependencies from ..dependencies import ApiDependencies

View File

@ -6,11 +6,12 @@ from fastapi import Body, HTTPException, Path, Query, Response
from fastapi.routing import APIRouter from fastapi.routing import APIRouter
from pydantic.fields import Field from pydantic.fields import Field
from invokeai.app.services.shared.pagination import PaginatedResults
# Importing * is bad karma but needed here for node detection # Importing * is bad karma but needed here for node detection
from ...invocations import * # noqa: F401 F403 from ...invocations import * # noqa: F401 F403
from ...invocations.baseinvocation import BaseInvocation from ...invocations.baseinvocation import BaseInvocation
from ...services.graph import Edge, EdgeConnection, Graph, GraphExecutionState, NodeAlreadyExecutedError from ...services.graph import Edge, EdgeConnection, Graph, GraphExecutionState, NodeAlreadyExecutedError
from ...services.item_storage import PaginatedResults
from ..dependencies import ApiDependencies from ..dependencies import ApiDependencies
session_router = APIRouter(prefix="/v1/sessions", tags=["sessions"]) session_router = APIRouter(prefix="/v1/sessions", tags=["sessions"])

View File

@ -3,9 +3,9 @@ import threading
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Optional, cast from typing import Optional, cast
from invokeai.app.services.image_record_storage import OffsetPaginatedResults
from invokeai.app.services.models.image_record import ImageRecord, deserialize_image_record from invokeai.app.services.models.image_record import ImageRecord, deserialize_image_record
from invokeai.app.services.shared.db import SqliteDatabase from invokeai.app.services.shared.db import SqliteDatabase
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
class BoardImageRecordStorageBase(ABC): class BoardImageRecordStorageBase(ABC):

View File

@ -5,9 +5,9 @@ from typing import Optional, Union, cast
from pydantic import BaseModel, Extra, Field from pydantic import BaseModel, Extra, Field
from invokeai.app.services.image_record_storage import OffsetPaginatedResults
from invokeai.app.services.models.board_record import BoardRecord, deserialize_board_record from invokeai.app.services.models.board_record import BoardRecord, deserialize_board_record
from invokeai.app.services.shared.db import SqliteDatabase from invokeai.app.services.shared.db import SqliteDatabase
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
from invokeai.app.util.misc import uuid_string from invokeai.app.util.misc import uuid_string

View File

@ -2,9 +2,9 @@ from abc import ABC, abstractmethod
from invokeai.app.services.board_images import board_record_to_dto from invokeai.app.services.board_images import board_record_to_dto
from invokeai.app.services.board_record_storage import BoardChanges from invokeai.app.services.board_record_storage import BoardChanges
from invokeai.app.services.image_record_storage import OffsetPaginatedResults
from invokeai.app.services.invoker import Invoker from invokeai.app.services.invoker import Invoker
from invokeai.app.services.models.board_record import BoardDTO from invokeai.app.services.models.board_record import BoardDTO
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
class BoardServiceABC(ABC): class BoardServiceABC(ABC):

View File

@ -3,27 +3,12 @@ import sqlite3
import threading import threading
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from datetime import datetime from datetime import datetime
from typing import Generic, Optional, TypeVar, cast from typing import Optional, cast
from pydantic import BaseModel, Field
from pydantic.generics import GenericModel
from invokeai.app.models.image import ImageCategory, ResourceOrigin from invokeai.app.models.image import ImageCategory, ResourceOrigin
from invokeai.app.services.models.image_record import ImageRecord, ImageRecordChanges, deserialize_image_record from invokeai.app.services.models.image_record import ImageRecord, ImageRecordChanges, deserialize_image_record
from invokeai.app.services.shared.db import SqliteDatabase from invokeai.app.services.shared.db import SqliteDatabase
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
T = TypeVar("T", bound=BaseModel)
class OffsetPaginatedResults(GenericModel, Generic[T]):
"""Offset-paginated results"""
# fmt: off
items: list[T] = Field(description="Items")
offset: int = Field(description="Offset from which to retrieve items")
limit: int = Field(description="Limit of items to get")
total: int = Field(description="Total number of items in result")
# fmt: on
# TODO: Should these excpetions subclass existing python exceptions? # TODO: Should these excpetions subclass existing python exceptions?

View File

@ -19,10 +19,10 @@ from invokeai.app.services.image_record_storage import (
ImageRecordDeleteException, ImageRecordDeleteException,
ImageRecordNotFoundException, ImageRecordNotFoundException,
ImageRecordSaveException, ImageRecordSaveException,
OffsetPaginatedResults,
) )
from invokeai.app.services.invoker import Invoker from invokeai.app.services.invoker import Invoker
from invokeai.app.services.models.image_record import ImageDTO, ImageRecord, ImageRecordChanges, image_record_to_dto from invokeai.app.services.models.image_record import ImageDTO, ImageRecord, ImageRecordChanges, image_record_to_dto
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
from invokeai.app.util.metadata import get_metadata_graph_from_raw_session from invokeai.app.util.metadata import get_metadata_graph_from_raw_session

View File

@ -1,24 +1,13 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Callable, Generic, Optional, TypeVar from typing import Callable, Generic, Optional, TypeVar
from pydantic import BaseModel, Field from pydantic import BaseModel
from pydantic.generics import GenericModel
from invokeai.app.services.shared.pagination import PaginatedResults
T = TypeVar("T", bound=BaseModel) T = TypeVar("T", bound=BaseModel)
class PaginatedResults(GenericModel, Generic[T]):
"""Paginated results"""
# fmt: off
items: list[T] = Field(description="Items")
page: int = Field(description="Current Page")
pages: int = Field(description="Total number of pages")
per_page: int = Field(description="Number of items per page")
total: int = Field(description="Total number of items in result")
# fmt: on
class ItemStorageABC(ABC, Generic[T]): class ItemStorageABC(ABC, Generic[T]):
_on_changed_callbacks: list[Callable[[T], None]] _on_changed_callbacks: list[Callable[[T], None]]
_on_deleted_callbacks: list[Callable[[str], None]] _on_deleted_callbacks: list[Callable[[str], None]]

View File

@ -18,7 +18,7 @@ from invokeai.app.services.session_queue.session_queue_common import (
SessionQueueItemDTO, SessionQueueItemDTO,
SessionQueueStatus, SessionQueueStatus,
) )
from invokeai.app.services.shared.models import CursorPaginatedResults from invokeai.app.services.shared.pagination import CursorPaginatedResults
class SessionQueueBase(ABC): class SessionQueueBase(ABC):

View File

@ -30,7 +30,7 @@ from invokeai.app.services.session_queue.session_queue_common import (
prepare_values_to_insert, prepare_values_to_insert,
) )
from invokeai.app.services.shared.db import SqliteDatabase from invokeai.app.services.shared.db import SqliteDatabase
from invokeai.app.services.shared.models import CursorPaginatedResults from invokeai.app.services.shared.pagination import CursorPaginatedResults
class SqliteSessionQueue(SessionQueueBase): class SqliteSessionQueue(SessionQueueBase):

View File

@ -1,14 +0,0 @@
from typing import Generic, TypeVar
from pydantic import BaseModel, Field
from pydantic.generics import GenericModel
GenericBaseModel = TypeVar("GenericBaseModel", bound=BaseModel)
class CursorPaginatedResults(GenericModel, Generic[GenericBaseModel]):
"""Cursor-paginated results"""
limit: int = Field(..., description="Limit of items to get")
has_more: bool = Field(..., description="Whether there are more items available")
items: list[GenericBaseModel] = Field(..., description="Items")

View File

@ -0,0 +1,42 @@
from typing import Generic, TypeVar
from pydantic import BaseModel, Field
from pydantic.generics import GenericModel
GenericBaseModel = TypeVar("GenericBaseModel", bound=BaseModel)
class CursorPaginatedResults(GenericModel, Generic[GenericBaseModel]):
"""
Cursor-paginated results
Generic must be a Pydantic model
"""
limit: int = Field(..., description="Limit of items to get")
has_more: bool = Field(..., description="Whether there are more items available")
items: list[GenericBaseModel] = Field(..., description="Items")
class OffsetPaginatedResults(GenericModel, Generic[GenericBaseModel]):
"""
Offset-paginated results
Generic must be a Pydantic model
"""
limit: int = Field(description="Limit of items to get")
offset: int = Field(description="Offset from which to retrieve items")
total: int = Field(description="Total number of items in result")
items: list[GenericBaseModel] = Field(description="Items")
class PaginatedResults(GenericModel, Generic[GenericBaseModel]):
"""
Paginated results
Generic must be a Pydantic model
"""
page: int = Field(description="Current Page")
pages: int = Field(description="Total number of pages")
per_page: int = Field(description="Number of items per page")
total: int = Field(description="Total number of items in result")
items: list[GenericBaseModel] = Field(description="Items")

View File

@ -5,8 +5,9 @@ from typing import Generic, Optional, TypeVar, get_args
from pydantic import BaseModel, parse_raw_as from pydantic import BaseModel, parse_raw_as
from invokeai.app.services.shared.db import SqliteDatabase from invokeai.app.services.shared.db import SqliteDatabase
from invokeai.app.services.shared.pagination import PaginatedResults
from .item_storage import ItemStorageABC, PaginatedResults from .item_storage import ItemStorageABC
T = TypeVar("T", bound=BaseModel) T = TypeVar("T", bound=BaseModel)