Skip to content

Commit

Permalink
refactor: Improve error management
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatelot committed Sep 1, 2024
1 parent b0d7928 commit 102ef65
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 37 deletions.
4 changes: 2 additions & 2 deletions bases/ecoindex/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ def analyze(
logger=logger,
)

for result, success in analysis_results:
for result, error in analysis_results:
results.append(result)
if not success:
if error:
count_errors += 1

progress.update(task, advance=1)
Expand Down
1 change: 1 addition & 0 deletions components/ecoindex/exceptions/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ def __init__(self, url: str, status: int, message: str):
self.message = message
self.url = url
self.status = status
self.msg = f"{status}: {message}"

pass
8 changes: 4 additions & 4 deletions components/ecoindex/scraper/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def run_page_analysis(
wait_after_scroll: int = 3,
wait_before_scroll: int = 3,
logger=None,
) -> tuple[Result, bool]:
) -> tuple[Result, str | None]:
"""Run the page analysis and return the result and a boolean indicating if the analysis was successful"""
scraper = EcoindexScraper(
url=str(url),
Expand All @@ -22,7 +22,7 @@ def run_page_analysis(
page_load_timeout=20,
)
try:
return (run(scraper.get_page_analysis()), True)
return (run(scraper.get_page_analysis()), None)
except Exception as e:
logger.error(f"{url} -- {e.msg if hasattr(e, 'msg') else e}")

Expand All @@ -36,7 +36,7 @@ def run_page_analysis(
nodes=0,
requests=0,
),
False,
e.msg if hasattr(e, "msg") else str(e),
)


Expand All @@ -47,7 +47,7 @@ def bulk_analysis(
wait_after_scroll: int = 0,
wait_before_scroll: int = 0,
logger=None,
) -> Generator[tuple[Result, bool], None, None]:
) -> Generator[tuple[Result, str | None], None, None]:
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_analysis = {}

Expand Down
5 changes: 3 additions & 2 deletions components/ecoindex/scraper/scrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ecoindex.models.compute import PageMetrics, Result, ScreenShot, WindowSize
from ecoindex.models.scraper import MimetypeAggregation, RequestItem, Requests
from ecoindex.utils.screenshots import convert_screenshot_to_webp, set_screenshot_rights
from playwright._impl._network import Response
from playwright.async_api import async_playwright
from typing_extensions import deprecated

Expand Down Expand Up @@ -72,7 +73,7 @@ async def scrap_page(self) -> PageMetrics:
ignore_https_errors=True,
)
response = await self.page.goto(self.url)
await self.check_page_response(response)
await self.check_page_response(response) # type: ignore

await self.page.wait_for_load_state()
sleep(self.wait_before_scroll)
Expand Down Expand Up @@ -142,7 +143,7 @@ def get_request_size(self, entry) -> int:
else:
return len(json.dumps(entry["response"]).encode("utf-8"))

async def check_page_response(self, response) -> None:
async def check_page_response(self, response: Response) -> None:
if response and response.status != 200:
raise EcoindexScraperStatusException(
url=self.url,
Expand Down
87 changes: 58 additions & 29 deletions development/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
from loguru import logger

st.set_page_config(
page_title="Analyse Ecoindex",
page_icon="🌍",
page_title="Ecoindex",
page_icon="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.866em%22 font-size=%22100%22>🟢</text></svg>",
initial_sidebar_state="auto",
)
st.title("Analyse Ecoindex")
st.title("Ecoindex")

form_container = st.container()
results_container = st.container()
tab_local, tab_website = st.tabs(["Ecoindex Local", "Ecoindex.fr"])

form_container = tab_local.container()
results_container = tab_local.container()
form_container.header("Lancer une analyse")

try:
import playwright # noqa

st.toast("Playwright est installé", icon="🎉")
except ImportError:
st.toast("Playwright n'est pas installé", icon="⚠️")
st.info("Pour configurer Playwright, veuillez suivre les instructions suivantes:")
Expand Down Expand Up @@ -51,13 +53,31 @@ def run_analysis(
progress_bar = results_container.progress(
0, text=f"{nb_analysis} analyse en cours..."
)
for i, (result, success) in enumerate(analysis_results):

for i, (result, error) in enumerate(analysis_results):
progress_bar.progress((i + 1) / nb_analysis)
analysis.append({"success": success, **result.__dict__})
analysis.append({**result.__dict__, "error": error})

progress_bar.empty()

df = pandas.DataFrame(analysis)
# Apply colors on error column style
# df.style.applymap(lambda x: "color: red" if x else "", subset=["error"])

# Apply colors on grade column
colors = {
"A": "#349A47",
"B": "#51B84B",
"C": "#CADB2A",
"D": "#F6EB15",
"E": "#FECD06",
"F": "#F99839",
"G": "#ED2124",
}
# df = df.style.applymap(
# lambda x: f"color: {colors[x]}" if x else "", subset=["grade"]
# )

results_container.header("Résultats de l'analyse")
results_container.dataframe(
df,
Expand Down Expand Up @@ -134,6 +154,10 @@ def run_analysis(
help="Type de page",
disabled=True,
),
"error": st.column_config.TextColumn(
"Erreur",
help="Erreur lors de l'analyse",
),
},
hide_index=True,
)
Expand All @@ -153,31 +177,36 @@ def run_analysis(

with st.expander("Options"):
col1, col2 = st.columns(2)
with col1:
wait_before_scroll = st.number_input(
label="Attendre avant de scroller",
value=3,
min_value=0,
help="Temps d'attente avant de scroller en secondes pour que la page se charge complètement",
)
wait_before_scroll = col1.number_input(
label="Attendre avant de scroller",
value=3,
min_value=0,
help="Temps d'attente avant de scroller en secondes pour que la page se charge complètement",
)

with col2:
wait_after_scroll = st.number_input(
label="Attendre après avoir scrollé",
value=3,
min_value=0,
help="Temps d'attente après avoir scrollé en secondes pour que la page se charge complètement",
)
wait_after_scroll = col2.number_input(
label="Attendre après avoir scrollé",
value=3,
min_value=0,
help="Temps d'attente après avoir scrollé en secondes pour que la page se charge complètement",
)
basic_auth = st.text_input(label="Authentification de base", disabled=True)
session_cookie = st.text_input(label="Cookie de session", disabled=True)

submitted = st.form_submit_button(
label="Lancer l'analyse",
on_click=run_analysis,
kwargs={
"urls": urls,
"sizes": sizes,
"wait_before_scroll": wait_before_scroll,
"wait_after_scroll": wait_after_scroll,
},
)

if submitted:
run_analysis(
urls=urls,
sizes=sizes,
wait_before_scroll=wait_before_scroll,
wait_after_scroll=wait_after_scroll,
)


container_website_search = tab_website.container()
container_website_results = tab_website.container()

container_website_search.header("Rechercher un résultat d'analyse")

0 comments on commit 102ef65

Please sign in to comment.