Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/dashboard/dashboard.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020-2021 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from pathlib import Path | from pathlib import Path | ||||
import dash | import dash | ||||
from dash.dependencies import Input, Output | from dash.dependencies import Input, Output | ||||
import dash_bootstrap_components as dbc | import dash_bootstrap_components as dbc | ||||
import dash_core_components as dcc | import dash_core_components as dcc | ||||
import dash_html_components as html | import dash_html_components as html | ||||
import plotly.graph_objects as go | import plotly.graph_objects as go | ||||
from ..model import Tree | from swh.model.from_disk import Directory | ||||
from ..data import MerkleNodeInfo, get_content_from | |||||
def generate_table_body(dir_path: Path, source: Tree): | |||||
def generate_table_body( | |||||
dir_path: bytes, source_tree: Directory, nodes_data: MerkleNodeInfo | |||||
): | |||||
""" | """ | ||||
Generate the data_table from the path taken from the chart. | Generate the data_table from the path taken from the chart. | ||||
For each file builds the html table rows showing the known status, a local link to | For each file builds the html table rows showing the known status, a local link to | ||||
the file and the relative SoftWare Heritage persistent IDentifier (SWHID). | the file and the relative SoftWare Heritage persistent IDentifier (SWHID). | ||||
""" | """ | ||||
contents = get_content_from(dir_path, source_tree, nodes_data) | |||||
data = [] | data = [] | ||||
for file_info in source.get_files_from_dir(dir_path): | for cnt, attr in contents.items(): | ||||
for file_path, attr in file_info.items(): | file_path = Path(cnt.decode()) | ||||
file_path = Path(file_path) | |||||
file_name = file_path.parts[len(file_path.parts) - 1] | file_name = file_path.parts[len(file_path.parts) - 1] | ||||
full_file_path = Path(Path(dir_path.decode()), file_path) | |||||
data.append( | data.append( | ||||
html.Tr( | html.Tr( | ||||
[ | [ | ||||
html.Td("✔" if attr["known"] else ""), | html.Td("✔" if attr["known"] else ""), | ||||
html.Td( | html.Td(html.A(file_name, href="file://" + str(full_file_path))), | ||||
html.A(file_name, href="file://" + str(file_path.resolve())) | |||||
), | |||||
html.Td(attr["swhid"]), | html.Td(attr["swhid"]), | ||||
] | ] | ||||
) | ) | ||||
) | ) | ||||
return [html.Tbody(data)] | return [html.Tbody(data)] | ||||
def run_app(graph_obj: go, source: Tree): | def run_app(graph_obj: go, source_tree: Directory, nodes_data: MerkleNodeInfo): | ||||
app = dash.Dash(__name__) | app = dash.Dash(__name__) | ||||
fig = go.Figure().add_trace(graph_obj) | fig = go.Figure().add_trace(graph_obj) | ||||
fig.update_layout(height=800,) | fig.update_layout(height=800,) | ||||
table_header = [ | table_header = [ | ||||
html.Thead(html.Tr([html.Th("KNOWN"), html.Th("FILE NAME"), html.Th("SWHID")])) | html.Thead(html.Tr([html.Th("KNOWN"), html.Th("FILE NAME"), html.Th("SWHID")])) | ||||
] | ] | ||||
Show All 29 Lines | def run_app(graph_obj: go, source_tree: Directory, nodes_data: MerkleNodeInfo): | ||||
) | ) | ||||
def update_files_table(click_data): | def update_files_table(click_data): | ||||
""" | """ | ||||
Callback that takes the input (directory path) from the chart and | Callback that takes the input (directory path) from the chart and | ||||
update the `files_table` children with the relative files. | update the `files_table` children with the relative files. | ||||
""" | """ | ||||
if click_data is not None: | if click_data is not None: | ||||
raw_path = click_data["points"][0]["label"] | full_path = click_data["points"][0]["label"] | ||||
full_path = ( | return ( | ||||
source.path.joinpath(raw_path) | table_header | ||||
if raw_path != str(source.path) | + generate_table_body(full_path.encode(), source_tree, nodes_data), | ||||
else Path(raw_path) | full_path, | ||||
) | ) | ||||
return table_header + generate_table_body(full_path, source), str(full_path) | |||||
else: | else: | ||||
return "", "" | return "", "" | ||||
app.run_server(debug=True, use_reloader=True) | app.run_server(debug=True, use_reloader=True) |