From 3ef6dcf5d91a69e662a150a382dd6b0d30d03eed Mon Sep 17 00:00:00 2001 From: Paul Bienkowski Date: Fri, 18 Feb 2022 13:42:32 +0100 Subject: [PATCH] Add lean mode (overpass source and no map view in frontend) --- api/config.dev.py | 1 + api/config.py.example | 5 +++++ api/obs/api/process.py | 23 +++++++++++++++++------ api/obs/api/routes/frontend.py | 29 +++++++++++++++++------------ frontend/src/App.tsx | 8 +++++--- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/api/config.dev.py b/api/config.dev.py index fae82f7..e0bc632 100644 --- a/api/config.dev.py +++ b/api/config.dev.py @@ -4,6 +4,7 @@ DEBUG = True VERBOSE = False AUTO_RESTART = True SECRET = "!!!!!!!!!!!!CHANGE ME!!!!!!!!!!!!" +LEAN_MODE = True POSTGRES_URL = "postgresql+asyncpg://obs:obs@postgres/obs" POSTGRES_POOL_SIZE = 20 POSTGRES_MAX_OVERFLOW = 2 * POSTGRES_POOL_SIZE diff --git a/api/config.py.example b/api/config.py.example index 94f7280..1aac6e2 100644 --- a/api/config.py.example +++ b/api/config.py.example @@ -7,6 +7,11 @@ DEBUG = False VERBOSE = DEBUG AUTO_RESTART = DEBUG +# Turn on lean mode to simplify the setup. Lots of features will be +# unavailable, but you will not need to manage OpenStreetMap data. Please make +# sure to configure the OBS_FACE_CACHE_DIR correctly for lean mode. +LEAN_MODE = False + # Required to encrypt or sign sessions, cookies, tokens, etc. SECRET = "!!!<<>>!!!" diff --git a/api/obs/api/process.py b/api/obs/api/process.py index b05bca7..0f3a1bf 100644 --- a/api/obs/api/process.py +++ b/api/obs/api/process.py @@ -25,7 +25,7 @@ from obs.face.filter import ( RequiredFieldsFilter, ) -from obs.face.osm import DataSource, DatabaseTileSource +from obs.face.osm import DataSource, DatabaseTileSource, OverpassTileSource from obs.api.db import OvertakingEvent, Track, make_session from obs.api.app import app @@ -33,6 +33,20 @@ from obs.api.app import app log = logging.getLogger(__name__) +def get_data_source(): + """ + Creates a data source based on the configuration of the portal. In *lean* + mode, the OverpassTileSource is used to fetch data on demand. In normal + mode, the roads database is used. + """ + if app.config.LEAN_MODE: + tile_source = OverpassTileSource(cache_dir=app.config.OBS_FACE_CACHE_DIR) + else: + tile_source = DatabaseTileSource() + + return DataSource(tile_source) + + async def process_tracks_loop(delay): while True: try: @@ -50,9 +64,7 @@ async def process_tracks_loop(delay): await asyncio.sleep(delay) continue - tile_source = DatabaseTileSource() - data_source = DataSource(tile_source) - + data_source = get_data_source() await process_track(session, track, data_source) except BaseException: log.exception("Failed to process track. Will continue.") @@ -66,8 +78,7 @@ async def process_tracks(tracks): :param tracks: A list of strings which """ - tile_source = DatabaseTileSource() - data_source = DataSource(tile_source) + data_source = get_data_source() async with make_session() as session: for track_id_or_slug in tracks: diff --git a/api/obs/api/routes/frontend.py b/api/obs/api/routes/frontend.py index c5547c0..2334e34 100644 --- a/api/obs/api/routes/frontend.py +++ b/api/obs/api/routes/frontend.py @@ -6,6 +6,7 @@ from sanic.exceptions import NotFound from obs.api.app import app if app.config.FRONTEND_CONFIG: + @app.get("/config.json") def get_frontend_config(req): result = { @@ -13,18 +14,22 @@ if app.config.FRONTEND_CONFIG: **req.app.config.FRONTEND_CONFIG, "apiUrl": f"{req.ctx.api_url}/api", "loginUrl": f"{req.ctx.api_url}/login", - "obsMapSource": { - "type": "vector", - "tiles": [ - req.ctx.api_url - + req.app.url_for("tiles", zoom="000", x="111", y="222.pbf") - .replace("000", "{z}") - .replace("111", "{x}") - .replace("222", "{y}") - ], - "minzoom": 12, - "maxzoom": 14, - }, + "obsMapSource": ( + None + if app.config.LEAN_MODE + else { + "type": "vector", + "tiles": [ + req.ctx.api_url + + req.app.url_for("tiles", zoom="000", x="111", y="222.pbf") + .replace("000", "{z}") + .replace("111", "{x}") + .replace("222", "{y}") + ], + "minzoom": 12, + "maxzoom": 14, + } + ), } return response.json(result) diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 4feb1de..ab116fa 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -59,6 +59,8 @@ const App = connect((state) => ({login: state.login}))(function App({login}) { const config = useConfig() const apiVersion = useObservable(() => from(api.get('/info')).pipe(pluck('version'))) + const hasMap = Boolean(config?.obsMapSource) + React.useEffect(() => { api.loadUser() }, []) @@ -72,7 +74,7 @@ const App = connect((state) => ({login: state.login}))(function App({login}) { OpenBikeSensor - {config?.obsMapSource && ( + {hasMap && ( Map @@ -110,9 +112,9 @@ const App = connect((state) => ({login: state.login}))(function App({login}) { - + {hasMap && - + }