|
| 1 | +"""Additional endpoints for the CADS Processing API Service.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +import cads_adaptors |
| 6 | +import cads_adaptors.exceptions |
| 7 | +import cads_broker |
| 8 | +import cads_catalogue |
| 9 | +import fastapi |
| 10 | +import ogc_api_processes_fastapi.exceptions |
| 11 | +import structlog |
| 12 | + |
| 13 | +from . import ( |
| 14 | + adaptors, |
| 15 | + auth, |
| 16 | + config, |
| 17 | + costing, |
| 18 | + db_utils, |
| 19 | + exceptions, |
| 20 | + limits, |
| 21 | + models, |
| 22 | + translators, |
| 23 | + utils, |
| 24 | +) |
| 25 | + |
| 26 | +SETTINGS = config.settings |
| 27 | + |
| 28 | + |
| 29 | +@exceptions.exception_logger |
| 30 | +def apply_constraints( |
| 31 | + process_id: str = fastapi.Path(..., description="Process identifier."), |
| 32 | + execution_content: models.Execute = fastapi.Body(...), |
| 33 | + portals: tuple[str] | None = fastapi.Depends(utils.get_portals), |
| 34 | +) -> dict[str, Any]: |
| 35 | + request = execution_content.model_dump() |
| 36 | + table = cads_catalogue.database.Resource |
| 37 | + catalogue_sessionmaker = db_utils.get_catalogue_sessionmaker( |
| 38 | + db_utils.ConnectionMode.read |
| 39 | + ) |
| 40 | + with catalogue_sessionmaker() as catalogue_session: |
| 41 | + dataset = utils.lookup_resource_by_id( |
| 42 | + resource_id=process_id, |
| 43 | + table=table, |
| 44 | + session=catalogue_session, |
| 45 | + portals=portals, |
| 46 | + ) |
| 47 | + adaptor: cads_adaptors.AbstractAdaptor = adaptors.instantiate_adaptor(dataset) |
| 48 | + try: |
| 49 | + constraints: dict[str, Any] = adaptor.apply_constraints( |
| 50 | + request.get("inputs", {}) |
| 51 | + ) |
| 52 | + except ( |
| 53 | + cads_adaptors.exceptions.ParameterError, |
| 54 | + cads_adaptors.exceptions.InvalidRequest, |
| 55 | + ) as exc: |
| 56 | + raise exceptions.InvalidParameter(detail=str(exc)) from exc |
| 57 | + |
| 58 | + return constraints |
| 59 | + |
| 60 | + |
| 61 | +@exceptions.exception_logger |
| 62 | +def estimate_cost( |
| 63 | + process_id: str = fastapi.Path(..., description="Process identifier."), |
| 64 | + request_origin: costing.RequestOrigin = fastapi.Query( |
| 65 | + "api", include_in_schema=False |
| 66 | + ), |
| 67 | + mandatory_inputs: bool = fastapi.Query(False, include_in_schema=False), |
| 68 | + execution_content: models.Execute = fastapi.Body(...), |
| 69 | + portals: tuple[str] | None = fastapi.Depends(utils.get_portals), |
| 70 | +) -> models.RequestCost: |
| 71 | + """ |
| 72 | + Estimate the cost with the highest cost/limit ratio of the request. |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + process_id : str |
| 77 | + Process ID. |
| 78 | + execution_content : models.Execute |
| 79 | + Request content. |
| 80 | +
|
| 81 | + Returns |
| 82 | + ------- |
| 83 | + models.RequestCost |
| 84 | + Info on the cost with the highest cost/limit ratio. |
| 85 | + """ |
| 86 | + request = execution_content.model_dump() |
| 87 | + table = cads_catalogue.database.Resource |
| 88 | + catalogue_sessionmaker = db_utils.get_catalogue_sessionmaker( |
| 89 | + db_utils.ConnectionMode.read |
| 90 | + ) |
| 91 | + with catalogue_sessionmaker() as catalogue_session: |
| 92 | + dataset = utils.lookup_resource_by_id( |
| 93 | + resource_id=process_id, |
| 94 | + table=table, |
| 95 | + session=catalogue_session, |
| 96 | + portals=portals, |
| 97 | + ) |
| 98 | + adaptor_properties = adaptors.get_adaptor_properties(dataset) |
| 99 | + costing_info = costing.compute_costing( |
| 100 | + request.get("inputs", {}), adaptor_properties, request_origin |
| 101 | + ) |
| 102 | + cost = costing.compute_highest_cost_limit_ratio(costing_info) |
| 103 | + if costing_info.cost_bar_steps: |
| 104 | + cost.cost_bar_steps = costing_info.cost_bar_steps |
| 105 | + try: |
| 106 | + costing.check_request_validity( |
| 107 | + request=request, |
| 108 | + request_origin=request_origin, |
| 109 | + mandatory_inputs=mandatory_inputs, |
| 110 | + adaptor_properties=adaptor_properties, |
| 111 | + ) |
| 112 | + except exceptions.InvalidRequest as exc: |
| 113 | + cost.request_is_valid = False |
| 114 | + cost.invalid_reason = exc.detail |
| 115 | + return cost |
| 116 | + |
| 117 | + |
| 118 | +@exceptions.exception_logger |
| 119 | +def get_api_request( |
| 120 | + process_id: str = fastapi.Path(..., description="Process identifier."), |
| 121 | + request: dict[str, Any] = fastapi.Body(...), |
| 122 | +) -> dict[str, str]: |
| 123 | + """Get CADS API request equivalent to the provided processing request. |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + process_id : str, optional |
| 128 | + Process identifier, by default fastapi.Path(...) |
| 129 | + request : dict[str, Any], optional |
| 130 | + Request, by default fastapi.Body(...) |
| 131 | +
|
| 132 | + Returns |
| 133 | + ------- |
| 134 | + dict[str, str] |
| 135 | + CDS API request. |
| 136 | + """ |
| 137 | + api_request_template = SETTINGS.api_request_template |
| 138 | + api_request = translators.format_api_request( |
| 139 | + api_request_template, process_id, request |
| 140 | + ) |
| 141 | + return {"api_request": api_request} |
| 142 | + |
| 143 | + |
| 144 | +@exceptions.exception_logger |
| 145 | +def delete_jobs( |
| 146 | + request: models.DeleteJobs = fastapi.Body(...), |
| 147 | + auth_info: models.AuthInfo = fastapi.Depends(auth.get_auth_info), |
| 148 | +) -> models.JobList: |
| 149 | + """Delete jobs from the processing queue. |
| 150 | +
|
| 151 | + Parameters |
| 152 | + ---------- |
| 153 | + request : models.DeleteJobsRequest |
| 154 | + Request body containing job IDs to delete. |
| 155 | +
|
| 156 | + Returns |
| 157 | + ------- |
| 158 | + models.JobList |
| 159 | + List of jobs that were successfully deleted. |
| 160 | + """ |
| 161 | + structlog.contextvars.bind_contextvars(user_uid=auth_info.user_uid) |
| 162 | + limits.check_rate_limits( |
| 163 | + SETTINGS.rate_limits.jobs.delete, |
| 164 | + auth_info, |
| 165 | + ) |
| 166 | + job_ids = request.job_ids |
| 167 | + compute_sessionmaker = db_utils.get_compute_sessionmaker( |
| 168 | + mode=db_utils.ConnectionMode.write |
| 169 | + ) |
| 170 | + jobs = [] |
| 171 | + with compute_sessionmaker() as compute_session: |
| 172 | + for job_id in job_ids: |
| 173 | + try: |
| 174 | + job = utils.get_job_from_broker_db( |
| 175 | + job_id=job_id, session=compute_session |
| 176 | + ) |
| 177 | + except ogc_api_processes_fastapi.exceptions.NoSuchJob: |
| 178 | + continue |
| 179 | + try: |
| 180 | + auth.verify_permission(auth_info.user_uid, job) |
| 181 | + except exceptions.PermissionDenied: |
| 182 | + continue |
| 183 | + job = cads_broker.database.set_dismissed_request( |
| 184 | + request_uid=job_id, session=compute_session |
| 185 | + ) |
| 186 | + jobs.append(utils.make_status_info(job)) |
| 187 | + job_list = models.JobList( |
| 188 | + jobs=jobs, |
| 189 | + ) |
| 190 | + return job_list |
0 commit comments