Skip to content

Commit

Permalink
Fix sub reports overview table
Browse files Browse the repository at this point in the history
  • Loading branch information
Rieven committed May 30, 2024
1 parent 2dfae1e commit e99693d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 62 deletions.
87 changes: 42 additions & 45 deletions rocky/reports/templates/report_overview/report_history_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<caption class="visually-hidden">{% translate "Reports:" %}</caption>
<thead>
<tr>
<th scope="col">{% translate "Name" %}</th>
<th scope="col">{% translate "Report ID" %}</th>
<th scope="col">{% translate "Report type" %}</th>
<th scope="col">{% translate "Objects" %}</th>
<th scope="col">{% translate "Reference date" %}</th>
Expand All @@ -28,7 +28,7 @@
{% if report.parent_report.input_ooi %}
1
{% else %}
{{ report.children_reports|length }}
{{ report.total_children_reports }}
{% endif %}
</td>
<td>{{ report.parent_report.observed_at|date }}</td>
Expand All @@ -48,63 +48,60 @@
<tr class="expando-row">
<td colspan="7">
<table>
<h5>{% translate "Selected Input Objects" %}</h5>
<h5>{% translate "Report types" %}</h5>
<p>
{% blocktranslate count counter=report.total_children_reports %}
This report consist of {{counter}} sub report with the following report type and object.
{% plural %}
This report consist of {{counter}} sub reports with the following report types and objects.
{% endblocktranslate %}
</p>
<thead>
<tr>
<th scope="col">{% translate "Object name" %}</th>
<th scope="col">{% translate "Object type" %}</th>
<th scope="col">{% translate "Clearance level" %}</th>
<th scope="col"></th>
<th scope="col">{% translate "Report type" %}</th>
<th scope="col">{% translate "Objects" %}</th>
</tr>
</thead>
<tbody>
{% for ooi, child_reports in report.children_reports.items %}
{% for report_type, total_objects in report.report_type_summary.items %}
<tr>
<td>
<a href="{% ooi_url "ooi_detail" ooi organization.code query=ooi.mandatory_fields %}">{{ ooi.human_readable }}</a>
</td>
<td>{{ ooi.ooi_type }}</td>
<td>
<ul class="level-indicator l{{ ooi.scan_profile.level }}">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</td>
<td>
<button type="button"
class="expando-button icon ti-chevron-down"
data-icon-open-class="icon ti-chevron-down"
data-icon-close-class="icon ti-chevron-up"
data-close-label="{% translate "Close children report details" %}"
aria-expanded="false"></button>
<span class="label tags-color-{{ report_type|get_report_type_label_style }}">{{ report_type|get_report_type_name }}</span>
</td>
<td>{{ total_objects }}</td>
</tr>
<tr class="expando-row">
<td colspan="4">
<table>
<thead>
<tr>
<th scope="col">{% translate "Report type" %}</th>
</tr>
</thead>
<tbody>
{% for child_report in child_reports %}
<tr>
<td>
<a href="{% url "view_report" organization.code %}?report_id={{ child_report.reference }}&observed_at={{ child_report.observed_at|date:"Y-m-d H:i:s:u" }}"
title="{% translate "Shows report details" %}"><span class="label tags-color-{{ child_report.report_type|get_report_type_label_style }}">{{ child_report.name }}</span></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
</tbody>
</table>
<table>
<h5>
{% translate "Sub reports" %}
({{ report.children_reports|length }}/{{ report.total_children_reports }})
</h5>
<thead>
<tr>
<th scope="col">{% translate "Report type" %}</th>
<th scope="col">{% translate "Object" %}</th>
<th scope="col">{% translate "Report ID" %}</th>
</tr>
</thead>
<tbody>
{% for child_report in report.children_reports %}
<tr>
<td>
<span class="label tags-color-{{ child_report.report_type|get_report_type_label_style }}">{{ child_report.report_type|get_report_type_name }}</span>
</td>
<td>{{ child_report.input_ooi }}</td>
<td>{{ child_report }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="button-container">
{% if report.total_children_reports > 5 %}
<a href="" class="button">{% translate "View all sub reports" %}</a>
{% endif %}
</div>
</td>
</tr>
{% endif %}
Expand Down
52 changes: 35 additions & 17 deletions rocky/rocky/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ def __getitem__(self, key: int | slice) -> list[HydratedFinding]:

class HydratedReport:
parent_report: Report
children_reports: dict[OOI, list[Report]] | None
children_reports: list[Report] | None
total_children_reports: int
report_type_summary: dict[str, int]


class ReportList:
Expand Down Expand Up @@ -307,33 +309,49 @@ def hydrate_report_list(self, reports: list[Report]) -> list[HydratedReport]:
hydrated_reports: list[HydratedReport] = []

for report in reports:
child_report_oois: set = set()
per_ooi_child_reports: dict[OOI, list[Report]] = {}
hydrated_report: HydratedReport = HydratedReport()

parent_report, children_reports = report

for child_report in children_reports:
child_report_oois.add(self.octopoes_connector.get(child_report.input_ooi, self.valid_time))
hydrated_report.total_children_reports = len(children_reports)
hydrated_report.report_type_summary = self.report_type_summary(children_reports)

if not parent_report.has_parent:
for ooi in child_report_oois:
hydrated_children_reports: list[Report] = []
for child_report in children_reports:
if (
str(child_report.parent_report) == str(parent_report)
and ooi.primary_key == child_report.input_ooi
):
hydrated_children_reports.append(child_report)

per_ooi_child_reports[ooi] = sorted(hydrated_children_reports, key=attrgetter("name"))

hydrated_report.children_reports = per_ooi_child_reports
hydrated_children_reports: list[Report] = []
for child_report in children_reports:
if str(child_report.parent_report) == str(parent_report):
hydrated_children_reports.append(child_report)
if len(hydrated_children_reports) >= 5: # We want to show only 5 children reports
break

hydrated_report.children_reports = sorted(hydrated_children_reports, key=attrgetter("name"))

hydrated_report.parent_report = parent_report
hydrated_reports.append(hydrated_report)

return hydrated_reports

@staticmethod
def report_type_summary(reports: list[Report]) -> dict[str, int]:
"""
Calculates per report type how many objects it consumed.
"""
report_types = set()
summary = {}

for report in reports:
report_types.add(report.report_type)

for report_type in report_types:
objects = []
for report in reports:
if report_type == report.report_type:
objects.append(report.input_ooi)

summary[report_type] = len(objects)

return summary


class ConnectorFormMixin:
connector_form_class: type[ObservedAtForm]
Expand Down

0 comments on commit e99693d

Please sign in to comment.