Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
✨ feat(report): Add global median stat (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatelot authored Jun 23, 2022
1 parent 6964f46 commit 4de658f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
4 changes: 3 additions & 1 deletion ecoindex_cli/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def analyze(
with progressbar(
length=len(urls) * len(window_sizes),
label="Processing",
show_percent=True,
show_pos=True,
) as progress:
error_found = False
with ThreadPoolExecutor(max_workers=max_workers) as executor:
Expand All @@ -159,7 +161,7 @@ def analyze(
results.append(future.result())
except Exception as e:
error_found = True
log.error(" -- " + url + " -- " + e.msg)
log.error(f" -- {url} -- {e.msg if hasattr(e, 'msg') else e}")

progress.update(1)

Expand Down
12 changes: 9 additions & 3 deletions ecoindex_cli/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Language(Enum):


class Target(Enum):
nodes = 878 # Arbitrary value, must be evaluated later
requests = 69 # Based on https://almanac.httparchive.org/en/2021/page-weight#requests (Mobile)
size = 1923 # Based on https://almanac.httparchive.org/en/2021/page-weight#page-weight-by-the-numbers (Mobile)
nodes = 500
requests = 27
size = 900


class GlobalMedian(Enum):
nodes = 693
requests = 78
size = 2410
47 changes: 33 additions & 14 deletions ecoindex_cli/report/report.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from pathlib import Path

from ecoindex_cli.enums import Language, Target
from ecoindex_cli.enums import GlobalMedian, Language, Target
from ecoindex_cli.files import get_translations
from jinja2 import Environment, FileSystemLoader
from matplotlib import pyplot
Expand All @@ -26,9 +26,21 @@ def __init__(
self.translations = get_translations(language=language)

def create_report(self) -> None:
self.create_histogram(property="requests", target=Target.requests.value)
self.create_histogram(property="size", target=Target.size.value)
self.create_histogram(property="nodes", target=Target.nodes.value)
self.create_histogram(
property="requests",
target=Target.requests.value,
global_median=GlobalMedian.requests.value,
)
self.create_histogram(
property="size",
target=Target.size.value,
global_median=GlobalMedian.size.value,
)
self.create_histogram(
property="nodes",
target=Target.nodes.value,
global_median=GlobalMedian.nodes.value,
)
self.create_grade_chart()
self.create_report_file()

Expand All @@ -42,6 +54,7 @@ def create_histogram(
self,
property: str,
target: int,
global_median: int,
) -> None:
median = round(self.dataframe[property].median())
self.prepare_graph(property=property)
Expand All @@ -50,11 +63,17 @@ def create_histogram(
median, color="blue", label=f"{self.translations['my_median']}: {median}"
)
ax.axvline(
target,
x=target,
color="red",
linestyle="--",
linestyle=":",
label=f"{self.translations['target_median']}: {target}",
)
ax.axvline(
x=global_median,
color="black",
linestyle=":",
label=f"{self.translations['global_median']}: {global_median}",
)
pyplot.legend()
fig = ax.get_figure()
fig.savefig(f"{self.output_path}/{property}.svg")
Expand Down Expand Up @@ -86,16 +105,16 @@ def create_grade_chart(self) -> None:
fig = ax.get_figure()
fig.savefig(f"{self.output_path}/grade.svg")

def get_property_comment(self, target: int, property: str) -> str:
if self.dataframe[property].median() <= target:
def get_property_comment(self, global_median: int, property: str) -> str:
if self.dataframe[property].median() <= global_median:
return (
f"<span style='color:green'>{self.translations['good_result']} <b>{round(self.dataframe[property].median(), 2)}</b> "
f"{self.translations['better_than']} <b>{target}</b></span>"
f"{self.translations['better_than']} <b>{global_median}</b></span>"
)

return (
f"<span style='color:red'>{self.translations['bad_result']} <b>{round(self.dataframe[property].median(), 2)}</b> "
f"{self.translations['worse_than']} <b>{target}</b></span>"
f"{self.translations['worse_than']} <b>{global_median}</b></span>"
)

def create_report_file(self) -> None:
Expand All @@ -120,7 +139,7 @@ def create_report_file(self) -> None:
["score", "size", "nodes", "requests", "ges", "water"]
]
.describe(percentiles=[0.5])
.loc[["median", "50%", "min", "max"]]
.loc[["mean", "50%", "min", "max"]]
.round(2)
.to_html(classes="table is-hoverable is-fullwidth is-bordered"),
"best": self.dataframe.nlargest(n=10, columns="score")[
Expand All @@ -130,15 +149,15 @@ def create_report_file(self) -> None:
["url", "score", "size", "nodes", "requests"]
].to_html(classes="table is-hoverable is-fullwidth is-bordered"),
"size_comment": self.get_property_comment(
target=Target.size.value,
global_median=GlobalMedian.size.value,
property="size",
),
"nodes_comment": self.get_property_comment(
target=Target.nodes.value,
global_median=GlobalMedian.nodes.value,
property="nodes",
),
"requests_comment": self.get_property_comment(
target=Target.requests.value,
global_median=GlobalMedian.requests.value,
property="requests",
),
}
Expand Down
5 changes: 3 additions & 2 deletions ecoindex_cli/report/translations/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ footer: <strong><a href="https://github.com/cnumr/ecoindex_cli">Ecoindex_cli</a>

good_result: 😃 Your result is great!
bad_result: 😞 You could do better on this...
better_than: is better than the target
worse_than: is worse than the target
better_than: is better than
worse_than: is worse than
my_median: My median
target_median: Target median
global_median: Global median
1 change: 1 addition & 0 deletions ecoindex_cli/report/translations/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ better_than: ", c'est mieux que"
worse_than: ", c'est moins bien que"
my_median: Ma valeur médiane
target_median: Valeur médiane cible
global_median: Valeur médiane globale

0 comments on commit 4de658f

Please sign in to comment.