Skip to content

Commit

Permalink
EPMCEOOS-6323 add n largest to top reports
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-afanasiev committed Feb 1, 2025
1 parent 2fe728f commit d99bda3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
23 changes: 22 additions & 1 deletion src/handlers/high_level_reports_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,30 @@ def post_department(self, event: DepartmentGetReportModel):
raise self._rmq.no_rabbitmq_response().exc()

builder = MaestroModelBuilder()
now = utc_datetime()

for typ in event.new_types:
_LOG.debug(f'Going to generate {typ} for {event.customer_id}')
rep = self._rms.get_exactly_for_customer(
customer=event.customer_id,
type_=typ,
start=typ.start(now),
end=typ.end(now)
)
if not rep:
_LOG.warning(
f'Cannot find {typ} for {event.customer_id} for the current month'
)
continue









# TODO: here generate



Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import copy
import heapq
import statistics
from datetime import datetime
from typing import Generator, Iterator, cast, Iterable
from typing import Generator, Iterable, Iterator, cast

from modular_sdk.models.customer import Customer
from modular_sdk.models.tenant import Tenant
from modular_sdk.modular import Modular

from helpers.constants import (
GLOBAL_REGION,
TACTICS_ID_MAPPING,
Cloud,
JobState,
ReportType,
TACTICS_ID_MAPPING,
)
from helpers.log_helper import get_logger
from helpers.time_helper import utc_datetime
Expand Down Expand Up @@ -50,6 +51,9 @@
# The business logic itself is quite confusing so no need to make it
# more unclear by providing more abstraction

TOP_TENANT_LENGTH = 10
TOP_CLOUD_LENGTH = 5


class MetricsContext:
"""
Expand Down Expand Up @@ -185,6 +189,18 @@ def __init__(
self._tenants_cache = {}
self._platforms_cache = {}

@staticmethod
def nlargest(
items: list[dict], n: int, reverse: bool = False
) -> list[dict]:
def key(i: dict):
return i.get('sort_by', 0)

if reverse:
return heapq.nlargest(n, items, key=key)
else:
return heapq.nsmallest(n, items, key=key)

@staticmethod
def yield_one_per_cloud(
it: Iterable[Tenant],
Expand Down Expand Up @@ -215,7 +231,11 @@ def yield_one_per_cloud(

@staticmethod
def base_clouds_payload() -> dict[str, list]:
return {Cloud.AWS.value: [], Cloud.AZURE.value: [], Cloud.GOOGLE.value: []}
return {
Cloud.AWS.value: [],
Cloud.AZURE.value: [],
Cloud.GOOGLE.value: [],
}

@classmethod
def build(cls) -> 'MetricsCollector':
Expand Down Expand Up @@ -1296,6 +1316,11 @@ def top_resources_by_cloud(
},
}
)
for cloud in cloud_tenant:
cloud_tenant[cloud] = self.nlargest(
cloud_tenant[cloud], TOP_CLOUD_LENGTH, True
)

yield (
self._rms.create(
key=self._rms.key_for_customer(report_type, ctx.customer.name),
Expand Down Expand Up @@ -1364,6 +1389,7 @@ def top_tenants_resources(
'data': clouds_data,
}
)
data = self.nlargest(data, TOP_TENANT_LENGTH, True)
yield (
self._rms.create(
key=self._rms.key_for_customer(report_type, ctx.customer.name),
Expand Down Expand Up @@ -1408,6 +1434,12 @@ def top_compliance_by_cloud(
'data': {st.full_name: cov for st, cov in total.items()},
}
)

for cloud in cloud_tenant:
cloud_tenant[cloud] = self.nlargest(
cloud_tenant[cloud], TOP_CLOUD_LENGTH, False
)

yield (
self._rms.create(
key=self._rms.key_for_customer(report_type, ctx.customer.name),
Expand Down Expand Up @@ -1470,6 +1502,7 @@ def top_tenants_compliance(
'data': clouds_data,
}
)
data = self.nlargest(data, TOP_TENANT_LENGTH, False)
yield (
self._rms.create(
key=self._rms.key_for_customer(report_type, ctx.customer.name),
Expand Down Expand Up @@ -1530,6 +1563,11 @@ def top_attacks_by_cloud(
],
}
)

for cloud in cloud_tenant:
cloud_tenant[cloud] = self.nlargest(
cloud_tenant[cloud], TOP_CLOUD_LENGTH, True
)
yield (
self._rms.create(
key=self._rms.key_for_customer(report_type, ctx.customer.name),
Expand Down Expand Up @@ -1591,6 +1629,7 @@ def top_tenants_attacks(
'data': clouds_data,
}
)
data = self.nlargest(data, TOP_TENANT_LENGTH, True)
yield (
self._rms.create(
key=self._rms.key_for_customer(report_type, ctx.customer.name),
Expand Down

0 comments on commit d99bda3

Please sign in to comment.