Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/plot.py
Show All 10 Lines | |||||
directories information retrieved from the model. The chart displays for | directories information retrieved from the model. The chart displays for | ||||
each directory the total number of files and the percentage of file known. | each directory the total number of files and the percentage of file known. | ||||
The size of the directory is defined by the total number of contents whereas | The size of the directory is defined by the total number of contents whereas | ||||
the color gradient is generated relying on the percentage of contents known. | the color gradient is generated relying on the percentage of contents known. | ||||
""" | """ | ||||
from typing import List, Dict, Tuple | from typing import List, Dict, Tuple | ||||
from pathlib import PosixPath | from pathlib import Path | ||||
from plotly.offline import offline | from plotly.offline import offline | ||||
import plotly.graph_objects as go | import plotly.graph_objects as go | ||||
import pandas as pd # type: ignore | import pandas as pd # type: ignore | ||||
import numpy as np # type: ignore | import numpy as np # type: ignore | ||||
def build_hierarchical_df( | def build_hierarchical_df( | ||||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Lines | total = pd.Series( | ||||
dict(id=root_name, parent="", contents=total_contents, known=total_avg) | dict(id=root_name, parent="", contents=total_contents, known=total_avg) | ||||
) | ) | ||||
complete_df = complete_df.append(total, ignore_index=True) | complete_df = complete_df.append(total, ignore_index=True) | ||||
return complete_df | return complete_df | ||||
def compute_max_depth(dirs_path: List[PosixPath], root: PosixPath) -> int: | def compute_max_depth(dirs_path: List[Path], root: Path) -> int: | ||||
"""Compute the maximum depth level of the given directory paths. | """Compute the maximum depth level of the given directory paths. | ||||
Example: for `var/log/kernel/` the depth level is 3 | Example: for `var/log/kernel/` the depth level is 3 | ||||
""" | """ | ||||
max_depth = 0 | max_depth = 0 | ||||
for dir_path in dirs_path: | for dir_path in dirs_path: | ||||
if dir_path == root: | if dir_path == root: | ||||
continue | continue | ||||
dir_depth = len(dir_path.parts) | dir_depth = len(dir_path.parts) | ||||
if dir_depth > max_depth: | if dir_depth > max_depth: | ||||
max_depth = dir_depth | max_depth = dir_depth | ||||
return max_depth | return max_depth | ||||
def generate_df_from_dirs( | def generate_df_from_dirs( | ||||
dirs: Dict[PosixPath, Tuple[int, int]], | dirs: Dict[Path, Tuple[int, int]], columns: List[str], root: Path, max_depth: int, | ||||
columns: List[str], | |||||
root: PosixPath, | |||||
max_depth: int, | |||||
) -> pd.DataFrame: | ) -> pd.DataFrame: | ||||
"""Generate a dataframe from the directories given in input. | """Generate a dataframe from the directories given in input. | ||||
Example: | Example: | ||||
given the following directories as input | given the following directories as input | ||||
.. code-block:: python | .. code-block:: python | ||||
Show All 9 Lines | .. code-block:: none | ||||
lev0 lev1 lev2 contents known | lev0 lev1 lev2 contents known | ||||
'var' 'var/log' '' 23 2 | 'var' 'var/log' '' 23 2 | ||||
'var' 'var/log' 'var/log/kernel' 5 0 | 'var' 'var/log' 'var/log/kernel' 5 0 | ||||
'var' 'var/log' 'var/log/telnet' 10 3 | 'var' 'var/log' 'var/log/telnet' 10 3 | ||||
""" | """ | ||||
def get_parents(path: PosixPath): | def get_parents(path: Path): | ||||
parts = path.parts[1:] if path.parts[0] == "/" else path.parts | parts = path.parts[1:] if path.parts[0] == "/" else path.parts | ||||
for i in range(1, len(parts) + 1): | for i in range(1, len(parts) + 1): | ||||
yield "/".join(parts[0:i]) | yield "/".join(parts[0:i]) | ||||
def get_dirs_array(): | def get_dirs_array(): | ||||
for dir_path, contents_info in dirs.items(): | for dir_path, contents_info in dirs.items(): | ||||
empty_lvl = max_depth - len(dir_path.parts) | empty_lvl = max_depth - len(dir_path.parts) | ||||
Show All 12 Lines | ) -> pd.DataFrame: | ||||
df["contents"] = pd.to_numeric(df["contents"]) | df["contents"] = pd.to_numeric(df["contents"]) | ||||
df["known"] = pd.to_numeric(df["known"]) | df["known"] = pd.to_numeric(df["known"]) | ||||
return df | return df | ||||
def generate_sunburst( | def generate_sunburst( | ||||
directories: Dict[PosixPath, Tuple[int, int]], root: PosixPath | directories: Dict[Path, Tuple[int, int]], root: Path | ||||
) -> go.Sunburst: | ) -> go.Sunburst: | ||||
"""Generate a sunburst chart from the directories given in input. | """Generate a sunburst chart from the directories given in input. | ||||
""" | """ | ||||
max_depth = compute_max_depth(list(directories.keys()), root) | max_depth = compute_max_depth(list(directories.keys()), root) | ||||
metrics_columns = ["contents", "known"] | metrics_columns = ["contents", "known"] | ||||
levels_columns = ["lev" + str(i) for i in range(max_depth)] | levels_columns = ["lev" + str(i) for i in range(max_depth)] | ||||
Show All 33 Lines |