-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref(crons): Remove Monitor.is_muted field, make it computed (stage 3) #103567
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Generated by Django 5.2.1 on 2025-11-13 19:47 | ||
|
|
||
| from sentry.new_migrations.migrations import CheckedMigration | ||
| from sentry.new_migrations.monkey.fields import SafeRemoveField | ||
| from sentry.new_migrations.monkey.state import DeletionAction | ||
|
|
||
|
|
||
| class Migration(CheckedMigration): | ||
| # This flag is used to mark that a migration shouldn't be automatically run in production. | ||
| # This should only be used for operations where it's safe to run the migration after your | ||
| # code has deployed. So this should not be used for most operations that alter the schema | ||
| # of a table. | ||
| # Here are some things that make sense to mark as post deployment: | ||
| # - Large data migrations. Typically we want these to be run manually so that they can be | ||
| # monitored and not block the deploy for a long period of time while they run. | ||
| # - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to | ||
| # run this outside deployments so that we don't block them. Note that while adding an index | ||
| # is a schema change, it's completely safe to run the operation after the code has deployed. | ||
| # Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment | ||
|
|
||
| is_post_deployment = False | ||
|
|
||
| dependencies = [ | ||
| ("monitors", "0011_backfill_monitor_environment_is_muted"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| SafeRemoveField( | ||
| model_name="monitor", | ||
| name="is_muted", | ||
| deletion_action=DeletionAction.MOVE_TO_PENDING, | ||
| ), | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,12 +238,6 @@ class Monitor(Model): | |
| check-in payloads. The slug can be changed. | ||
| """ | ||
|
|
||
| is_muted = models.BooleanField(default=False, db_default=False) | ||
| """ | ||
| Monitor is operating normally but will not produce incidents or produce | ||
| occurrences into the issues platform. | ||
| """ | ||
|
|
||
| name = models.CharField(max_length=128) | ||
| """ | ||
| Human readable name of the monitor. Used for display purposes. | ||
|
|
@@ -439,23 +433,22 @@ def normalize_before_relocation_import( | |
| self.guid = uuid4() | ||
| return old_pk | ||
|
|
||
| def ensure_is_muted(self) -> None: | ||
| @property | ||
| def is_muted(self) -> bool: | ||
|
Comment on lines
+436
to
+437
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want a cached property? Currently this will execute queries each time the property is accessed.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think for now I am OK with a brief N+1, but I'm going to get rid of this property after I think. |
||
| """ | ||
| Dual-write: Sync is_muted from monitor environments back to the monitor. | ||
|
|
||
| Sets Monitor.is_muted based on whether all MonitorEnvironments are muted. | ||
| If there are no environments, is_muted remains unchanged. | ||
| A monitor is considered muted if ALL of its environments are muted. | ||
| If a monitor has no environments, it is considered unmuted. | ||
| """ | ||
| # Count total environments and muted environments | ||
| env_counts = MonitorEnvironment.objects.filter(monitor_id=self.id).aggregate( | ||
| total=models.Count("id"), muted=models.Count("id", filter=Q(is_muted=True)) | ||
| ) | ||
|
|
||
| # Only update if there are environments | ||
| if env_counts["total"] > 0: | ||
| all_muted = env_counts["total"] == env_counts["muted"] | ||
| if self.is_muted != all_muted: | ||
| self.update(is_muted=all_muted) | ||
| # If no environments exist, monitor is not muted | ||
| if env_counts["total"] == 0: | ||
| return False | ||
|
|
||
| # Monitor is muted only if ALL environments are muted | ||
| return env_counts["total"] == env_counts["muted"] | ||
|
|
||
|
|
||
| def check_organization_monitor_limit(organization_id: int) -> None: | ||
|
Comment on lines
444
to
454
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: N+1 query occurs when serializing a list of monitors because 🔍 Detailed AnalysisWhen serializing a list of monitors, accessing the 💡 Suggested FixTo resolve the N+1 query, either compute 🤖 Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good 👍🏻