-
Notifications
You must be signed in to change notification settings - Fork 941
feat(application): restore last project on startup (#3439) #3541
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
Draft
ashwinvaidya17
wants to merge
14
commits into
main
Choose a base branch
from
fix/startup_project_selection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
cbf3f2a
Fix startup project restoration
yvolokitin 3d424f5
address review issues
yvolokitin e3644ca
test(application): mock startup project selection in Playwright
yvolokitin 0cf0542
fix(application): make startup project fallback deterministic
yvolokitin d44a1fe
minor fixes
ashwinvaidya17 700b798
Address PR comments
ashwinvaidya17 3d6d8a4
Fix tests + import
ashwinvaidya17 3ee0dd2
fix fixtures
ashwinvaidya17 a6806ad
Potential fix for pull request finding
ashwinvaidya17 e7e9d87
Fix python checks
ashwinvaidya17 2fe9ae6
remove database
ashwinvaidya17 fc39430
Apply suggestions from code review
ashwinvaidya17 8139f23
merge alembic versions
ashwinvaidya17 82ac82a
update license headers
ashwinvaidya17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
application/backend/src/api/endpoints/project_selection_endpoints.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from typing import Annotated | ||
|
|
||
| from fastapi import APIRouter, Body, Depends, HTTPException, status | ||
|
|
||
| from api.dependencies import get_project_selection_service | ||
| from api.endpoints import API_PREFIX | ||
| from pydantic_models import LastUsedProjectUpdate, StartupProjectSelection | ||
| from services import ProjectSelectionService, ResourceNotFoundError | ||
|
|
||
| router = APIRouter(prefix=f"{API_PREFIX}/projects", tags=["Project"]) | ||
|
|
||
|
|
||
| @router.get("/startup-selection") | ||
| async def get_startup_project_selection( | ||
| project_selection_service: Annotated[ProjectSelectionService, Depends(get_project_selection_service)], | ||
| ) -> StartupProjectSelection: | ||
| """Return the project that should be restored when the app starts.""" | ||
| return await project_selection_service.get_startup_project_selection() | ||
|
|
||
|
|
||
| @router.put( | ||
| "/last-used", | ||
| status_code=status.HTTP_204_NO_CONTENT, | ||
| responses={ | ||
| status.HTTP_204_NO_CONTENT: {"description": "Last used project stored successfully"}, | ||
| status.HTTP_404_NOT_FOUND: {"description": "Project not found"}, | ||
| }, | ||
| ) | ||
| async def update_last_used_project( | ||
| project_selection: Annotated[LastUsedProjectUpdate, Body()], | ||
| project_selection_service: Annotated[ProjectSelectionService, Depends(get_project_selection_service)], | ||
| ) -> None: | ||
| """Persist the project that should be restored on the next application start.""" | ||
| try: | ||
| await project_selection_service.set_last_used_project(project_selection.project_id) | ||
| except ResourceNotFoundError as error: | ||
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=error.message) from error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
application/backend/src/pydantic_models/project_selection.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from enum import StrEnum | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from utils.short_uuid import ShortUUID | ||
|
|
||
|
|
||
| class StartupProjectSelectionSource(StrEnum): | ||
| LAST_USED = "last_used" | ||
| ACTIVE_PIPELINE = "active_pipeline" | ||
| FIRST_PROJECT = "first_project" | ||
| NONE = "none" | ||
|
|
||
|
|
||
| class StartupProjectSelection(BaseModel): | ||
| project_id: ShortUUID | None = None | ||
| source: StartupProjectSelectionSource | ||
|
|
||
|
|
||
| class LastUsedProjectUpdate(BaseModel): | ||
| project_id: ShortUUID |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import sqlalchemy as sa | ||
| from sqlalchemy.ext.asyncio.session import AsyncSession | ||
|
|
||
| from db.schema import AppStateDB | ||
|
|
||
| APP_STATE_ID = 1 | ||
|
|
||
|
|
||
| class AppStateRepository: | ||
| """Repository for persisted application-wide state.""" | ||
|
|
||
| def __init__(self, db: AsyncSession): | ||
| self.db = db | ||
|
|
||
| async def get_last_used_project_id(self) -> str | None: | ||
| result = await self.db.execute( | ||
| sa.select(AppStateDB.last_used_project_id).where(AppStateDB.id == APP_STATE_ID), | ||
| ) | ||
| return result.scalar_one_or_none() | ||
|
|
||
| async def set_last_used_project_id(self, project_id: str) -> None: | ||
| state = await self.db.get(AppStateDB, APP_STATE_ID) | ||
|
|
||
| if state is None: | ||
| self.db.add(AppStateDB(id=APP_STATE_ID, last_used_project_id=project_id)) | ||
| else: | ||
| state.last_used_project_id = project_id | ||
|
|
||
| await self.db.commit() | ||
|
|
||
| async def clear_last_used_project_id(self) -> None: | ||
| state = await self.db.get(AppStateDB, APP_STATE_ID) | ||
| if state is None: | ||
| return | ||
|
|
||
| state.last_used_project_id = None | ||
| await self.db.commit() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
application/backend/src/services/project_selection_service.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # Copyright (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
|
|
||
| from db import get_async_db_session_ctx | ||
| from pydantic_models import StartupProjectSelection, StartupProjectSelectionSource | ||
| from repositories import AppStateRepository, PipelineRepository, ProjectRepository | ||
| from services.exceptions import ResourceNotFoundError, ResourceType | ||
| from utils.short_uuid import ShortUUID | ||
|
|
||
|
|
||
| async def _resolve_last_used( | ||
| app_state_repo: AppStateRepository, | ||
| project_repo: ProjectRepository, | ||
| ) -> StartupProjectSelection | None: | ||
| last_used_project_id = await app_state_repo.get_last_used_project_id() | ||
| if last_used_project_id is None: | ||
| return None | ||
|
|
||
| last_used_project = await project_repo.get_by_id(last_used_project_id) | ||
| if last_used_project is not None: | ||
| return StartupProjectSelection( | ||
| project_id=last_used_project.id, | ||
| source=StartupProjectSelectionSource.LAST_USED, | ||
| ) | ||
|
|
||
| await app_state_repo.clear_last_used_project_id() | ||
| return None | ||
|
|
||
|
|
||
| async def _resolve_active_pipeline(pipeline_repo: PipelineRepository) -> StartupProjectSelection | None: | ||
| active_pipeline = await pipeline_repo.get_active_pipeline() | ||
| if active_pipeline is None: | ||
| return None | ||
| return StartupProjectSelection( | ||
| project_id=active_pipeline.project_id, | ||
| source=StartupProjectSelectionSource.ACTIVE_PIPELINE, | ||
| ) | ||
|
|
||
|
|
||
| async def _resolve_first_project(project_repo: ProjectRepository) -> StartupProjectSelection | None: | ||
| first_project = await project_repo.get_first_project() | ||
| if first_project is None: | ||
| return None | ||
| return StartupProjectSelection( | ||
| project_id=first_project.id, | ||
| source=StartupProjectSelectionSource.FIRST_PROJECT, | ||
| ) | ||
|
|
||
|
|
||
| class ProjectSelectionService: | ||
| @staticmethod | ||
| async def get_startup_project_selection() -> StartupProjectSelection: | ||
| """Resolve startup project in a deterministic order. | ||
|
|
||
| Priority: | ||
| 1. last used project | ||
| 2. project that owns the active pipeline | ||
| 3. first project in the existing project list | ||
| """ | ||
| async with get_async_db_session_ctx() as session: | ||
| app_state_repo = AppStateRepository(session) | ||
| project_repo = ProjectRepository(session) | ||
| pipeline_repo = PipelineRepository(session) | ||
|
|
||
| selection = await _resolve_last_used(app_state_repo, project_repo) | ||
|
|
||
| if selection is None: | ||
| selection = await _resolve_active_pipeline(pipeline_repo) | ||
|
|
||
| if selection is None: | ||
| selection = await _resolve_first_project(project_repo) | ||
|
|
||
| if selection is None: | ||
| selection = StartupProjectSelection(source=StartupProjectSelectionSource.NONE) | ||
|
|
||
| return selection | ||
|
|
||
| @staticmethod | ||
| async def set_last_used_project(project_id: ShortUUID) -> None: | ||
| async with get_async_db_session_ctx() as session: | ||
| project_repo = ProjectRepository(session) | ||
| if await project_repo.get_by_id(project_id) is None: | ||
| raise ResourceNotFoundError(resource_type=ResourceType.PROJECT, resource_id=str(project_id)) | ||
|
|
||
| app_state_repo = AppStateRepository(session) | ||
| await app_state_repo.set_last_used_project_id(str(project_id)) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.