2023-06-29 16:01:52 +02:00
|
|
|
|
import typing
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
from django.core.validators import MinLengthValidator
|
2023-09-28 11:57:49 +08:00
|
|
|
|
from django.db import models, transaction
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
2023-09-28 11:57:49 +08:00
|
|
|
|
from apps.alerts.models import AlertReceiveChannel, ChannelFilter
|
|
|
|
|
|
from apps.metrics_exporter.helpers import metrics_add_integration_to_cache, metrics_bulk_update_team_label_cache
|
2023-05-25 20:26:13 +02:00
|
|
|
|
from apps.metrics_exporter.metrics_cache_manager import MetricsCacheManager
|
2022-06-03 08:09:47 -06:00
|
|
|
|
from common.public_primary_keys import generate_public_primary_key, increase_public_primary_key_length
|
|
|
|
|
|
|
2023-06-29 16:01:52 +02:00
|
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
|
|
from django.db.models.manager import RelatedManager
|
|
|
|
|
|
|
|
|
|
|
|
from apps.alerts.models import AlertGroupLogRecord
|
2023-08-03 11:43:03 +02:00
|
|
|
|
from apps.grafana_plugin.helpers.client import GrafanaAPIClient
|
2023-08-01 20:21:02 +02:00
|
|
|
|
from apps.schedules.models import CustomOnCallShift
|
2023-08-03 11:43:03 +02:00
|
|
|
|
from apps.user_management.models import Organization, User
|
2023-06-29 16:01:52 +02:00
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
2023-08-03 11:43:03 +02:00
|
|
|
|
def generate_public_primary_key_for_team() -> str:
|
2022-06-03 08:09:47 -06:00
|
|
|
|
prefix = "T"
|
|
|
|
|
|
new_public_primary_key = generate_public_primary_key(prefix)
|
|
|
|
|
|
|
|
|
|
|
|
failure_counter = 0
|
|
|
|
|
|
while Team.objects.filter(public_primary_key=new_public_primary_key).exists():
|
|
|
|
|
|
new_public_primary_key = increase_public_primary_key_length(
|
|
|
|
|
|
failure_counter=failure_counter, prefix=prefix, model_name="Team"
|
|
|
|
|
|
)
|
|
|
|
|
|
failure_counter += 1
|
|
|
|
|
|
|
|
|
|
|
|
return new_public_primary_key
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-03 11:43:03 +02:00
|
|
|
|
class TeamManager(models.Manager["Team"]):
|
2022-06-03 08:09:47 -06:00
|
|
|
|
@staticmethod
|
2023-08-03 11:43:03 +02:00
|
|
|
|
def sync_for_organization(
|
|
|
|
|
|
organization: "Organization", api_teams: typing.List["GrafanaAPIClient.Types.GrafanaTeam"]
|
|
|
|
|
|
) -> None:
|
2022-06-03 08:09:47 -06:00
|
|
|
|
grafana_teams = {team["id"]: team for team in api_teams}
|
2023-08-03 11:43:03 +02:00
|
|
|
|
existing_team_ids: typing.Set[int] = set(organization.teams.all().values_list("team_id", flat=True))
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
# create missing teams
|
|
|
|
|
|
teams_to_create = tuple(
|
|
|
|
|
|
Team(
|
|
|
|
|
|
organization_id=organization.pk,
|
|
|
|
|
|
team_id=team["id"],
|
|
|
|
|
|
name=team["name"],
|
|
|
|
|
|
email=team["email"],
|
|
|
|
|
|
avatar_url=team["avatarUrl"],
|
|
|
|
|
|
)
|
|
|
|
|
|
for team in grafana_teams.values()
|
|
|
|
|
|
if team["id"] not in existing_team_ids
|
|
|
|
|
|
)
|
2023-09-28 11:57:49 +08:00
|
|
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
|
|
organization.teams.bulk_create(teams_to_create, batch_size=5000)
|
|
|
|
|
|
# Retrieve primary keys for the newly created users
|
|
|
|
|
|
#
|
|
|
|
|
|
# If the model’s primary key is an AutoField, the primary key attribute can only be retrieved
|
|
|
|
|
|
# on certain databases (currently PostgreSQL, MariaDB 10.5+, and SQLite 3.35+).
|
|
|
|
|
|
# On other databases, it will not be set.
|
|
|
|
|
|
# https://docs.djangoproject.com/en/4.1/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create
|
|
|
|
|
|
created_teams = organization.teams.exclude(team_id__in=existing_team_ids)
|
|
|
|
|
|
direct_paging_integrations_to_create = []
|
|
|
|
|
|
for team in created_teams:
|
|
|
|
|
|
alert_receive_channel = AlertReceiveChannel(
|
|
|
|
|
|
organization=organization,
|
|
|
|
|
|
team=team,
|
|
|
|
|
|
integration=AlertReceiveChannel.INTEGRATION_DIRECT_PAGING,
|
|
|
|
|
|
verbal_name=f"Direct paging ({team.name if team else 'No'} team)",
|
|
|
|
|
|
)
|
|
|
|
|
|
direct_paging_integrations_to_create.append(alert_receive_channel)
|
|
|
|
|
|
AlertReceiveChannel.objects.bulk_create(direct_paging_integrations_to_create, batch_size=5000)
|
|
|
|
|
|
created_direct_paging_integrations = AlertReceiveChannel.objects.filter(
|
|
|
|
|
|
organization=organization,
|
|
|
|
|
|
integration=AlertReceiveChannel.INTEGRATION_DIRECT_PAGING,
|
|
|
|
|
|
).exclude(team__team_id__in=existing_team_ids)
|
|
|
|
|
|
default_channel_filters_to_create = []
|
|
|
|
|
|
for integration in created_direct_paging_integrations:
|
|
|
|
|
|
channel_filter = ChannelFilter(
|
|
|
|
|
|
alert_receive_channel=integration,
|
|
|
|
|
|
filtering_term=None,
|
|
|
|
|
|
is_default=True,
|
|
|
|
|
|
order=0,
|
|
|
|
|
|
)
|
|
|
|
|
|
default_channel_filters_to_create.append(channel_filter)
|
|
|
|
|
|
ChannelFilter.objects.bulk_create(default_channel_filters_to_create, batch_size=5000)
|
|
|
|
|
|
|
|
|
|
|
|
# Add direct paging integrations to metrics cache
|
|
|
|
|
|
for integration in direct_paging_integrations_to_create:
|
|
|
|
|
|
metrics_add_integration_to_cache(integration)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
# delete excess teams
|
|
|
|
|
|
team_ids_to_delete = existing_team_ids - grafana_teams.keys()
|
|
|
|
|
|
organization.teams.filter(team_id__in=team_ids_to_delete).delete()
|
|
|
|
|
|
|
2023-05-25 20:26:13 +02:00
|
|
|
|
# collect teams diffs to update metrics cache
|
2023-08-03 11:43:03 +02:00
|
|
|
|
metrics_teams_to_update: MetricsCacheManager.TeamsDiffMap = {}
|
2023-05-25 20:26:13 +02:00
|
|
|
|
for team_id in team_ids_to_delete:
|
|
|
|
|
|
metrics_teams_to_update = MetricsCacheManager.update_team_diff(
|
|
|
|
|
|
metrics_teams_to_update, team_id, deleted=True
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
# update existing teams if any fields have changed
|
|
|
|
|
|
teams_to_update = []
|
|
|
|
|
|
for team in organization.teams.filter(team_id__in=existing_team_ids):
|
|
|
|
|
|
grafana_team = grafana_teams[team.team_id]
|
|
|
|
|
|
if (
|
|
|
|
|
|
team.name != grafana_team["name"]
|
|
|
|
|
|
or team.email != grafana_team["email"]
|
|
|
|
|
|
or team.avatar_url != grafana_team["avatarUrl"]
|
|
|
|
|
|
):
|
2023-05-25 20:26:13 +02:00
|
|
|
|
if team.name != grafana_team["name"]:
|
|
|
|
|
|
# collect teams diffs to update metrics cache
|
|
|
|
|
|
metrics_teams_to_update = MetricsCacheManager.update_team_diff(
|
|
|
|
|
|
metrics_teams_to_update, team.id, new_name=grafana_team["name"]
|
|
|
|
|
|
)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
team.name = grafana_team["name"]
|
|
|
|
|
|
team.email = grafana_team["email"]
|
|
|
|
|
|
team.avatar_url = grafana_team["avatarUrl"]
|
|
|
|
|
|
teams_to_update.append(team)
|
|
|
|
|
|
organization.teams.bulk_update(teams_to_update, ["name", "email", "avatar_url"], batch_size=5000)
|
|
|
|
|
|
|
2023-05-25 20:26:13 +02:00
|
|
|
|
metrics_bulk_update_team_label_cache(metrics_teams_to_update, organization.id)
|
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
class Team(models.Model):
|
2023-07-28 17:11:38 +02:00
|
|
|
|
current_team_users: "RelatedManager['User']"
|
2023-08-01 20:21:02 +02:00
|
|
|
|
custom_on_call_shifts: "RelatedManager['CustomOnCallShift']"
|
2023-06-29 16:01:52 +02:00
|
|
|
|
oncall_schedules: "RelatedManager['AlertGroupLogRecord']"
|
2023-07-28 17:11:38 +02:00
|
|
|
|
users: "RelatedManager['User']"
|
2023-06-29 16:01:52 +02:00
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
public_primary_key = models.CharField(
|
|
|
|
|
|
max_length=20,
|
|
|
|
|
|
validators=[MinLengthValidator(settings.PUBLIC_PRIMARY_KEY_MIN_LENGTH + 1)],
|
|
|
|
|
|
unique=True,
|
|
|
|
|
|
default=generate_public_primary_key_for_team,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-03 11:43:03 +02:00
|
|
|
|
objects = TeamManager()
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
team_id = models.PositiveIntegerField()
|
|
|
|
|
|
organization = models.ForeignKey(
|
|
|
|
|
|
to="user_management.Organization",
|
|
|
|
|
|
related_name="teams",
|
|
|
|
|
|
on_delete=models.deletion.CASCADE,
|
|
|
|
|
|
)
|
|
|
|
|
|
users = models.ManyToManyField(to="user_management.User", related_name="teams")
|
|
|
|
|
|
name = models.CharField(max_length=300)
|
|
|
|
|
|
email = models.CharField(max_length=300, null=True, blank=True, default=None)
|
|
|
|
|
|
avatar_url = models.URLField()
|
2023-03-22 00:57:20 +08:00
|
|
|
|
|
|
|
|
|
|
# If is_sharing_resources_to_all is False only team members and admins can access it and it's resources
|
|
|
|
|
|
# if it's True every oncall organization user can access it
|
|
|
|
|
|
is_sharing_resources_to_all = models.BooleanField(default=False)
|