Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit f946209

Browse files
committed
Add experimental startup project settings cache, and add /cache_project_settings route to trigger caching explicitly
1 parent 28c8119 commit f946209

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

config_service.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def __init__(self, logger):
5151
'QWC2_THEMES_CONFIG', os.path.join(qwc2_path, 'themesConfig.json')
5252
)
5353

54+
if os.environ.get("__QWC_CONFIG_SERVICE_PROJECT_SETTINGS_STARTUP_CACHE", "0") == "1":
55+
self.cache_project_settings()
56+
5457
def last_update(self):
5558
"""Return UTC timestamp of last permissions update."""
5659
# get modification time of QWC2 themes config file
@@ -157,3 +160,6 @@ def resource_restrictions(self, resource_type, params, username, group):
157160
return {
158161
'restrictions': restrictions
159162
}
163+
164+
def cache_project_settings(self):
165+
return self.permission_handlers["ogc"].cache_project_settings()

ogc_service_permission.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,3 +443,54 @@ def filter_permitted_layers(self, permitted_layers, permissions):
443443

444444
# update restricted_group_layers
445445
permissions['restricted_group_layers'] = {} # TODO
446+
447+
def collect_theme_urls(self, themes, theme_urls):
448+
if "items" in themes:
449+
for entry in themes["items"]:
450+
theme_urls.add(entry["url"])
451+
if "groups" in themes:
452+
for theme_group in themes["groups"]:
453+
self.collect_theme_urls(theme_group, theme_urls)
454+
455+
def cache_project_settings(self):
456+
qwc2_path = os.environ.get('QWC2_PATH', 'qwc2/')
457+
themes_config_path = os.environ.get(
458+
'QWC2_THEMES_CONFIG', os.path.join(qwc2_path, 'themesConfig.json')
459+
)
460+
mtime = self.themesConfigMTime()
461+
462+
with open(themes_config_path) as fh:
463+
data = json.load(fh)
464+
# Collect themes
465+
theme_urls = set()
466+
themes = data["themes"] if "themes" in data else {}
467+
self.collect_theme_urls(themes, theme_urls)
468+
469+
if not "WMS" in self.project_settings_cache:
470+
self.project_settings_cache["WMS"] = {}
471+
472+
cached = []
473+
for url in theme_urls:
474+
if not url.startswith(self.qgis_server_url):
475+
self.logger.warn("Not caching external WMS: %s" % url)
476+
continue
477+
478+
ows_name = url[len(self.qgis_server_url):]
479+
response = requests.get(
480+
urljoin(self.qgis_server_url, ows_name),
481+
params={
482+
'SERVICE': "WMS",
483+
'VERSION': '1.3.0',
484+
'REQUEST': 'GetProjectSettings'
485+
},
486+
timeout=30
487+
)
488+
489+
if response.status_code == requests.codes.ok:
490+
self.logger.info("Cached project settings for %s" % ows_name)
491+
self.project_settings_cache["WMS"][ows_name] = {
492+
"document": response.content,
493+
"timestamp": mtime
494+
}
495+
cached.append(ows_name)
496+
return {"cached_settings": cached}

server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
)]
2828
])
2929

30+
cache_project_settings_response = create_model(api, 'CacheProjectSettings', [
31+
['cached_settings', fields.Raw(
32+
required=True,
33+
description='List of service names for which settings were cached',
34+
example=['name1', 'name2']
35+
)]
36+
])
37+
3038
resource_permissions_response = create_model(api, 'Resource permissions', [
3139
['resource_type', fields.String(required=True, description='Resource type',
3240
example='map')],
@@ -65,6 +73,13 @@ def get(self):
6573
"""Get timestamp of last permissions update"""
6674
return config_service.last_update()
6775

76+
@api.route('/cache_project_settings')
77+
class CacheProjectSettings(Resource):
78+
@api.doc('cache_project_settings')
79+
@api.marshal_with(cache_project_settings_response)
80+
def get(self):
81+
"""Cache all known project settings"""
82+
return config_service.cache_project_settings()
6883

6984
@api.route('/permissions/<resource_type>')
7085
@api.param('resource_type', 'Resource type (e.g. <i>map</i>, <i>layer</i>)',

0 commit comments

Comments
 (0)