Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-28 07:24:56

0001 """Seed the 'teams' context + the first team, @prodops.
0002 
0003 Teams are Entry rows with kind='team', context='teams', Entry.name='@<team>'.
0004 Entry.content is the whitespace-delimited email list; Entry.title is the
0005 human-readable name. Alarm configs can reference @<team> in their
0006 recipients list; the engine expands at send-time.
0007 
0008 Also updates the alarm_panda_failure_rate_sakib config from a hard-coded
0009 (srahman1,wenaus) pair to ['@prodops'] — same membership, one indirection.
0010 """
0011 from __future__ import annotations
0012 
0013 import time
0014 import uuid
0015 
0016 from django.db import migrations
0017 
0018 
0019 CONTEXT_NAME = 'teams'
0020 
0021 TEAMS = [
0022     {
0023         'name': '@prodops',
0024         'title': 'Production ops',
0025         'content': 'srahman1@bnl.gov wenaus@gmail.com',
0026         'data': {'entry_id': 'team_prodops'},
0027     },
0028 ]
0029 
0030 
0031 def seed(apps, schema_editor):
0032     Entry = apps.get_model('remote_app', 'Entry')
0033     EntryContext = apps.get_model('remote_app', 'EntryContext')
0034     now = time.time()
0035 
0036     ctx, _ = EntryContext.objects.get_or_create(
0037         name=CONTEXT_NAME,
0038         defaults={
0039             'title': 'Teams',
0040             'description': (
0041                 'Named recipient aliases. Referenced from alarm configs '
0042                 'and elsewhere as @<teamname>; resolve at send-time to '
0043                 'the whitespace-delimited email list in Entry.content.'
0044             ),
0045             'timestamp_created': now,
0046             'timestamp_modified': now,
0047         },
0048     )
0049 
0050     for t in TEAMS:
0051         if Entry.objects.filter(context=ctx, kind='team', name=t['name']).exists():
0052             continue
0053         Entry.objects.create(
0054             id=str(uuid.uuid4()),
0055             title=t['title'],
0056             content=t['content'],
0057             kind='team',
0058             context=ctx,
0059             name=t['name'],
0060             data=t['data'],
0061             status='active',
0062             archived=False,
0063             timestamp_created=now,
0064             timestamp_modified=now,
0065         )
0066 
0067     # Retro-update the Sakib alarm to use @prodops instead of hard-coded
0068     # (srahman1, wenaus). Same membership; one indirection to edit later.
0069     sakib_alarm = (Entry.objects
0070                    .filter(context_id='swf-alarms', kind='alarm',
0071                            data__entry_id='alarm_panda_failure_rate_sakib')
0072                    .first())
0073     if sakib_alarm is not None:
0074         data = dict(sakib_alarm.data or {})
0075         if data.get('recipients') != ['@prodops']:
0076             data['recipients'] = ['@prodops']
0077             sakib_alarm.data = data
0078             sakib_alarm.timestamp_modified = now
0079             sakib_alarm.save()
0080 
0081     # Add renotification_window_hours to any existing alarm config that
0082     # doesn't have it (feature added after the initial seed applied).
0083     defaults = {
0084         'alarm_panda_failure_rate_sakib': 24,
0085         'alarm_panda_failure_rate_eic_all': 48,
0086     }
0087     for alarm in Entry.objects.filter(context_id='swf-alarms', kind='alarm'):
0088         data = dict(alarm.data or {})
0089         if 'renotification_window_hours' in data:
0090             continue
0091         eid = data.get('entry_id', '')
0092         data['renotification_window_hours'] = defaults.get(eid, 24)
0093         alarm.data = data
0094         alarm.timestamp_modified = now
0095         alarm.save()
0096 
0097 
0098 def unseed(apps, schema_editor):
0099     Entry = apps.get_model('remote_app', 'Entry')
0100     EntryContext = apps.get_model('remote_app', 'EntryContext')
0101     Entry.objects.filter(context__name=CONTEXT_NAME).delete()
0102     EntryContext.objects.filter(name=CONTEXT_NAME).delete()
0103     # Revert the Sakib alarm's recipients.
0104     sakib_alarm = (Entry.objects
0105                    .filter(context_id='swf-alarms', kind='alarm',
0106                            data__entry_id='alarm_panda_failure_rate_sakib')
0107                    .first())
0108     if sakib_alarm is not None:
0109         data = dict(sakib_alarm.data or {})
0110         if data.get('recipients') == ['@prodops']:
0111             data['recipients'] = ['srahman1@bnl.gov', 'wenaus@gmail.com']
0112             sakib_alarm.data = data
0113             sakib_alarm.save()
0114 
0115 
0116 class Migration(migrations.Migration):
0117     dependencies = [('remote_app', '0002_seed_alarms')]
0118     operations = [migrations.RunPython(seed, unseed)]