-
Notifications
You must be signed in to change notification settings - Fork 0
Webapp file ingestion changes #206
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
Mesh-ach
wants to merge
7
commits into
develop
Choose a base branch
from
WebappFileIngestionChanges
base: develop
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.
+333
−2
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
39e8715
added new list bronze datasets endpoint
Mesh-ach b80c003
added databricks to gcs upload functionality
Mesh-ach d70ed0e
fixed formatting
Mesh-ach 0403afb
fixed formatting
Mesh-ach fbec688
fixed formatting
Mesh-ach bb3447b
fixed formatting
Mesh-ach 3f06762
fixed formatting
Mesh-ach 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
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
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
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 |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import logging | ||
| from sqlalchemy.exc import IntegrityError | ||
| import re | ||
| import requests | ||
| from ..validation import HardValidationError | ||
| from ..validation_error_formatter import format_validation_error | ||
| import pandas as pd | ||
|
|
@@ -179,6 +180,18 @@ class ValidationResult(BaseModel): | |
| source: str | ||
|
|
||
|
|
||
| class BronzeImportRequest(BaseModel): | ||
| """Request to import a dataset from the institution's bronze volume into GCS.""" | ||
|
|
||
| name: str | ||
|
|
||
|
|
||
| class BronzeImportResponse(BaseModel): | ||
| """Response for bronze import request.""" | ||
|
|
||
| file_name: str | ||
|
|
||
|
|
||
| class DataOverview(BaseModel): | ||
| """All data for a given institution (batches and files).""" | ||
|
|
||
|
|
@@ -1659,6 +1672,132 @@ def get_upload_url( | |
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(ve)) | ||
|
|
||
|
|
||
| @router.get("/{inst_id}/input/bronze-datasets", response_model=list[str]) | ||
| def list_bronze_datasets( | ||
| inst_id: str, | ||
| current_user: Annotated[BaseUser, Depends(get_current_active_user)], | ||
| sql_session: Annotated[Session, Depends(get_session)], | ||
| databricks_control: Annotated[DatabricksControl, Depends(DatabricksControl)], | ||
| ) -> Any: | ||
| """List `.csv` files directly under the institution's Databricks bronze volume root.""" | ||
| has_access_to_inst_or_err(inst_id, current_user) | ||
| local_session.set(sql_session) | ||
|
|
||
| inst = ( | ||
| local_session.get() | ||
| .execute(select(InstTable).where(InstTable.id == str_to_uuid(inst_id))) | ||
| .scalar_one_or_none() | ||
| ) | ||
| if inst is None: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_404_NOT_FOUND, | ||
| detail="Institution not found.", | ||
| ) | ||
|
|
||
| try: | ||
| return databricks_control.list_bronze_volume_csvs(inst.name) | ||
| except ValueError as ve: | ||
| msg = str(ve) | ||
| if "not configured" in msg.lower(): | ||
| raise HTTPException(status_code=status.HTTP_501_NOT_IMPLEMENTED, detail=msg) | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=msg | ||
| ) | ||
|
|
||
|
|
||
| @router.post( | ||
| "/{inst_id}/input/upload-from-volume-to-gcs-bucket", | ||
| response_model=BronzeImportResponse, | ||
| ) | ||
| def upload_from_volume_to_gcs_bucket( | ||
| inst_id: str, | ||
| req: BronzeImportRequest, | ||
| current_user: Annotated[BaseUser, Depends(get_current_active_user)], | ||
| sql_session: Annotated[Session, Depends(get_session)], | ||
| storage_control: Annotated[StorageControl, Depends(StorageControl)], | ||
| databricks_control: Annotated[DatabricksControl, Depends(DatabricksControl)], | ||
| ) -> Any: | ||
| """Import a selected dataset from the institution's bronze volume into GCS unvalidated/.""" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can a user select multiple datasets? For example a cohort and a course file? |
||
| has_access_to_inst_or_err(inst_id, current_user) | ||
| local_session.set(sql_session) | ||
|
|
||
| inst = ( | ||
| local_session.get() | ||
| .execute(select(InstTable).where(InstTable.id == str_to_uuid(inst_id))) | ||
| .scalar_one_or_none() | ||
| ) | ||
| if inst is None: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_404_NOT_FOUND, | ||
| detail="Institution not found.", | ||
| ) | ||
|
|
||
| requested_name = (req.name or "").strip() | ||
| if not requested_name: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
| detail="Dataset name is required.", | ||
| ) | ||
| if "/" in requested_name: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
| detail="Dataset name can't contain '/'.", | ||
| ) | ||
|
|
||
| # Ensure this is actually present in the bronze root (and matches naming rules). | ||
| try: | ||
| available = databricks_control.list_bronze_volume_csvs(inst.name) | ||
| except ValueError as ve: | ||
| msg = str(ve) | ||
| if "not configured" in msg.lower(): | ||
| raise HTTPException(status_code=status.HTTP_501_NOT_IMPLEMENTED, detail=msg) | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=msg | ||
| ) | ||
|
|
||
| available_map = {x.lower(): x for x in available} | ||
| file_name = available_map.get(requested_name.lower()) | ||
| if not file_name: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_404_NOT_FOUND, | ||
| detail="Bronze dataset not found.", | ||
| ) | ||
|
|
||
| stream = None | ||
| try: | ||
| stream = databricks_control.download_bronze_volume_file(inst.name, file_name) | ||
| upload_url = storage_control.generate_upload_signed_url( | ||
| get_external_bucket_name(inst_id), file_name | ||
| ) | ||
| resp = requests.put( | ||
| upload_url, | ||
| data=stream, | ||
| headers={"Content-Type": "text/csv"}, | ||
| timeout=600, | ||
| ) | ||
| resp.raise_for_status() | ||
| except ValueError as ve: | ||
| raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(ve)) | ||
| except requests.RequestException as rexc: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| detail=f"Failed to upload dataset to GCS: {rexc}", | ||
| ) | ||
| except Exception as e: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| detail=f"Unexpected error importing dataset: {e}", | ||
| ) | ||
| finally: | ||
| if stream is not None and hasattr(stream, "close"): | ||
| try: | ||
| stream.close() | ||
| except Exception: | ||
| pass | ||
|
|
||
| return {"file_name": file_name} | ||
|
|
||
|
|
||
| @router.post("/{inst_id}/add-custom-school-job/{job_run_id}") | ||
| def add_custom_school_job( | ||
| inst_id: str, | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So frontend flow will be... FE first list available datasets through
"/{inst_id}/input/bronze-datasets", then user selects a CSV, then clicks upload or something (which then makes a call to"/{inst_id}/input/upload-from-volume-to-gcs-bucket") and this creates an unvalidated batch? Then we proceed with validation to create a batch correct?