Skip to content

Commit aa7a580

Browse files
committed
override get and filter
1 parent ad7dac7 commit aa7a580

22 files changed

+55
-65
lines changed

src/sentry/api/serializers/models/rule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def _fetch_workflows(self, item_list: Sequence[Workflow]) -> BaseQuerySet[Workfl
347347
to_attr="prefetched_wdcgs",
348348
)
349349
workflows = (
350-
Workflow.objects.filter(id__in=[wf.id for wf in item_list], status=ObjectStatus.ACTIVE)
350+
Workflow.objects.filter(id__in=[wf.id for wf in item_list])
351351
.select_related("when_condition_group")
352352
.prefetch_related("when_condition_group__conditions")
353353
.prefetch_related(workflow_dcg_prefetch)

src/sentry/digests/notifications.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import Any, NamedTuple, TypeAlias
77

88
from sentry import features, tsdb
9-
from sentry.constants import ObjectStatus
109
from sentry.digests.types import IdentifierKey, Notification, Record, RecordWithRuleObjects
1110
from sentry.eventstore.models import Event
1211
from sentry.models.group import Group, GroupStatus
@@ -172,9 +171,9 @@ def get_rules_from_workflows(project: Project, workflow_ids: set[int]) -> dict[i
172171
return rules
173172

174173
# Fetch all workflows in bulk
175-
workflows = Workflow.objects.filter(
176-
organization_id=project.organization_id, status=ObjectStatus.ACTIVE
177-
).in_bulk(workflow_ids)
174+
workflows = Workflow.objects.filter(organization_id=project.organization_id).in_bulk(
175+
workflow_ids
176+
)
178177

179178
# We are only processing the workflows in the digest if under the new flag
180179
# This should be ok since we should only add workflow_ids to redis when under this flag

src/sentry/incidents/endpoints/organization_alert_rule_details.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
)
2626
from sentry.apidocs.examples.metric_alert_examples import MetricAlertExamples
2727
from sentry.apidocs.parameters import GlobalParams, MetricAlertParams
28-
from sentry.constants import ObjectStatus
2928
from sentry.incidents.endpoints.bases import OrganizationAlertRuleEndpoint
3029
from sentry.incidents.endpoints.serializers.alert_rule import (
3130
AlertRuleSerializer,
@@ -69,9 +68,7 @@ def fetch_alert_rule(
6968

7069
if features.has("organizations:workflow-engine-rule-serializers", organization):
7170
try:
72-
detector = Detector.objects.get(
73-
alertruledetector__alert_rule_id=alert_rule.id, status=ObjectStatus.ACTIVE
74-
)
71+
detector = Detector.objects.get(alertruledetector__alert_rule_id=alert_rule.id)
7572
return Response(
7673
serialize(
7774
detector,
@@ -146,9 +143,7 @@ def update_alert_rule(
146143
if features.has("organizations:workflow-engine-rule-serializers", organization):
147144
validator.save()
148145
try:
149-
detector = Detector.objects.get(
150-
alertruledetector__alert_rule_id=alert_rule.id, status=ObjectStatus.ACTIVE
151-
)
146+
detector = Detector.objects.get(alertruledetector__alert_rule_id=alert_rule.id)
152147
return Response(
153148
serialize(
154149
detector,

src/sentry/incidents/endpoints/organization_alert_rule_index.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ def create_metric_alert(
125125
alert_rule = validator.save()
126126
if features.has("organizations:workflow-engine-rule-serializers", organization):
127127
try:
128-
detector = Detector.objects.get(
129-
alertruledetector__alert_rule_id=alert_rule.id, status=ObjectStatus.ACTIVE
130-
)
128+
detector = Detector.objects.get(alertruledetector__alert_rule_id=alert_rule.id)
131129
return Response(
132130
serialize(
133131
detector,
@@ -162,7 +160,6 @@ def fetch_metric_alert(
162160
if features.has("organizations:workflow-engine-rule-serializers", organization):
163161
detectors = Detector.objects.filter(
164162
alertruledetector__alert_rule_id__in=[alert_rule.id for alert_rule in alert_rules],
165-
status=ObjectStatus.ACTIVE,
166163
)
167164
if not len(detectors):
168165
return Response(status=status.HTTP_404_NOT_FOUND)

src/sentry/incidents/endpoints/project_alert_rule_task_details.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from sentry.api.base import region_silo_endpoint
99
from sentry.api.bases.project import ProjectEndpoint, ProjectSettingPermission
1010
from sentry.api.serializers import serialize
11-
from sentry.constants import ObjectStatus
1211
from sentry.incidents.endpoints.serializers.workflow_engine_detector import (
1312
WorkflowEngineDetectorSerializer,
1413
)
@@ -51,7 +50,6 @@ def get(self, request: Request, project, task_uuid) -> Response:
5150
try:
5251
detector = Detector.objects.get(
5352
alertruledetector__alert_rule_id=alert_rule.id,
54-
status=ObjectStatus.ACTIVE,
5553
)
5654
except Detector.DoesNotExist:
5755
raise Http404

src/sentry/incidents/endpoints/serializers/workflow_engine_data_condition.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from django.db.models import Subquery
77

88
from sentry.api.serializers import Serializer, serialize
9-
from sentry.constants import ObjectStatus
109
from sentry.incidents.endpoints.serializers.workflow_engine_action import (
1110
WorkflowEngineActionSerializer,
1211
)
@@ -45,7 +44,6 @@ def get_attrs(
4544
detector_trigger.condition_group
4645
for detector_trigger in detector_triggers.values()
4746
],
48-
status=ObjectStatus.ACTIVE,
4947
).values_list("id", flat=True)
5048
)
5149
workflow_dcg_ids = DataConditionGroup.objects.filter(
@@ -89,9 +87,7 @@ def serialize(
8987
**kwargs: Any,
9088
) -> dict[str, Any]:
9189
# XXX: we are assuming that the obj/DataCondition is a detector trigger
92-
detector = Detector.objects.get(
93-
workflow_condition_group=obj.condition_group, status=ObjectStatus.ACTIVE
94-
)
90+
detector = Detector.objects.get(workflow_condition_group=obj.condition_group)
9591

9692
alert_rule_trigger_id = DataConditionAlertRuleTrigger.objects.values_list(
9793
"alert_rule_trigger_id", flat=True

src/sentry/incidents/subscription_processor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ def process_update(self, subscription_update: QuerySubscriptionUpdate) -> None:
322322
detector = Detector.objects.get(
323323
data_sources__source_id=str(self.subscription.id),
324324
data_sources__type=DATA_SOURCE_SNUBA_QUERY_SUBSCRIPTION,
325-
status=ObjectStatus.ACTIVE,
326325
)
327326
comparison_delta = detector.config.get("comparison_delta")
328327
except Detector.DoesNotExist:

src/sentry/notifications/notification_action/metric_alert_registry/handlers/email_metric_alert_handler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from sentry.constants import ObjectStatus
21
from sentry.incidents.action_handlers import email_users
32
from sentry.incidents.models.incident import TriggerStatus
43
from sentry.incidents.typings.metric_detector import (
@@ -42,9 +41,7 @@ def send_alert(
4241
project: Project,
4342
) -> None:
4443

45-
detector = Detector.objects.get(
46-
id=alert_context.action_identifier_id, status=ObjectStatus.ACTIVE
47-
)
44+
detector = Detector.objects.get(id=alert_context.action_identifier_id)
4845
if not detector:
4946
raise ValueError("Detector not found")
5047

src/sentry/notifications/notification_action/metric_alert_registry/handlers/msteams_metric_alert_handler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from sentry.constants import ObjectStatus
21
from sentry.incidents.models.incident import TriggerStatus
32
from sentry.incidents.typings.metric_detector import (
43
AlertContext,
@@ -35,9 +34,7 @@ def send_alert(
3534

3635
from sentry.integrations.msteams.utils import send_incident_alert_notification
3736

38-
detector = Detector.objects.get(
39-
id=alert_context.action_identifier_id, status=ObjectStatus.ACTIVE
40-
)
37+
detector = Detector.objects.get(id=alert_context.action_identifier_id)
4138
if not detector:
4239
raise ValueError("Detector not found")
4340

src/sentry/notifications/notification_action/metric_alert_registry/handlers/slack_metric_alert_handler.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from sentry.constants import ObjectStatus
21
from sentry.incidents.models.incident import TriggerStatus
32
from sentry.incidents.typings.metric_detector import (
43
AlertContext,
@@ -34,9 +33,7 @@ def send_alert(
3433
project: Project,
3534
) -> None:
3635

37-
detector = Detector.objects.get(
38-
id=alert_context.action_identifier_id, status=ObjectStatus.ACTIVE
39-
)
36+
detector = Detector.objects.get(id=alert_context.action_identifier_id)
4037
if not detector:
4138
raise ValueError("Detector not found")
4239

src/sentry/uptime/models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ def get_active_auto_monitor_count_for_org(organization: Organization) -> int:
254254
ProjectUptimeSubscriptionMode.AUTO_DETECTED_ONBOARDING,
255255
ProjectUptimeSubscriptionMode.AUTO_DETECTED_ACTIVE,
256256
],
257-
status=ObjectStatus.ACTIVE,
258257
).count()
259258

260259

src/sentry/uptime/subscriptions/subscriptions.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ def create_project_uptime_subscription(
190190
type=UptimeDomainCheckFailure.slug,
191191
project__organization=project.organization,
192192
config__mode=ProjectUptimeSubscriptionMode.MANUAL,
193-
status=ObjectStatus.ACTIVE,
194193
).count()
195194
if (
196195
not override_manual_org_limit
@@ -446,7 +445,6 @@ def is_url_auto_monitored_for_project(project: Project, url: str) -> bool:
446445
ProjectUptimeSubscriptionMode.AUTO_DETECTED_ONBOARDING.value,
447446
ProjectUptimeSubscriptionMode.AUTO_DETECTED_ACTIVE.value,
448447
),
449-
status=ObjectStatus.ACTIVE,
450448
)
451449
.select_related("data_sources")
452450
.values_list("data_sources__source_id", flat=True)
@@ -467,7 +465,6 @@ def get_auto_monitored_detectors_for_project(project: Project) -> list[Detector]
467465
ProjectUptimeSubscriptionMode.AUTO_DETECTED_ONBOARDING.value,
468466
ProjectUptimeSubscriptionMode.AUTO_DETECTED_ACTIVE.value,
469467
),
470-
status=ObjectStatus.ACTIVE,
471468
)
472469
)
473470

src/sentry/workflow_engine/endpoints/organization_detector_details.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def convert_args(self, request: Request, detector_id, *args, **kwargs):
6060
args, kwargs = super().convert_args(request, *args, **kwargs)
6161
try:
6262
detector = Detector.objects.select_related("project").get(
63-
id=detector_id, status=ObjectStatus.ACTIVE
63+
id=detector_id,
6464
)
6565
if detector.project.organization_id != kwargs["organization"].id:
6666
raise ResourceDoesNotExist

src/sentry/workflow_engine/endpoints/organization_detector_index.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
RESPONSE_UNAUTHORIZED,
1919
)
2020
from sentry.apidocs.parameters import DetectorParams, GlobalParams, OrganizationParams
21-
from sentry.constants import ObjectStatus
2221
from sentry.db.models.query import in_icontains, in_iexact
2322
from sentry.issues import grouptype
2423
from sentry.models.organization import Organization
@@ -97,7 +96,7 @@ def get(self, request: Request, organization: Organization) -> Response:
9796
Return a list of detectors for a given organization.
9897
"""
9998
projects = self.get_projects(request, organization)
100-
queryset = Detector.objects.filter(project_id__in=projects, status=ObjectStatus.ACTIVE)
99+
queryset = Detector.objects.filter(project_id__in=projects)
101100

102101
if raw_query := request.GET.get("query"):
103102
tokenized_query = tokenize_query(raw_query)

src/sentry/workflow_engine/endpoints/organization_workflow_index.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
RESPONSE_UNAUTHORIZED,
1919
)
2020
from sentry.apidocs.parameters import GlobalParams, OrganizationParams, WorkflowParams
21-
from sentry.constants import ObjectStatus
2221
from sentry.db.models.query import in_icontains, in_iexact
2322
from sentry.search.utils import tokenize_query
2423
from sentry.workflow_engine.endpoints.serializers import WorkflowSerializer
@@ -43,7 +42,7 @@ def convert_args(self, request: Request, workflow_id, *args, **kwargs):
4342
args, kwargs = super().convert_args(request, *args, **kwargs)
4443
try:
4544
kwargs["workflow"] = Workflow.objects.get(
46-
organization=kwargs["organization"], id=workflow_id, status=ObjectStatus.ACTIVE
45+
organization=kwargs["organization"], id=workflow_id
4746
)
4847
except Workflow.DoesNotExist:
4948
raise ResourceDoesNotExist
@@ -81,9 +80,7 @@ def get(self, request, organization):
8180
"""
8281
sort_by = SortByParam.parse(request.GET.get("sortBy", "id"), SORT_COL_MAP)
8382

84-
queryset = Workflow.objects.filter(
85-
organization_id=organization.id, status=ObjectStatus.ACTIVE
86-
)
83+
queryset = Workflow.objects.filter(organization_id=organization.id)
8784

8885
if raw_query := request.GET.get("query"):
8986
tokenized_query = tokenize_query(raw_query)

src/sentry/workflow_engine/endpoints/validators/detector_workflow.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from sentry import audit_log
77
from sentry.api.serializers.rest_framework.base import CamelSnakeSerializer
8-
from sentry.constants import ObjectStatus
98
from sentry.utils.audit import create_audit_entry
109
from sentry.workflow_engine.models.detector import Detector
1110
from sentry.workflow_engine.models.detector_workflow import DetectorWorkflow
@@ -47,14 +46,12 @@ def create(self, validated_data):
4746
detector = Detector.objects.get(
4847
project__organization=self.context["organization"],
4948
id=validated_data["detector_id"],
50-
status=ObjectStatus.ACTIVE,
5149
)
5250
if not can_edit_detector(detector, self.context["request"]):
5351
raise PermissionDenied
5452
workflow = Workflow.objects.get(
5553
organization=self.context["organization"],
5654
id=validated_data["workflow_id"],
57-
status=ObjectStatus.ACTIVE,
5855
)
5956
except (Detector.DoesNotExist, Workflow.DoesNotExist) as e:
6057
raise serializers.ValidationError(str(e))

src/sentry/workflow_engine/migration_helpers/issue_alert_migration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _create_detector_lookup(self) -> Detector:
8888
if self.is_dry_run:
8989
created = True
9090
error_detector = Detector.objects.filter(
91-
type=ErrorGroupType.slug, project=self.project, status=ObjectStatus.ACTIVE
91+
type=ErrorGroupType.slug, project=self.project
9292
).first()
9393
if error_detector:
9494
created = not AlertRuleDetector.objects.filter(

src/sentry/workflow_engine/models/detector.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import builtins
44
import logging
55
from collections.abc import Callable
6-
from typing import TYPE_CHECKING, Any
6+
from typing import TYPE_CHECKING, Any, ClassVar
77

88
from django.conf import settings
99
from django.db import models
@@ -15,6 +15,7 @@
1515
from sentry.constants import ObjectStatus
1616
from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model
1717
from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey
18+
from sentry.db.models.manager.base import BaseManager
1819
from sentry.issues import grouptype
1920
from sentry.issues.grouptype import GroupType
2021
from sentry.models.owner_base import OwnerModel
@@ -27,6 +28,20 @@
2728
logger = logging.getLogger(__name__)
2829

2930

31+
class DetectorManager(BaseManager["Detector"]):
32+
def filter(self, *args, **kwargs):
33+
# Only add the default status if no status is explicitly provided
34+
if "status" not in kwargs:
35+
kwargs["status"] = ObjectStatus.ACTIVE
36+
return super().filter(*args, **kwargs)
37+
38+
def get(self, *args, **kwargs):
39+
# Only add the default status if no status is explicitly provided
40+
if "status" not in kwargs:
41+
kwargs["status"] = ObjectStatus.ACTIVE
42+
return super().get(*args, **kwargs)
43+
44+
3045
@region_silo_model
3146
class Detector(DefaultFieldsModel, OwnerModel, JSONConfigBase):
3247
__relocation_scope__ = RelocationScope.Organization
@@ -63,6 +78,8 @@ class Detector(DefaultFieldsModel, OwnerModel, JSONConfigBase):
6378
# The user that created the detector
6479
created_by_id = HybridCloudForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete="SET_NULL")
6580

81+
objects: ClassVar[DetectorManager] = DetectorManager()
82+
6683
class Meta(OwnerModel.Meta):
6784
constraints = OwnerModel.Meta.constraints
6885

src/sentry/workflow_engine/models/workflow.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import replace
2-
from typing import Any
2+
from typing import Any, ClassVar
33

44
from django.conf import settings
55
from django.db import models
@@ -10,13 +10,28 @@
1010
from sentry.constants import ObjectStatus
1111
from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model, sane_repr
1212
from sentry.db.models.fields.hybrid_cloud_foreign_key import HybridCloudForeignKey
13+
from sentry.db.models.manager.base import BaseManager
1314
from sentry.models.owner_base import OwnerModel
1415
from sentry.workflow_engine.models.data_condition import DataCondition, is_slow_condition
1516
from sentry.workflow_engine.types import WorkflowEventData
1617

1718
from .json_config import JSONConfigBase
1819

1920

21+
class WorkflowManager(BaseManager["Workflow"]):
22+
def filter(self, *args, **kwargs):
23+
# Only add the default status if no status is explicitly provided
24+
if "status" not in kwargs:
25+
kwargs["status"] = ObjectStatus.ACTIVE
26+
return super().filter(*args, **kwargs)
27+
28+
def get(self, *args, **kwargs):
29+
# Only add the default status if no status is explicitly provided
30+
if "status" not in kwargs:
31+
kwargs["status"] = ObjectStatus.ACTIVE
32+
return super().get(*args, **kwargs)
33+
34+
2035
@region_silo_model
2136
class Workflow(DefaultFieldsModel, OwnerModel, JSONConfigBase):
2237
"""
@@ -61,6 +76,8 @@ class Workflow(DefaultFieldsModel, OwnerModel, JSONConfigBase):
6176
"additionalProperties": False,
6277
}
6378

79+
objects: ClassVar[WorkflowManager] = WorkflowManager()
80+
6481
__repr__ = sane_repr("name", "organization_id")
6582

6683
class Meta:

0 commit comments

Comments
 (0)