-
Notifications
You must be signed in to change notification settings - Fork 6
feat: added batch fill for dataset size and files #1302
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
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f31c789
added batch fill for dataset size and files
cka-y 3d09173
updated readme
cka-y 38cc37d
tests
cka-y b56a193
Merge branch 'main' into feat/1260
cka-y f1f331f
updated process dataset function
cka-y c671e8c
pr comments
cka-y f83037c
adding secret to task executor
cka-y cb7a075
adding secret to task executor
cka-y 038c89c
modifies secret manager
cka-y 2c2e625
fix: unique secret dicts in tf
cka-y 7070698
fix: unique_secret_dict_map
cka-y 9ecc333
merge main
cka-y 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
72 changes: 72 additions & 0 deletions
72
functions-python/tasks_executor/src/tasks/dataset_files/README.md
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,72 @@ | ||
| # Rebuild Missing Dataset Files | ||
|
|
||
| This task rebuilds missing extracted files in GTFS datasets. | ||
| It downloads datasets from their `hosted_url`, extracts all files, computes zipped and unzipped sizes, calculates hashes, uploads the files to a GCS bucket, and updates the database. | ||
|
|
||
| --- | ||
|
|
||
| ## Task ID | ||
|
|
||
| Use task ID: `rebuild_missing_dataset_files` | ||
|
|
||
| --- | ||
|
|
||
| ## Usage | ||
|
|
||
| The function accepts the following payload: | ||
|
|
||
| ```json | ||
| { | ||
| "dry_run": true, // [optional] If true, do not upload or modify the database (default: true) | ||
| "after_date": "YYYY-MM-DD", // [optional] Only include datasets downloaded after this ISO date | ||
| "latest_only": true // [optional] If true, only process the latest version of each dataset (default: true) | ||
|
cka-y marked this conversation as resolved.
|
||
| } | ||
| ``` | ||
|
|
||
| ### Example: | ||
|
|
||
| ```json | ||
| { | ||
| "dry_run": false, | ||
| "after_date": "2025-07-01", | ||
| "latest_only": true | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## What It Does | ||
|
|
||
| For each GTFS dataset with missing file information (missing zipped/unzipped sizes or missing extracted files), this function: | ||
|
|
||
| 1. Downloads the `.zip` file from its `hosted_url` | ||
| 2. Computes the zipped size in bytes | ||
| 3. Extracts all GTFS files locally | ||
| 4. Computes the unzipped size in bytes | ||
| 5. Uploads each extracted file to a GCS bucket with the structure: | ||
|
|
||
| ``` | ||
| {feed-stable-id}/{dataset-stable-id}/extracted/{file_name} | ||
| ``` | ||
| 6. Makes each file publicly accessible and stores its GCS URL | ||
| 7. Computes SHA256 hashes for each file | ||
| 8. Stores metadata in the `Gtfsfile` table for later use | ||
|
|
||
| --- | ||
|
|
||
| ## GCP Environment Variables | ||
|
|
||
| The function requires the following environment variables: | ||
|
|
||
| | Variable | Description | | ||
| | ---------------------- | ---------------------------------------------------------------------------- | | ||
| | `DATASETS_BUCKET_NAME` | The name of the GCS bucket used to store extracted GTFS files | | ||
|
|
||
| --- | ||
|
|
||
| ## Additional Notes | ||
|
|
||
| * This function **disables SSL verification** when downloading files, as the sources are trusted internally. | ||
| * Commits to the database occur in batches of 5 datasets to improve performance and avoid large transaction blocks. | ||
| * If `dry_run` is enabled, no downloads, uploads, or DB modifications are performed. Only the number of affected datasets is logged. | ||
| * The function is safe to rerun. It will only affect datasets missing required file metadata. | ||
213 changes: 213 additions & 0 deletions
213
functions-python/tasks_executor/src/tasks/dataset_files/rebuild_missing_dataset_files.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,213 @@ | ||
| import logging | ||
| import os | ||
| import uuid | ||
| import hashlib | ||
| import shutil | ||
| import tempfile | ||
| import zipfile | ||
| import urllib.request | ||
| import ssl | ||
|
|
||
| from google.cloud import storage | ||
| from shared.database.database import with_db_session | ||
| from shared.database_gen.sqlacodegen_models import Gtfsdataset, Gtfsfile | ||
|
|
||
| # Disable SSL verification — trusted internal sources only | ||
| ssl._create_default_https_context = ssl._create_unverified_context | ||
|
cka-y marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def rebuild_missing_dataset_files_handler(payload) -> dict: | ||
| """ | ||
| Entry point for rebuilding missing GTFS dataset files. | ||
|
|
||
| Args: | ||
| payload (dict): Task parameters including 'dry_run' and 'after_date'. | ||
|
|
||
| Returns: | ||
| dict: Result message and number of datasets processed. | ||
| """ | ||
| dry_run = payload.get("dry_run", True) | ||
| after_date = payload.get("after_date", None) | ||
| latest_only = payload.get("latest_only", True) | ||
|
|
||
| return rebuild_missing_dataset_files( | ||
| dry_run=dry_run, after_date=after_date, latest_only=latest_only | ||
| ) | ||
|
|
||
|
|
||
| def get_datasets_with_missing_files_query(db_session, after_date, latest_only): | ||
| """ | ||
| Query GTFS datasets missing zipped/unzipped size or extracted files. | ||
|
|
||
| Args: | ||
| db_session: SQLAlchemy DB session. | ||
| after_date (str): Filter datasets downloaded after this ISO date. | ||
| latest_only (bool): Whether to include only latest datasets. | ||
|
|
||
| Returns: | ||
| Query: SQLAlchemy query object. | ||
| """ | ||
| query = ( | ||
| db_session.query(Gtfsdataset) | ||
| .filter(~Gtfsdataset.hosted_url.is_(None)) | ||
| .filter( | ||
| Gtfsdataset.zipped_size_bytes.is_(None) | ||
| | Gtfsdataset.unzipped_size_bytes.is_(None) | ||
| | ~Gtfsdataset.gtfsfiles.any() | ||
| ) | ||
| ) | ||
|
|
||
| if after_date: | ||
| query = query.filter(Gtfsdataset.downloaded_at >= after_date) | ||
|
|
||
| if latest_only: | ||
| query = query.filter(Gtfsdataset.latest) | ||
|
|
||
| return query | ||
|
|
||
|
|
||
| def download_to_file(url: str, local_path: str): | ||
|
cka-y marked this conversation as resolved.
Outdated
|
||
| """ | ||
| Downloads a file from URL and writes it to local disk. | ||
| """ | ||
| with urllib.request.urlopen(url) as response, open(local_path, "wb") as out_file: | ||
| shutil.copyfileobj(response, out_file) | ||
|
|
||
|
|
||
| def process_dataset(dataset: Gtfsdataset): | ||
| """ | ||
| Downloads, extracts, uploads, and indexes files for a GTFS dataset. | ||
|
|
||
| Args: | ||
| dataset (Gtfsdataset): The dataset to process. | ||
| """ | ||
| hosted_url = dataset.hosted_url | ||
| stable_id = dataset.stable_id | ||
| logging.info("Processing dataset %s with URL %s", stable_id, hosted_url) | ||
| bucket_name = os.environ["DATASETS_BUCKET_NAME"] | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| zip_path = os.path.join(tmp_dir, "dataset.zip") | ||
| download_to_file(hosted_url, zip_path) | ||
| dataset.zipped_size_bytes = os.path.getsize(zip_path) | ||
|
|
||
| with zipfile.ZipFile(zip_path, "r") as zip_ref: | ||
| zip_ref.extractall(tmp_dir) | ||
|
|
||
| dataset.unzipped_size_bytes = sum( | ||
| os.path.getsize(os.path.join(root, f)) | ||
| for root, _, files in os.walk(tmp_dir) | ||
| for f in files | ||
| ) | ||
|
|
||
| storage_client = storage.Client() | ||
| bucket = storage_client.bucket(bucket_name) | ||
| gtfs_files = [] | ||
|
|
||
| for root, _, files in os.walk(tmp_dir): | ||
| for file_name in files: | ||
| file_path = os.path.join(root, file_name) | ||
| blob_path = f"{'-'.join(stable_id.split('-')[:2])}/{stable_id}/extracted/{file_name}" | ||
|
cka-y marked this conversation as resolved.
Outdated
|
||
| blob = bucket.blob(blob_path) | ||
| blob.upload_from_filename(file_path) | ||
| blob.make_public() | ||
|
cka-y marked this conversation as resolved.
Outdated
|
||
|
|
||
| with open(file_path, "rb") as f: | ||
| sha256_hash = hashlib.sha256(f.read()).hexdigest() | ||
|
|
||
| gtfs_files.append( | ||
| Gtfsfile( | ||
| id=str(uuid.uuid4()), | ||
| file_name=file_name, | ||
| file_size_bytes=os.path.getsize(file_path), | ||
| hash=sha256_hash, | ||
| hosted_url=blob.public_url, | ||
| ) | ||
| ) | ||
|
|
||
| dataset.gtfsfiles = gtfs_files | ||
| extracted_data = { | ||
| "zipped_size_bytes": dataset.zipped_size_bytes, | ||
| "unzipped_size_bytes": dataset.unzipped_size_bytes, | ||
| "file_count": len(gtfs_files), | ||
| "files": [ | ||
| { | ||
| "file_name": file.file_name, | ||
| "file_size_bytes": file.file_size_bytes, | ||
| "hash": file.hash, | ||
| "hosted_url": file.hosted_url, | ||
| } | ||
| for file in gtfs_files | ||
| ], | ||
| } | ||
| logging.info(extracted_data) | ||
|
|
||
|
|
||
| @with_db_session | ||
| def rebuild_missing_dataset_files( | ||
| db_session, | ||
| dry_run: bool = True, | ||
| after_date: str = None, | ||
| latest_only: bool = True, | ||
| ) -> dict: | ||
| """ | ||
| Processes GTFS datasets missing extracted files and updates database. | ||
|
|
||
| Args: | ||
| db_session: SQLAlchemy DB session. | ||
| dry_run (bool): If True, only logs how many would be processed. | ||
| after_date (str): Only consider datasets downloaded after this ISO date. | ||
| latest_only (bool): Whether to include only latest datasets. | ||
|
|
||
| Returns: | ||
| dict: Result summary. | ||
| """ | ||
| datasets = get_datasets_with_missing_files_query( | ||
| db_session, after_date=after_date, latest_only=latest_only | ||
| ) | ||
|
|
||
| if dry_run: | ||
| total = datasets.count() | ||
| logging.info( | ||
| "Dry run mode: %d datasets found with missing files. After date filter: %s", | ||
| total, | ||
| after_date, | ||
| ) | ||
| return { | ||
| "message": f"Dry run: {total} datasets with missing files found.", | ||
| "total_processed": total, | ||
| } | ||
|
|
||
| total_processed = 0 | ||
| count = 0 | ||
| batch_count = 5 | ||
|
cka-y marked this conversation as resolved.
|
||
| logging.info("Starting to process datasets with missing files...") | ||
|
|
||
| for dataset in datasets.all(): | ||
| process_dataset(dataset) | ||
|
cka-y marked this conversation as resolved.
Outdated
|
||
| count += 1 | ||
| total_processed += 1 | ||
|
|
||
| if count % batch_count == 0: | ||
| db_session.commit() | ||
| logging.info( | ||
| "Committed %d datasets. Total processed: %d", | ||
| batch_count, | ||
| total_processed, | ||
| ) | ||
|
|
||
| db_session.commit() | ||
| logging.info("All datasets processed and committed. Total: %d", total_processed) | ||
|
|
||
| result = { | ||
| "message": "Rebuild missing dataset files completed.", | ||
| "total_processed": total_processed, | ||
| "params": { | ||
| "dry_run": dry_run, | ||
| "after_date": after_date, | ||
| "latest_only": latest_only, | ||
| "datasets_bucket_name": os.environ.get("DATASETS_BUCKET_NAME"), | ||
| }, | ||
| } | ||
| logging.info("Task summary: %s", result) | ||
| return result | ||
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ALTER TABLE GtfsFile DROP COLUMN IF EXISTS hash; | ||
| ALTER TABLE GtfsFile DROP COLUMN IF EXISTS hosted_url; | ||
| ALTER TABLE GtfsFile ADD COLUMN hash varchar(255); | ||
| ALTER TABLE GtfsFile ADD COLUMN hosted_url varchar(255); | ||
|
|
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.