|
| 1 | +import logging |
| 2 | +import traceback |
| 3 | + |
| 4 | +from controller import organization |
| 5 | +from starlette.endpoints import HTTPEndpoint |
| 6 | +from starlette.responses import PlainTextResponse, JSONResponse |
| 7 | +from submodules.s3 import controller as s3 |
| 8 | +from submodules.model.business_objects import organization |
| 9 | + |
| 10 | +from controller.transfer import manager as upload_manager |
| 11 | +from controller.upload_task import manager as task_manager |
| 12 | +from controller.auth import manager as auth_manager |
| 13 | +from controller.transfer import manager as transfer_manager |
| 14 | +from controller.auth import manager as auth |
| 15 | + |
| 16 | +from submodules.model import enums |
| 17 | +from util.notification import create_notification |
| 18 | +from submodules.model.enums import NotificationType |
| 19 | +from submodules.model.models import UploadTask |
| 20 | +from submodules.model.business_objects import general |
| 21 | +from util import notification |
| 22 | +from controller.tokenization import tokenization_service |
| 23 | + |
| 24 | +logging.basicConfig(level=logging.DEBUG) |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class Notify(HTTPEndpoint): |
| 29 | + async def post(self, request) -> PlainTextResponse: |
| 30 | + data = await request.json() |
| 31 | + file_path = data["Key"] |
| 32 | + |
| 33 | + if len(file_path.split("/")) != 4: |
| 34 | + # We need handling for lf execution notification here. |
| 35 | + # ATM we have a different path of handling in util/payload_scheduler.py update_records method |
| 36 | + return PlainTextResponse("OK") |
| 37 | + |
| 38 | + org_id, project_id, upload_task_id, file_name = file_path.split("/") |
| 39 | + if len(project_id) != 36: |
| 40 | + return PlainTextResponse("OK") |
| 41 | + if upload_task_id == "download": |
| 42 | + return PlainTextResponse("OK") |
| 43 | + if org_id == "archive": |
| 44 | + return PlainTextResponse("OK") |
| 45 | + |
| 46 | + task = task_manager.get_upload_task_secure( |
| 47 | + upload_task_id=upload_task_id, |
| 48 | + project_id=project_id, |
| 49 | + file_name=file_name, |
| 50 | + ) |
| 51 | + is_global_update = True if task.file_type == "project" else False |
| 52 | + try: |
| 53 | + init_file_import(task, project_id, is_global_update) |
| 54 | + except Exception: |
| 55 | + file_import_error_handling(task, project_id, is_global_update) |
| 56 | + notification.send_organization_update(project_id, "project_update", True) |
| 57 | + return PlainTextResponse("OK") |
| 58 | + |
| 59 | + |
| 60 | +class FileExport(HTTPEndpoint): |
| 61 | + def get(self, request) -> JSONResponse: |
| 62 | + project_id = request.path_params["project_id"] |
| 63 | + user_id = request.query_params["user_id"] |
| 64 | + num_samples = request.query_params.get("num_samples") |
| 65 | + auth_manager.check_project_access_from_user_id(user_id, project_id) |
| 66 | + result = transfer_manager.export_records(project_id, num_samples) |
| 67 | + return JSONResponse(result) |
| 68 | + |
| 69 | + |
| 70 | +class KnowledgeBaseExport(HTTPEndpoint): |
| 71 | + def get(self, request) -> JSONResponse: |
| 72 | + project_id = request.path_params["project_id"] |
| 73 | + list_id = request.path_params["knowledge_base_id"] |
| 74 | + user_id = request.query_params["user_id"] |
| 75 | + auth_manager.check_project_access_from_user_id(user_id, project_id) |
| 76 | + result = transfer_manager.export_knowledge_base(project_id, list_id) |
| 77 | + return JSONResponse(result) |
| 78 | + |
| 79 | + |
| 80 | +class PrepareImport(HTTPEndpoint): |
| 81 | + async def post(self, request) -> JSONResponse: |
| 82 | + auth.check_is_demo_without_info() |
| 83 | + project_id = request.path_params["project_id"] |
| 84 | + request_body = await request.json() |
| 85 | + |
| 86 | + user_id = request_body["user_id"] |
| 87 | + auth_manager.check_project_access_from_user_id(user_id, project_id) |
| 88 | + file_name = request_body["file_name"] |
| 89 | + file_type = request_body["file_type"] |
| 90 | + file_import_options = request_body.get("file_import_options") |
| 91 | + task = task_manager.create_upload_task( |
| 92 | + user_id, project_id, file_name, file_type, file_import_options |
| 93 | + ) |
| 94 | + org_id = organization.get_id_by_project_id(project_id) |
| 95 | + credentials_and_id = s3.get_upload_credentials_and_id( |
| 96 | + org_id, f"{project_id}/{task.id}" |
| 97 | + ) |
| 98 | + return JSONResponse(credentials_and_id) |
| 99 | + |
| 100 | + |
| 101 | +class UploadTask(HTTPEndpoint): |
| 102 | + def get(self, request) -> JSONResponse: |
| 103 | + auth.check_is_demo_without_info() |
| 104 | + project_id = request.path_params["project_id"] |
| 105 | + task_id = request.path_params["task_id"] |
| 106 | + user_id = request.query_params["user_id"] |
| 107 | + auth_manager.check_project_access_from_user_id(user_id, project_id) |
| 108 | + task = task_manager.get_upload_task(project_id, task_id) |
| 109 | + task_dict = { |
| 110 | + "id": str(task.id), |
| 111 | + "file_name": str(task.file_name), |
| 112 | + "file_type": str(task.file_type), |
| 113 | + "progress": task.progress, |
| 114 | + "state": str(task.state), |
| 115 | + "started_at": str(task.started_at), |
| 116 | + } |
| 117 | + return JSONResponse(task_dict) |
| 118 | + |
| 119 | + |
| 120 | +def init_file_import(task: UploadTask, project_id: str, is_global_update: bool) -> None: |
| 121 | + if "records" in task.file_type: |
| 122 | + upload_manager.import_records(project_id, task) |
| 123 | + elif "project" in task.file_type: |
| 124 | + upload_manager.import_project(project_id, task) |
| 125 | + elif "knowledge_base" in task.file_type: |
| 126 | + upload_manager.import_knowledge_base(project_id, task) |
| 127 | + |
| 128 | + notification.send_organization_update( |
| 129 | + project_id, f"file_upload:{str(task.id)}:state:{task.state}", is_global_update |
| 130 | + ) |
| 131 | + if task.file_type != "knowledge_base": |
| 132 | + tokenization_service.request_tokenize_project(project_id, str(task.user_id)) |
| 133 | + |
| 134 | + |
| 135 | +def file_import_error_handling( |
| 136 | + task: UploadTask, project_id: str, is_global_update: bool |
| 137 | +) -> None: |
| 138 | + general.rollback() |
| 139 | + task.state = enums.UploadStates.ERROR.value |
| 140 | + general.commit() |
| 141 | + create_notification( |
| 142 | + NotificationType.IMPORT_FAILED, |
| 143 | + task.user_id, |
| 144 | + task.project_id, |
| 145 | + task.file_type, |
| 146 | + ) |
| 147 | + logger.error( |
| 148 | + task_manager.get_upload_task_message( |
| 149 | + task, |
| 150 | + ) |
| 151 | + ) |
| 152 | + print(traceback.format_exc(), flush=True) |
| 153 | + notification.send_organization_update( |
| 154 | + project_id, f"file_upload:{str(task.id)}:state:{task.state}", is_global_update |
| 155 | + ) |
0 commit comments