Skip to content

Commit 6fb36ad

Browse files
committed
extend substitution support
Signed-off-by: Sylvain Hellegouarch <[email protected]>
1 parent e54a5cf commit 6fb36ad

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
## [Unreleased][]
44

5-
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-reporting/compare/0.15.0...HEAD
5+
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-reporting/compare/0.16.0...HEAD
6+
7+
## [0.16.0][] - 2023-12-05
8+
9+
[0.16.0]: https://github.com/chaostoolkit/chaostoolkit-reporting/compare/0.15.0...0.16.0
10+
11+
### Added
12+
13+
- Extended support for substituoon to tolerance and pauses properties
614

715
## [0.15.0][] - 2023-11-30
816

chaosreport/__init__.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
import pygal
2121
import pypandoc
2222
import semver
23-
from chaoslib import merge_vars, substitute
23+
from chaoslib import substitute
2424
from chaoslib.caching import cache_activities, lookup_activity
25-
from chaoslib.configuration import load_configuration
26-
from chaoslib.secret import load_secrets
2725
from chaoslib.types import Configuration, Experiment, Journal, Run, Secrets
2826
from jinja2 import Environment, PackageLoader
2927
from logzero import logger
@@ -36,7 +34,7 @@
3634
"generate_report_header",
3735
"save_report",
3836
]
39-
__version__ = "0.15.0"
37+
__version__ = "0.16.0"
4038

4139
curdir = os.getcwd()
4240
basedir = os.path.dirname(__file__)
@@ -248,20 +246,17 @@ def generate_report_header(
248246

249247

250248
def generate_report(
251-
journal_path: str,
249+
journal: Dict[str, Any],
252250
export_format: str = "markdown",
253-
var: Dict[str, Any] = None,
254-
var_file: List[str] = None,
251+
config: Configuration = None,
252+
secrets: Secrets = None,
255253
) -> str:
256254
"""
257255
Generate a report document from a chaostoolkit journal.
258256
259257
The report is first generated from the markdown template and converted to
260258
the desired format using Pandoc.
261259
"""
262-
with io.open(journal_path) as fp:
263-
journal = json.load(fp)
264-
265260
# inject some pre-processed values into the journal for rendering
266261
experiment = journal["experiment"]
267262
cache_activities(experiment)
@@ -275,11 +270,6 @@ def generate_report(
275270

276271
generate_chart_from_metric_probes(journal, export_format)
277272
add_contribution_model(journal, export_format)
278-
config_vars, secret_vars = merge_vars(var, var_file) or (None, None)
279-
config = load_configuration(
280-
experiment.get("configuration", {}), config_vars
281-
)
282-
secrets = load_secrets(experiment.get("secrets", {}), secret_vars)
283273
template = get_report_template(
284274
journal["chaoslib-version"], configuration=config, secrets=secrets
285275
)
@@ -364,13 +354,23 @@ def get_report_template(
364354
env.globals["pretty_duration"] = lambda d0, d1: date.delta(
365355
dateparser.parse(d0), dateparser.parse(d1), words=False
366356
)[0]
367-
env.globals["substitute"] = (
368-
lambda args: {
369-
k: substitute(v, configuration, secrets) for k, v in args.items()
370-
}
371-
if isinstance(args, dict)
372-
else args
373-
)
357+
358+
def substitution(args, is_tolerance: bool = False) -> Any:
359+
if is_tolerance:
360+
if isinstance(args, dict):
361+
if args.get("type") == "probe":
362+
args = args.get("provider", {}).get("arguments")
363+
args.pop("value", None)
364+
365+
if isinstance(args, dict):
366+
return {
367+
k: substitute(v, configuration, secrets)
368+
for k, v in args.items()
369+
}
370+
371+
return substitute(args, configuration, secrets)
372+
373+
env.globals["substitute"] = substitution
374374

375375
if not report_version:
376376
return env.get_template(default_template)

chaosreport/cli.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# -*- coding: utf-8 -*-
2+
import io
3+
import json
24
import os
35
from glob import glob
46
from typing import Any, Dict, List
57

68
import click
7-
from chaoslib import convert_vars
9+
from chaoslib import convert_vars, merge_vars
10+
from chaoslib.configuration import load_configuration
11+
from chaoslib.secret import load_secrets
812

913
from chaosreport import generate_report, generate_report_header, save_report
1014

@@ -70,7 +74,18 @@ def report(
7074
if len(journal) == 1 and not os.path.isfile(journal[0]):
7175
journal = glob(journal)
7276

73-
for journal in journal:
74-
reports.append(generate_report(journal, export_format, var, var_file))
77+
for journal_path in journal:
78+
with io.open(journal_path) as fp:
79+
j = json.load(fp)
80+
81+
experiment = j["experiment"]
82+
config_vars, secret_vars = merge_vars(var, var_file) or (None, None)
83+
config = load_configuration(
84+
experiment.get("configuration", {}), config_vars
85+
)
86+
secrets = load_secrets(experiment.get("secrets", {}), secret_vars)
87+
88+
reports.append(generate_report(j, export_format, config, secrets))
89+
7590
save_report(header, reports, report_path, export_format)
7691
click.echo("Report generated as '{f}'".format(f=report))

chaosreport/template/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ The steady state was {%if steady_states.before.steady_state_met %} verified {% e
6868

6969
| Probe | Tolerance | Verified |
7070
| -------------------------------------------------------------- | --------------------------- | ------ | {% for probe in steady_states.before.probes %}
71-
| {{probe.activity.name}} | {{probe.activity.tolerance}} | {{probe.tolerance_met}} | {% endfor %}
71+
| {{probe.activity.name}} | {{substitute(probe.activity.tolerance, True)}} | {{probe.tolerance_met}} | {% endfor %}
7272

7373
##### After Run
7474

7575
The steady state was {%if steady_states.after.steady_state_met %} verified {% else %} not verified. {% endif %}
7676

7777
| Probe | Tolerance | Verified |
7878
| -------------------------------------------------------------- | --------------------------- | ------ | {% for probe in steady_states.after.probes %}
79-
| {{probe.activity.name}} | {{probe.activity.tolerance}} | {{probe.tolerance_met}} | {% endfor %}
79+
| {{probe.activity.name}} | {{substitute(probe.activity.tolerance, True)}} | {{probe.tolerance_met}} | {% endfor %}
8080

8181
{% endif %}
8282

@@ -106,8 +106,8 @@ The experiment was conducted on {{start|pretty_date}} and lasted roughly
106106
| **Started** | {{item.start | pretty_date}} |
107107
| **Ended** | {{item.end | pretty_date}} |
108108
| **Duration** | {{pretty_duration(item.start, item.end)}} | {% if item.activity.get("pauses", {}).get("before") %}
109-
| **Paused Before** | {{item.activity.pauses.before}}s | {% endif %} {% if item.activity.get("pauses", {}).get("after") %}
110-
| **Paused After** | {{item.activity.pauses.after}}s | {% endif %}
109+
| **Paused Before** | {{substitute(item.activity.pauses.before)}}s | {% endif %} {% if item.activity.get("pauses", {}).get("after") %}
110+
| **Paused After** | {{substitute(item.activity.pauses.after)}}s | {% endif %}
111111

112112
The {{item.activity.type}} provider that was executed:
113113

0 commit comments

Comments
 (0)