Skip to content
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

Google Floods Part 1: Adding backend + creating Gauge Points; COUNTRY=cambodia #1328

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c5c1215
Adding Google Floods Backend
Aug 8, 2024
b9722d3
Viewing the gauges on the frontend, adding error handling to backend
gislawill Aug 12, 2024
bc92d38
Update comment
gislawill Aug 12, 2024
08a4499
Updates to support tooltip content
gislawill Aug 12, 2024
0653f1e
copy update
gislawill Aug 12, 2024
7cd866d
Refactor title state management
gislawill Aug 12, 2024
94e1588
Fixing and adding server tests
gislawill Aug 12, 2024
6ac3aad
update set_env
ericboucher Aug 13, 2024
eb29b6e
Merge branch 'master' into feature/google-floods/1317
ericboucher Aug 13, 2024
6be26d7
update snapshots
ericboucher Aug 13, 2024
2fe481a
Merge branch 'master' into feature/google-floods/1317
gislawill Aug 15, 2024
8e84cbb
Adding support for multi country deployments and adding google flood …
gislawill Aug 21, 2024
efdf2c9
Use interpolation for popup titles
gislawill Aug 21, 2024
8fa327c
Adding pytest-recording
gislawill Aug 21, 2024
f5ffbf6
Merge branch 'master' into feature/google-floods/1317
ericboucher Aug 21, 2024
438ee04
Update test_google_floods_api.py
ericboucher Aug 21, 2024
e369704
Merge branch 'feature/google-floods/1317' of https://github.com/WFP-V…
ericboucher Aug 21, 2024
6547f3b
Fixing river template
gislawill Aug 22, 2024
ac58dca
More selective with pytest recording
gislawill Aug 22, 2024
65af7e7
Merge branch 'master' into feature/google-floods/1317
ericboucher Aug 22, 2024
0adb49d
Update API URL
ericboucher Aug 22, 2024
dc94f60
Merge branch 'master' into feature/google-floods/1317
ericboucher Sep 25, 2024
b0780fa
Merge branch 'master' into feature/google-floods/1317
gislawill Oct 7, 2024
7f401bb
Adding support for Cambodia
gislawill Oct 7, 2024
341a3a7
Merge branch 'master' into feature/google-floods/1317
gislawill Oct 7, 2024
1e51479
Merge branch 'master' into feature/google-floods/1317
gislawill Oct 8, 2024
2a890cf
Adding date support
gislawill Oct 9, 2024
6d33543
Google Floods Part 2: Adding tooltip graph; COUNTRY=cambodia (#1330)
gislawill Oct 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions api/app/googleflood.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Get data from Humanitarian Data Cube (HDC) API"""
import logging
from os import getenv

import requests

logger = logging.getLogger(__name__)

GOOGLE_FLOODS_API_KEY = getenv("GOOGLE_FLOODS_API_KEY", "")
if GOOGLE_FLOODS_API_KEY == "":
logger.warning("Missing backend parameter: GOOGLE_FLOODS_API_KEY")

def format_to_geojson(data):
geojson = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [data["gaugeLocation"]["longitude"], data["gaugeLocation"]["latitude"]]
},
"properties": {
"gaugeId": data["gaugeId"],
"issuedTime": data["issuedTime"],
# "forecastTimeRange": data["forecastTimeRange"],
# "forecastChange": data["forecastChange"],
# "forecastTrend": data["forecastTrend"],
"severity": data["severity"],
"source": data["source"],
"qualityVerified": data["qualityVerified"]
}
}
if "inundationMapSet" in data:
geojson["properties"]["inundationMapSet"] = data["inundationMapSet"]
return geojson


def get_google_floods_gauges(
iso2: str,
asGeojson: bool = True,
):
"""Get statistical charts data"""
logging.info(GOOGLE_FLOODS_API_KEY)

URL = f'https://floodforecasting.googleapis.com/v1/floodStatus:searchLatestFloodStatusByArea?key={GOOGLE_FLOODS_API_KEY}'
response = requests.post(
URL,
json={'regionCode': iso2}
).json().get('floodStatuses', [])

if asGeojson:
geojson_feature_collection = {
"type": "FeatureCollection",
"features": [format_to_geojson(data) for data in response]
}
return geojson_feature_collection
return response


8 changes: 8 additions & 0 deletions api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from app.database.alert_model import AlchemyEncoder, AlertModel
from app.database.database import AlertsDataBase
from app.database.user_info_model import UserInfoModel
from app.googleflood import get_google_floods_gauges
from app.hdc import get_hdc_stats
from app.kobo import get_form_dates, get_form_responses, parse_datetime_params
from app.models import AcledRequest, RasterGeotiffModel
Expand Down Expand Up @@ -412,3 +413,10 @@ def post_raster_geotiff(raster_geotiff: RasterGeotiffModel):
return JSONResponse(
content={"download_url": presigned_download_url}, status_code=200
)


@app.get("/google-floods-gauges")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're gonna add a bunch of integrations, it will be worthwile to think about a standardized approach and common architecture. And maybe expose them through a sub endpoint of the API to keep things cleaner. wdyt @lowriech ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a good question, @ericboucher. Do you see other APIs we're already leveraging fitting into this integration category (kobo, hdc stats, others)? Or are you thinking just for new integrations?

I'm wary of premature abstraction and trying to determine if I think now is the right time to align on this standardized approach.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like low-hanging fruit to abstract once we decide on a pattern. So yes, agree we should think about it, but not sure about the velocity of adding entirely new providers. Would vote to wait until we see more pattern around this.

def get_google_floods_gauges_api(
iso2: str,
):
return get_google_floods_gauges(iso2)
3 changes: 3 additions & 0 deletions api/app/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
log_cli = true
log_cli_level = INFO
20 changes: 20 additions & 0 deletions api/app/tests/test_google_floods_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from app.googleflood import get_google_floods_gauges
from app.main import app
from fastapi.testclient import TestClient
import logging, json

client = TestClient(app)

def test_get_google_floods_gauges():
gauges = get_google_floods_gauges("BD")
assert len(gauges) > 0


def test_get_google_floods_gauges_api():
response = client.get("/google-floods-gauges?iso2=BD")
assert response.status_code == 200
logging.info(json.dumps(response.json()))
# logging.info(response.json())
assert len(response.json()) > 0


Loading