| 1 | from typing import List |
| 2 | from typing import Optional |
| 3 | |
| 4 | from fastapi import HTTPException |
| 5 | from loguru import logger |
| 6 | from pydantic import BaseModel |
| 7 | from pydantic import Field |
| 8 | from pydantic import model_validator |
| 9 | |
| 10 | |
| 11 | class FlowSpecParameter(BaseModel): |
| 12 | key: str |
| 13 | value: str |
| 14 | comment: Optional[str] = None |
| 15 | |
| 16 | |
| 17 | class FlowSpec(BaseModel): |
| 18 | artifact: str |
| 19 | parameters: Optional[List[FlowSpecParameter]] = Field( |
| 20 | None, |
| 21 | description="The parameters of the artifact.", |
| 22 | ) |
| 23 | |
| 24 | |
| 25 | class FlowRequest(BaseModel): |
| 26 | creator: str |
| 27 | user_data: str |
| 28 | client_id: str |
| 29 | flow_id: str |
| 30 | urgent: bool |
| 31 | artifacts: List[str] |
| 32 | specs: Optional[List[FlowSpec]] = Field( |
| 33 | None, |
| 34 | description="The specs of the artifacts.", |
| 35 | ) |
| 36 | cpu_limit: int |
| 37 | iops_limit: int |
| 38 | progress_timeout: int |
| 39 | timeout: int |
| 40 | max_rows: int |
| 41 | max_upload_bytes: int |
| 42 | trace_freq_sec: int |
| 43 | allow_custom_overrides: bool |
| 44 | log_batch_time: int |
| 45 | compiled_collector_args: List[str] |
| 46 | ops_per_second: int |
| 47 | |
| 48 | @model_validator(mode="before") |
| 49 | @classmethod |
| 50 | def validate_specs(cls, values): |
| 51 | if "specs" in values and values["specs"] is not None: |
| 52 | validated_specs = [] |
| 53 | for spec in values["specs"]: |
| 54 | try: |
| 55 | validated_spec = FlowSpec(**spec) |
| 56 | validated_specs.append(validated_spec) |
| 57 | except Exception as e: |
| 58 | # raise HTTPException(status_code=400, detail=f"Failed to validate spec: {e}") |
| 59 | logger.error(f"Failed to validate spec: {e}") |
| 60 | values["specs"] = validated_specs |
| 61 | return values |
| 62 | |
| 63 | |
| 64 | class FlowQueryStat(BaseModel): |
| 65 | status: str |
| 66 | error_message: str |
| 67 | backtrace: str |
| 68 | duration: int |
| 69 | last_active: int |
| 70 | first_active: int |
| 71 | names_with_response: List[str] |
| 72 | Artifact: str |
| 73 | log_rows: int |
| 74 | uploaded_files: int |
| 75 | uploaded_bytes: int |
| 76 | expected_uploaded_bytes: int |
| 77 | result_rows: int |
| 78 | query_id: int |
| 79 | total_queries: int |
| 80 | |
| 81 | |
| 82 | class FlowClientSession(BaseModel): |
| 83 | client_id: str |
| 84 | session_id: str |
| 85 | request: FlowRequest |
| 86 | backtrace: str |
| 87 | create_time: int |
| 88 | start_time: int |
| 89 | active_time: int |
| 90 | total_uploaded_files: int |
| 91 | total_expected_uploaded_bytes: int |
| 92 | total_uploaded_bytes: int |
| 93 | total_collected_rows: int |
| 94 | total_logs: int |
| 95 | total_requests: int |
| 96 | outstanding_requests: int |
| 97 | next_response_id: int |
| 98 | execution_duration: int |
| 99 | state: str |
| 100 | status: str |
| 101 | artifacts_with_results: List[str] |
| 102 | query_stats: List[FlowQueryStat] |
| 103 | uploaded_files: List[str] |
| 104 | user_notified: bool |
| 105 | logs: List[str] |
| 106 | dirty: bool |
| 107 | total_loads: int |
| 108 | |
| 109 | |
| 110 | class FlowResponse(BaseModel): |
| 111 | results: List[FlowClientSession] |
| 112 | success: bool |
| 113 | message: str |
| 114 | |
| 115 | |
| 116 | class RetrieveFlowRequest(BaseModel): |
| 117 | client_id: str |
| 118 | session_id: str |
| 119 | |
| 120 | @model_validator(mode="before") |
| 121 | @classmethod |
| 122 | def validate_session_id(cls, values): |
| 123 | if "session_id" in values and values["session_id"] == "": |
| 124 | raise HTTPException( |
| 125 | status_code=400, |
| 126 | detail="The session_id cannot be an empty string", |
| 127 | ) |
| 128 | return values |