-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from Vizzuality/SKY30-141
Analysis cloud function
- Loading branch information
Showing
15 changed files
with
522 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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 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,32 @@ | ||
import os | ||
import ssl | ||
import sqlalchemy | ||
|
||
def connect_tcp_socket() -> sqlalchemy.engine.base.Engine: | ||
"""Initializes a TCP connection pool for a Cloud SQL instance of Postgres.""" | ||
# Note: Saving credentials in environment variables is convenient, but not | ||
# secure - consider a more secure solution such as | ||
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help | ||
# keep secrets safe. | ||
db_host = os.environ[ | ||
"DATABASE_HOST" | ||
] # e.g. '127.0.0.1' ('172.17.0.1' if deployed to GAE Flex) | ||
db_user = os.environ["DATABASE_USERNAME"] # e.g. 'my-db-user' | ||
db_pass = os.environ["DATABASE_PASSWORD"] # e.g. 'my-db-password' | ||
db_name = os.environ["DATABASE_NAME"] # e.g. 'my-database' | ||
db_port = 5432 # e.g. 5432 | ||
|
||
pool = sqlalchemy.create_engine( | ||
# Equivalent URL: | ||
# postgresql+pg8000://<db_user>:<db_pass>@<db_host>:<db_port>/<db_name> | ||
sqlalchemy.engine.url.URL.create( | ||
drivername="postgresql+pg8000", | ||
username=db_user, | ||
password=db_pass, | ||
host=db_host, | ||
port=db_port, | ||
database=db_name, | ||
), | ||
# ... | ||
) | ||
return pool |
This file contains 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,36 @@ | ||
import functions_framework | ||
import sqlalchemy | ||
|
||
from connect_tcp import connect_tcp_socket | ||
|
||
db = connect_tcp_socket() | ||
|
||
@functions_framework.http | ||
def index(request): | ||
"""HTTP Cloud Function. | ||
Args: | ||
request (flask.Request): The request object. | ||
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data> | ||
Returns: | ||
The response text, or any set of values that can be turned into a | ||
Response object using `make_response` | ||
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>. | ||
Note: | ||
For more information on how Flask integrates with Cloud | ||
Functions, see the `Writing HTTP functions` page. | ||
<https://cloud.google.com/functions/docs/writing/http#http_frameworks> | ||
""" | ||
return get_locations_stats(db) | ||
|
||
def get_locations_stats(db: sqlalchemy.engine.base.Engine) -> dict: | ||
with db.connect() as conn: | ||
stmt = sqlalchemy.text( | ||
"SELECT COUNT(*) FROM locations WHERE type=:type" | ||
) | ||
regions_count = conn.execute(stmt, parameters={"type": "region"}).scalar() | ||
countries_count = conn.execute(stmt, parameters={"type": "country"}).scalar() | ||
|
||
return { | ||
"regions_count": regions_count, | ||
"countries_count": countries_count | ||
} |
This file contains 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,3 @@ | ||
functions-framework | ||
sqlalchemy | ||
pg8000 |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.