Skip to content

Commit 354104e

Browse files
authored
Update results href (#203)
* update results href * update * update protocol stripping
1 parent aa32a8a commit 354104e

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

cads_processing_api_service/config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919

20+
import os
21+
import random
22+
2023
import pydantic_settings
2124

2225
API_REQUEST_TEMPLATE = """import cdsapi
@@ -64,10 +67,20 @@ class Settings(pydantic_settings.BaseSettings):
6467
anonymous_licences_message: str = ANONYMOUS_LICENCES_MESSAGE
6568
deprecation_warning_message: str = DEPRECATION_WARNING_MESSAGE
6669

70+
download_nodes_config: str = "/etc/retrieve-api/download-nodes.config"
71+
6772
@property
6873
def profiles_api_url(self) -> str:
6974
return f"http://{self.profiles_service}:{self.profiles_api_service_port}"
7075

76+
@property
77+
def data_volume(self) -> str:
78+
data_volumes_config_path = self.download_nodes_config
79+
with open(data_volumes_config_path) as fp:
80+
data_volumes = [os.path.expandvars(line.rstrip("\n")) for line in fp]
81+
data_volume = random.choice(data_volumes)
82+
return data_volume
83+
7184

7285
def ensure_settings(
7386
settings: Settings | None = None,

cads_processing_api_service/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import datetime
1919
import enum
2020
import threading
21+
import urllib.parse
2122
from typing import Any, Callable, Mapping
2223

2324
import cachetools
@@ -469,6 +470,14 @@ def get_job_from_broker_db(
469470
return job
470471

471472

473+
def update_results_href(local_path: str, data_volume: str | None = None) -> str:
474+
if data_volume is None:
475+
data_volume = config.ensure_settings().data_volume
476+
file_path = local_path.split("://", 1)[-1]
477+
results_href = urllib.parse.urljoin(data_volume, file_path)
478+
return results_href
479+
480+
472481
def get_results_from_job(
473482
job: cads_broker.SystemRequest, session: sqlalchemy.orm.Session
474483
) -> dict[str, Any]:
@@ -496,11 +505,12 @@ def get_results_from_job(
496505
if job_status == "successful":
497506
try:
498507
asset_value = job.cache_entry.result["args"][0] # type: ignore
499-
results = {"asset": {"value": asset_value}}
500508
except Exception:
501509
raise exceptions.JobResultsExpired(
502510
detail=f"results of job {job_id} expired"
503511
)
512+
asset_value["href"] = update_results_href(asset_value["file:local_path"])
513+
results = {"asset": {"value": asset_value}}
504514
elif job_status == "failed":
505515
error_messages = get_job_events(
506516
job=job, session=session, event_type="user_visible_error"

tests/test_30_utils.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,47 @@ def test_get_job_from_broker_db() -> None:
243243
job = utils.get_job_from_broker_db("1234", session=mock_session)
244244

245245

246+
def test_update_results_href() -> None:
247+
data_volume = "http://data_volume/"
248+
249+
local_path = "protocol://results/1234"
250+
updated_href = utils.update_results_href(local_path, data_volume)
251+
exp_updated_href = "http://data_volume/results/1234"
252+
assert updated_href == exp_updated_href
253+
254+
local_path = "results/1234"
255+
updated_href = utils.update_results_href(local_path, data_volume)
256+
exp_updated_href = "http://data_volume/results/1234"
257+
assert updated_href == exp_updated_href
258+
259+
246260
def test_get_results_from_job() -> None:
247261
mock_session = unittest.mock.Mock(spec=sqlalchemy.orm.Session)
248262
job = cads_broker.SystemRequest(
249263
**{
250264
"status": "successful",
251265
"request_uid": "1234",
252266
"cache_entry": cacholote.database.CacheEntry(
253-
result={"args": [{"key": "value"}]}
267+
result={
268+
"args": [{"key": "value", "file:local_path": "test_local_path"}]
269+
}
254270
),
255271
}
256272
)
257-
results = utils.get_results_from_job(job, session=mock_session)
258-
exp_results = {"asset": {"value": {"key": "value"}}}
273+
with unittest.mock.patch(
274+
"cads_processing_api_service.utils.update_results_href"
275+
) as mock_update_results_href:
276+
mock_update_results_href.return_value = "test_href"
277+
results = utils.get_results_from_job(job, session=mock_session)
278+
exp_results = {
279+
"asset": {
280+
"value": {
281+
"key": "value",
282+
"file:local_path": "test_local_path",
283+
"href": "test_href",
284+
}
285+
}
286+
}
259287
assert results == exp_results
260288

261289
job = cads_broker.SystemRequest(

0 commit comments

Comments
 (0)