|
| 1 | +""" |
| 2 | +Service layer for project operations. |
| 3 | +Separates business logic from database operations. |
| 4 | +""" |
| 5 | +import os |
| 6 | +from typing import Dict, Any, Optional |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +from db import ( |
| 10 | + create_project as db_create_project, |
| 11 | + get_project as db_get_project, |
| 12 | + get_project_by_id as db_get_project_by_id, |
| 13 | + list_projects as db_list_projects, |
| 14 | + update_project_status as db_update_project_status, |
| 15 | + delete_project as db_delete_project, |
| 16 | + get_or_create_project as db_get_or_create_project, |
| 17 | + get_project_stats, |
| 18 | +) |
| 19 | +from logger import get_logger |
| 20 | + |
| 21 | +logger = get_logger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class ProjectService: |
| 25 | + """ |
| 26 | + Service layer for project management operations. |
| 27 | + Provides high-level business logic for projects. |
| 28 | + """ |
| 29 | + |
| 30 | + @staticmethod |
| 31 | + def create_project(project_path: str, name: Optional[str] = None) -> Dict[str, Any]: |
| 32 | + """ |
| 33 | + Create a new project with validation. |
| 34 | + |
| 35 | + Args: |
| 36 | + project_path: Path to project directory |
| 37 | + name: Optional project name |
| 38 | + |
| 39 | + Returns: |
| 40 | + Project metadata dictionary |
| 41 | + |
| 42 | + Raises: |
| 43 | + ValueError: If path is invalid |
| 44 | + RuntimeError: If creation fails |
| 45 | + """ |
| 46 | + # Validate path |
| 47 | + if not project_path: |
| 48 | + raise ValueError("Project path cannot be empty") |
| 49 | + |
| 50 | + abs_path = os.path.abspath(project_path) |
| 51 | + |
| 52 | + if not os.path.exists(abs_path): |
| 53 | + raise ValueError(f"Project path does not exist: {abs_path}") |
| 54 | + |
| 55 | + if not os.path.isdir(abs_path): |
| 56 | + raise ValueError(f"Project path is not a directory: {abs_path}") |
| 57 | + |
| 58 | + # Create project |
| 59 | + try: |
| 60 | + project = db_create_project(abs_path, name) |
| 61 | + logger.info(f"Created project {project['id']} at {abs_path}") |
| 62 | + return project |
| 63 | + except Exception as e: |
| 64 | + logger.error(f"Failed to create project: {e}") |
| 65 | + raise RuntimeError(f"Failed to create project: {e}") from e |
| 66 | + |
| 67 | + @staticmethod |
| 68 | + def get_project(project_path: str) -> Optional[Dict[str, Any]]: |
| 69 | + """Get project by path.""" |
| 70 | + return db_get_project(project_path) |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def get_project_by_id(project_id: str) -> Optional[Dict[str, Any]]: |
| 74 | + """Get project by ID.""" |
| 75 | + return db_get_project_by_id(project_id) |
| 76 | + |
| 77 | + @staticmethod |
| 78 | + def list_all_projects() -> list: |
| 79 | + """List all projects.""" |
| 80 | + return db_list_projects() |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def delete_project(project_id: str) -> None: |
| 84 | + """ |
| 85 | + Delete a project with validation. |
| 86 | + |
| 87 | + Args: |
| 88 | + project_id: Project identifier |
| 89 | + |
| 90 | + Raises: |
| 91 | + ValueError: If project not found |
| 92 | + """ |
| 93 | + project = db_get_project_by_id(project_id) |
| 94 | + if not project: |
| 95 | + raise ValueError(f"Project not found: {project_id}") |
| 96 | + |
| 97 | + try: |
| 98 | + db_delete_project(project_id) |
| 99 | + logger.info(f"Deleted project {project_id}") |
| 100 | + except Exception as e: |
| 101 | + logger.error(f"Failed to delete project: {e}") |
| 102 | + raise RuntimeError(f"Failed to delete project: {e}") from e |
| 103 | + |
| 104 | + @staticmethod |
| 105 | + def update_status(project_id: str, status: str, timestamp: Optional[str] = None) -> None: |
| 106 | + """ |
| 107 | + Update project status. |
| 108 | + |
| 109 | + Args: |
| 110 | + project_id: Project identifier |
| 111 | + status: New status (created, indexing, ready, error) |
| 112 | + timestamp: Optional timestamp |
| 113 | + """ |
| 114 | + db_update_project_status(project_id, status, timestamp) |
| 115 | + logger.debug(f"Updated project {project_id} status to {status}") |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def get_or_create(project_path: str, name: Optional[str] = None) -> Dict[str, Any]: |
| 119 | + """Get existing project or create new one.""" |
| 120 | + return db_get_or_create_project(project_path, name) |
| 121 | + |
| 122 | + @staticmethod |
| 123 | + def get_stats(project_id: str) -> Dict[str, Any]: |
| 124 | + """ |
| 125 | + Get project statistics. |
| 126 | + |
| 127 | + Args: |
| 128 | + project_id: Project identifier |
| 129 | + |
| 130 | + Returns: |
| 131 | + Statistics dictionary with file_count and embedding_count |
| 132 | + |
| 133 | + Raises: |
| 134 | + ValueError: If project not found |
| 135 | + """ |
| 136 | + project = db_get_project_by_id(project_id) |
| 137 | + if not project: |
| 138 | + raise ValueError(f"Project not found: {project_id}") |
| 139 | + |
| 140 | + db_path = project["database_path"] |
| 141 | + return get_project_stats(db_path) |
| 142 | + |
| 143 | + @staticmethod |
| 144 | + def is_indexed(project_id: str) -> bool: |
| 145 | + """ |
| 146 | + Check if project has been indexed. |
| 147 | + |
| 148 | + Args: |
| 149 | + project_id: Project identifier |
| 150 | + |
| 151 | + Returns: |
| 152 | + True if project has indexed files |
| 153 | + """ |
| 154 | + try: |
| 155 | + stats = ProjectService.get_stats(project_id) |
| 156 | + return stats.get("file_count", 0) > 0 |
| 157 | + except ValueError: |
| 158 | + return False |
| 159 | + |
| 160 | + @staticmethod |
| 161 | + def validate_project_ready(project_id: str) -> tuple: |
| 162 | + """ |
| 163 | + Validate that project is ready for queries. |
| 164 | + |
| 165 | + Args: |
| 166 | + project_id: Project identifier |
| 167 | + |
| 168 | + Returns: |
| 169 | + Tuple of (is_ready: bool, error_message: Optional[str]) |
| 170 | + """ |
| 171 | + project = db_get_project_by_id(project_id) |
| 172 | + if not project: |
| 173 | + return False, "Project not found" |
| 174 | + |
| 175 | + if not os.path.exists(project["path"]): |
| 176 | + return False, "Project path does not exist" |
| 177 | + |
| 178 | + if not ProjectService.is_indexed(project_id): |
| 179 | + return False, "Project not indexed yet" |
| 180 | + |
| 181 | + return True, None |
0 commit comments