-
Notifications
You must be signed in to change notification settings - Fork 0
Time selection for web downloads #263
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
Open
klpoland
wants to merge
7
commits into
master
Choose a base branch
from
feature-kpoland-add-drf-time-selection-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2b3748d
adding time based filtering and slider ui
klpoland 39c4885
fix label updates
klpoland 1edcd0b
fix backend issues, enhance filter labels
klpoland bb97412
add start and end time inputs
klpoland 5aae2ca
add tests, generalize download modal, move js from template to manage…
klpoland 0aa1c3a
add flatpickr to handle datetime selection for better control
klpoland d53e603
clean up download action manager, refactor js, filters, tests
klpoland 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
133 changes: 133 additions & 0 deletions
133
gateway/sds_gateway/api_methods/helpers/temporal_filtering.py
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,133 @@ | ||
| import re | ||
|
|
||
| from django.db.models import QuerySet | ||
|
|
||
| from opensearchpy.exceptions import NotFoundError as OpenSearchNotFoundError | ||
| from sds_gateway.api_methods.models import CaptureType, Capture, File, DRF_RF_FILENAME_REGEX_STR | ||
| from sds_gateway.api_methods.utils.opensearch_client import get_opensearch_client | ||
| from sds_gateway.api_methods.utils.relationship_utils import get_capture_files | ||
| from loguru import logger as log | ||
|
|
||
| # Digital RF spec: rf@SECONDS.MILLISECONDS.h5 (e.g. rf@1396379502.000.h5) | ||
| # https://github.com/MITHaystack/digital_rf | ||
| DRF_RF_FILENAME_PATTERN = re.compile( | ||
| r"^rf@(\d+)\.(\d+)\.h5$", | ||
| re.IGNORECASE, | ||
| ) | ||
|
|
||
|
|
||
| def drf_rf_filename_from_ms(ms: int) -> str: | ||
| """Format ms as DRF rf data filename (canonical for range queries).""" | ||
| return f"rf@{ms // 1000}.{ms % 1000:03d}.h5" | ||
|
|
||
|
|
||
| def drf_rf_filename_to_ms(file_name: str) -> int | None: | ||
| """ | ||
| Parse DRF rf data filename to milliseconds. | ||
| Handles rf@SECONDS.MILLISECONDS.h5; fractional part padded to 3 digits. | ||
| """ | ||
| name = file_name.strip() | ||
| match = DRF_RF_FILENAME_PATTERN.match(name) | ||
| if not match: | ||
| return None | ||
| try: | ||
| seconds = int(match.group(1)) | ||
| frac = match.group(2).ljust(3, "0")[:3] | ||
| return seconds * 1000 + int(frac) | ||
| except (ValueError, TypeError): | ||
| return None | ||
|
|
||
|
|
||
| def _catch_capture_type_error(capture_type: CaptureType) -> None: | ||
| if capture_type != CaptureType.DigitalRF: | ||
| msg = "Only DigitalRF captures are supported for temporal filtering." | ||
| log.error(msg) | ||
| raise ValueError(msg) | ||
|
|
||
|
|
||
| def get_capture_bounds(capture_type: CaptureType, capture_uuid: str) -> tuple[int, int]: | ||
| """Get start and end bounds for capture from opensearch.""" | ||
| _catch_capture_type_error(capture_type) | ||
|
|
||
| client = get_opensearch_client() | ||
| index = f"captures-{capture_type}" | ||
|
|
||
| try: | ||
| response = client.get(index=index, id=capture_uuid) | ||
| except OpenSearchNotFoundError as e: | ||
| raise ValueError( | ||
| f"Capture {capture_uuid} not found in OpenSearch index {index}" | ||
| ) from e | ||
|
|
||
| if not response.get("found"): | ||
| raise ValueError( | ||
| f"Capture {capture_uuid} not found in OpenSearch index {index}" | ||
| ) | ||
|
|
||
| source = response.get("_source", {}) | ||
| search_props = source.get("search_props", {}) | ||
| start_time = search_props.get("start_time", 0) | ||
| end_time = search_props.get("end_time", 0) | ||
| return start_time, end_time | ||
|
|
||
|
|
||
| def get_file_cadence(capture_type: CaptureType, capture: Capture) -> int: | ||
| """Get the file cadence in milliseconds. OpenSearch bounds are in seconds.""" | ||
| _catch_capture_type_error(capture_type) | ||
|
|
||
| capture_uuid = str(capture.uuid) | ||
| start_time, end_time = get_capture_bounds(capture_type, capture_uuid) | ||
|
|
||
| count = capture.get_drf_data_files_stats()["total_count"] | ||
| if count == 0: | ||
| return 0 | ||
| duration_sec = end_time - start_time | ||
| duration_ms = duration_sec * 1000 | ||
| return max(1, int(duration_ms / count)) | ||
|
|
||
|
|
||
| def filter_capture_data_files_selection_bounds( | ||
| capture_type: CaptureType, | ||
| capture: Capture, | ||
| start_time: int, # relative ms from start of capture (from UI) | ||
| end_time: int, # relative ms from start of capture (from UI) | ||
| ) -> QuerySet[File]: | ||
| """Filter the capture file selection bounds to the given start and end times.""" | ||
| _catch_capture_type_error(capture_type) | ||
| epoch_start_sec, _ = get_capture_bounds(capture_type, str(capture.uuid)) | ||
| epoch_start_ms = epoch_start_sec * 1000 | ||
| start_ms = epoch_start_ms + start_time | ||
| end_ms = epoch_start_ms + end_time | ||
|
|
||
| start_file_name = drf_rf_filename_from_ms(start_ms) | ||
| end_file_name = drf_rf_filename_from_ms(end_ms) | ||
|
|
||
| data_files = capture.get_drf_data_files_queryset() | ||
| return data_files.filter( | ||
| name__gte=start_file_name, | ||
| name__lte=end_file_name, | ||
| ).order_by("name") | ||
|
|
||
| def get_capture_files_with_temporal_filter( | ||
| capture_type: CaptureType, | ||
| capture: Capture, | ||
| start_time: int | None = None, # milliseconds since start of capture | ||
| end_time: int | None = None, | ||
| ) -> QuerySet[File]: | ||
| """Get the capture files with temporal filtering.""" | ||
| _catch_capture_type_error(capture_type) | ||
|
|
||
| if start_time is None or end_time is None: | ||
| log.warning("Start or end time is None, returning all capture files without temporal filtering") | ||
| return get_capture_files(capture) | ||
|
|
||
| # get non-data files | ||
| non_data_files = get_capture_files(capture).exclude(name__regex=DRF_RF_FILENAME_REGEX_STR) | ||
|
|
||
| # get data files with temporal filtering | ||
| data_files = filter_capture_data_files_selection_bounds( | ||
| capture_type, capture, start_time, end_time | ||
| ) | ||
|
|
||
| # return all files | ||
| return non_data_files.union(data_files) |
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.