Skip to content
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

Improved performance of Rom DB fetch #883

Merged
merged 7 commits into from
May 26, 2024
Merged
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
1 change: 0 additions & 1 deletion backend/endpoints/responses/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class ScreenshotSchema(BaseAsset):
class UploadedScreenshotsResponse(TypedDict):
uploaded: int
screenshots: list[ScreenshotSchema]
url_screenshots: list[str]
merged_screenshots: list[str]


Expand Down
19 changes: 10 additions & 9 deletions backend/endpoints/responses/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,8 @@ class RomSchema(BaseModel):

multi: bool
files: list[str]
url_screenshots: list[str]
merged_screenshots: list[str]
full_path: str

sibling_roms: list["RomSchema"] = Field(default_factory=list)
user_saves: list[SaveSchema] = Field(default_factory=list)
user_states: list[StateSchema] = Field(default_factory=list)
user_screenshots: list[ScreenshotSchema] = Field(default_factory=list)
user_notes: list[RomNoteSchema] = Field(default_factory=list)

class Config:
from_attributes = True

Expand All @@ -119,8 +111,17 @@ def sort_comparator(self) -> str:
.lower()
)


class DetailedRomSchema(RomSchema):
merged_screenshots: list[str]
sibling_roms: list["RomSchema"] = Field(default_factory=list)
user_saves: list[SaveSchema] = Field(default_factory=list)
user_states: list[StateSchema] = Field(default_factory=list)
user_screenshots: list[ScreenshotSchema] = Field(default_factory=list)
user_notes: list[RomNoteSchema] = Field(default_factory=list)

@classmethod
def from_orm_with_request(cls, db_rom: Rom, request: Request) -> "RomSchema":
def from_orm_with_request(cls, db_rom: Rom, request: Request) -> "DetailedRomSchema":
rom = cls.model_validate(db_rom)
user_id = request.user.id

Expand Down
1 change: 0 additions & 1 deletion backend/endpoints/responses/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ class SearchRomSchema(BaseModel):
summary: str
igdb_url_cover: str = ""
moby_url_cover: str = ""
url_screenshots: list[str]
13 changes: 7 additions & 6 deletions backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
AddRomsResponse,
CustomStreamingResponse,
RomSchema,
DetailedRomSchema,
RomNoteSchema,
)
from exceptions.fs_exceptions import RomAlreadyExistsException
Expand Down Expand Up @@ -119,17 +120,17 @@ def get_roms(
"/roms/{id}",
[] if DISABLE_DOWNLOAD_ENDPOINT_AUTH else ["roms.read"],
)
def get_rom(request: Request, id: int) -> RomSchema:
def get_rom(request: Request, id: int) -> DetailedRomSchema:
"""Get rom endpoint

Args:
request (Request): Fastapi Request object
id (int): Rom internal id

Returns:
RomSchema: Rom stored in the database
DetailedRomSchema: Rom stored in the database
"""
return RomSchema.from_orm_with_request(db_rom_handler.get_roms(id), request)
return DetailedRomSchema.from_orm_with_request(db_rom_handler.get_roms(id), request)


@protected_route(
Expand Down Expand Up @@ -248,7 +249,7 @@ async def update_rom(
rename_as_igdb: bool = False,
remove_cover: bool = False,
artwork: Optional[UploadFile] = File(None),
) -> RomSchema:
) -> DetailedRomSchema:
"""Update rom endpoint

Args:
Expand All @@ -261,7 +262,7 @@ async def update_rom(
HTTPException: If a rom already have that name when enabling the rename_as_igdb flag

Returns:
RomSchema: Rom stored in the database
DetailedRomSchema: Rom stored in the database
"""

data = await request.form()
Expand Down Expand Up @@ -375,7 +376,7 @@ async def update_rom(

db_rom_handler.update_rom(id, cleaned_data)

return db_rom_handler.get_roms(id)
return DetailedRomSchema.from_orm_with_request(db_rom_handler.get_roms(id), request)


@protected_route(router.post, "/roms/delete", ["roms.write"])
Expand Down
1 change: 0 additions & 1 deletion backend/endpoints/screenshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ def add_screenshots(
return {
"uploaded": len(screenshots),
"screenshots": [s for s in rom.screenshots if s.user_id == current_user.id],
"url_screenshots": rom.url_screenshots,
"merged_screenshots": rom.merged_screenshots,
}
1 change: 0 additions & 1 deletion backend/endpoints/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ async def search_rom(
"summary": "",
"igdb_url_cover": "",
"moby_url_cover": "",
"url_screenshots": [],
},
**item,
}
Expand Down
7 changes: 0 additions & 7 deletions backend/endpoints/tests/test_rom.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from fastapi.testclient import TestClient
from unittest.mock import patch

Expand Down Expand Up @@ -45,12 +44,6 @@ def test_update_rom(rename_file_mock, get_rom_by_id_mock, access_token, rom):
"file_name": "Metroid Prime Remastered.zip",
"summary": "summary test",
"url_cover": "https://images.igdb.com/igdb/image/upload/t_cover_big/co2l7z.jpg",
"url_screenshots": json.dumps(
[
"https://images.igdb.com/igdb/image/upload/t_original/qhiqlmwvvuaqxxn4cxlr.jpg",
"https://images.igdb.com/igdb/image/upload/t_original/kqkixazzsokqgoxmuish.jpg",
]
),
"genres": '[{"id": 5, "name": "Shooter"}, {"id": 8, "name": "Platform"}, {"id": 31, "name": "Adventure"}]',
"franchises": '[{"id": 756, "name": "Metroid"}]',
"collections": '[{"id": 243, "name": "Metroid"}, {"id": 6240, "name": "Metroid Prime"}]',
Expand Down
18 changes: 12 additions & 6 deletions backend/models/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
DateTime,
func,
UniqueConstraint,
select,
and_,
or_,
)
from sqlalchemy.orm import Mapped, relationship

Expand Down Expand Up @@ -108,14 +111,17 @@ def merged_screenshots(self) -> list[str]:
def get_sibling_roms(self) -> list["Rom"]:
from handler.database import db_rom_handler

if not self.igdb_id:
return []

with db_rom_handler.session.begin() as session:
return session.scalars(
db_rom_handler.get_roms(platform_id=self.platform_id).filter(
Rom.id != self.id,
Rom.igdb_id == self.igdb_id,
select(Rom).where(
and_(
Rom.platform_id == self.platform_id,
Rom.id != self.id,
or_(
and_(Rom.igdb_id == self.igdb_id, Rom.igdb_id != None), # noqa
and_(Rom.moby_id == self.moby_id, Rom.moby_id != None), # noqa
),
)
)
).all()

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import socket from "@/services/socket";
import storeConfig from "@/stores/config";
import storeGalleryFilter from "@/stores/galleryFilter";
import storeHeartbeat from "@/stores/heartbeat";
import storeRoms, { type Rom } from "@/stores/roms";
import storeRoms, { type SimpleRom } from "@/stores/roms";
import storePlatforms from "@/stores/platforms";
import storeAuth from "@/stores/auth";
import storeScanning from "@/stores/scanning";
Expand Down Expand Up @@ -42,7 +42,7 @@ socket.on(
}
);

socket.on("scan:scanning_rom", (rom: Rom) => {
socket.on("scan:scanning_rom", (rom: SimpleRom) => {
scanningStore.set(true);
if (romsStore.platformID === rom.platform_id) {
romsStore.add([rom]);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/__generated__/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/AddFirmwareResponse.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/AddRomsResponse.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/Body_token_token_post.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/ConfigResponse.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/CursorPage_RomSchema_.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions frontend/src/__generated__/models/DetailedRomSchema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/FirmwareSchema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/HTTPValidationError.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/HeartbeatResponse.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/IGDBPlatform.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/IGDBRelatedGame.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/MessageResponse.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/MetadataSourcesDict.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/MobyGamesPlatform.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frontend/src/__generated__/models/PlatformSchema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/RomIGDBMetadata.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/src/__generated__/models/RomMobyMetadata.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/src/__generated__/models/RomNoteSchema.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading