Skip to content

Commit df7308b

Browse files
feat: transitFeedSyncProcessor implementation (#760)
1 parent b9b1bc5 commit df7308b

File tree

16 files changed

+1205
-12
lines changed

16 files changed

+1205
-12
lines changed

functions-python/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ The function configuration file contains the following properties:
3232
- `min_instance_count`: The minimum number of function instances that can be created in response to a load.
3333
- `available_cpu_count`: The number of CPU cores that are available to the function.
3434

35+
# Local Setup
36+
37+
## Requirements
38+
The requirements to run the functions locally might differ depending on the Google cloud dependencies. Please refer to each function to make sure all the requirements are met.
39+
40+
## Install the Google Cloud SDK
41+
To be able to run the functions locally, the Google Cloud SDK should be installed. Please refer to the [Google Cloud SDK documentation](https://cloud.google.com/sdk/docs/install) for more information.
42+
43+
## Install the Google Cloud Emulators
44+
45+
```bash
46+
gcloud components install cloud-datastore-emulator
47+
```
48+
49+
- Install the Pub/Sub emulator
50+
```bash
51+
gcloud components install pubsub-emulator
52+
```
3553

3654
# Useful scripts
3755
- To locally execute a function use the following command:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[run]
2+
omit =
3+
*/test*/*
4+
*/database_gen/*
5+
*/dataset_service/*
6+
*/helpers/*
7+
8+
[report]
9+
exclude_lines =
10+
if __name__ == .__main__.:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Environment variables for tokens function to run locally. Delete this line after rename the file.
2+
FEEDS_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/MobilityDatabase
3+
PROJECT_ID=my-project-id
4+
PUBSUB_TOPIC_NAME=my-topic
5+
TRANSITLAND_API_KEY=your-api-key
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Batch Datasets
2+
This directory contains the GCP serverless function that triggers the sync feeds in transitland.
3+
The function publish one Pub/Sub message per transitland feed to be synced.
4+
```json
5+
{
6+
"message": {
7+
"data":
8+
{
9+
external_id="feeds_onestop_id",
10+
feed_id="feed_id",
11+
execution_id=execution_id,
12+
feed_url="feed_url",
13+
spec="spec",
14+
auth_info_url="auth_info_url",
15+
auth_param_name="auth_param_name",
16+
type="type",
17+
operator_name="operator_name",
18+
country="country",
19+
state_province="state_province",
20+
city_name="city_name",
21+
source="TLD",
22+
payload_type=payload_type
23+
}
24+
}
25+
}
26+
```
27+
28+
# Function configuration
29+
The function is configured using the following environment variables:
30+
- `PUBSUB_TOPIC`: The Pub/Sub topic to publish the messages to.
31+
- `PROJECT_ID`: The GCP Project id.
32+
- `TRANSITLAND_API_KEY`: The Transitland API key(secret).
33+
34+
# Local development
35+
The local development of this function follows the same steps as the other functions.
36+
37+
Install Google Pub/Sub emulator, please refer to the [README.md](../README.md) file for more information.
38+
39+
## Python requirements
40+
41+
- Install the requirements
42+
```bash
43+
pip install -r ./functions-python/feed_sync_dispatcher_transitland/requirements.txt
44+
```
45+
46+
## Test locally with Google Cloud Emulators
47+
48+
- Execute the following commands to start the emulators:
49+
```bash
50+
gcloud beta emulators pubsub start --project=test-project --host-port='localhost:8043'
51+
```
52+
53+
- Create a Pub/Sub topic in the emulator:
54+
```bash
55+
curl -X PUT "http://localhost:8043/v1/projects/test-project/topics/feed-sync-transitland"
56+
```
57+
58+
- Start function
59+
```bash
60+
export PUBSUB_EMULATOR_HOST=localhost:8043 && ./scripts/function-python-run.sh --function_name feed_sync_dispatcher_transitland
61+
```
62+
63+
- [Optional]: Create a local subscription to print published messages:
64+
```bash
65+
./scripts/pubsub_message_print.sh feed-sync-transitland
66+
```
67+
68+
- Execute function
69+
```bash
70+
curl http://localhost:8080
71+
```
72+
73+
- To run/debug from your IDE use the file `main_local_debug.py`
74+
75+
# Test
76+
- Run the tests
77+
```bash
78+
./scripts/api-tests.sh --folder functions-python/feed_sync_dispatcher_transitland
79+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "feed-sync-dispatcher-transitland",
3+
"description": "Feed Sync Dispatcher for Transitland",
4+
"entry_point": "feed_sync_dispatcher_transitland",
5+
"timeout": 540,
6+
"memory": "512Mi",
7+
"trigger_http": true,
8+
"include_folders": ["database_gen", "helpers"],
9+
"secret_environment_variables": [
10+
{
11+
"key": "FEEDS_DATABASE_URL"
12+
}
13+
],
14+
"ingress_settings": "ALLOW_INTERNAL_AND_GCLB",
15+
"max_instance_request_concurrency": 20,
16+
"max_instance_count": 10,
17+
"min_instance_count": 0,
18+
"available_cpu": 1
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Code to be able to debug locally without affecting the runtime cloud function
2+
3+
4+
# Requirements:
5+
# - Google Cloud SDK installed
6+
# - Make sure to have the following environment variables set in your .env.local file
7+
# - Local database in running state
8+
# - Follow the instructions in the README.md file
9+
#
10+
# Usage:
11+
# - python feed_sync_dispatcher_transitland/main_local_debug.py
12+
13+
from src.main import feed_sync_dispatcher_transitland
14+
from dotenv import load_dotenv
15+
16+
# Load environment variables from .env.local
17+
load_dotenv(dotenv_path=".env.local_test")
18+
19+
if __name__ == "__main__":
20+
21+
class RequestObject:
22+
def __init__(self, headers):
23+
self.headers = headers
24+
25+
request = RequestObject({"X-Cloud-Trace-Context": "1234567890abcdef"})
26+
feed_sync_dispatcher_transitland(request)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Common packages
2+
functions-framework==3.*
3+
google-cloud-logging
4+
psycopg2-binary==2.9.6
5+
aiohttp~=3.10.5
6+
asyncio~=3.4.3
7+
urllib3~=2.2.2
8+
requests~=2.32.3
9+
attrs~=23.1.0
10+
pluggy~=1.3.0
11+
certifi~=2024.8.30
12+
pandas
13+
14+
# SQL Alchemy and Geo Alchemy
15+
SQLAlchemy==2.0.23
16+
geoalchemy2==0.14.7
17+
18+
# Google specific packages for this function
19+
google-cloud-pubsub
20+
cloudevents~=1.10.1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Faker
2+
pytest~=7.4.3

functions-python/feed_sync_dispatcher_transitland/src/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)