Skip to content

Commit 3395f43

Browse files
authored
feat: Transport Data Gouv feeds fetching (#1478)
1 parent d362425 commit 3395f43

File tree

13 files changed

+1108
-16
lines changed

13 files changed

+1108
-16
lines changed

.github/workflows/api-deployer.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,13 @@ jobs:
297297
env:
298298
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
299299
TRANSITLAND_API_KEY: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/TansitLand API Key/credential"
300+
TDG_API_TOKEN: "op://rbiv7rvkkrsdlpcrz3bmv7nmcu/Transport.data.gouv.fr API Token/credential"
300301
OPERATIONS_OAUTH2_CLIENT_ID: ${{ inputs.OPERATIONS_OAUTH2_CLIENT_ID_1PASSWORD }}
301302

302303
- name: Populate Variables
303304
run: |
304305
scripts/replace-variables.sh -in_file infra/backend.conf.rename_me -out_file infra/backend.conf -variables BUCKET_NAME,OBJECT_PREFIX
305-
scripts/replace-variables.sh -in_file infra/vars.tfvars.rename_me -out_file infra/vars.tfvars -variables PROJECT_ID,REGION,ENVIRONMENT,DEPLOYER_SERVICE_ACCOUNT,FEED_API_IMAGE_VERSION,OAUTH2_CLIENT_ID,OAUTH2_CLIENT_SECRET,GLOBAL_RATE_LIMIT_REQ_PER_MINUTE,ARTIFACT_REPO_NAME,VALIDATOR_ENDPOINT,TRANSITLAND_API_KEY,OPERATIONS_OAUTH2_CLIENT_ID
306+
scripts/replace-variables.sh -in_file infra/vars.tfvars.rename_me -out_file infra/vars.tfvars -variables PROJECT_ID,REGION,ENVIRONMENT,DEPLOYER_SERVICE_ACCOUNT,FEED_API_IMAGE_VERSION,OAUTH2_CLIENT_ID,OAUTH2_CLIENT_SECRET,GLOBAL_RATE_LIMIT_REQ_PER_MINUTE,ARTIFACT_REPO_NAME,VALIDATOR_ENDPOINT,TRANSITLAND_API_KEY,OPERATIONS_OAUTH2_CLIENT_ID,TDG_API_TOKEN
306307
307308
- uses: hashicorp/setup-terraform@v3
308309
with:

functions-python/tasks_executor/function_config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"environment_variables": [
1111
{
1212
"key": "DATASETS_BUCKET_NAME"
13+
},
14+
{
15+
"key": "TDG_API_TOKEN"
1316
}
1417
],
1518
"secret_environment_variables": [

functions-python/tasks_executor/src/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from shared.helpers.logger import init_logger
2424
from tasks.data_import.transitfeeds.sync_transitfeeds import sync_transitfeeds_handler
25+
from tasks.data_import.transportdatagouv.import_tdg_feeds import import_tdg_handler
2526
from tasks.dataset_files.rebuild_missing_dataset_files import (
2627
rebuild_missing_dataset_files_handler,
2728
)
@@ -109,6 +110,10 @@
109110
"description": "Syncs data from TransitFeeds to the database.",
110111
"handler": sync_transitfeeds_handler,
111112
},
113+
"tdg_import": {
114+
"description": "Imports TDG data into the system.",
115+
"handler": import_tdg_handler,
116+
},
112117
}
113118

114119

functions-python/tasks_executor/src/tasks/data_import/data_import_utils.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import uuid
33
from datetime import datetime
4-
from typing import Tuple, Type, TypeVar
4+
from typing import Tuple, Type, TypeVar, Optional
55

66
from sqlalchemy import select
77
from sqlalchemy.orm import Session
@@ -10,13 +10,14 @@
1010
Feed,
1111
Officialstatushistory,
1212
Entitytype,
13+
License,
1314
)
1415

1516
logger = logging.getLogger(__name__)
1617
T = TypeVar("T", bound="Feed")
1718

1819

19-
def _get_or_create_entity_type(session: Session, entity_type_name: str) -> Entitytype:
20+
def get_or_create_entity_type(session: Session, entity_type_name: str) -> Entitytype:
2021
"""Get or create an Entitytype by name."""
2122
logger.debug("Looking up Entitytype name=%s", entity_type_name)
2223
et = session.scalar(select(Entitytype).where(Entitytype.name == entity_type_name))
@@ -45,7 +46,21 @@ def get_feed(
4546
return feed
4647

4748

48-
def _get_or_create_feed(
49+
def get_license(session: Session, license_id: str) -> Optional[License]:
50+
"""Get a License by ID."""
51+
logger.debug("Lookup License id=%s", license_id)
52+
if not license_id:
53+
logger.debug("No License ID provided")
54+
return None
55+
license = session.get(License, license_id)
56+
if license:
57+
logger.debug("Found existing License id=%s", license_id)
58+
return license
59+
logger.debug("No License found with id=%s", license_id)
60+
return None
61+
62+
63+
def get_or_create_feed(
4964
session: Session,
5065
model: Type[T],
5166
stable_id: str,

functions-python/tasks_executor/src/tasks/data_import/jbda/import_jbda_feeds.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
)
4040
from shared.helpers.pub_sub import trigger_dataset_download
4141
from tasks.data_import.data_import_utils import (
42-
_get_or_create_entity_type,
43-
_get_or_create_feed,
42+
get_or_create_entity_type,
43+
get_or_create_feed,
4444
)
4545

4646
T = TypeVar("T", bound="Feed")
@@ -307,9 +307,9 @@ def _upsert_rt_feeds(
307307
)
308308
continue
309309

310-
et = _get_or_create_entity_type(db_session, entity_type_name)
310+
et = get_or_create_entity_type(db_session, entity_type_name)
311311
rt_stable_id = f"{stable_id}-{entity_type_name}"
312-
rt_feed, is_new_rt = _get_or_create_feed(
312+
rt_feed, is_new_rt = get_or_create_feed(
313313
db_session, Gtfsrealtimefeed, rt_stable_id, "gtfs_rt"
314314
)
315315

@@ -388,9 +388,7 @@ def _process_feed(
388388

389389
# Upsert/lookup schedule feed
390390
stable_id = f"jbda-{org_id}-{feed_id}"
391-
gtfs_feed, is_new_gtfs = _get_or_create_feed(
392-
db_session, Gtfsfeed, stable_id, "gtfs"
393-
)
391+
gtfs_feed, is_new_gtfs = get_or_create_feed(db_session, Gtfsfeed, stable_id, "gtfs")
394392

395393
# Diff detection
396394
api_sched_fp = _build_api_schedule_fingerprint(item, dbody, producer_url)

functions-python/tasks_executor/src/tasks/data_import/transitfeeds/sync_transitfeeds.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
Gtfsdataset,
3535
)
3636
from tasks.data_import.data_import_utils import (
37-
_get_or_create_feed,
38-
_get_or_create_entity_type,
37+
get_or_create_feed,
38+
get_or_create_entity_type,
3939
get_feed,
4040
)
4141

@@ -80,7 +80,7 @@ def _process_feeds(
8080
feed_stable_id,
8181
)
8282

83-
feed, is_new = _get_or_create_feed(
83+
feed, is_new = get_or_create_feed(
8484
db_session, model_cls, feed_stable_id, feed_kind, is_official=False
8585
)
8686
# All TransitFeeds imports are marked deprecated
@@ -274,7 +274,7 @@ def _rt_on_is_new(session: Session, feed, row: pd.Series) -> None:
274274
)
275275
if entity_types:
276276
feed.entitytypes = [
277-
_get_or_create_entity_type(session, et) for et in entity_types
277+
get_or_create_entity_type(session, et) for et in entity_types
278278
]
279279
logger.info(
280280
"[GTFS_RT] Set %d entity types for %s",

0 commit comments

Comments
 (0)