Skip to content

Commit

Permalink
Report Schedules List (#3608)
Browse files Browse the repository at this point in the history
Co-authored-by: JP Bruins Slot <[email protected]>
Co-authored-by: Jan Klopper <[email protected]>
Co-authored-by: ammar92 <[email protected]>
Co-authored-by: Madelon D <[email protected]>
Co-authored-by: noamblitz <[email protected]>
Co-authored-by: Donny Peeters <[email protected]>
Co-authored-by: noamblitz <[email protected]>
Co-authored-by: stephanie0x00 <[email protected]>
  • Loading branch information
9 people authored Oct 3, 2024
1 parent c6c3638 commit a70ff24
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 56 deletions.
36 changes: 24 additions & 12 deletions rocky/reports/templates/report_overview/report_history.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
{% extends "layouts/base.html" %}

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

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

<main id="main-content" tabindex="-1" class="katalogus">
<section>
{% include "report_overview/report_overview_header.html" %}
{% include "report_overview/report_overview_navigation.html" with active="history" %}

<div class="horizontal-scroll">
<h2>{% translate "Reports history" %}</h2>
<p>{% translate "An overview of all your generated reports." %}</p>
<p class="de-emphasized">
{% blocktranslate with length=reports|length total=total_oois %}Showing {{ length }} of {{ total }} reports{% endblocktranslate %}
</p>
{% include "report_overview/report_history_table.html" %}
{% include "partials/list_paginator.html" %}

<section>
<div class="horizontal-scroll">
<h2>{% translate "Reports history" %}</h2>
<p>{% translate "An overview of all your generated reports." %}</p>
<p class="de-emphasized">
{% blocktranslate with length=reports|length total=total_oois %}Showing {{ length }} of {{ total }} reports{% endblocktranslate %}
</p>
{% include "report_overview/report_history_table.html" %}
{% include "partials/list_paginator.html" %}

</div>
</section>
</div>
</section>
</main>
{% endblock content %}
16 changes: 0 additions & 16 deletions rocky/reports/templates/report_overview/report_overview.html

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,8 @@
<div>
<nav class="tabs" aria-label="{% translate "Plugins Navigation" %}">
<ul>
<li {% if active == "recent" %}aria-current="page"{% endif %}>
<a href="">{% translate "Recent" %}</a>
</li>
<li {% if active == "presets" %}aria-current="page"{% endif %}>
<a href="">{% translate "Presets" %}</a>
</li>
<li {% if active == "scheduled" %}aria-current="page"{% endif %}>
<a href="">{% translate "Scheduled" %}</a>
</li>
<li {% if active == "folders" %}aria-current="page"{% endif %}>
<a href="">{% translate "Folders" %}</a>
<a href="{% url 'scheduled_reports' organization.code %}">{% translate "Scheduled" %}</a>
</li>
<li {% if active == "history" %}aria-current="page"{% endif %}>
<a href="{% url 'report_history' organization.code %}">{% translate "History" %}</a>
Expand Down
26 changes: 26 additions & 0 deletions rocky/reports/templates/report_overview/scheduled_reports.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "layouts/base.html" %}

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

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

<main id="main-content" tabindex="-1" class="katalogus">
<section>
{% include "report_overview/report_overview_header.html" %}
{% include "report_overview/report_overview_navigation.html" with active="scheduled" %}

<div class="horizontal-scroll">
<h2>{% translate "Scheduled reports" %}</h2>
<p>{% translate "View your scheduled reports." %}</p>
<p class="de-emphasized">
{% blocktranslate with length=scheduled_reports|length total=total_oois %}Showing {{ length }} scheduled reports{% endblocktranslate %}
</p>
{% include "report_overview/scheduled_reports_table.html" %}
{% include "partials/list_paginator.html" %}

</div>
</section>
</main>
{% endblock content %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{% load i18n %}
{% load ooi_extra %}
{% load report_extra %}

<div class="horizontal-scroll">
<table>
<caption class="visually-hidden">{% translate "Scheduled reports:" %}</caption>
<thead>
<tr>
<th scope="col">{% translate "Report Name" %}</th>
<th scope="col">{% translate "Subreport Name" %}</th>
<th scope="col">{% translate "Report types" %}</th>
<th scope="col">{% translate "Scheduled for" %}</th>
<th scope="col">{% translate "Recurrence" %}</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% for schedule in scheduled_reports %}
{% if schedule.recipe %}
<tr>
<td class="nowrap">{{ schedule.recipe.report_name_format }}</td>
<td class="nowrap">{{ schedule.recipe.subreport_name_format }}</td>
<td>
{% for report_type in schedule.recipe.report_types %}
{% if forloop.counter0 < 2 %}
<span class="label tags-color-{{ report_type|get_report_type_label_style }}">{{ report_type|get_report_type_name }}</span>
{% endif %}
{% if forloop.counter0 == 2 %}
<span class="label tags-color-grey-2">+ {{ schedule.recipe.report_types|length|add:"-2" }}</span>
{% endif %}
{% endfor %}
</td>
<td class="nowrap">{{ schedule.deadline_at }}</td>
<td class="nowrap">{{ schedule.cron }}</td>
{% if schedule.reports %}
<td class="actions">
<button class="expando-button"
data-icon-open-class="icon ti-chevron-down"
data-icon-close-class="icon ti-chevron-up"
data-close-label="{% translate "Close details" %}">
{% translate "Open details" %}
</button>
</td>
{% else %}
<td></td>
{% endif %}
</tr>
{% if schedule.reports %}
<tr class="expando-row">
<td colspan="6">
<table>
<caption class="visually-hidden">{% translate "Scheduled Reports:" %}</caption>
<thead>
<tr>
<th scope="col">{% translate "Name" %}</th>
<th scope="col">{% translate "Report type" %}</th>
<th scope="col">{% translate "Input Object" %}</th>
<th scope="col">{% translate "Reference date" %}</th>
<th scope="col">{% translate "Creation date" %}</th>
</tr>
</thead>
<tbody>
{% for report in schedule.reports %}
<tr>
<td>
<a href="{% url "view_report" organization.code %}?report_id={{ report.reference }}&observed_at={{ report.observed_at|date:"Y-m-d H:i:s:u" }}"
title="{% translate "Show report details" %}">{{ report.name }}</a>
</td>
<td>
<span class="label tags-color-{{ report.report_type|get_report_type_label_style }}">{{ report.report_type|get_report_type_name }}</span>
</td>
<td>
{% for ooi in report.input_oois %}<span>{{ ooi|human_readable }}</span>{% endfor %}
</td>
<td class="nowrap">{{ report.observed_at|date }}</td>
<td class="nowrap">{{ report.date_generated }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</td>
</tr>
{% endif %}
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
3 changes: 2 additions & 1 deletion rocky/reports/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
ReportTypesSelectionMultiReportView,
SetupScanMultiReportView,
)
from reports.views.report_overview import ReportHistoryView, SubreportView
from reports.views.report_overview import ReportHistoryView, ScheduledReportsView, SubreportView

# Report overview urls
urlpatterns = [
path("", ReportsLandingView.as_view(), name="reports"),
path("scheduled-reports/", ScheduledReportsView.as_view(), name="scheduled_reports"),
path("report-history/", ReportHistoryView.as_view(), name="report_history"),
path("report-history/subreports", SubreportView.as_view(), name="subreports"),
]
Expand Down
2 changes: 1 addition & 1 deletion rocky/reports/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:

self.create_report_schedule(report_recipe)

return redirect(reverse("report_history", kwargs={"organization_code": self.organization.code}))
return redirect(reverse("scheduled_reports", kwargs={"organization_code": self.organization.code}))

messages.error(request, _("Empty name should not be possible."))
return PostRedirect(self.get_previous())
Expand Down
63 changes: 62 additions & 1 deletion rocky/reports/views/report_overview.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
from datetime import datetime
from typing import Any

import structlog
from django.contrib import messages
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import ListView

from octopoes.models import Reference
from octopoes.models.exception import ObjectNotFoundException
from octopoes.models.ooi.reports import Report, ReportRecipe
from reports.views.base import ReportBreadcrumbs, get_selection
from rocky.paginator import RockyPaginator
from rocky.views.mixins import OctopoesView, ReportList
from rocky.views.scheduler import SchedulerView

logger = structlog.get_logger(__name__)


class BreadcrumbsReportOverviewView(ReportBreadcrumbs):
Expand All @@ -25,6 +36,56 @@ def build_breadcrumbs(self):
return breadcrumbs


class ScheduledReportsView(BreadcrumbsReportOverviewView, SchedulerView, ListView):
"""
Shows all the reports that have ever been generated for the organization.
"""

paginate_by = 20
breadcrumbs_step = 2
context_object_name = "reports"
paginator = RockyPaginator
template_name = "report_overview/scheduled_reports.html"
task_type = "report"
context_object_name = "scheduled_reports"

def get_recipe_ooi_tree(self, ooi_pk: str) -> ReportRecipe | None:
try:
return self.octopoes_api_connector.get_tree(
Reference.from_str(f"ReportRecipe|{ooi_pk}"),
valid_time=self.observed_at,
depth=1,
types={ReportRecipe, Report},
)
except ObjectNotFoundException:
return messages.error(self.request, f"Report recipe with id {ooi_pk} not found.")

def get_queryset(self) -> list[dict[str, Any]]:
report_schedules = self.get_report_schedules()

recipes = []
if report_schedules:
for schedule in report_schedules:
recipe_id = schedule["data"]["report_recipe_id"]
# TODO: This is a workaround to get the recipes and reports.
# We should create an endpoint for this in octopoes
recipe_ooi_tree = self.get_recipe_ooi_tree(recipe_id)
if recipe_ooi_tree is not None:
recipe_tree = recipe_ooi_tree.store.values()
recipe_ooi = [ooi for ooi in recipe_tree if isinstance(ooi, ReportRecipe)][0]
report_oois = [ooi for ooi in recipe_tree if isinstance(ooi, Report)]
recipes.append(
{
"recipe": recipe_ooi,
"cron": schedule["schedule"],
"deadline_at": datetime.fromisoformat(schedule["deadline_at"]),
"reports": report_oois,
}
)

return recipes


class ReportHistoryView(BreadcrumbsReportOverviewView, OctopoesView, ListView):
"""
Shows all the reports that have ever been generated for the organization.
Expand All @@ -34,7 +95,7 @@ class ReportHistoryView(BreadcrumbsReportOverviewView, OctopoesView, ListView):
breadcrumbs_step = 2
context_object_name = "reports"
paginator = RockyPaginator
template_name = "report_overview/report_overview.html"
template_name = "report_overview/report_history.html"

def get_queryset(self) -> ReportList:
return ReportList(
Expand Down
Loading

0 comments on commit a70ff24

Please sign in to comment.