From 66510c0327758c84ada7ec454265423f79f65c0a Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sat, 18 May 2024 17:18:15 -0400 Subject: [PATCH 1/8] Improve fetch times of platforms endpoint --- backend/config/__init__.py | 3 ++- backend/endpoints/platform.py | 4 +--- backend/endpoints/responses/platform.py | 14 ++---------- backend/models/platform.py | 13 +++++------ docker/init_scripts/init | 2 +- env.template | 3 +++ .../__generated__/models/PlatformSchema.ts | 2 +- frontend/src/components/Details/Emulation.vue | 2 +- .../Dialog/Platform/ViewFirmware.vue | 22 +++++++++---------- 9 files changed, 28 insertions(+), 37 deletions(-) diff --git a/backend/config/__init__.py b/backend/config/__init__.py index a4e7296f3..064f94254 100644 --- a/backend/config/__init__.py +++ b/backend/config/__init__.py @@ -6,10 +6,11 @@ load_dotenv() -# UVICORN +# GUNICORN DEV_PORT: Final = int(os.environ.get("VITE_BACKEND_DEV_PORT", "5000")) DEV_HOST: Final = "0.0.0.0" ROMM_HOST: Final = os.environ.get("ROMM_HOST", DEV_HOST) +GUNICORN_WORKERS: Final = int(os.environ.get("GUNICORN_WORKERS", 2)) # PATHS ROMM_BASE_PATH: Final = os.environ.get("ROMM_BASE_PATH", "/romm") diff --git a/backend/endpoints/platform.py b/backend/endpoints/platform.py index d0b1da142..96327afe3 100644 --- a/backend/endpoints/platform.py +++ b/backend/endpoints/platform.py @@ -88,9 +88,7 @@ def get_platform(request: Request, id: int) -> PlatformSchema: PlatformSchema: Platform """ - return PlatformSchema.from_orm_with_request( - db_platform_handler.get_platforms(id), request - ) + return db_platform_handler.get_platforms(id) @protected_route(router.put, "/platforms/{id}", ["platforms.write"]) diff --git a/backend/endpoints/responses/platform.py b/backend/endpoints/responses/platform.py index f1810e2a0..7cea12f84 100644 --- a/backend/endpoints/responses/platform.py +++ b/backend/endpoints/responses/platform.py @@ -1,9 +1,8 @@ from typing import Optional from pydantic import BaseModel, Field -from fastapi import Request -from models.platform import Platform from .firmware import FirmwareSchema + class PlatformSchema(BaseModel): id: int slug: str @@ -14,16 +13,7 @@ class PlatformSchema(BaseModel): name: str logo_path: Optional[str] = "" rom_count: int - - firmware_files: list[FirmwareSchema] = Field(default_factory=list) + firmware: list[FirmwareSchema] = Field(default_factory=list) class Config: from_attributes = True - - @classmethod - def from_orm_with_request(cls, db_platform: Platform, request: Request) -> "PlatformSchema": - platform = cls.model_validate(db_platform) - platform.firmware_files = [ - FirmwareSchema.model_validate(f) for f in sorted(db_platform.firmware, key=lambda x: x.file_name) - ] - return platform diff --git a/backend/models/platform.py b/backend/models/platform.py index 8709b407a..210c73396 100644 --- a/backend/models/platform.py +++ b/backend/models/platform.py @@ -1,8 +1,9 @@ +from sqlalchemy import Column, Integer, String, select, func +from sqlalchemy.orm import Mapped, relationship, column_property + from models.base import BaseModel from models.rom import Rom from models.firmware import Firmware -from sqlalchemy import Column, Integer, String -from sqlalchemy.orm import Mapped, relationship class Platform(BaseModel): @@ -24,11 +25,9 @@ class Platform(BaseModel): "Firmware", lazy="selectin", back_populates="platform" ) - @property - def rom_count(self) -> int: - from handler.database import db_platform_handler - - return db_platform_handler.get_rom_count(self.id) + rom_count = column_property( + select(func.count(Rom.id)).where(Rom.platform_id == id).scalar_subquery() + ) def __repr__(self) -> str: return self.name diff --git a/docker/init_scripts/init b/docker/init_scripts/init index ff0a3be14..d0437a983 100755 --- a/docker/init_scripts/init +++ b/docker/init_scripts/init @@ -46,7 +46,7 @@ start_bin_gunicorn () { --bind=0.0.0.0:5000 \ --bind=unix:/tmp/gunicorn.sock \ --pid=/tmp/gunicorn.pid \ - --workers 2 \ + --workers ${GUNICORN_WORKERS:=2} \ main:app & } diff --git a/env.template b/env.template index 87caeaa07..068a21e0c 100644 --- a/env.template +++ b/env.template @@ -1,6 +1,9 @@ ROMM_BASE_PATH=/path/to/romm_mock VITE_BACKEND_DEV_PORT=5000 + +# Gunicorn (optional) ROMM_HOST=localhost +GUNICORN_WORKERS=4 # (2 × CPU cores) + 1 # IGDB credentials IGDB_CLIENT_ID= diff --git a/frontend/src/__generated__/models/PlatformSchema.ts b/frontend/src/__generated__/models/PlatformSchema.ts index 97fdcc252..43183ff22 100644 --- a/frontend/src/__generated__/models/PlatformSchema.ts +++ b/frontend/src/__generated__/models/PlatformSchema.ts @@ -15,6 +15,6 @@ export type PlatformSchema = { name: string; logo_path?: (string | null); rom_count: number; - firmware_files?: Array; + firmware?: Array; }; diff --git a/frontend/src/components/Details/Emulation.vue b/frontend/src/components/Details/Emulation.vue index 530b9eecf..915005a6f 100644 --- a/frontend/src/components/Details/Emulation.vue +++ b/frontend/src/components/Details/Emulation.vue @@ -44,7 +44,7 @@ function onFullScreenChange() { label="BIOS" v-model="biosRef" :items=" - props.platform.firmware_files?.map((f) => ({ + props.platform.firmware?.map((f) => ({ title: f.file_name, value: f, })) ?? [] diff --git a/frontend/src/components/Dialog/Platform/ViewFirmware.vue b/frontend/src/components/Dialog/Platform/ViewFirmware.vue index 3bb5139c9..e16ebe90d 100644 --- a/frontend/src/components/Dialog/Platform/ViewFirmware.vue +++ b/frontend/src/components/Dialog/Platform/ViewFirmware.vue @@ -45,7 +45,7 @@ function uploadFirmware() { .then(({ data }) => { const { uploaded, firmware } = data; if (selectedPlatform.value) { - selectedPlatform.value.firmware_files = firmware; + selectedPlatform.value.firmware = firmware; } emitter?.emit("snackbarShow", { @@ -75,8 +75,8 @@ function deleteFirmware() { .deleteFirmware({ firmware: selectedFirmware.value, deleteFromFs: false }) .then(() => { if (selectedPlatform.value) { - selectedPlatform.value.firmware_files = - selectedPlatform.value.firmware_files?.filter( + selectedPlatform.value.firmware = + selectedPlatform.value.firmware?.filter( (firmware) => !selectedFirmware.value.includes(firmware) ); } @@ -91,12 +91,12 @@ function deleteFirmware() { } function allFirmwareSelected() { - if (selectedPlatform.value?.firmware_files?.length == 0) { + if (selectedPlatform.value?.firmware?.length == 0) { return false; } return ( selectedFirmware.value.length === - selectedPlatform.value?.firmware_files?.length + selectedPlatform.value?.firmware?.length ); } @@ -104,7 +104,7 @@ function selectAllFirmware() { if (allFirmwareSelected()) { selectedFirmware.value = []; } else { - selectedFirmware.value = selectedPlatform.value?.firmware_files ?? []; + selectedFirmware.value = selectedPlatform.value?.firmware ?? []; } } @@ -223,14 +223,14 @@ function selectAllFirmware() {