Skip to content

Commit

Permalink
improving metric organization and adding title
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsochat@stanford.edu>
  • Loading branch information
vsoch committed Dec 23, 2020
1 parent 8c1bf2f commit c395547
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 45 deletions.
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,17 @@ data on the level of individual files, or summary results:
for name, metric in extractor:
# Changedlines <caliper.metrics.collection.changedlines.metric.Changedlines at 0x7f7cd24f4940>
# A lookup of v1..v2 with a list of files
# A lookup with file level changes
metric.get_file_results()
# A lookup of v1..v2 for summed changed
metric.get_summed_results()
# A lookup with group or summed changed
metric.get_group_results()
# A lookup with "by-file" and "by-group" that includes both the above
metric.get_results()
```
For example, an entry in summed results might look like this:
For example, an entry in group results might look like this:
```
{'0.2.34..0.2.35': {'size': 0, 'insertions': 4, 'deletions': 4, 'lines': 8}}
Expand All @@ -381,15 +384,15 @@ We will eventually have more examples for how to parse and use this data.
To extract and view metrics, you can use `caliper view`
```bash
$ calipler view --help
usage: caliper view [-h] [--metric METRIC] [--outdir OUTDIR] [--force] input
usage: caliper view [-h] [--metric METRIC] [--title TITLE] [--outdir OUTDIR] [--force] input
positional arguments:
input input data file to visualize.
optional arguments:
-h, --help show this help message and exit
--metric METRIC a metric to extract
--title TITLE the title for the graph (defaults to one set by metric)
--outdir OUTDIR output directory to write files (defaults to temporary directory)
--force if a file exists, do not overwrite.
```
Expand All @@ -398,7 +401,14 @@ For example, let's say we want to view an already extracted metric. We would pro
as input:
```bash
$ caliper view ../caliper-metrics/github/spack/spack/changedlines/changedlines-summed-results.json
$ caliper view ../caliper-metrics/pypi/singularity-cli/changedlines/changedlines-results.json
```
We might also add a custom title:
```bash
$ caliper view ../caliper-metrics/pypi/singularity-cli/changedlines/changedlines-results.json --title "Singularity Registry Client Version Changes"
```
Note that caliper will attempt to derive the metric name from the file. If you've renamed the
Expand All @@ -407,8 +417,12 @@ file, then you'll need to provide it directly:
```bash
$ caliper view --metric changedlines ../caliper-metrics/github/spack/spack/changedlines/changedlines-summed-results.json
```
Note from the usage that you can also select an output directory. Caliper tries
to derive the name of the metric from the filename (e.g., `<metric>-results.json`
however if you rename the file, you can specify the metric directly with `--metric`.
You can see an example in [docs](https://vsoch.github.io/caliper/). We expect to have
more examples when we improve the documentation.
## Use Cases
Expand Down
5 changes: 5 additions & 0 deletions caliper/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ def get_parser():
help="a metric to extract",
)

view.add_argument(
"--title",
help="the title for the graph (defaults to one set by metric)",
)

view.add_argument(
"input",
help="input data file to visualize.",
Expand Down
2 changes: 0 additions & 2 deletions caliper/client/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,3 @@ def main(args, extra):
# Cleanup, unless disabled
if not args.no_cleanup:
client.cleanup(force=True)

print("Results written to %s" % outdir)
2 changes: 1 addition & 1 deletion caliper/client/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def main(args, extra):
# prepare top level output directory
outdir = args.outdir or os.getcwd()
metric = client.get_metric(metric)
metric.plot_results(args.input, outdir, force=args.force)
metric.plot_results(args.input, outdir, force=args.force, title=args.title)
32 changes: 13 additions & 19 deletions caliper/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,20 @@ def save_all(self, outdir, force=False):

package_dir = os.path.join(outdir, self.manager.name, self.manager.uri)

written = True
for _, extractor in self._extractors.items():
extractor_dir = os.path.join(package_dir, extractor.name)
mkdir_p(extractor_dir)

# Write results to file
for results, outfile in [
(
extractor.get_file_results(),
os.path.join(
extractor_dir, "%s-file-results.json" % extractor.name
),
),
(
extractor.get_summed_results(),
os.path.join(
extractor_dir, "%s-summed-results.json" % extractor.name
),
),
]:
if os.path.exists(outfile) and not force:
logger.warning("%s exists and force is False, skipping." % outfile)
continue
write_json(results, outfile)
# Prepare to write results to file
outfile = os.path.join(extractor_dir, "%s-results.json" % extractor.name)
if os.path.exists(outfile) and not force:
logger.warning("%s exists and force is False, skipping." % outfile)
continue

written = True
results = extractor.get_results()
write_json(results, outfile)

if written:
logger.info("Results written to %s" % outdir)
21 changes: 15 additions & 6 deletions caliper/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,23 @@ def _extract(self, commit):
pass

@abstractmethod
def get_results(self):
"""return a lookup of the acceptable results type. This varies by
metric, but if given a results dictionary, the metric should be able
to match a result to a visualization, for example.
"""
return {
"by-file": self.get_file_results(),
"by-group": self.get_group_results(),
}

def get_file_results(self):
pass
return []

@abstractmethod
def get_summed_results(self):
pass
def get_group_results(self):
return []

def plot_results(self, result_file, outdir=None, force=False):
def plot_results(self, result_file, outdir=None, force=False, title=None):
"""Given a metric has a template and a function to generate data
for it, create the graph for the user.
"""
Expand All @@ -53,7 +62,7 @@ def plot_results(self, result_file, outdir=None, force=False):
from caliper.metrics.graphs import generate_graph

outdir = outdir or get_tmpdir("%s-graph-" % self.name)
data = self.get_plot_data(result_file)
data = self.get_plot_data(result_file, title=title)
generate_graph(template=template, data=data, outdir=outdir, force=force)
else:
logger.warning("A metric must have template.html and get_plot_data.")
Expand Down
15 changes: 7 additions & 8 deletions caliper/metrics/collection/changedlines/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Changedlines(ChangeMetricBase):
def __init__(self, git):
super().__init__(git=git, filename=__file__)

def get_plot_data(self, result_file):
def get_plot_data(self, result_file, title=None):
"""Given extracted data, return data to render into a template. This
function should load data into self._data.
"""
Expand All @@ -30,11 +30,10 @@ def get_plot_data(self, result_file):
if not self._data:
logger.exit("Data file %s is empty." % filename)

# We currently only plot insertions and deletions
keys = list(list(self._data.values())[0].keys())
for key in ["insertions", "deletions"]:
if key not in keys:
logger.exit("key %s is missing from %s." % (key, filename))
# We currently support just the group plot
if "by-group" not in self._data:
logger.exit("by-group key is missing from data.")
self._data = self._data["by-group"]

# Prepare datasets, each of a different color, and title
labels = self._derive_labels()
Expand All @@ -47,7 +46,7 @@ def get_plot_data(self, result_file):

return {
"datasets": datasets,
"title": "Insertions and Deletions",
"title": title or "Insertions and Deletions",
"labels": labels,
}

Expand Down Expand Up @@ -89,7 +88,7 @@ def get_file_results(self):
"""return a lookup of changes, where each change has a list of files"""
return self._data

def get_summed_results(self):
def get_group_results(self):
"""Get summed values (e.g., lines changed) across files"""
results = {}
summary_keys = ["insertions", "deletions", "lines"]
Expand Down
2 changes: 1 addition & 1 deletion caliper/metrics/collection/totalcounts/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def get_file_results(self):
"""return a lookup of changes, where each change has a list of files"""
return self._data

def get_summed_results(self):
def get_group_results(self):
"""Get summed values (e.g., lines changed) across files"""
return self._data
62 changes: 62 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!doctype html>
<html>
<head>
<title>Stacked Bar Chart with Groups</title>
<script src="https://www.chartjs.org/dist/2.9.4/Chart.min.js"></script>
<style>
canvas {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<div style="width: 75%">
<canvas id="canvas"></canvas>
</div>
<script>
var barChartData = {
labels: [ "EMPTY..0.1.0", "0.1.0..0.1.1", "0.1.1..0.1.10", "0.1.2..0.1.4", "0.1.4..0.1.5", "0.1.5..0.1.6", "0.1.6..0.1.7", "0.1.7..0.1.8", "0.1.8..0.1.9", "0.1.10..0.1.2"],
datasets: [{
label: 'Insertions',
backgroundColor: "turquoise",
stack: 'Stack 0',
data: [ 1378, 31, 104, 9, 44, 4, 6, 4, 23, 20]
},{
label: 'Deletions',
backgroundColor: "tomato",
stack: 'Stack 0',
data: [ 0, 14, 33, 6, 10, 4, 5, 4, 6, 79]
}]

};
window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
window.myBar = new Chart(ctx, {
type: 'bar',
data: barChartData,
options: {
title: {
display: true,
text: 'Singularity Registry Client Version Changes'
},
tooltips: {
mode: 'index',
intersect: false
},
responsive: true,
scales: {
xAxes: [{
stacked: true,
}],
yAxes: [{
stacked: true
}]
}
}
});
};
</script>
</body>
</html>

0 comments on commit c395547

Please sign in to comment.