Optimize shot table loading performance

- Pass episode_id filter to backend API instead of client-side filtering,
  reducing payload size when an episode is selected
- Skip getShot refetch in ShotDetailPanel when initialShot prop is provided
- Fix redundant DB query in list_shots: read project.custom_task_statuses
  directly from the already-fetched project object
- Add missing indexes on Task.shot_id, Task.assigned_user_id,
  Task.deleted_at and Episode.project_id; add add_perf_indexes.py
  migration script to apply them to existing databases
- Center login page layout
- Update CLAUDE.md and AGENTS.md with correct venv path

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-22 00:30:18 +08:00
parent 0dd37d1706
commit 0acf9ddff2
13 changed files with 151 additions and 81 deletions
+18
View File
@@ -0,0 +1,18 @@
"""One-time migration: add performance indexes for shot table queries."""
from sqlalchemy import text
from database import engine
indexes = [
"CREATE INDEX IF NOT EXISTS ix_tasks_shot_id ON tasks (shot_id)",
"CREATE INDEX IF NOT EXISTS ix_tasks_assigned_user_id ON tasks (assigned_user_id)",
"CREATE INDEX IF NOT EXISTS ix_tasks_deleted_at ON tasks (deleted_at)",
"CREATE INDEX IF NOT EXISTS ix_episodes_project_id ON episodes (project_id)",
]
with engine.connect() as conn:
for sql in indexes:
conn.execute(text(sql))
print(f"OK: {sql}")
conn.commit()
print("Done.")
+1 -1
View File
@@ -17,7 +17,7 @@ class Episode(Base):
__tablename__ = "episodes"
id = Column(Integer, primary_key=True, index=True)
project_id = Column(Integer, ForeignKey("projects.id"), nullable=False)
project_id = Column(Integer, ForeignKey("projects.id"), nullable=False, index=True)
name = Column(String, nullable=False, index=True)
description = Column(String)
episode_number = Column(Integer, nullable=False)
+3 -3
View File
@@ -44,9 +44,9 @@ class Task(Base):
id = Column(Integer, primary_key=True, index=True)
project_id = Column(Integer, ForeignKey("projects.id"), nullable=False)
episode_id = Column(Integer, ForeignKey("episodes.id"), nullable=True)
shot_id = Column(Integer, ForeignKey("shots.id"), nullable=True)
shot_id = Column(Integer, ForeignKey("shots.id"), nullable=True, index=True)
asset_id = Column(Integer, ForeignKey("assets.id"), nullable=True)
assigned_user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
assigned_user_id = Column(Integer, ForeignKey("users.id"), nullable=True, index=True)
task_type = Column(String, nullable=False) # Changed from Enum to String to support custom task types
name = Column(String, nullable=False, index=True)
description = Column(Text)
@@ -56,7 +56,7 @@ class Task(Base):
updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
# Soft deletion columns
deleted_at = Column(DateTime(timezone=True), nullable=True)
deleted_at = Column(DateTime(timezone=True), nullable=True, index=True)
deleted_by = Column(Integer, ForeignKey("users.id"), nullable=True)
# Relationships
+61 -1
View File
@@ -266,9 +266,17 @@ async def list_shots(
)
for project in projects:
custom_types = project.custom_shot_task_types or []
raw = project.custom_task_statuses
if isinstance(raw, str):
try:
import json
raw = json.loads(raw)
except (json.JSONDecodeError, TypeError):
raw = []
custom_statuses = raw if isinstance(raw, list) else []
project_data[project.id] = {
'task_types': STANDARD_SHOT_TASK_TYPES + custom_types,
'custom_statuses': get_project_custom_statuses(project.id, db)
'custom_statuses': custom_statuses
}
# OPTIMIZATION: Group results by shot and aggregate task data efficiently
@@ -606,6 +614,58 @@ async def get_shot(
shot_data = ShotResponse.model_validate(shot)
shot_data.task_count = task_count
# Add project_name from episode.project (already eager loaded)
if shot.episode and shot.episode.project:
shot_data.project_name = shot.episode.project.name
# Add task status information (similar to list endpoint)
project = shot.episode.project if shot.episode else None
# Get all task types: standard + custom
from routers.shots import STANDARD_SHOT_TASK_TYPES
project_task_types = list(STANDARD_SHOT_TASK_TYPES) # Start with standard types
if project and project.custom_shot_task_types:
custom_types = project.custom_shot_task_types or []
if isinstance(custom_types, list):
for ct in custom_types:
if isinstance(ct, dict) and 'type' in ct:
project_task_types.append(ct['type'])
elif isinstance(ct, str):
project_task_types.append(ct)
# Initialize task_status and task_ids dictionaries
task_status_dict = {}
task_ids_dict = {}
task_details_list = []
# Initialize with default not_started for all project task types
for task_type_init in project_task_types:
task_status_dict[task_type_init] = "not_started"
# Build task information from active tasks
for task in active_tasks:
task_type = task.task_type.value if hasattr(task.task_type, 'value') else task.task_type
task_status = task.status.value if hasattr(task.status, 'value') else task.status
task_id = task.id
assigned_user_id = task.assigned_user_id
# Update task status
task_status_dict[task_type] = task_status
task_ids_dict[task_type] = task_id
# Add to task details
task_details_list.append(TaskStatusInfo(
task_type=task_type,
status=task_status,
task_id=task_id,
assigned_user_id=assigned_user_id
))
shot_data.task_status = task_status_dict
shot_data.task_ids = task_ids_dict
shot_data.task_details = task_details_list
return shot_data
+13 -8
View File
@@ -28,6 +28,14 @@ class ShotUpdate(BaseModel):
project_id: Optional[int] = Field(None, description="Project ID - must match episode's project")
class TaskStatusInfo(BaseModel):
"""Task status information for table display"""
task_type: str # String to support custom task types
status: str # Changed from TaskStatus enum to str to support custom statuses
task_id: Optional[int] = None
assigned_user_id: Optional[int] = None
class ShotResponse(ShotBase):
id: int
project_id: int # Make required in response
@@ -40,19 +48,16 @@ class ShotResponse(ShotBase):
# Optional computed field for display
project_name: Optional[str] = Field(None, description="Project name for display purposes")
# Task status information for detail display
task_status: Dict[str, Optional[str]] = Field(default_factory=dict, description="Task status by task type")
task_ids: Dict[str, int] = Field(default_factory=dict, description="Task IDs by task type")
task_details: List[TaskStatusInfo] = Field(default_factory=list, description="Detailed task information")
class Config:
from_attributes = True
class TaskStatusInfo(BaseModel):
"""Task status information for table display"""
task_type: str # String to support custom task types
status: str # Changed from TaskStatus enum to str to support custom statuses
task_id: Optional[int] = None
assigned_user_id: Optional[int] = None
class ShotListResponse(BaseModel):
id: int
name: str