-
Notifications
You must be signed in to change notification settings - Fork 6
feat: sync transitfeeds data with the mdb #1451
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0cd8b73
[fun] sync transitfeeds with mdb
cka-y e4a42bb
[fun] added latest dataset + deprecated status
cka-y d7af85c
fix: returned response
cka-y 48bc25d
fix: lint + coding style
cka-y 508d3c8
feat: sync transitfeeds to mdb
cka-y 6663e77
merge: main
cka-y cfcb441
lint: fix
cka-y ec65ceb
Merge branch 'main' into feat/1017
cka-y 717c72a
fix: moved csv content outside of functions dir
cka-y 9e7faba
fix: datetime prob
cka-y 6658c9d
fix: using data from main
cka-y 8420cfa
Merge branch 'main' into feat/1017
cka-y 9dd038a
fix: unused constants
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,3 +28,6 @@ google-cloud-storage | |
| # Configuration | ||
| python-dotenv==1.0.0 | ||
| pycountry | ||
|
|
||
| # Other utilities | ||
| pandas | ||
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
99 changes: 99 additions & 0 deletions
99
functions-python/tasks_executor/src/tasks/data_import/data_import_utils.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,99 @@ | ||
| import logging | ||
| import uuid | ||
| from datetime import datetime | ||
| from typing import Tuple, Type, TypeVar | ||
|
|
||
| from sqlalchemy import select | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from shared.database_gen.sqlacodegen_models import ( | ||
| Feed, | ||
| Officialstatushistory, | ||
| Entitytype, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| T = TypeVar("T", bound="Feed") | ||
|
|
||
|
|
||
| def _get_or_create_entity_type(session: Session, entity_type_name: str) -> Entitytype: | ||
| """Get or create an Entitytype by name.""" | ||
| logger.debug("Looking up Entitytype name=%s", entity_type_name) | ||
| et = session.scalar(select(Entitytype).where(Entitytype.name == entity_type_name)) | ||
| if et: | ||
| logger.debug("Found existing Entitytype name=%s", entity_type_name) | ||
| return et | ||
| et = Entitytype(name=entity_type_name) | ||
| session.add(et) | ||
| session.flush() | ||
| logger.info("Created Entitytype name=%s", entity_type_name) | ||
| return et | ||
|
|
||
|
|
||
| def get_feed( | ||
| session: Session, | ||
| stable_id: str, | ||
| model: Type[T] = Feed, | ||
| ) -> T | None: | ||
| """Get a Feed by stable_id.""" | ||
| logger.debug("Lookup feed stable_id=%s", stable_id) | ||
| feed = session.scalar(select(model).where(model.stable_id == stable_id)) | ||
| if feed: | ||
| logger.debug("Found existing feed stable_id=%s id=%s", stable_id, feed.id) | ||
| else: | ||
| logger.debug("No Feed found with stable_id=%s", stable_id) | ||
| return feed | ||
|
|
||
|
|
||
| def _get_or_create_feed( | ||
| session: Session, | ||
| model: Type[T], | ||
| stable_id: str, | ||
| data_type: str, | ||
| is_official: bool = True, | ||
| official_notes: str = "Imported from JBDA as official feed.", | ||
cka-y marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| reviewer_email: str = "[email protected]", | ||
| ) -> Tuple[T, bool]: | ||
| """Generic helper to get or create a Feed subclass (Gtfsfeed, Gtfsrealtimefeed) by stable_id.""" | ||
| logger.debug( | ||
| "Lookup feed model=%s stable_id=%s", | ||
| getattr(model, "__name__", str(model)), | ||
| stable_id, | ||
| ) | ||
| feed = session.scalar(select(model).where(model.stable_id == stable_id)) | ||
| if feed: | ||
| logger.info( | ||
| "Found existing %s stable_id=%s id=%s", | ||
| getattr(model, "__name__", str(model)), | ||
| stable_id, | ||
| feed.id, | ||
| ) | ||
| return feed, False | ||
|
|
||
| new_id = str(uuid.uuid4()) | ||
| feed = model( | ||
| id=new_id, | ||
| data_type=data_type, | ||
| stable_id=stable_id, | ||
| official=is_official, | ||
| official_updated_at=datetime.now(), | ||
| ) | ||
| if is_official: | ||
| feed.officialstatushistories = [ | ||
| Officialstatushistory( | ||
| is_official=True, | ||
| reviewer_email=reviewer_email, | ||
| timestamp=datetime.now(), | ||
| notes=official_notes, | ||
| ) | ||
| ] | ||
| session.add(feed) | ||
| session.flush() | ||
| logger.info( | ||
| "Created %s stable_id=%s id=%s data_type=%s", | ||
| getattr(model, "__name__", str(model)), | ||
| stable_id, | ||
| new_id, | ||
| data_type, | ||
| ) | ||
| return feed, True | ||
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 |
|---|---|---|
|
|
@@ -20,27 +20,28 @@ | |
| import os | ||
| import uuid | ||
| from datetime import datetime | ||
| from typing import Optional, Tuple, Dict, Any, List, Final, Type, TypeVar | ||
| from typing import Optional, Tuple, Dict, Any, List, Final, TypeVar | ||
|
|
||
| import requests | ||
| import pycountry | ||
| import requests | ||
| from sqlalchemy import select, and_ | ||
| from sqlalchemy.orm import Session | ||
| from sqlalchemy.exc import IntegrityError | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from shared.common.locations_utils import create_or_get_location | ||
| from shared.database.database import with_db_session | ||
| from shared.database_gen.sqlacodegen_models import ( | ||
| Feed, | ||
| Gtfsfeed, | ||
| Gtfsrealtimefeed, | ||
| Entitytype, | ||
| Feedrelatedlink, | ||
| Externalid, | ||
| Officialstatushistory, | ||
| ) | ||
|
|
||
| from shared.helpers.pub_sub import trigger_dataset_download | ||
| from tasks.data_import.data_import_utils import ( | ||
| _get_or_create_entity_type, | ||
| _get_or_create_feed, | ||
| ) | ||
|
|
||
| T = TypeVar("T", bound="Feed") | ||
|
|
||
|
|
@@ -99,20 +100,6 @@ def import_jbda_handler(payload: dict | None = None) -> dict: | |
| return result | ||
|
|
||
|
|
||
| def _get_or_create_entity_type(session: Session, entity_type_name: str) -> Entitytype: | ||
| """Get or create an Entitytype by name.""" | ||
| logger.debug("Looking up Entitytype name=%s", entity_type_name) | ||
| et = session.scalar(select(Entitytype).where(Entitytype.name == entity_type_name)) | ||
| if et: | ||
| logger.debug("Found existing Entitytype name=%s", entity_type_name) | ||
| return et | ||
| et = Entitytype(name=entity_type_name) | ||
| session.add(et) | ||
| session.flush() | ||
| logger.info("Created Entitytype name=%s", entity_type_name) | ||
| return et | ||
|
|
||
|
|
||
| def get_gtfs_file_url( | ||
| detail_body: Dict[str, Any], rid: str = "current" | ||
| ) -> Optional[str]: | ||
|
|
@@ -144,53 +131,6 @@ def get_gtfs_file_url( | |
| return None | ||
|
|
||
|
|
||
| def _get_or_create_feed( | ||
| session: Session, model: Type[T], stable_id: str, data_type: str | ||
| ) -> Tuple[T, bool]: | ||
| """Generic helper to get or create a Feed subclass (Gtfsfeed, Gtfsrealtimefeed) by stable_id.""" | ||
| logger.debug( | ||
| "Lookup feed model=%s stable_id=%s", | ||
| getattr(model, "__name__", str(model)), | ||
| stable_id, | ||
| ) | ||
| feed = session.scalar(select(model).where(model.stable_id == stable_id)) | ||
| if feed: | ||
| logger.info( | ||
| "Found existing %s stable_id=%s id=%s", | ||
| getattr(model, "__name__", str(model)), | ||
| stable_id, | ||
| feed.id, | ||
| ) | ||
| return feed, False | ||
|
|
||
| new_id = str(uuid.uuid4()) | ||
| feed = model( | ||
| id=new_id, | ||
| data_type=data_type, | ||
| stable_id=stable_id, | ||
| official=True, | ||
| official_updated_at=datetime.now(), | ||
| ) | ||
| feed.officialstatushistories = [ | ||
| Officialstatushistory( | ||
| is_official=True, | ||
| reviewer_email="[email protected]", | ||
| timestamp=datetime.now(), | ||
| notes="Imported from JBDA as official feed.", | ||
| ) | ||
| ] | ||
| session.add(feed) | ||
| session.flush() | ||
| logger.info( | ||
| "Created %s stable_id=%s id=%s data_type=%s", | ||
| getattr(model, "__name__", str(model)), | ||
| stable_id, | ||
| new_id, | ||
| data_type, | ||
| ) | ||
| return feed, True | ||
|
|
||
|
|
||
| def _update_common_feed_fields( | ||
| feed: Feed, list_item: dict, detail: dict, producer_url: str | ||
| ) -> None: | ||
|
|
||
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.