# What this PR does Remove [`apps.get_model`](https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.apps.get_model) invocations and use inline `import` statements in places where models are imported within functions/methods to avoid circular imports. I believe `import` statements are more appropriate for most use cases as they allow for better static code analysis & formatting, and solve the issue of circular imports without being unnecessarily dynamic as `apps.get_model`. With `import` statements, it's possible to: - Jump to model definitions in most IDEs - Automatically sort inline imports with `isort` - Find import errors faster/easier (most IDEs highlight broken imports) - Have more consistency across regular & inline imports when importing models This PR also adds a flake8 rule to ban imports of `django.apps.apps`, so it's harder to use `apps.get_model` by mistake (it's possible to ignore this rule by using `# noqa: I251`). The rule is not enforced on directories with migration files, because `apps.get_model` is often used to get a historical state of a model, which is useful when writing migrations ([see this SO answer for more details](https://stackoverflow.com/a/37769213)). So `apps.get_model` is considered OK in migrations (even necessary in some cases). ## 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] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not required)
84 lines
2.7 KiB
Python
84 lines
2.7 KiB
Python
import logging
|
|
from urllib.parse import urljoin
|
|
|
|
from django.utils import timezone
|
|
|
|
from apps.oss_installation import constants as oss_constants
|
|
from apps.schedules.ical_utils import list_users_to_notify_from_ical_for_period
|
|
|
|
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(connector, identity):
|
|
link = None
|
|
if connector is None:
|
|
status = oss_constants.CLOUD_NOT_SYNCED
|
|
elif identity is None:
|
|
status = oss_constants.CLOUD_SYNCED_USER_NOT_FOUND
|
|
link = connector.cloud_url
|
|
else:
|
|
if identity.phone_number_verified:
|
|
status = oss_constants.CLOUD_SYNCED_PHONE_VERIFIED
|
|
else:
|
|
status = oss_constants.CLOUD_SYNCED_PHONE_NOT_VERIFIED
|
|
|
|
link = urljoin(connector.cloud_url, f"a/grafana-oncall-app/?page=users&p=1&id={identity.cloud_id}")
|
|
return status, link
|