Skip to content

Commit

Permalink
Merge branch 'tonybaloney:master' into unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
devdanzin authored Sep 4, 2023
2 parents 43cc490 + 099bc78 commit 4b63be0
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 92 deletions.
20 changes: 19 additions & 1 deletion docs/source/commands/graph.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ The x-axis will be the historic revisions (typically git commits) on a scale of
.. image:: ../_static/custom_x_axis_graph.png
:align: center

You can provide more than one path for ``wily graph`` so it displays one line per file path. If the path is a directory, all files contained in it and subdirectories will be added to the graph.

.. code-block:: none
$ wily graph example.py example2.py example3.py tests/ -m loc
If one or more of the provided paths is a directory, you can use the ``--aggregate`` option to get a line with aggregated total metric values for all files in that path.

.. code-block:: none
$ wily tests/ -m loc --aggregate
By default, ``wily graph`` will only plot revisions where metric values have changed. To show all revisions, use the ``--all`` option.

.. code-block:: none
$ wily tests/ -m loc --all
By default, ``wily graph`` will create a file, ``wily-report.html`` in the current directory and open it using the browser configured in the $BROWSER environment variable (the default on the OS).
To save the output to a specific HTML file and not open it, provide the ``-o`` flag and the name of the output file.

Expand All @@ -46,4 +64,4 @@ Command Line Usage

.. click:: wily.__main__:graph
:prog: wily
:show-nested:
:show-nested:
22 changes: 17 additions & 5 deletions docs/source/commands/report.rst
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
Report Command
==============

The report command will show a CLI table of metrics for the list of files provided. It is useful for comparing differences and trends between revisions.
The report command will show a CLI table or an HTML report of metrics for the file provided. It is useful for comparing differences and trends between revisions.

.. image:: ../_static/wily_report.png
:align: center

Examples
--------

To show a report, simply give the name or path to the file you want to report on
To show a report, simply give the name or path to the file you want to report on.

.. code-block:: none
$ wily report example.py
By default, wily will show the default metrics (typically Lines-of-code, cyclomatic complexity and maintainability index)
By default, wily will show the default metrics (typically Lines-of-code, cyclomatic complexity and maintainability index).

To change the metrics, provide the metric names (run ``wily list-metrics`` for a list) as arguments.

.. code-block:: none
$ wily report example.py loc sloc comments
Wily report will show all available revisions, to only show a set number, add the ``-n`` or ``--number`` flag
Wily report will show all available revisions, to only show a set number, add the ``-n`` or ``--number`` flag.

.. code-block:: none
$ wily report example.py -n 10
Wily report will display all revisions, to only show those where metric values have changed, add the ``-c`` or ``--changes`` flag.

.. code-block:: none
$ wily report example.py --changes
Similar to the index command, ``wily report`` will not show the commit message. To add the message to the output, add the ``--message`` flag.

.. code-block:: none
$ wily report example.py --message
By default, wily will show a CLI table report of metrics. To create an HTML report instead, pass the ``--format HTML`` option.

.. code-block:: none
$ wily report example.py --format HTML
Command Line Usage
------------------

.. click:: wily.__main__:report
:prog: wily
:show-nested:
:show-nested:
15 changes: 10 additions & 5 deletions src/wily/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,20 +386,25 @@ def diff(ctx, files, metrics, all, detail, revision, wrap):
Graph all .py files within src/ for the raw.loc metric
$ wily graph src/ raw.loc
$ wily graph src/ -m raw.loc
Graph test.py against raw.loc and cyclomatic.complexity metrics
$ wily graph src/test.py raw.loc cyclomatic.complexity
$ wily graph src/test.py -m raw.loc,cyclomatic.complexity
Graph test.py against raw.loc and raw.sloc on the x-axis
$ wily graph src/test.py raw.loc --x-axis raw.sloc
$ wily graph src/test.py -m raw.loc --x-axis raw.sloc
"""
)
)
@click.argument("path", type=click.Path(resolve_path=False))
@click.argument("metrics", nargs=-1, required=True)
@click.argument("path", nargs=-1, type=click.Path(resolve_path=False))
@click.option(
"-m",
"--metrics",
required=True,
help=_("Comma-separated list of metrics, see list-metrics for choices"),
)
@click.option(
"-o",
"--output",
Expand Down
14 changes: 11 additions & 3 deletions src/wily/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ def build(config: WilyConfig, archiver: Archiver, operators: List[Operator]) ->

# Copy the ir from any unchanged files from the prev revision
if not seed:
missing_indices = set(revision.tracked_files) - indices
# File names in result are platform dependent, so we convert
# to Path and back to str.
files = {str(pathlib.Path(f)) for f in revision.tracked_files}
missing_indices = files - indices
# TODO: Check existence of file path.
for missing in missing_indices:
# Don't copy aggregate keys as their values may have changed
Expand All @@ -155,9 +158,14 @@ def build(config: WilyConfig, archiver: Archiver, operators: List[Operator]) ->
for deleted in revision.deleted_files:
result.pop(deleted, None)

# Add empty path for storing total aggregates
dirs = [""]
# Directory names in result are platform dependent, so we convert
# to Path and back to str.
dirs += [str(pathlib.Path(d)) for d in revision.tracked_dirs if d]
# Aggregate metrics across all root paths using the aggregate function in the metric
# Note assumption is that nested dirs are listed after parent..
for root in revision.tracked_dirs:
# Note assumption is that nested dirs are listed after parent, hence sorting.
for root in sorted(dirs):
# find all matching entries recursively
aggregates = [
path for path in result.keys() if path.startswith(root)
Expand Down
8 changes: 2 additions & 6 deletions src/wily/commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,11 @@ def diff(
if metric.metric_type in (int, float) and new != "-" and current != "-":
if current > new: # type: ignore
metrics_data.append(
"{0:n} -> \u001b[{2}m{1:n}\u001b[0m".format(
current, new, BAD_COLORS[metric.measure]
)
f"{current:n} -> \u001b[{BAD_COLORS[metric.measure]}m{new:n}\u001b[0m"
)
elif current < new: # type: ignore
metrics_data.append(
"{0:n} -> \u001b[{2}m{1:n}\u001b[0m".format(
current, new, GOOD_COLORS[metric.measure]
)
f"{current:n} -> \u001b[{GOOD_COLORS[metric.measure]}m{new:n}\u001b[0m"
)
else:
metrics_data.append(f"{current:n} -> {new:n}")
Expand Down
51 changes: 33 additions & 18 deletions src/wily/commands/graph.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
Draw graph in HTML for a specific metric.
Graph command.
TODO: Add multiple lines for multiple files
Draw graph in HTML for a specific metric.
"""

from pathlib import Path
from typing import Optional, Tuple, Union
from typing import Optional, Tuple

import plotly.graph_objs as go
import plotly.offline
Expand All @@ -22,10 +22,17 @@ def metric_parts(metric):
return operator.name, met.name


def path_startswith(filename: str, path: str) -> bool:
"""Check whether a filename starts with a given path in platform-agnostic way."""
filepath = Path(filename).resolve()
path_ = Path(path).resolve()
return str(filepath).startswith(str(path_))


def graph(
config: WilyConfig,
path: str,
metrics: Union[Tuple[str], Tuple[str, str]],
path: Tuple[str, ...],
metrics: str,
output: Optional[str] = None,
x_axis: Optional[str] = None,
changes: bool = True,
Expand Down Expand Up @@ -55,27 +62,35 @@ def graph(
else:
x_operator, x_key = metric_parts(x_axis)

y_metric = resolve_metric(metrics[0])
title = f"{x_axis.capitalize()} of {y_metric.description} for {path}{' aggregated' if aggregate else ''}"
metrics_list = metrics.split(",")

y_metric = resolve_metric(metrics_list[0])

if not aggregate:
tracked_files = set()
for rev in state.index[state.default_archiver].revisions:
tracked_files.update(rev.revision.tracked_files)
paths = {
tracked_file
for tracked_file in tracked_files
if tracked_file.startswith(path)
} or {path}
paths = (
tuple(
tracked_file
for tracked_file in tracked_files
if any(path_startswith(tracked_file, p) for p in path)
)
or path
)
else:
paths = {path}
paths = path

operator, key = metric_parts(metrics[0])
if len(metrics) == 1: # only y-axis
title = (
f"{x_axis.capitalize()} of {y_metric.description}"
f"{(' for ' + paths[0]) if len(paths) == 1 else ''}{' aggregated' if aggregate else ''}"
)
operator, key = metric_parts(metrics_list[0])
if len(metrics_list) == 1: # only y-axis
z_axis = z_operator = z_key = ""
else:
z_axis = resolve_metric(metrics[1])
z_operator, z_key = metric_parts(metrics[1])
z_axis = resolve_metric(metrics_list[1])
z_operator, z_key = metric_parts(metrics_list[1])
for path_ in paths:
current_path = str(Path(path_))
x = []
Expand Down Expand Up @@ -129,7 +144,7 @@ def graph(
ids=state.index[state.default_archiver].revision_keys,
text=labels,
marker={
"size": 0 if z_axis is None else z,
"size": 0 if not z_axis else z,
"color": list(range(len(y))),
# "colorscale": "Viridis",
},
Expand Down
8 changes: 7 additions & 1 deletion src/wily/commands/list_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ def list_metrics(wrap: bool) -> None:
tabulate.tabulate(
headers=headers,
tabular_data=[
(m.name, m.description, m.metric_type, m.measure, m.aggregate)
(
m.name,
m.description,
m.metric_type.__name__,
m.measure,
m.aggregate.__name__,
)
for m in operator.operator_cls.metrics
],
tablefmt=style,
Expand Down
20 changes: 0 additions & 20 deletions src/wily/decorators.py

This file was deleted.

12 changes: 6 additions & 6 deletions src/wily/templates/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/*//////////////////////////////////////////////////////////////////
[ RESTYLE TAG ]*/
* {
margin: 0px;
padding: 0px;
margin: 0px;
padding: 0px;
box-sizing: border-box;
}

Expand Down Expand Up @@ -69,7 +69,7 @@ ul, li {
.wrap-table100 {
width: 960px;
border-radius: 10px;
overflow: hidden;
overflow: scroll;
}

table {
Expand Down Expand Up @@ -175,7 +175,7 @@ tr:hover {
padding-right: 15px;
margin: 0;
}

tr td {
border: none;
padding-left: 30px;
Expand All @@ -185,7 +185,7 @@ tr:hover {
tr td:nth-child(1) {
padding-left: 30px;
}

tr td {
font-size: 18px;
color: #555555;
Expand All @@ -208,4 +208,4 @@ tr:hover {

.orange-color {
color: darkorange;
}
}
Loading

0 comments on commit 4b63be0

Please sign in to comment.