Skip to content

Commit

Permalink
file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rieven committed Jan 7, 2025
1 parent 5529aed commit 818a8eb
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 232 deletions.
19 changes: 19 additions & 0 deletions rocky/crisis_room/templates/crisis_room.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "layouts/base.html" %}

{% load i18n %}
{% load static %}

{% block content %}
{% include "header.html" %}

<main id="main-content" tabindex="-1" class="crisisroom">
<section>
<div>
<h1>{% translate "Crisis Room" %}</h1>
<p class="emphasized">{% translate "Crisis Room overview for all organizations" %}</p>
</div>
{% include "crisis_room_findings.html" %}

</section>
</main>
{% endblock content %}
97 changes: 41 additions & 56 deletions rocky/crisis_room/templates/crisis_room_findings.html
Original file line number Diff line number Diff line change
@@ -1,66 +1,51 @@
{% extends "layouts/base.html" %}

{% load i18n %}
{% load static %}

{% block content %}
{% include "header.html" %}

<main id="main-content" tabindex="-1" class="crisisroom">
{% if organizations_dashboards %}
{% if organizations_findings_summary.total_finding_types != 0 %}
<section>
<div>
<h1>{% translate "Crisis Room" %}</h1>
<p class="emphasized">{% translate "Crisis Room overview for all organizations" %}</p>
</div>
{% if organizations_dashboards %}
{% if organizations_findings_summary.total_finding_types != 0 %}
<section>
<div>
<div>
<h2>{% translate "Findings overview" %}</h2>
<p>
{% blocktranslate %}
This overview shows the total number of findings per
severity that have been identified for all organizations.
{% endblocktranslate %}
</p>
</div>
{% include "partials/report_severity_totals_table.html" with data=organizations_findings_summary %}

</div>
</section>
<section>
<div>
<div>
<h2>{% translate "Findings per organization" %}</h2>
<p>
{% blocktranslate %}
This table shows the findings that have been identiefied for each organization,
sorted by the finding types and grouped by organizations.
{% endblocktranslate %}
</p>
</div>
{% include "findings_report/report.html" with is_dashboard_findings="yes" %}
<div>
<h2>{% translate "Findings overview" %}</h2>
<p>
{% blocktranslate %}
This overview shows the total number of findings per
severity that have been identified for all organizations.
{% endblocktranslate %}
</p>
</div>
{% include "partials/report_severity_totals_table.html" with data=organizations_findings_summary %}

</div>
</section>
{% else %}
<h3>{% translate "Findings overview" %}</h3>
</div>
</section>
<section>
<div>
<div>
<h2>{% translate "Findings per organization" %}</h2>
<p>
{% blocktranslate %}
No findings have been identified yet. As soon as they have been
identified, they will be shown on this page.
This table shows the findings that have been identiefied for each organization,
sorted by the finding types and grouped by organizations.
{% endblocktranslate %}
</p>
{% endif %}
{% else %}
<p>
{% blocktranslate %}
There are no organizations yet. After creating an organization,
the identified findings with severity 'critical' and 'high' will be shown here.
{% endblocktranslate %}
</p>
{% endif %}
</div>
{% include "findings_report/report.html" with is_dashboard_findings="yes" %}

</div>
</section>
</main>
{% endblock content %}
{% else %}
<h3>{% translate "Findings overview" %}</h3>
<p>
{% blocktranslate %}
No findings have been identified yet. As soon as they have been
identified, they will be shown on this page.
{% endblocktranslate %}
</p>
{% endif %}
{% else %}
<p>
{% blocktranslate %}
There are no organizations yet. After creating an organization,
the identified findings with severity 'critical' and 'high' will be shown here.
{% endblocktranslate %}
</p>
{% endif %}
2 changes: 1 addition & 1 deletion rocky/crisis_room/templates/crisis_room_header.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<nav class="tabs" aria-label="{% translate "Crisis Room Navigation" %}">
<ul>
<li {% if active == "findings" %}aria-current="page"{% endif %}>
<a href="{% url 'crisis_room_findings' %}">{% translate "Findings" %}</a>
<a href="{% url 'crisis_room' %}">{% translate "Findings" %}</a>
</li>
</ul>
</nav>
Expand Down
5 changes: 1 addition & 4 deletions rocky/crisis_room/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@
from . import views

# Crisis room overview urls
urlpatterns = [
path("", views.CrisisRoomAllOrganizations.as_view(), name="crisis_room"),
path("findings", views.CrisisRoomFindings.as_view(), name="crisis_room_findings"),
]
urlpatterns = [path("", views.CrisisRoom.as_view(), name="crisis_room")]
103 changes: 2 additions & 101 deletions rocky/crisis_room/views.py
Original file line number Diff line number Diff line change
@@ -1,122 +1,23 @@
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Any

import structlog
from account.models import KATUser
from django.conf import settings
from django.contrib import messages
from django.http import HttpResponse
from django.http.request import HttpRequest
from django.shortcuts import redirect
from django.urls.base import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import TemplateView
from pydantic import TypeAdapter
from reports.report_types.findings_report.report import SEVERITY_OPTIONS
from tools.forms.base import ObservedAtForm
from tools.models import Organization, OrganizationMember
from tools.view_helpers import BreadcrumbsMixin

from crisis_room.management.commands.dashboard import FINDINGS_DASHBOARD_NAME
from crisis_room.models import DashboardData
from octopoes.connector import ConnectorException
from octopoes.connector.octopoes import OctopoesAPIConnector
from octopoes.models import Reference
from octopoes.models.ooi.findings import RiskLevelSeverity
from rocky.bytes_client import BytesClient, get_bytes_client
from rocky.views.mixins import ConnectorFormMixin, ObservedAtMixin

logger = structlog.get_logger(__name__)


# dataclass to store finding type counts
@dataclass
class OrganizationFindingCountPerSeverity:
name: str
code: str
finding_count_per_severity: dict[str, int]

@property
def total(self) -> int:
return sum(self.finding_count_per_severity.values())

@property
def total_critical(self) -> int:
try:
return self.finding_count_per_severity[RiskLevelSeverity.CRITICAL.value]
except KeyError:
return 0


class CrisisRoomView(BreadcrumbsMixin, ConnectorFormMixin, ObservedAtMixin, TemplateView):
template_name = "crisis_room/crisis_room.html"
connector_form_class = ObservedAtForm
breadcrumbs = [{"url": "", "text": "Crisis Room"}]

def sort_by_total(
self, finding_counts: list[OrganizationFindingCountPerSeverity]
) -> list[OrganizationFindingCountPerSeverity]:
is_desc = self.request.GET.get("sort_total_by", "desc") != "asc"
return sorted(finding_counts, key=lambda x: x.total, reverse=is_desc)

def sort_by_severity(
self, finding_counts: list[OrganizationFindingCountPerSeverity]
) -> list[OrganizationFindingCountPerSeverity]:
is_desc = self.request.GET.get("sort_critical_by", "desc") != "asc"
return sorted(finding_counts, key=lambda x: x.total_critical, reverse=is_desc)

def get_finding_type_severity_count(self, organization: Organization) -> dict[str, int]:
try:
api_connector = OctopoesAPIConnector(
settings.OCTOPOES_API, organization.code, timeout=settings.ROCKY_OUTGOING_REQUEST_TIMEOUT
)
return api_connector.count_findings_by_severity(valid_time=self.observed_at)
except ConnectorException:
messages.add_message(
self.request,
messages.ERROR,
_("Failed to get list of findings for organization {}, check server logs for more details.").format(
organization.code
),
)
logger.exception("Failed to get list of findings for organization %s", organization.code)
return {}

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

user: KATUser = self.request.user

# query each organization's finding type count
org_finding_counts_per_severity = [
OrganizationFindingCountPerSeverity(
name=org.name, code=org.code, finding_count_per_severity=self.get_finding_type_severity_count(org)
)
for org in user.organizations
]

context["breadcrumb_list"] = [{"url": reverse("crisis_room"), "text": "CRISIS ROOM"}]
context["organizations"] = user.organizations

context["org_finding_counts_per_severity"] = self.sort_by_total(org_finding_counts_per_severity)
context["org_finding_counts_per_severity_critical"] = self.sort_by_severity(org_finding_counts_per_severity)

context["observed_at_form"] = self.get_connector_form()
context["observed_at"] = self.observed_at.date()

return context


class CrisisRoomAllOrganizations(TemplateView):
"""
Crisis Room langding page.
"""

def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
return redirect(reverse("crisis_room_findings"))


class DashboardService:
observed_at = datetime.now(timezone.utc) # we can later set any observed_at

Expand Down Expand Up @@ -218,8 +119,8 @@ def get_organizations_findings_summary(
return summary


class CrisisRoomFindings(TemplateView):
template_name = "crisis_room_findings.html"
class CrisisRoom(TemplateView):
template_name = "crisis_room.html"

def setup(self, request: HttpRequest, *args: Any, **kwargs: Any) -> None:
super().setup(request, *args, **kwargs)
Expand Down
69 changes: 0 additions & 69 deletions rocky/tests/test_crisis_room.py

This file was deleted.

2 changes: 1 addition & 1 deletion rocky/tests/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_crisis_room_findings_dashboard(rf, mocker, client_member, findings_dash
)
summary(findings_dashboard_mock_data)

request = setup_request(rf.get("crisis_room_findings"), client_member.user)
request = setup_request(rf.get("crisis_room"), client_member.user)
response = CrisisRoomFindings.as_view()(request)

assert response.status_code == 200
Expand Down

0 comments on commit 818a8eb

Please sign in to comment.