Skip to content

Commit

Permalink
test: deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
cka-y committed Dec 19, 2024
1 parent 00b12c3 commit e2a9065
Show file tree
Hide file tree
Showing 19 changed files with 1,240 additions and 9 deletions.
22 changes: 18 additions & 4 deletions functions-python/helpers/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os

import google.cloud.logging
from google.cloud.logging_v2 import Client
Expand Down Expand Up @@ -45,11 +46,24 @@ def __init__(self, name):
@staticmethod
def init_logger() -> Client:
"""
Initializes the logger
Initializes the logger for both local debugging and GCP environments.
"""
client = google.cloud.logging.Client()
client.get_default_handler()
client.setup_logging()
try:
# Check if running in a GCP environment (default credentials available)
if os.getenv("ENV") == "local":
# Local environment: use standard logging
logging.basicConfig(level=logging.DEBUG)
logging.info("Local logger initialized (standard logging).")
client = None # Return None since cloud client is not used
else:
client = google.cloud.logging.Client()
client.setup_logging()
logging.info("Google Cloud Logging initialized.")
except Exception as e:
logging.error(f"Failed to initialize logging: {e}")
logging.basicConfig(level=logging.DEBUG)
logging.info("Fallback to standard local logging.")
client = None
return client

def get_logger(self) -> Client:
Expand Down
9 changes: 9 additions & 0 deletions functions-python/reverse_geolocation/.coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[run]
omit =
*/test*/*
*/helpers/*
*/database_gen/*

[report]
exclude_lines =
if __name__ == .__main__.:
5 changes: 5 additions & 0 deletions functions-python/reverse_geolocation/.env.rename_me
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Environment variables for the reverse_geolocation functions
FEEDS_DATABASE_URL=${{FEEDS_DATABASE_URL}}
BUCKET_NAME=${{BUCKET_NAME}}
MAX_RETRIES=${{MAX_RETRIES}}
# TODO: add the other env variables here
11 changes: 11 additions & 0 deletions functions-python/reverse_geolocation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Reverse Geolocation

## Function Workflow


## Function Configuration


## Local Development

Local development of these functions should follow standard practices for GCP serverless functions. For general instructions on setting up the development environment, refer to the main [README.md](../README.md) file.
20 changes: 20 additions & 0 deletions functions-python/reverse_geolocation/function_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "reverse-geolocation",
"description": "Function to reverse geolocate coordinates",
"entry_point": "reverse_geolocation",
"timeout": 3600,
"trigger_http": false,
"include_folders": ["database_gen", "helpers"],
"environment_variables": [],
"secret_environment_variables": [
{
"key": "FEEDS_DATABASE_URL"
}
],
"ingress_settings": "ALLOW_ALL",
"max_instance_request_concurrency": 1,
"max_instance_count": 10,
"min_instance_count": 0,
"available_cpu": 1,
"available_memory": "4Gi"
}
29 changes: 29 additions & 0 deletions functions-python/reverse_geolocation/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Common packages
functions-framework==3.*
google-cloud-logging
psycopg2-binary==2.9.6
aiohttp~=3.10.5
asyncio~=3.4.3
urllib3~=2.2.2
requests~=2.32.3
attrs~=23.1.0
pluggy~=1.3.0
certifi~=2024.7.4

# SQL Alchemy and Geo Alchemy
SQLAlchemy==2.0.23
geoalchemy2==0.14.7

# Google specific packages for this function
google-cloud-pubsub
google-cloud-datastore
google-cloud-storage
google-cloud-bigquery
cloudevents~=1.10.1
google-cloud-tasks
google-cloud-storage

# Additional packages for this function
pycountry
shapely
gtfs-kit
4 changes: 4 additions & 0 deletions functions-python/reverse_geolocation/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Faker
pytest~=7.4.3
urllib3-mock
requests-mock
Empty file.
1 change: 1 addition & 0 deletions functions-python/reverse_geolocation/src/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ERROR_STATUS_CODE = 299 # Custom status code to stop Cloud Tasks retries
39 changes: 39 additions & 0 deletions functions-python/reverse_geolocation/src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import logging
from typing import Dict, Tuple

import flask
import functions_framework
from cloudevents.http import CloudEvent

from reverse_geolocation_aggregator import reverse_geolocation_aggregate as aggregator
from reverse_geolocation_processor import reverse_geolocation_process as processor
from reverse_geolocator import reverse_geolocation as geolocator

# Initialize logging
logging.basicConfig(level=logging.INFO)


@functions_framework.cloud_event
def reverse_geolocation(cloud_event: CloudEvent):
"""Function that is triggered when a new dataset is uploaded to extract the location information."""
return geolocator(cloud_event)


@functions_framework.http
def reverse_geolocation_process(
request: flask.Request,
) -> Tuple[str, int] | Tuple[Dict, int]:
"""
Main function to handle reverse geolocation population.
"""
return processor(request)


@functions_framework.http
def reverse_geolocation_aggregate(
request: flask.Request,
) -> Tuple[str, int] | Tuple[Dict, int]:
"""
Main function to handle reverse geolocation population.
"""
return aggregator(request)
Loading

0 comments on commit e2a9065

Please sign in to comment.