Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/remove unneeded tree lookups on ooi views #4032

Merged
merged 9 commits into from
Jan 28, 2025
27 changes: 14 additions & 13 deletions rocky/rocky/locale/django.pot
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-12-11 11:14+0000\n"
"POT-Creation-Date: 2025-01-23 20:12+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -1093,7 +1093,6 @@ msgstr ""

#: katalogus/templates/partials/plugin_settings_required.html
#: rocky/templates/findings/finding_add.html
#: rocky/templates/partials/ooi_detail_related_object.html
#: rocky/templates/partials/ooi_list_toolbar.html
msgid "Add"
msgstr ""
Expand Down Expand Up @@ -3055,7 +3054,6 @@ msgstr ""
#: reports/report_types/dns_report/report.html
#: reports/report_types/findings_report/report.html
#: reports/report_types/vulnerability_report/report.html
#: reports/templates/report_overview/scheduled_reports_table.html
#: reports/templates/summary/selected_plugins.html
#: rocky/templates/crisis_room/crisis_room_findings_block.html
#: rocky/templates/findings/finding_list.html
Expand Down Expand Up @@ -6437,11 +6435,11 @@ msgid "Related objects"
msgstr ""

#: rocky/templates/partials/ooi_detail_related_object.html
msgid "No related objects added to"
msgid "No related object known for"
msgstr ""

#: rocky/templates/partials/ooi_detail_related_object.html
msgid "Use the button below to add a related object."
msgid "Add related"
msgstr ""

#: rocky/templates/partials/ooi_detail_toolbar.html
Expand Down Expand Up @@ -6737,13 +6735,12 @@ msgid "Manually added"
msgstr ""

#: rocky/templates/tasks/ooi_detail_task_list.html
#: rocky/templates/tasks/plugin_detail_task_list.html
msgid "There are no tasks for"
msgid "There have been no tasks"
msgstr ""

#: rocky/templates/tasks/ooi_detail_task_list.html
#: rocky/templates/tasks/plugin_detail_task_list.html
msgid "List of tasks for"
#: rocky/templates/tasks/partials/tab_navigation.html
msgid "List of tasks"
msgstr ""

#: rocky/templates/tasks/partials/stats.html
Expand All @@ -6762,10 +6759,6 @@ msgstr ""
msgid "Could not load stats, Scheduler error."
msgstr ""

#: rocky/templates/tasks/partials/tab_navigation.html
msgid "List of tasks"
msgstr ""

#: rocky/templates/tasks/partials/task_actions.html
msgid "Yielded objects"
msgstr ""
Expand All @@ -6778,6 +6771,14 @@ msgstr ""
msgid "Download task data"
msgstr ""

#: rocky/templates/tasks/plugin_detail_task_list.html
msgid "There are no tasks for"
msgstr ""

#: rocky/templates/tasks/plugin_detail_task_list.html
msgid "List of tasks for"
msgstr ""

#: rocky/templates/two_factor/_wizard_actions.html
msgid "Log in"
msgstr ""
Expand Down
10 changes: 5 additions & 5 deletions rocky/rocky/templates/oois/ooi_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
{% include "tasks/ooi_detail_task_list.html" %}

</div>
<div>
{% if not ooi|is_finding and not ooi|is_finding_type %}
{% if not ooi|is_finding and not ooi|is_finding_type %}
<div>
{% include "oois/ooi_detail_findings_overview.html" with severity_summary=findings_severity_summary severity_summary_totals=severity_summary_totals %}

{% endif %}
</div>
</div>
{% endif %}
<div>
<h2>{% translate "Scan" %} {{ ooi.human_readable }} {% translate "using boefjes" %}</h2>
<h2>{% translate "Scan" %} {% translate "using boefjes" %}</h2>
{% if enabled_boefjes_available %}
<div class="horizontal-view">
<div>
Expand Down
12 changes: 5 additions & 7 deletions rocky/rocky/templates/partials/ooi_detail_related_object.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
<div class="horizontal-scroll">
<h2>{% translate "Related objects" %}</h2>
{% if not related %}
<p>
{% translate "No related objects added to" %} {{ ooi.get_ooi_type }}.
<br>
{% translate "Use the button below to add a related object." %}
</p>
<p>{% translate "No related object known for" %} {{ ooi.get_ooi_type }}.</p>
{% endif %}
{% if not ooi_past_due %}
{% if not ooi|is_finding and not ooi|is_finding_type %}
<div class="horizontal-view {% if related %}toolbar{% endif %}">
<a href="{% ooi_url "ooi_add_related" ooi.primary_key organization.code %}"
class="button ghost">{% translate "Add" %}</a>
class="button ghost">{% translate "Add related" %}</a>
</div>
{% endif %}
{% endif %}
Expand All @@ -32,7 +28,9 @@ <h2>{% translate "Related objects" %}</h2>
<td>
<a href="{% ooi_url "ooi_detail" related_ooi.reference organization.code query=related_ooi.mandatory_fields %}">{{ related_ooi.reference.human_readable }}</a>
</td>
<td>{{ related_ooi.name }}</td>
<td>
<a href="{% url "ooi_list" organization.code %}?ooi_type={{ related_ooi.reference.class_ }}">{{ related_ooi.reference.class_ }}</a>
</td>
</tr>
{% endfor %}
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions rocky/rocky/templates/tasks/ooi_detail_task_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<h2>{% translate "Tasks" %}</h2>
{% ooi_url 'ooi_detail' ooi.primary_key organization.code query=mandatory_fields as this_url %}
{% if not task_list %}
<p class="emphasized">{% translate "There are no tasks for" %} {{ ooi }}</p>
<p class="emphasized">{% translate "There have been no tasks" %}</p>
{% include "tasks/partials/task_filter.html" with clear_filter_url=this_url %}

{% else %}
<p class="emphasized">{% translate "List of tasks for" %} {{ ooi }}</p>
<p class="emphasized">{% translate "List of tasks" %}</p>
{% include "tasks/partials/task_filter.html" with clear_filter_url=this_url %}

<table rf-selector="table-scan-history">
Expand Down
12 changes: 7 additions & 5 deletions rocky/rocky/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,30 +518,32 @@ def get_ooi_properties(self, ooi: OOI) -> dict:


class SingleOOITreeMixin(SingleOOIMixin):
tree: ReferenceTree
@cached_property
def tree(self) -> ReferenceTree:
return self.get_ooi_tree(depth=1)

def get_depth(self):
try:
return min(int(self.request.GET.get("depth", DEPTH_DEFAULT)), DEPTH_MAX)
except ValueError:
return DEPTH_DEFAULT

def get_ooi(self, pk: str | None = None, observed_at: datetime | None = None) -> OOI:
def get_ooi_tree(self, pk: str | None = None, observed_at: datetime | None = None, depth: int | None = None) -> OOI:
if pk is None:
pk = self.get_ooi_id()

if observed_at is None:
observed_at = self.observed_at

ref = Reference.from_str(pk)
depth = self.get_depth()
depth = depth or self.get_depth()

try:
self.tree = self.octopoes_api_connector.get_tree(ref, valid_time=observed_at, depth=depth)
tree = self.octopoes_api_connector.get_tree(ref, valid_time=observed_at, depth=depth)
except Exception as e:
self.handle_connector_exception(e)

return self.tree.store[str(self.tree.root.reference)]
return tree


class SeveritiesMixin:
Expand Down
10 changes: 9 additions & 1 deletion rocky/rocky/views/ooi_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ class OOITreeView(BaseOOIDetailView, TemplateView):
template_name = "oois/ooi_tree.html"
connector_form_class = OoiTreeSettingsForm

def __init__(self):
super().__init__()
self._tree_dict = None

def get_tree_dict(self):
return create_object_tree_item_from_ref(self.tree.root, self.tree.store)
if self._tree_dict is None:
tree = self.get_ooi_tree()
self._tree_dict = create_object_tree_item_from_ref(tree.root, tree.store)

return self._tree_dict

def get_filtered_tree(self, tree_dict: dict) -> dict:
filtered_types = self.request.GET.getlist("ooi_type", [])
Expand Down
5 changes: 3 additions & 2 deletions rocky/rocky/views/ooi_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ class BaseOOIDetailView(BreadcrumbsMixin, SingleOOITreeMixin, ConnectorFormMixin

def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)
self.ooi = self.get_ooi()
tree = self.tree
self.ooi = tree.store[tree.root.reference]

def get_current_ooi(self) -> OOI | None:
"""
Expand All @@ -150,7 +151,7 @@ def get_current_ooi(self) -> OOI | None:
if self.observed_at.date() == now.date():
return self.ooi
Comment on lines 151 to 152
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check still needed? the tree uses the self.observed_at property of the view anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. That might apply to the tree-based views, but I think this check is also used within the detail view

try:
return self.get_ooi(pk=self.get_ooi_id(), observed_at=now)
return self.get_ooi_tree(self.get_ooi_id(), observed_at=now).store[self.get_ooi_id()]
except Http404:
return None

Expand Down
11 changes: 10 additions & 1 deletion rocky/tests/objects/test_objects_graph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import ANY, call

from django.urls import resolve, reverse
from pytest_django.asserts import assertContains

from octopoes.models import Reference
from octopoes.models.tree import ReferenceTree
from rocky.views.ooi_tree import OOIGraphView
from tests.conftest import setup_request
Expand Down Expand Up @@ -32,6 +35,12 @@ def test_ooi_graph(rf, client_member, mock_organization_view_octopoes):
response = OOIGraphView.as_view()(request, organization_code=client_member.organization.code)

assert response.status_code == 200
assert mock_organization_view_octopoes().get_tree.call_count == 1
mock_organization_view_octopoes().get_tree.assert_has_calls(
[
call(Reference("Network|testnetwork"), valid_time=ANY, depth=1),
call(Reference("Network|testnetwork"), valid_time=ANY, depth=9),
]
)

assertContains(response, "testnetwork")
assertContains(response, "KAT-000")
11 changes: 10 additions & 1 deletion rocky/tests/objects/test_objects_tree.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import ANY, call

from django.urls import resolve, reverse
from pytest_django.asserts import assertContains

from octopoes.models import Reference
from octopoes.models.tree import ReferenceTree
from rocky.views.ooi_tree import OOITreeView
from tests.conftest import setup_request
Expand Down Expand Up @@ -30,7 +33,13 @@ def test_ooi_tree(rf, client_member, mock_organization_view_octopoes):
response = OOITreeView.as_view()(request, organization_code=client_member.organization.code)

assert response.status_code == 200
assert mock_organization_view_octopoes().get_tree.call_count == 1
mock_organization_view_octopoes().get_tree.assert_has_calls(
[
call(Reference("Network|testnetwork"), valid_time=ANY, depth=1),
call(Reference("Network|testnetwork"), valid_time=ANY, depth=9),
]
)

assertContains(response, "testnetwork")
assertContains(response, "KAT-000")

Expand Down
Loading