Commit 90ca360a authored by Pierre Dittgen's avatar Pierre Dittgen
Browse files

Improve home page loading speed using background update

parent e1edd7b1
"""Util UI functions."""
import threading
from flask import flash
......@@ -20,3 +22,34 @@ def flash_success(msg):
def flash_info(msg):
"""Flash bootstrap info message."""
flash(msg, "info")
class ThreadUpdater:
"""Fire last known value and update on background."""
def __init__(self, func):
# First time run to get an initial value
self.val = func()
self.func = func
self.thread = None
@property
def value(self):
self._background_update()
return self.val
def _background_update(self):
# Don't run if an update is already running
if self.thread is not None:
return
# Run func to update self.val and reset self.thread
# to show that background update is done.
def doIt():
self.val = self.func()
self.thread = None
# Init and start thread
self.thread = threading.Thread(target=doIt)
self.thread.start()
......@@ -27,7 +27,7 @@ from validata_core.helpers import (
from . import app, config, fetch_schema, pdf_service, schema_catalog_registry
from .model import Section
from .ui_util import flash_error, flash_warning
from .ui_util import ThreadUpdater, flash_error, flash_warning
from .validata_util import strip_accents
log = logging.getLogger(__name__)
......@@ -556,67 +556,70 @@ def retrieve_schema_catalog(section: Section):
# Routes
@app.route("/")
def home():
"""Home page."""
def iter_sections():
"""Yield sections of the home page, filled with schema metadata."""
# Iterate on all sections
for section in config.CONFIG.homepage.sections:
def iter_sections():
"""Yield sections of the home page, filled with schema metadata."""
# Iterate on all sections
for section in config.CONFIG.homepage.sections:
# section with only links to external validators
if section.links:
yield section
continue
# section with only links to external validators
if section.links:
yield section
continue
# section with catalog
if section.catalog is None:
# skip section
continue
# section with catalog
if section.catalog is None:
# skip section
continue
# retrieving schema catatalog
schema_catalog, catalog_error = retrieve_schema_catalog(section)
if schema_catalog is None:
yield catalog_error
continue
# retrieving schema catatalog
schema_catalog, catalog_error = retrieve_schema_catalog(section)
if schema_catalog is None:
yield catalog_error
# Working on catalog
schema_info_list = []
for schema_reference in schema_catalog.references:
# retain tableschema only
if schema_reference.get_schema_type() != "tableschema":
continue
# Working on catalog
schema_info_list = []
for schema_reference in schema_catalog.references:
# retain tableschema only
if schema_reference.get_schema_type() != "tableschema":
continue
# Loads default table schema for each schema reference
schema_info = {"name": schema_reference.name}
try:
table_schema = fetch_schema(schema_reference.get_schema_url())
except json.JSONDecodeError:
schema_info["err"] = True
schema_info["title"] = (
f"le format du schéma « {schema_info['name']} » "
"n'est pas reconnu"
)
except Exception:
schema_info["err"] = True
schema_info["title"] = (
f"le schéma « {schema_info['name']} » " "n'est pas disponible"
)
else:
schema_info["title"] = (
table_schema.get("title") or schema_info["name"]
)
schema_info_list.append(schema_info)
schema_info_list = sorted(
schema_info_list, key=lambda sc: strip_accents(sc["title"].lower())
)
# Loads default table schema for each schema reference
schema_info = {"name": schema_reference.name}
try:
table_schema = fetch_schema(schema_reference.get_schema_url())
except json.JSONDecodeError:
schema_info["err"] = True
schema_info["title"] = (
f"le format du schéma « {schema_info['name']} » "
"n'est pas reconnu"
)
except Exception:
schema_info["err"] = True
schema_info["title"] = (
f"le schéma « {schema_info['name']} » " "n'est pas disponible"
)
else:
schema_info["title"] = table_schema.get("title") or schema_info["name"]
schema_info_list.append(schema_info)
schema_info_list = sorted(
schema_info_list, key=lambda sc: strip_accents(sc["title"].lower())
)
yield {
**{k: v for k, v in section.dict().items() if k != "catalog"},
"catalog": schema_info_list,
}
yield {
**{k: v for k, v in section.dict().items() if k != "catalog"},
"catalog": schema_info_list,
}
section_updater = ThreadUpdater(lambda: list(iter_sections()))
@app.route("/")
def home():
"""Home page."""
return render_template("home.html", config=config, sections=list(iter_sections()))
return render_template("home.html", config=config, sections=section_updater.value)
@app.route("/pdf")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment