From aa9bc8b63f73f4cb1dade1fda0f2fec03fb397cf Mon Sep 17 00:00:00 2001 From: mijauexe Date: Fri, 23 May 2025 14:15:33 +0200 Subject: [PATCH 1/2] Initial WIP --- core/db/models/project_state.py | 99 ++++++++++++++++++++++++++++++++- core/state/state_manager.py | 3 + core/ui/api_server.py | 8 +-- 3 files changed, 103 insertions(+), 7 deletions(-) diff --git a/core/db/models/project_state.py b/core/db/models/project_state.py index 8fade612a6..cc7b060bb1 100644 --- a/core/db/models/project_state.py +++ b/core/db/models/project_state.py @@ -5,7 +5,7 @@ from uuid import UUID, uuid4 from sqlalchemy import ForeignKey, UniqueConstraint, and_, delete, inspect, select from sqlalchemy.ext.asyncio import AsyncSession -from sqlalchemy.orm import Mapped, mapped_column, relationship +from sqlalchemy.orm import Mapped, load_only, mapped_column, relationship from sqlalchemy.orm.attributes import flag_modified from sqlalchemy.sql import func @@ -600,3 +600,100 @@ class ProjectState(Base): query = select(ProjectState).where(ProjectState.id == state_id) result = await session.execute(query) return result.scalar_one_or_none() + + @staticmethod + async def get_all_epics_and_tasks(session: "AsyncSession", branch_id: UUID) -> list: + epics_and_tasks = [] + + try: + query = ( + select(ProjectState) + .options(load_only(ProjectState.id, ProjectState.epics, ProjectState.tasks)) + .where(and_(ProjectState.branch_id == branch_id, ProjectState.action.isnot(None))) + ) + + # query = ( + # select(ProjectState) + # .options(load_only( + # ProjectState.epics, + # ProjectState.tasks, + # )) + # .where(ProjectState.branch_id == branch_id) + # .where(text("json_array_length(tasks) > 0")) + # .where(text(""" + # NOT EXISTS ( + # SELECT 1 + # FROM json_each(project_states.tasks) + # WHERE json_extract(json_each.value, '$.status') NOT IN ('documented', 'done') + # ) + # """)) + # ) + + result = await session.execute(query) + project_states = result.scalars().all() + + def has_epic(epic_type: str): + return any(epic1.get("source", "") == epic_type for epic1 in epics_and_tasks) + + def find_epic_by_id(epic_id: str, sub_epic_id: str): + return next( + ( + epic + for epic in epics_and_tasks + if epic.get("id", "") == epic_id and epic.get("sub_epic_id", "") == sub_epic_id + ), + None, + ) + + def find_task_in_epic(task_id: str, epic): + if not epic: + return None + return next((task for task in epic.get("tasks", []) if task.get("id", "") == task_id), None) + + for state in project_states: + epics, tasks = state.epics, state.tasks + epic = epics[-1] + + if epics[-1] in ["spec_writer", "frontend"]: + for epic in state.epics: + if epic["source"] == "spec_writer" and not has_epic("spec_writer"): + epics_and_tasks.insert(0, {"source": "spec_writer", "tasks": []}) + + if epic["source"] == "frontend" and not has_epic("frontend"): + epics_and_tasks.insert(1, {"source": "frontend", "tasks": []}) + + else: + for sub_epic in epic.get("sub_epics", []): + if not find_epic_by_id(epic["id"], sub_epic["id"]): + epics_and_tasks.append( + { + "id": epic["id"], + "sub_epic_id": sub_epic["id"], + "source": epic["source"], + "description": sub_epic.get("description", ""), + "tasks": [], + } + ) + + for task in tasks: + epic_in_list = find_epic_by_id(epic["id"], task.get("sub_epic_id")) + if not epic_in_list: + continue + task_in_epic_list = find_task_in_epic(task["id"], epic_in_list) + if not task_in_epic_list: + epic_in_list["tasks"].append( + { + "id": task.get("id"), + "status": task.get("status"), + "description": task.get("description"), + } + ) + else: + # Update the status of the task if it already exists + task_in_epic_list["status"] = task.get("status") + + except Exception as e: + log.error(f"Error while getting epics and tasks: {e}") + return [] + + return epics_and_tasks diff --git a/core/state/state_manager.py b/core/state/state_manager.py index 486e32a1e0..4d6569e7d1 100644 --- a/core/state/state_manager.py +++ b/core/state/state_manager.py @@ -111,6 +111,9 @@ class StateManager: async def get_project_state_by_id(self, state_id: UUID) -> Optional[ProjectState]: return await ProjectState.get_by_id(self.current_session, state_id) + async def get_all_epics_and_tasks(self, branch_id: UUID) -> list: + return await ProjectState.get_all_epics_and_tasks(self.current_session, branch_id) + async def find_user_input(self, project_state, branch_id) -> Optional[list["UserInput"]]: return await UserInput.find_user_inputs(self.current_session, project_state, branch_id) diff --git a/core/ui/api_server.py b/core/ui/api_server.py index b3b6877ffe..12e82b05d3 100644 --- a/core/ui/api_server.py +++ b/core/ui/api_server.py @@ -398,16 +398,12 @@ class IPCServer: """ try: # Get current state - current_state = self.state_manager.current_state - - # Extract epics and tasks - epics = current_state.epics if current_state.epics else [] - tasks = current_state.tasks if current_state.tasks else [] + epics_and_tasks = await self.state_manager.get_all_epics_and_tasks(self.state_manager.branch.id) # Send response with the same request_id from the incoming message response = Message( type=MessageType.EPICS_AND_TASKS, - content={"epics": epics, "tasks": tasks}, + content={"epicsAndTasks": epics_and_tasks}, request_id=message.request_id, # Include the request_id from the incoming message ) log.debug(f"Sending epics and tasks response with request_id: {message.request_id}") From b9c09a7d60f64a0f206f88f26343d28655a93c47 Mon Sep 17 00:00:00 2001 From: LeonOstrez <41999013+LeonOstrez@users.noreply.github.com> Date: Wed, 28 May 2025 13:24:55 +0200 Subject: [PATCH 2/2] Update project_state.py --- core/db/models/project_state.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/core/db/models/project_state.py b/core/db/models/project_state.py index cc7b060bb1..30da369959 100644 --- a/core/db/models/project_state.py +++ b/core/db/models/project_state.py @@ -612,23 +612,6 @@ class ProjectState(Base): .where(and_(ProjectState.branch_id == branch_id, ProjectState.action.isnot(None))) ) - # query = ( - # select(ProjectState) - # .options(load_only( - # ProjectState.epics, - # ProjectState.tasks, - # )) - # .where(ProjectState.branch_id == branch_id) - # .where(text("json_array_length(tasks) > 0")) - # .where(text(""" - # NOT EXISTS ( - # SELECT 1 - # FROM json_each(project_states.tasks) - # WHERE json_extract(json_each.value, '$.status') NOT IN ('documented', 'done') - # ) - # """)) - # ) - result = await session.execute(query) project_states = result.scalars().all()