diff --git a/hc/api/migrations/0062_auto_20190720_1350.py b/hc/api/migrations/0062_auto_20190720_1350.py new file mode 100644 index 00000000..c4e428bb --- /dev/null +++ b/hc/api/migrations/0062_auto_20190720_1350.py @@ -0,0 +1,37 @@ +# Generated by Django 2.2.3 on 2019-07-20 13:50 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0061_webhook_values'), + ] + + operations = [ + migrations.AlterField( + model_name='channel', + name='kind', + field=models.CharField(choices=[('email', 'Email'), ('webhook', 'Webhook'), ('hipchat', 'HipChat'), ('slack', 'Slack'), ('pd', 'PagerDuty'), ('pagertree', 'PagerTree'), ('pagerteam', 'Pager Team'), ('po', 'Pushover'), ('pushbullet', 'Pushbullet'), ('opsgenie', 'OpsGenie'), ('victorops', 'VictorOps'), ('discord', 'Discord'), ('telegram', 'Telegram'), ('sms', 'SMS'), ('zendesk', 'Zendesk'), ('trello', 'Trello'), ('matrix', 'Matrix'), ('whatsapp', 'WhatsApp')], max_length=20), + ), + migrations.AlterField( + model_name='flip', + name='processed', + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AlterField( + model_name='tokenbucket', + name='updated', + field=models.DateTimeField(default=django.utils.timezone.now), + ), + migrations.AddIndex( + model_name='check', + index=models.Index(condition=models.Q(_negated=True, status='down'), fields=['alert_after'], name='api_check_aa_not_down'), + ), + migrations.AddIndex( + model_name='flip', + index=models.Index(condition=models.Q(processed=None), fields=['processed'], name='api_flip_not_processed'), + ), + ] diff --git a/hc/api/models.py b/hc/api/models.py index d7e11e39..32313aa3 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -74,6 +74,17 @@ class Check(models.Model): alert_after = models.DateTimeField(null=True, blank=True, editable=False) status = models.CharField(max_length=6, choices=STATUSES, default="new") + class Meta: + indexes = [ + # Index for the alert_after field. Excludes rows with status=down. + # Used in the sendalerts management command. + models.Index( + fields=["alert_after"], + name="api_check_aa_not_down", + condition=~models.Q(status="down"), + ) + ] + def __str__(self): return "%s (%d)" % (self.name or self.code, self.id) @@ -640,10 +651,21 @@ class Notification(models.Model): class Flip(models.Model): owner = models.ForeignKey(Check, models.CASCADE) created = models.DateTimeField() - processed = models.DateTimeField(null=True, blank=True, db_index=True) + processed = models.DateTimeField(null=True, blank=True) old_status = models.CharField(max_length=8, choices=STATUSES) new_status = models.CharField(max_length=8, choices=STATUSES) + class Meta: + indexes = [ + # For quickly looking up unprocessed flips. + # Used in the sendalerts management command. + models.Index( + fields=["processed"], + name="api_flip_not_processed", + condition=models.Q(processed=None), + ) + ] + def send_alerts(self): if self.new_status == "up" and self.old_status in ("new", "paused"): # Don't send alerts on new->up and paused->up transitions