Skip to content

Commit

Permalink
Fix report names for scheduled reports (#3726)
Browse files Browse the repository at this point in the history
Co-authored-by: stephanie0x00 <[email protected]>
Co-authored-by: Jan Klopper <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2024
1 parent 12c5788 commit 1c08cde
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions rocky/reports/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ class CustomReportScheduleForm(BaseRockyForm):

class ParentReportNameForm(BaseRockyForm):
parent_report_name = forms.CharField(
label=_("Report name format"), required=False, initial="{report type} for {oois_count} objects"
label=_("Report name format"), required=False, initial="${report_type} for ${oois_count} objects"
)


class ChildReportNameForm(BaseRockyForm):
child_report_name = forms.CharField(
label=_("Subreports name format"), required=True, initial="{report type} for {ooi}"
label=_("Subreports name format"), required=True, initial="${report_type} for ${ooi}"
)


Expand Down
27 changes: 19 additions & 8 deletions rocky/reports/runner/report_runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timezone
from string import Template

from django.conf import settings
from tools.models import Organization
Expand Down Expand Up @@ -30,18 +31,28 @@ def run(self, report_task: ReportTask) -> None:
)

self.bytes_client.organization = report_task.organisation_id
report_names = []
oois_count = 0
subreport_names = []
oois_count = len(recipe.input_recipe["input_oois"])

for report_type_id, data in report_data.items():
oois_count += len(data)
report_type = get_report_by_id(report_type_id)

for ooi in data:
report_name = recipe.subreport_name_format.replace("{ooi}", ooi).replace(
"{report type}", str(report_type.name)
ooi_human_readable = Reference.from_str(ooi).human_readable
subreport_name = Template(recipe.subreport_name_format).safe_substitute(
ooi=ooi_human_readable, report_type=str(report_type.name)
)
report_names.append((report_name, report_name))
subreport_names.append((subreport_name, subreport_name))

parent_report_name = Template(recipe.report_name_format).safe_substitute(oois_count=str(oois_count))

if "${ooi}" in parent_report_name and oois_count == 1:
ooi = recipe.input_recipe["input_oois"][0]
ooi_human_readable = Reference.from_str(ooi).human_readable
parent_report_name = Template(parent_report_name).safe_substitute(ooi=ooi_human_readable)
if "${report_type}" in parent_report_name and len(report_types) == 1:
report_type = get_report_by_id(recipe.report_types[0])
parent_report_name = Template(parent_report_name).safe_substitute(report_type=str(report_type.name))

save_report_data(
self.bytes_client,
Expand All @@ -56,8 +67,8 @@ def run(self, report_task: ReportTask) -> None:
}
},
report_data,
report_names,
recipe.report_name_format.replace("{oois_count}", str(oois_count)),
subreport_names,
parent_report_name,
)

self.bytes_client.organization = None
6 changes: 3 additions & 3 deletions rocky/reports/templates/partials/report_names_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ <h2>{% translate "Report name" %}</h2>
{% blocktranslate trimmed %}
To make the report names more descriptive, you can include placeholders for the
object name, the report type and/or the reference date. For subreports and reports over a single object,
use the placeholder "{ooi}" for the object name, "{report type}" for the report type and use a
use the placeholder "${ooi}" for the object name, "${report_type}" for the report type and use a
<a href="https://strftime.org/" target="_blank" rel="noopener">Python strftime code</a> for the reference
date. For reports over multiple objects, use "{oois_count}" for the number of objects in the report.
date. For reports over multiple objects, use "${oois_count}" for the number of objects in the report.
{% endblocktranslate %}
</p>
<p>
{% blocktranslate trimmed %}
For example, the format "{report type} for {ooi} at %x" could generate:
For example, the format "${report_type} for ${ooi} at %x" could generate:
"DNS Report for example.com at 01/01/25".
{% endblocktranslate %}
</p>
Expand Down
6 changes: 3 additions & 3 deletions rocky/reports/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def save_report_data(
raw_id = bytes_client.upload_raw(
raw=ReportDataDict(input_data).model_dump_json().encode(), manual_mime_types={"openkat/report"}
)
name = now.strftime(parent_report_name.replace("{report type}", str(ConcatenatedReport.name)))
name = now.strftime(parent_report_name.replace("${report_type}", str(ConcatenatedReport.name)))

if not name or name.isspace():
name = ConcatenatedReport.name
Expand Down Expand Up @@ -163,7 +163,7 @@ def save_report_data(
manual_mime_types={"openkat/report"},
)
report_type = get_report_by_id(report_type_id)
name = now.strftime(parent_report_name.replace("{report type}", str(report_type.name)))
name = now.strftime(parent_report_name.replace("${report_type}", str(report_type.name)))

if not name or name.isspace():
name = ConcatenatedReport.name
Expand Down Expand Up @@ -205,7 +205,7 @@ def save_report(self, report_names: list) -> Report | None:
self.get_input_data(),
report_data,
report_names,
report_names[0][0],
report_names[0][1],
)

# If OOI could not be found or the date is incorrect, it will be shown to the user as a message error
Expand Down
6 changes: 3 additions & 3 deletions rocky/tests/integration/test_report_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def test_run_report_task(octopoes_api_connector: OctopoesAPIConnector, report_ru

recipe = ReportRecipe(
recipe_id="abc4e52b-812c-4cc2-8196-35fb8efc63ca",
report_name_format="Concatenated report for {oois_count} objects",
subreport_name_format="{report type} for {ooi} in %Y",
report_name_format="Concatenated report for ${oois_count} objects",
subreport_name_format="${report_type} for ${ooi} in %Y",
input_recipe={"input_oois": [oois["hostnames"][0].reference, oois["hostnames"][1].reference]},
report_types=["dns-report"],
cron_expression="* * * * *",
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_run_report_task(octopoes_api_connector: OctopoesAPIConnector, report_ru
assert len(subreports) == 2

assert report.name == "Concatenated report for 2 objects"
assert "DNS Report for Hostname|test|a.example.com in 2024" in {x.name for x in subreports}
assert "DNS Report for a.example.com in 2024" in {x.name for x in subreports}

# FIXME: the naming logic in reports/views/mixins.py 107-112 is not right. We expect to find example.com in this
# set, but instead only find a.example.com because when ooi_name is 'example.com', the check:
Expand Down

0 comments on commit 1c08cde

Please sign in to comment.