Skip to content

Commit 464841c

Browse files
author
Jon
committed
Added excluded sites environment variable for HEROIC
1 parent e972323 commit 464841c

4 files changed

Lines changed: 57 additions & 40 deletions

File tree

configdb/hardware/heroic.py

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@ def heroic_site_id(site: Site):
2929
''' Extract a HEROIC id for the site.
3030
This concatenates the observatory.site for human readability
3131
'''
32-
return settings.HEROIC_OBSERVATORY + '.' + site.code
32+
return f"{settings.HEROIC_OBSERVATORY}.{site.code}"
3333

3434

3535
def heroic_telescope_id(telescope: Telescope):
3636
''' Extract a HEROIC id for the telescope
3737
This concatenates the observatory.site.telescope for human readability
3838
'''
39-
return heroic_site_id(telescope.enclosure.site) + '.' + telescope.enclosure.code + '-' + telescope.code
39+
return f"{heroic_site_id(telescope.enclosure.site)}.{telescope.enclosure.code}-{telescope.code}"
4040

4141

4242
def heroic_instrument_id(instrument: Instrument):
4343
''' Extract a HEROIC id for the instrument
4444
This concatenates the observatory.site.telescope.instrument
4545
for human-readability.
4646
'''
47-
return heroic_telescope_id(instrument.telescope) + '.' + instrument.code
47+
return f"{heroic_telescope_id(instrument.telescope)}.{instrument.code}"
4848

4949

5050
def heroic_optical_element_groups(instrument: Instrument):
@@ -128,23 +128,24 @@ def send_to_heroic(api_endpoint: str, payload: dict, update: bool = False):
128128
def create_heroic_instrument(instrument: Instrument):
129129
''' Create a new instrument payload and send it to HEROIC
130130
'''
131-
instrument_payload = {
132-
'id': heroic_instrument_id(instrument),
133-
'name': f"{instrument.instrument_type.name} - {instrument.code}",
134-
'telescope': heroic_telescope_id(instrument.telescope),
135-
'available': True
136-
}
137-
try:
138-
send_to_heroic('instruments/', instrument_payload)
139-
except Exception as e:
140-
logger.error(f'Failed to create heroic instrument {str(instrument)}: {repr(e)}')
131+
if (instrument.telescope.enclosure.site.code not in settings.HEROIC_EXCLUDE_SITES):
132+
instrument_payload = {
133+
'id': heroic_instrument_id(instrument),
134+
'name': f"{instrument.instrument_type.name} - {instrument.code}",
135+
'telescope': heroic_telescope_id(instrument.telescope),
136+
'available': True
137+
}
138+
try:
139+
send_to_heroic('instruments/', instrument_payload)
140+
except Exception as e:
141+
logger.error(f'Failed to create heroic instrument {str(instrument)}: {repr(e)}')
141142

142143

143144
def update_heroic_instrument_capabilities(instrument: Instrument):
144145
''' Send the current instrument capabilities of an instrument to HEROIC
145146
if it is not DISABLED and heroic is set up in settings.py
146147
'''
147-
if can_submit_to_heroic() and instrument.state != 'DISABLED':
148+
if can_submit_to_heroic() and instrument.state != 'DISABLED' and instrument.telescope.enclosure.site.code not in settings.HEROIC_EXCLUDE_SITES:
148149
capabilities = instrument_to_heroic_instrument_capabilities(instrument)
149150
try:
150151
send_to_heroic('instrument-capabilities/', capabilities)
@@ -155,25 +156,27 @@ def update_heroic_instrument_capabilities(instrument: Instrument):
155156
def create_heroic_telescope(telescope: Telescope):
156157
''' Create a new telescope payload and send it to HEROIC
157158
'''
158-
telescope_payload = telescope_to_heroic_telescope_properties(telescope)
159-
telescope_payload['id'] = heroic_telescope_id(telescope)
160-
telescope_payload['status'] = telescope_status_conversion(telescope)
161-
if telescope_payload['status'] != 'SCHEDULABLE':
162-
telescope_payload['reason'] = 'Telescope is currently marked as inactive to prevent usage'
163-
try:
164-
send_to_heroic('telescopes/', telescope_payload)
165-
except Exception as e:
166-
logger.error(f'Failed to create heroic telescope {str(telescope)}: {repr(e)}')
159+
if telescope.enclosure.site.code not in settings.HEROIC_EXCLUDE_SITES:
160+
telescope_payload = telescope_to_heroic_telescope_properties(telescope)
161+
telescope_payload['id'] = heroic_telescope_id(telescope)
162+
telescope_payload['status'] = telescope_status_conversion(telescope)
163+
if telescope_payload['status'] != 'SCHEDULABLE':
164+
telescope_payload['reason'] = 'Telescope is currently marked as inactive to prevent usage'
165+
try:
166+
send_to_heroic('telescopes/', telescope_payload)
167+
except Exception as e:
168+
logger.error(f'Failed to create heroic telescope {str(telescope)}: {repr(e)}')
167169

168170

169171
def update_heroic_telescope_properties(telescope: Telescope):
170172
''' Send updated telescope properties to HEROIC when they change
171173
'''
172-
telescope_update_payload = telescope_to_heroic_telescope_properties(telescope)
173-
try:
174-
send_to_heroic(f'telescopes/{heroic_telescope_id(telescope)}/', telescope_update_payload, update=True)
175-
except Exception as e:
176-
logger.error(f'Failed to update heroic telescope {str(telescope)}: {repr(e)}')
174+
if telescope.enclosure.site.code not in settings.HEROIC_EXCLUDE_SITES:
175+
telescope_update_payload = telescope_to_heroic_telescope_properties(telescope)
176+
try:
177+
send_to_heroic(f'telescopes/{heroic_telescope_id(telescope)}/', telescope_update_payload, update=True)
178+
except Exception as e:
179+
logger.error(f'Failed to update heroic telescope {str(telescope)}: {repr(e)}')
177180

178181

179182
def site_to_heroic_site_properties(site: Site):
@@ -191,19 +194,21 @@ def site_to_heroic_site_properties(site: Site):
191194
def create_heroic_site(site: Site):
192195
''' Create a new site payload and send it to HEROIC
193196
'''
194-
site_payload = site_to_heroic_site_properties(site)
195-
site_payload['id'] = heroic_site_id(site)
196-
try:
197-
send_to_heroic('sites/', site_payload)
198-
except Exception as e:
199-
logger.error(f'Failed to create heroic site {str(site)}: {repr(e)}')
197+
if site.code not in settings.HEROIC_EXCLUDE_SITES:
198+
site_payload = site_to_heroic_site_properties(site)
199+
site_payload['id'] = heroic_site_id(site)
200+
try:
201+
send_to_heroic('sites/', site_payload)
202+
except Exception as e:
203+
logger.error(f'Failed to create heroic site {str(site)}: {repr(e)}')
200204

201205

202206
def update_heroic_site(site: Site):
203207
''' Send updated site properties to HEROIC when they change
204208
'''
205-
site_payload = site_to_heroic_site_properties(site)
206-
try:
207-
send_to_heroic(f'sites/{heroic_site_id(site)}/', site_payload, update=True)
208-
except Exception as e:
209-
logger.error(f'Failed to update heroic site {str(site)}: {repr(e)}')
209+
if site.code not in settings.HEROIC_EXCLUDE_SITES:
210+
site_payload = site_to_heroic_site_properties(site)
211+
try:
212+
send_to_heroic(f'sites/{heroic_site_id(site)}/', site_payload, update=True)
213+
except Exception as e:
214+
logger.error(f'Failed to update heroic site {str(site)}: {repr(e)}')

configdb/hardware/signals/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
''' These signals right now are only used for when HEROIC details are set in the settings
2-
They are used to propogate model information to the HEROIC service
2+
They are used to propagate model information to the HEROIC service
33
'''
44
from django.dispatch import receiver
55
from django.db.models.signals import pre_save

configdb/hardware/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ def test_update_instrument_state_calls_out_to_heroic(self, mock_send):
178178
}
179179
mock_send.assert_called_with('instrument-capabilities/', expected_capabilities)
180180

181+
@override_settings(HEROIC_EXCLUDE_SITES=['tst'])
182+
def test_update_instrument_state_on_excluded_site_does_not_call_out_to_heroic(self, mock_send):
183+
instrument_update = {
184+
'state': Instrument.MANUAL
185+
}
186+
self.client.patch(
187+
reverse('instrument-detail', args=(self.instrument.id,)),
188+
data=instrument_update, format='json'
189+
)
190+
mock_send.assert_not_called()
191+
181192
def test_update_instrument_cameras_calls_out_to_heroic(self, mock_send):
182193
optical_element = mixer.blend(OpticalElement, name='myOE', code='myoe1', schedulable=True)
183194
optical_element_group = mixer.blend(OpticalElementGroup, optical_elements=[optical_element], type='filters')

configdb/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def get_list_from_env(variable, default=None):
163163
HEROIC_API_URL = os.getenv('HEROIC_API_URL', '')
164164
HEROIC_API_TOKEN = os.getenv('HEROIC_API_TOKEN', '')
165165
HEROIC_OBSERVATORY = os.getenv('HEROIC_OBSERVATORY', '')
166+
HEROIC_EXCLUDE_SITES = get_list_from_env('HEROIC_EXCLUDE_SITES', '')
166167

167168
CORS_ORIGIN_ALLOW_ALL = True
168169

0 commit comments

Comments
 (0)