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

Sub reports for Aggregate Report #3852

Merged
merged 13 commits into from
Nov 21, 2024
5 changes: 5 additions & 0 deletions rocky/reports/report_types/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ class AggregateReportSubReports(TypedDict):
optional: list[type[Report]]


class SubReportPlugins(TypedDict):
required: list[type[Report]]
optional: list[type[Report]]
Rieven marked this conversation as resolved.
Show resolved Hide resolved


class AggregateReport(BaseReport):
reports: AggregateReportSubReports

Expand Down
67 changes: 46 additions & 21 deletions rocky/reports/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from octopoes.models.ooi.reports import Report
from reports.report_types.aggregate_organisation_report.report import aggregate_reports
from reports.report_types.concatenated_report.report import ConcatenatedReport
from reports.report_types.definitions import BaseReport, SubReportPlugins
from reports.report_types.helpers import REPORTS, get_report_by_id
from reports.report_types.multi_organization_report.report import MultiOrganizationReport, collect_report_data
from reports.views.base import BaseReportView, ReportDataDict
Expand Down Expand Up @@ -56,6 +57,22 @@ def collect_reports(observed_at: datetime, octopoes_connector: OctopoesAPIConnec
return error_reports, report_data


def get_child_input_data(input_data: dict[str, Any], ooi: str, report_type: type[BaseReport]):
required_plugins = list(input_data["input_data"]["plugins"]["required"])
optional_plugins = list(input_data["input_data"]["plugins"]["optional"])

child_plugins: SubReportPlugins = {"required": [], "optional": []}

child_plugins["required"] = [
plugin_id for plugin_id in required_plugins if plugin_id in report_type.plugins["required"]
]
child_plugins["optional"] = [
plugin_id for plugin_id in optional_plugins if plugin_id in report_type.plugins["optional"]
]

return {"input_data": {"input_oois": [ooi], "report_types": [report_type.id], "plugins": child_plugins}}


def save_report_data(
bytes_client,
observed_at,
Expand Down Expand Up @@ -116,21 +133,7 @@ def save_report_data(
name_to_save = updated_name
break

required_plugins = list(input_data["input_data"]["plugins"]["required"])
optional_plugins = list(input_data["input_data"]["plugins"]["optional"])

child_plugins: dict[str, list[str]] = {"required": [], "optional": []}

child_plugins["required"] = [
plugin_id for plugin_id in required_plugins if plugin_id in report_type.plugins["required"]
]
child_plugins["optional"] = [
plugin_id for plugin_id in optional_plugins if plugin_id in report_type.plugins["optional"]
]

child_input_data = {
"input_data": {"input_oois": [ooi], "report_types": [report_type_id], "plugins": child_plugins}
}
child_input_data = get_child_input_data(input_data, ooi, report_type)

raw_id = bytes_client.upload_raw(
raw=ReportDataDict({"report_data": data["data"]} | child_input_data).model_dump_json().encode(),
Expand Down Expand Up @@ -208,8 +211,7 @@ def save_aggregate_report_data(
report_recipe: Reference | None = None,
) -> Report:
observed_at = get_observed_at

now = datetime.utcnow()
Rieven marked this conversation as resolved.
Show resolved Hide resolved
now = datetime.now(timezone.utc)

# Create the report
report_data_raw_id = bytes_client.upload_raw(
Expand All @@ -230,7 +232,7 @@ def save_aggregate_report_data(
organization_name=organization.name,
organization_tags=[tag.name for tag in organization.tags.all()],
data_raw_id=report_data_raw_id,
date_generated=datetime.now(timezone.utc),
date_generated=now,
input_oois=ooi_pks,
observed_at=observed_at,
parent_report=None,
Expand All @@ -240,12 +242,35 @@ def save_aggregate_report_data(
create_ooi(octopoes_api_connector, bytes_client, report_ooi, observed_at)

# Save the child reports to bytes

for ooi, types in report_data.items():
for report_type, data in types.items():
bytes_client.upload_raw(
raw=ReportDataDict(data | input_data).model_dump_json().encode(), manual_mime_types={"openkat/report"}
for report_type_id, data in types.items():
report_type = get_report_by_id(report_type_id)
child_input_data = get_child_input_data(input_data, ooi, report_type)

raw_id = bytes_client.upload_raw(
raw=ReportDataDict({"report_data": data} | child_input_data).model_dump_json().encode(),
manual_mime_types={"openkat/report"},
)

aggregate_sub_report_ooi = Report(
name=str(report_type.name),
report_type=report_type_id,
template=report_type.template_path,
report_id=uuid4(),
organization_code=organization.code,
organization_name=organization.name,
organization_tags=[tag.name for tag in organization.tags.all()],
data_raw_id=raw_id,
date_generated=now,
input_oois=[ooi],
observed_at=observed_at,
parent_report=report_ooi.reference,
has_parent=True,
)

create_ooi(octopoes_api_connector, bytes_client, aggregate_sub_report_ooi, observed_at)

return report_ooi


Expand Down