Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove deprecated backend code #2502

Merged
merged 11 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions engine/apps/alerts/migrations/0020_auto_20230711_1532.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.20 on 2023-07-11 15:32

from django.db import migrations, models
import django_migration_linter as linter


class Migration(migrations.Migration):

dependencies = [
('alerts', '0019_auto_20230705_1619'),
]

operations = [
linter.IgnoreMigration(),
migrations.RemoveField(
model_name='alertgroup',
name='active_cache_for_web_calculation_id',
),
migrations.RemoveField(
model_name='alertgroup',
name='estimate_escalation_finish_time',
),
migrations.RemoveField(
model_name='alertgroup',
name='cached_render_for_web',
),
Comment on lines +15 to +26
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The DROP COLUMN operation supports the "Instant" algorithm in MySQL v8.0 so this should be a very quick operation (docs)

]
42 changes: 1 addition & 41 deletions engine/apps/alerts/models/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from django.dispatch import receiver
from django.utils import timezone
from django.utils.functional import cached_property
from django_deprecate_fields import deprecate_field

from apps.alerts.constants import AlertGroupState
from apps.alerts.escalation_snapshot import EscalationSnapshotMixin
Expand Down Expand Up @@ -163,7 +162,7 @@ class AlertGroup(AlertGroupSlackRenderingMixin, EscalationSnapshotMixin, models.
(USER, "user"),
(NOT_YET, "not yet"),
(LAST_STEP, "last escalation step"),
(ARCHIVED, "archived"),
(ARCHIVED, "archived"), # deprecated. don't use
(WIPED, "wiped"),
(DISABLE_MAINTENANCE, "stop maintenance"),
(NOT_YET_STOP_AUTORESOLVE, "not yet, autoresolve disabled"),
Expand Down Expand Up @@ -327,10 +326,6 @@ def status(self):
related_name="dependent_alert_groups",
)

# cached_render_for_web and active_cache_for_web_calculation_id are deprecated
cached_render_for_web = models.JSONField(default=dict)
active_cache_for_web_calculation_id = models.CharField(max_length=100, null=True, default=None)

# NOTE: we should probably migrate this field to models.UUIDField as it's ONLY ever being
# set to the result of uuid.uuid1
last_unique_unacknowledge_process_id: UUID | None = models.CharField(max_length=100, null=True, default=None)
Expand Down Expand Up @@ -361,9 +356,6 @@ def status(self):

raw_escalation_snapshot = JSONField(null=True, default=None)

# THIS FIELD IS DEPRECATED AND SHOULD EVENTUALLY BE REMOVED
estimate_escalation_finish_time = deprecate_field(models.DateTimeField(null=True, default=None))

# This field is used for constraints so we can use get_or_create() in concurrent calls
# https://docs.djangoproject.com/en/3.2/ref/models/querysets/#get-or-create
# Combined with unique_together below, it allows only one alert group with
Expand Down Expand Up @@ -715,38 +707,6 @@ def resolve_by_source(self):
for dependent_alert_group in self.dependent_alert_groups.all():
dependent_alert_group.resolve_by_source()

# deprecated
def resolve_by_archivation(self):
AlertGroupLogRecord = apps.get_model("alerts", "AlertGroupLogRecord")
# if incident was silenced, unsilence it without starting escalation
if self.silenced:
self.un_silence()
self.log_records.create(
type=AlertGroupLogRecord.TYPE_UN_SILENCE,
silence_delay=None,
reason="Resolve by archivation",
)
self.archive()
self.stop_escalation()
if not self.resolved:
self.resolve(resolved_by=AlertGroup.ARCHIVED)

log_record = self.log_records.create(type=AlertGroupLogRecord.TYPE_RESOLVED)

logger.debug(
f"send alert_group_action_triggered_signal for alert_group {self.pk}, "
f"log record {log_record.pk} with type '{log_record.get_type_display()}', action source: archivation"
)

alert_group_action_triggered_signal.send(
sender=self.resolve_by_archivation,
log_record=log_record.pk,
action_source=None,
)

for dependent_alert_group in self.dependent_alert_groups.all():
dependent_alert_group.resolve_by_archivation()

Comment on lines -719 to -749
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolve_by_archivation was only ever invoked in the following two spots:

  • CheckAlertIsUnarchivedMixin.check_alert_is_unarchived: this method seemed to be deprecated because the logic inside the method was as such:
def check_alert_is_unarchived(self, slack_team_identity, payload, alert_group, warning=True):
    alert_group_is_unarchived = alert_group.started_at.date() > self.organization.archive_alerts_from
    if not alert_group_is_unarchived:
        if warning:
            warning_text = "Action is impossible: the Alert is archived."
            self.open_warning_window(payload, warning_text)
        if not alert_group.resolved or not alert_group.is_archived:
            alert_group.resolve_by_archivation()
    return alert_group_is_unarchived

organization.archive_alerts_from is always set to 1970-01-01 (only value that exists in our production database) so therefore the above logic will never invoke AlertGroup.resolve_by_archivation

  • in the resolve_archived_incidents_for_organization Celery task

this celery task was only ever queued by CurrentOrganizationSerializer.update:

def update(self, instance, validated_data):
    current_archive_date = instance.archive_alerts_from
    archive_alerts_from = validated_data.get("archive_alerts_from")

    result = super().update(instance, validated_data)
    if archive_alerts_from is not None and current_archive_date != archive_alerts_from:
        if current_archive_date > archive_alerts_from:
            unarchive_incidents_for_organization.apply_async(
                (instance.pk,),
            )
        resolve_archived_incidents_for_organization.apply_async(
            (instance.pk,),
        )

    return result

archive_alerts_from will always be None here

def resolve_by_last_step(self):
AlertGroupLogRecord = apps.get_model("alerts", "AlertGroupLogRecord")
initial_state = self.state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def resolve_alert_group_by_source_if_needed(alert_group_pk):
alert_group.save(update_fields=["resolved_by"])
if alert_group.resolved_by == alert_group.NOT_YET_STOP_AUTORESOLVE:
return "alert_group is too big to auto-resolve"
print("YOLO")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


last_alert = AlertForAlertManager.objects.get(pk=alert_group.alerts.last().pk)
if alert_group.is_alert_a_resolve_signal(last_alert):
alert_group.resolve_by_source()
Expand Down
68 changes: 1 addition & 67 deletions engine/apps/api/serializers/organization.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import datetime
from dataclasses import asdict

import humanize
import pytz
from django.apps import apps
from django.utils import timezone
from rest_framework import fields, serializers
from rest_framework import serializers

from apps.base.models import LiveSetting
from apps.phone_notifications.phone_provider import get_phone_provider
from apps.slack.models import SlackTeamIdentity
from apps.slack.tasks import resolve_archived_incidents_for_organization, unarchive_incidents_for_organization
from apps.user_management.models import Organization
from common.api_helpers.mixins import EagerLoadingMixin


class CustomDateField(fields.TimeField):
def to_internal_value(self, data):
try:
archive_datetime = datetime.datetime.fromisoformat(data).astimezone(pytz.UTC)
except (TypeError, ValueError):
raise serializers.ValidationError({"archive_alerts_from": ["Invalid date format"]})
if archive_datetime.date() >= timezone.now().date():
raise serializers.ValidationError({"archive_alerts_from": ["Invalid date. Date must be less than today."]})
return archive_datetime
Comment on lines -18 to -26
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not refrenced anywhere



class FastSlackTeamIdentitySerializer(serializers.ModelSerializer):
class Meta:
model = SlackTeamIdentity
Expand All @@ -37,7 +21,6 @@ class OrganizationSerializer(EagerLoadingMixin, serializers.ModelSerializer):
slack_team_identity = FastSlackTeamIdentitySerializer(read_only=True)

name = serializers.CharField(required=False, allow_null=True, allow_blank=True, source="org_title")
# name_slug = serializers.CharField(required=False, allow_null=True, allow_blank=False)
maintenance_till = serializers.ReadOnlyField(source="till_maintenance_timestamp")
slack_channel = serializers.SerializerMethodField()

Expand All @@ -48,21 +31,15 @@ class Meta:
fields = [
"pk",
"name",
# "name_slug",
# "is_new_version",
"slack_team_identity",
"maintenance_mode",
"maintenance_till",
# "incident_retention_web_report",
# "number_of_employees",
"slack_channel",
]
read_only_fields = [
"is_new_version",
"slack_team_identity",
"maintenance_mode",
"maintenance_till",
# "incident_retention_web_report",
]

def get_slack_channel(self, obj):
Expand All @@ -82,22 +59,18 @@ def get_slack_channel(self, obj):


class CurrentOrganizationSerializer(OrganizationSerializer):
limits = serializers.SerializerMethodField()
env_status = serializers.SerializerMethodField()
banner = serializers.SerializerMethodField()

class Meta(OrganizationSerializer.Meta):
fields = [
*OrganizationSerializer.Meta.fields,
"limits",
"archive_alerts_from",
"is_resolution_note_required",
"env_status",
"banner",
]
read_only_fields = [
*OrganizationSerializer.Meta.read_only_fields,
"limits",
"banner",
]

Expand All @@ -109,10 +82,6 @@ def get_banner(self, obj):
)[0]
return banner.json_value

def get_limits(self, obj):
user = self.context["request"].user
return obj.notifications_limit_web_report(user)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limits is not used by the web UI


def get_env_status(self, obj):
# deprecated in favour of ConfigAPIView.
# All new env statuses should be added there
Expand All @@ -122,44 +91,9 @@ def get_env_status(self, obj):
phone_provider_config = get_phone_provider().flags
return {
"telegram_configured": telegram_configured,
"twilio_configured": phone_provider_config.configured, # keep for backward compatibility
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not referenced anywhere (search)

"phone_provider": asdict(phone_provider_config),
}

def get_stats(self, obj):
if isinstance(obj.cached_seconds_saved_by_amixr, int):
verbal_time_saved_by_amixr = humanize.naturaldelta(
datetime.timedelta(seconds=obj.cached_seconds_saved_by_amixr)
)
else:
verbal_time_saved_by_amixr = None

result = {
"grouped_percent": obj.cached_grouped_percent,
"alerts_count": obj.cached_alerts_count,
"noise_reduction": obj.cached_noise_reduction,
"average_response_time": humanize.naturaldelta(obj.cached_average_response_time),
"verbal_time_saved_by_amixr": verbal_time_saved_by_amixr,
}

return result
Comment on lines -129 to -145
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not referenced anywhere, or consumed by the web UI


def update(self, instance, validated_data):
current_archive_date = instance.archive_alerts_from
archive_alerts_from = validated_data.get("archive_alerts_from")

result = super().update(instance, validated_data)
if archive_alerts_from is not None and current_archive_date != archive_alerts_from:
if current_archive_date > archive_alerts_from:
unarchive_incidents_for_organization.apply_async(
(instance.pk,),
)
resolve_archived_incidents_for_organization.apply_async(
(instance.pk,),
)

return result
Comment on lines -147 to -161
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see this comment here for a breakdown of why we can get rid of this



class FastOrganizationSerializer(serializers.ModelSerializer):
pk = serializers.CharField(read_only=True, source="public_primary_key")
Expand Down
39 changes: 0 additions & 39 deletions engine/apps/api/tests/test_subscription.py

This file was deleted.

2 changes: 0 additions & 2 deletions engine/apps/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
SlackTeamSettingsAPIView,
UnAcknowledgeTimeoutOptionsAPIView,
)
from .views.subscription import SubscriptionView
from .views.team import TeamViewSet
from .views.telegram_channels import TelegramChannelViewSet
from .views.user import CurrentUserView, UserView
Expand Down Expand Up @@ -83,7 +82,6 @@
GetChannelVerificationCode.as_view(),
name="api-get-channel-verification-code",
),
optional_slash_path("current_subscription", SubscriptionView.as_view(), name="subscription"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this endpoint is not referenced/consumed by the web plugin

optional_slash_path("terraform_file", TerraformGitOpsView.as_view(), name="terraform_file"),
optional_slash_path("terraform_imports", TerraformStateView.as_view(), name="terraform_imports"),
optional_slash_path("maintenance", MaintenanceAPIView.as_view(), name="maintenance"),
Expand Down
3 changes: 0 additions & 3 deletions engine/apps/api/views/alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,6 @@ def enrich(self, alert_groups):
alert_group_pks = [alert_group.pk for alert_group in alert_groups]
queryset = AlertGroup.all_objects.filter(pk__in=alert_group_pks).order_by("-pk")

# do not load cached_render_for_web as it's deprecated and can be very large
queryset = queryset.defer("cached_render_for_web")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the only reference to cached_render_for_web


queryset = self.get_serializer_class().setup_eager_loading(queryset)
alert_groups = list(queryset)

Expand Down
16 changes: 0 additions & 16 deletions engine/apps/api/views/subscription.py

This file was deleted.

7 changes: 2 additions & 5 deletions engine/apps/slack/scenarios/alertgroup_appearance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
from apps.api.permissions import RBACPermission
from apps.slack.scenarios import scenario_step

from .step_mixins import AlertGroupActionsMixin, CheckAlertIsUnarchivedMixin
from .step_mixins import AlertGroupActionsMixin


class OpenAlertAppearanceDialogStep(CheckAlertIsUnarchivedMixin, AlertGroupActionsMixin, scenario_step.ScenarioStep):
class OpenAlertAppearanceDialogStep(AlertGroupActionsMixin, scenario_step.ScenarioStep):
REQUIRED_PERMISSIONS = [RBACPermission.Permissions.CHATOPS_WRITE]

def process_scenario(self, slack_user_identity, slack_team_identity, payload):
Expand All @@ -17,9 +17,6 @@ def process_scenario(self, slack_user_identity, slack_team_identity, payload):
self.open_unauthorized_warning(payload)
return

if not self.check_alert_is_unarchived(slack_team_identity, payload, alert_group):
return

private_metadata = {
"organization_id": self.organization.pk if self.organization else alert_group.organization.pk,
"alert_group_pk": alert_group.pk,
Expand Down
Loading