-
Notifications
You must be signed in to change notification settings - Fork 1
Add non-blocking PACE links to simulation details #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tomvothecoder
merged 3 commits into
E3SM-Project:main
from
tomvothecoder:feature/173-pace-links
May 5, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import json | ||
| import threading | ||
| import time | ||
| import urllib.error | ||
| import urllib.parse | ||
| import urllib.request | ||
| from typing import Annotated, Any | ||
|
|
||
| from fastapi import APIRouter, HTTPException, Query | ||
|
|
||
| from app.common.schemas.base import CamelOutBaseModel | ||
|
|
||
| PACE_BASE_URL = "https://pace.ornl.gov" | ||
| PACE_LOOKUP_TIMEOUT_SECONDS = 5.0 | ||
| PACE_CACHE_TTL_SECONDS = 300.0 | ||
|
|
||
| _PACE_CACHE_LOCK = threading.Lock() | ||
| _PACE_CACHE: dict[str, tuple[float, str | None]] = {} | ||
|
|
||
| router = APIRouter(prefix="/pace", tags=["PACE"]) | ||
|
|
||
|
|
||
| class PaceResolutionOut(CamelOutBaseModel): | ||
| execution_id: str | ||
| experiment_id: str | None | ||
|
|
||
|
|
||
| @router.get( | ||
| "/resolve", | ||
| response_model=PaceResolutionOut, | ||
| responses={ | ||
| 200: {"description": "PACE resolution result."}, | ||
| 422: {"description": "Validation error."}, | ||
| }, | ||
| ) | ||
| def resolve_pace_execution( | ||
| execution_id: Annotated[ | ||
| str, | ||
| Query( | ||
| ..., | ||
| description="Simulation execution ID to resolve to a PACE experiment ID.", | ||
| ), | ||
| ], | ||
| ) -> PaceResolutionOut: | ||
| normalized_execution_id = _normalize_execution_id(execution_id) | ||
|
|
||
| return PaceResolutionOut( | ||
| execution_id=normalized_execution_id, | ||
| experiment_id=_resolve_experiment_id(normalized_execution_id), | ||
| ) | ||
|
|
||
|
|
||
| def _normalize_execution_id(execution_id: str) -> str: | ||
| normalized_execution_id = execution_id.strip() | ||
| if not normalized_execution_id: | ||
| raise HTTPException(status_code=422, detail="execution_id must not be blank") | ||
|
|
||
| return normalized_execution_id | ||
|
|
||
|
|
||
| def _resolve_experiment_id(execution_id: str) -> str | None: | ||
| cache_hit, cached_experiment_id = _get_cached_experiment_id(execution_id) | ||
| if cache_hit: | ||
| return cached_experiment_id | ||
|
|
||
| request = urllib.request.Request( | ||
| _build_pace_lookup_url(execution_id), | ||
| headers={"Accept": "application/json"}, | ||
| ) | ||
|
|
||
| try: | ||
| with urllib.request.urlopen( | ||
| request, timeout=PACE_LOOKUP_TIMEOUT_SECONDS | ||
| ) as response: | ||
| if response.status != 200: | ||
| _set_cached_experiment_id(execution_id, None) | ||
| return None | ||
|
|
||
| response_body = response.read().decode("utf-8") | ||
| except ( | ||
| TimeoutError, | ||
| UnicodeDecodeError, | ||
| urllib.error.HTTPError, | ||
| urllib.error.URLError, | ||
| ): | ||
| _set_cached_experiment_id(execution_id, None) | ||
| return None | ||
|
|
||
| try: | ||
| payload = json.loads(response_body) | ||
|
tomvothecoder marked this conversation as resolved.
|
||
| except json.JSONDecodeError: | ||
| experiment_id = _extract_experiment_id(response_body) | ||
| else: | ||
| experiment_id = _extract_experiment_id(payload) | ||
|
|
||
| _set_cached_experiment_id(execution_id, experiment_id) | ||
| return experiment_id | ||
|
|
||
|
|
||
| def _build_pace_lookup_url(execution_id: str) -> str: | ||
| encoded_execution_id = urllib.parse.quote(execution_id, safe="") | ||
| return f"{PACE_BASE_URL}/ajax/specificSearch/lid:{encoded_execution_id}/expid" | ||
|
|
||
|
|
||
| def _extract_experiment_id(payload: Any) -> str | None: | ||
| direct_experiment_id = _normalize_experiment_id(payload) | ||
| if direct_experiment_id is not None: | ||
| return direct_experiment_id | ||
|
|
||
| if not isinstance(payload, list) or not payload: | ||
| return None | ||
|
|
||
| first_item = payload[0] | ||
| if not isinstance(first_item, dict): | ||
| return None | ||
|
|
||
| return _normalize_experiment_id(first_item.get("expid")) | ||
|
|
||
|
|
||
| def _normalize_experiment_id(value: Any) -> str | None: | ||
| if isinstance(value, int): | ||
| return str(value) | ||
|
|
||
| if not isinstance(value, str): | ||
| return None | ||
|
|
||
| normalized_experiment_id = value.strip() | ||
| if not normalized_experiment_id or not normalized_experiment_id.isdigit(): | ||
| return None | ||
|
|
||
| return normalized_experiment_id | ||
|
|
||
|
|
||
| def _get_cached_experiment_id(execution_id: str) -> tuple[bool, str | None]: | ||
| now = time.monotonic() | ||
| with _PACE_CACHE_LOCK: | ||
| cached_entry = _PACE_CACHE.get(execution_id) | ||
| if cached_entry is None: | ||
| return False, None | ||
|
|
||
| expires_at, experiment_id = cached_entry | ||
| if expires_at <= now: | ||
| _PACE_CACHE.pop(execution_id, None) | ||
| return False, None | ||
|
|
||
| return True, experiment_id | ||
|
|
||
|
|
||
| def _set_cached_experiment_id(execution_id: str, experiment_id: str | None) -> None: | ||
| with _PACE_CACHE_LOCK: | ||
| _PACE_CACHE[execution_id] = ( | ||
| time.monotonic() + PACE_CACHE_TTL_SECONDS, | ||
| experiment_id, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.