Skip to content

Commit

Permalink
Limit use of USE_WFWX (#4002)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgboss authored Oct 16, 2024
1 parent 71e17bb commit 6b15aa7
Show file tree
Hide file tree
Showing 37 changed files with 1,724 additions and 188 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ jobs:
shell: bash
run: |
oc login "${{ secrets.OPENSHIFT_CLUSTER }}" --token="${{ secrets.OC4_DEV_TOKEN }}"
MODULE_NAME=api SECOND_LEVEL_DOMAIN="apps.silver.devops.gov.bc.ca" VANITY_DOMAIN="${SUFFIX}-dev-psu.apps.silver.devops.gov.bc.ca" USE_WFWX="True" ENVIRONMENT="development" bash openshift/scripts/oc_deploy.sh ${SUFFIX} apply
MODULE_NAME=api SECOND_LEVEL_DOMAIN="apps.silver.devops.gov.bc.ca" VANITY_DOMAIN="${SUFFIX}-dev-psu.apps.silver.devops.gov.bc.ca" ENVIRONMENT="development" bash openshift/scripts/oc_deploy.sh ${SUFFIX} apply
## TODO: re-enable once crunchy is deployed: https://app.zenhub.com/workspaces/bcws---agile-psu-5e321393e038fba5bbe203b8/issues/gh/bcgov/wps/2340
- name: Hourly actuals cronjob
Expand Down
1 change: 0 additions & 1 deletion api/app/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This is a sample .env file that can be used for local development.
ORIGINS="http://localhost http://localhost:3000 http://127.0.0.1 http://127.0.0.1:3000"
HOSTNAME=localhost
USE_WFWX=False
WFWX_AUTH_URL=https://somewhere
WFWX_BASE_URL=https://somewhere
WFWX_USER=someusear
Expand Down
12 changes: 4 additions & 8 deletions api/app/hourlies.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,10 @@ async def get_hourly_readings(
"""
start_time_stamp, end_time_stamp = _get_time_interval(time_of_interest)

if wfwx_api.use_wfwx():
# Limit the number of concurrent connections.
async with ClientSession(connector=TCPConnector(limit=10)) as session:
header = await wfwx_api.get_auth_header(session)
return await wfwx_api.get_hourly_readings(
session, header, station_codes, start_time_stamp, end_time_stamp)

return await fetch_hourly_readings_from_db(station_codes, start_time_stamp, end_time_stamp)
# Limit the number of concurrent connections.
async with ClientSession(connector=TCPConnector(limit=10)) as session:
header = await wfwx_api.get_auth_header(session)
return await wfwx_api.get_hourly_readings(session, header, station_codes, start_time_stamp, end_time_stamp)


async def get_hourly_readings_in_time_interval(
Expand Down
34 changes: 5 additions & 29 deletions api/app/stations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import app.db.database
from app.db.crud.stations import get_noon_forecast_observation_union
from app.wildfire_one import wfwx_api
from app.wildfire_one.wfwx_api import (get_auth_header,
get_detailed_stations,
get_station_data,
use_wfwx)
from app.wildfire_one.wfwx_api import get_auth_header, get_detailed_stations, get_station_data

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -104,36 +101,16 @@ async def _get_detailed_stations(time_of_interest: datetime):
return geojson_stations


def _get_stations_by_codes_local(station_codes: List[int]) -> List[WeatherStation]:
""" Get a list of stations by code, from local json files. """
logger.info('Using pre-generated json to retrieve station by code')
with open(weather_stations_file_path, encoding="utf-8") as file_pointer:
stations = json.load(file_pointer)
results = []
for station in stations['weather_stations']:
if int(station['code']) in station_codes:
results.append(WeatherStation(**station))
return results


async def get_stations_by_codes(station_codes: List[int]) -> List[WeatherStation]:
""" Get a list of stations by code, from WFWX Fireweather API. """
if use_wfwx():
logger.info('Fetching stations from WFWX')
return await wfwx_api.get_stations_by_codes(station_codes)
return _get_stations_by_codes_local(station_codes)
"""Get a list of stations by code, from WFWX Fireweather API."""
return await wfwx_api.get_stations_by_codes(station_codes)


async def get_stations_from_source(
station_source: StationSourceEnum = StationSourceEnum.WILDFIRE_ONE) -> List[WeatherStation]:
""" Get list of stations from some source (ideally WFWX Fireweather API)
"""
if station_source == StationSourceEnum.UNSPECIFIED:
# If station source is unspecified, check configuration:
if use_wfwx():
return await get_stations_asynchronously()
elif station_source == StationSourceEnum.WILDFIRE_ONE:
# Get from wildfire one:
if station_source == StationSourceEnum.UNSPECIFIED or station_source == StationSourceEnum.WILDFIRE_ONE:
return await get_stations_asynchronously()
# Get from local:
return _get_stations_local()
Expand All @@ -145,8 +122,7 @@ async def fetch_detailed_stations_as_geojson(
-> List[GeoJsonDetailedWeatherStation]:
""" Fetch a detailed list of stations. i.e. more than just the fire station name and code,
throw some observations and forecast in the mix. """
if station_source == StationSourceEnum.WILDFIRE_ONE or (
station_source == StationSourceEnum.UNSPECIFIED and use_wfwx()):
if station_source == StationSourceEnum.UNSPECIFIED or station_source == StationSourceEnum.WILDFIRE_ONE:
# Get from wildfire one:
logger.info('requesting detailed stations...')
result = await get_detailed_stations(time_of_interest)
Expand Down
7 changes: 0 additions & 7 deletions api/app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def anyio_backend():
def mock_env(monkeypatch):
"""Automatically mock environment variable"""
monkeypatch.setenv("BASE_URI", "https://python-test-base-uri")
monkeypatch.setenv("USE_WFWX", "False")
monkeypatch.setenv("WFWX_USER", "user")
monkeypatch.setenv("WFWX_SECRET", "secret")
monkeypatch.setenv("WFWX_AUTH_URL", "https://wf1/pub/oauth2/v1/oauth/token")
Expand Down Expand Up @@ -158,12 +157,6 @@ def mock_get_prediction_run(session, prediction_model_id: int, prediction_run_ti
monkeypatch.setattr(app.jobs.env_canada, "get_prediction_run", mock_get_prediction_run)


@pytest.fixture()
def mock_env_with_use_wfwx(monkeypatch):
"""Set environment variable USE_WFWX to 'True'"""
monkeypatch.setenv("USE_WFWX", "True")


@pytest.fixture()
def mock_jwt_decode(monkeypatch):
"""Mock pyjwt's decode method"""
Expand Down
27 changes: 27 additions & 0 deletions api/app/tests/fixtures/wf1/lookup.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==230 or stationCode==239'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==230_or_stationCode==239.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': ''}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==209 or stationCode==211 or stationCode==302'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==209_or_stationCode==211_or_stationCode==302.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==322'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==322.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==322 or stationCode==838'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==322_or_stationCode==838.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==838'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==838.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==838 or stationCode==209'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==838_or_stationCode==209.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==956'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==956.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==999'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==999.json"
},
"{'size': '1000', 'sort': 'displayLabel', 'page': 0, 'query': 'stationCode==209 or stationCode==322'}": {
"None": "wfwx/v1/stations/rsql__size_1000_sort_displayLabel_page_0_query_stationCode==209_or_stationCode==322.json"
}
}
},
Expand Down
Loading

0 comments on commit 6b15aa7

Please sign in to comment.