oncall-engine/engine/apps/oss_installation/utils.py
Joey Orlando bfcc0b9f29
update URLs constructed by the backend to support IRM plugin (#5137)
# What this PR does

Introduces a new class,
`apps.grafana_plugin.ui_url_builder.UIURLBuilder`, which is responsible
for... building UI URLs (😄). The class mainly does two things:
- it will decide if the URL should point to `grafana-oncall-app` or
`grafana-irm-app` based on the value of
`organization.is_grafana_irm_enabled` (**NOTE**: this value isn't yet
being set + defaults to `False`; logic for setting this value will be
done in a subsequent PR)
- Adds `enum`s, `OnCallPage` and `IncidentPage` to DRYify hardcoded UI
URLs (in case we decide to change these slightly in the near future)

## Checklist

- [x] Unit, integration, and e2e (if applicable) tests updated
- [x] Documentation added (or `pr:no public docs` PR label added if not
required)
- [x] Added the relevant release notes label (see labels prefixed w/
`release:`). These labels dictate how your PR will
    show up in the autogenerated release notes.
2024-10-09 08:55:10 -04:00

88 lines
2.8 KiB
Python

import logging
import typing
from django.utils import timezone
from apps.grafana_plugin.ui_url_builder import UIURLBuilder
from apps.oss_installation.constants import CloudSyncStatus
from apps.schedules.ical_utils import list_users_to_notify_from_ical_for_period
if typing.TYPE_CHECKING:
from apps.user_management.models import Organization
logger = logging.getLogger(__name__)
def active_oss_users_count():
"""
active_oss_users_count returns count of active users of oss installation.
"""
from apps.alerts.models import AlertGroupLogRecord, EscalationPolicy
from apps.base.models import UserNotificationPolicyLogRecord
from apps.schedules.models import OnCallSchedule
# Take logs for previous 24 hours
start = timezone.now() - timezone.timedelta(hours=24)
end = timezone.now()
# Take schedules for current month
schedule_start = timezone.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0)
schedule_end = (schedule_start + timezone.timedelta(days=32)).replace(day=1)
unique_active_users = set()
unique_active_users.update(
list(
UserNotificationPolicyLogRecord.objects.filter(
created_at__gte=start, created_at__lt=end, author__isnull=False
)
.values_list("author_id", flat=True)
.distinct()
)
)
unique_active_users.update(
list(
AlertGroupLogRecord.objects.filter(
type__in=AlertGroupLogRecord.TYPES_FOR_LICENCE_CALCULATION,
created_at__gte=start,
created_at__lt=end,
author__isnull=False,
)
.values_list("author_id", flat=True)
.distinct()
)
)
# Get active users from notification policies
unique_active_users.update(
list(
EscalationPolicy.objects.filter(notify_to_users_queue__isnull=False).values_list(
"notify_to_users_queue__id", flat=True
)
)
)
for schedule in OnCallSchedule.objects.all():
users_from_schedule = list_users_to_notify_from_ical_for_period(schedule, schedule_start, schedule_end)
for user in users_from_schedule:
unique_active_users.add(user.pk)
return len(unique_active_users)
def cloud_user_identity_status(org: "Organization", connector, identity):
link = None
if connector is None:
status = CloudSyncStatus.NOT_SYNCED
elif identity is None:
status = CloudSyncStatus.SYNCED_USER_NOT_FOUND
link = connector.cloud_url
else:
if identity.phone_number_verified:
status = CloudSyncStatus.SYNCED_PHONE_VERIFIED
else:
status = CloudSyncStatus.SYNCED_PHONE_NOT_VERIFIED
link = UIURLBuilder(org, base_url=connector.cloud_url).users(f"?p=1&id={identity.cloud_id}")
return status, link