diff --git a/engine/apps/metrics_exporter/helpers.py b/engine/apps/metrics_exporter/helpers.py index b24c9d37..443dd933 100644 --- a/engine/apps/metrics_exporter/helpers.py +++ b/engine/apps/metrics_exporter/helpers.py @@ -21,13 +21,23 @@ from apps.metrics_exporter.constants import ( ) +def get_organization_ids_from_db(): + AlertReceiveChannel = apps.get_model("alerts", "AlertReceiveChannel") + # get only not deleted organizations that have integrations + organizations_ids = ( + AlertReceiveChannel.objects.filter(organization__deleted_at__isnull=True) + .values_list("organization_id", flat=True) + .distinct() + ) + organizations_ids = list(organizations_ids) + return organizations_ids + + def get_organization_ids(): - """Try getting organizations ids from cache, otherwise get from db and save values in cache""" - Organization = apps.get_model("user_management", "Organization") + """Try to get organizations ids from cache, otherwise get from db and save values in cache""" organizations_ids = cache.get(METRICS_ORGANIZATIONS_IDS, []) if not organizations_ids: - organizations_ids = Organization.objects.all().values_list("id", flat=True) - organizations_ids = list(organizations_ids) + organizations_ids = get_organization_ids_from_db() cache.set(organizations_ids, METRICS_ORGANIZATIONS_IDS, METRICS_ORGANIZATIONS_IDS_CACHE_TIMEOUT) return organizations_ids diff --git a/engine/apps/metrics_exporter/tasks.py b/engine/apps/metrics_exporter/tasks.py index 0ff315b3..72ef066f 100644 --- a/engine/apps/metrics_exporter/tasks.py +++ b/engine/apps/metrics_exporter/tasks.py @@ -19,9 +19,9 @@ from apps.metrics_exporter.helpers import ( get_metric_alert_groups_total_key, get_metrics_cache_timer_key, get_metrics_recalculation_timeout, + get_organization_ids_from_db, get_response_time_period, ) -from apps.user_management.models import Organization from common.custom_celery_tasks import shared_dedicated_queue_retry_task from common.database import get_random_readonly_database_key_if_present_otherwise_default @@ -30,8 +30,7 @@ from common.database import get_random_readonly_database_key_if_present_otherwis autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else None ) def save_organizations_ids_in_cache(): - organizations_ids = Organization.objects.all().values_list("id", flat=True) - organizations_ids = list(organizations_ids) + organizations_ids = get_organization_ids_from_db() cache.set(organizations_ids, METRICS_ORGANIZATIONS_IDS, METRICS_ORGANIZATIONS_IDS_CACHE_TIMEOUT) @@ -80,11 +79,7 @@ def calculate_and_cache_metrics(organization_id, force=False): integrations = ( AlertReceiveChannel.objects.using(get_random_readonly_database_key_if_present_otherwise_default()) - .filter( - ~Q(integration=AlertReceiveChannel.INTEGRATION_MAINTENANCE) - & Q(organization__deleted_at__isnull=True) - & Q(organization_id=organization_id) - ) + .filter(~Q(integration=AlertReceiveChannel.INTEGRATION_MAINTENANCE) & Q(organization_id=organization_id)) .select_related("organization", "team") .prefetch_related("alert_groups") )