Merge pull request #164 from Pythagora-io/feature/improve-get-epics-and-tasks

Improve getting all epics and tasks
pull/1160/head
LeonOstrez 1 year ago committed by GitHub
commit ffed33a66c

@ -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
@ -623,3 +623,83 @@ 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)))
)
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

@ -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)

@ -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}")

Loading…
Cancel
Save