Skip to content

Commit

Permalink
updated observation end update to also cancel overlapping non rapid r…
Browse files Browse the repository at this point in the history
…esponse blocks when end time is extended
  • Loading branch information
Jon committed Aug 30, 2019
1 parent e212530 commit 7667e84
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
20 changes: 20 additions & 0 deletions observation_portal/observations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from observation_portal.requestgroups.models import RequestGroup, AcquisitionConfig, GuidingConfig, Target
from observation_portal.proposals.models import Proposal

import logging

logger = logging.getLogger()


class SummarySerializer(serializers.ModelSerializer):
class Meta:
Expand Down Expand Up @@ -332,8 +336,24 @@ def validate(self, data):
def update(self, instance, validated_data):
if validated_data['end'] > instance.start:
# Only update the end time if it is > start time
old_end_time = instance.end
instance.end = validated_data['end']
instance.save()
# Cancel observations that used to be under this observation
if instance.end > old_end_time:
observations = Observation.objects.filter(
site=instance.site,
enclosure=instance.enclosure,
telescope=instance.telescope,
start__lte=instance.end,
start__gte=old_end_time,
state='PENDING'
).exclude(
request__request_group__observation_type=RequestGroup.RAPID_RESPONSE
)
num_canceled = Observation.cancel(observations)
logger.info(
"updated end time for observation {instance.id} to {instance.end}. Canceled {num_canceled} overlapping observations.")

return instance

Expand Down
45 changes: 40 additions & 5 deletions observation_portal/observations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,15 @@ def setUp(self):
configuration.save()

@staticmethod
def _generate_observation_data(request_id, configuration_id_list, guide_camera_name='xx03'):
def _generate_observation_data(request_id, configuration_id_list, guide_camera_name='xx03',
start="2016-09-05T22:35:39Z", end="2016-09-05T23:35:40Z"):
observation = {
"request": request_id,
"site": "tst",
"enclosure": "domb",
"telescope": "1m0a",
"start": "2016-09-05T22:35:39Z",
"end": "2016-09-05T23:35:40Z",
"start": start,
"end": end,
"configuration_statuses": []
}
for configuration_id in configuration_id_list:
Expand Down Expand Up @@ -950,6 +951,17 @@ class TestUpdateObservationApi(TestObservationApiBase):
def setUp(self):
super().setUp()

def _create_clone_observation(self, observation, start, end):
return mixer.blend(
Observation,
site=observation.site,
enclosure=observation.enclosure,
telescope=observation.telescope,
start=start.replace(tzinfo=timezone.utc),
end=end.replace(tzinfo=timezone.utc),
request=observation.request
)

def test_update_observation_end_time_succeeds(self):
original_end = datetime(2016, 9, 5, 23, 35, 40).replace(tzinfo=timezone.utc)
observation = self._generate_observation_data(
Expand All @@ -965,9 +977,32 @@ def test_update_observation_end_time_succeeds(self):
observation.refresh_from_db()
self.assertEqual(observation.end, new_end)

def test_update_observation_end_time_cancels_overlapping_observations(self):
original_end = datetime(2016, 9, 2, 23, 35, 40).replace(tzinfo=timezone.utc)
self.window.start = datetime(2016, 9, 1, tzinfo=timezone.utc)
self.window.save()
observation = self._generate_observation_data(
self.requestgroup.requests.first().id, [self.requestgroup.requests.first().configurations.first().id],
start="2016-09-02T22:35:39Z",
end="2016-09-02T23:35:40Z"
)
self._create_observation(observation)
observation = Observation.objects.first()
cancel_obs_1 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 35, 41), datetime(2016, 9, 2, 23, 39, 59))
cancel_obs_2 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 40, 0), datetime(2016, 9, 2, 13, 55, 34))
extra_obs_1 = self._create_clone_observation(observation, datetime(2016, 9, 2, 23, 55, 35), datetime(2016, 9, 3, 0, 14, 21))


self.assertEqual(1, 2)
new_end = datetime(2016, 9, 2, 23, 47, 22).replace(tzinfo=timezone.utc)
update_data = {"end": datetime.strftime(new_end, '%Y-%m-%dT%H:%M:%SZ')}
self.client.patch(reverse('api:observations-detail', args=(observation.id,)), update_data)
observation.refresh_from_db()
self.assertEqual(observation.end, new_end)
cancel_obs_1.refresh_from_db()
self.assertEqual(cancel_obs_1.state, 'CANCELED')
cancel_obs_2.refresh_from_db()
self.assertEqual(cancel_obs_2.state, 'CANCELED')
extra_obs_1.refresh_from_db()
self.assertEqual(extra_obs_1.state, 'PENDING')

def test_update_observation_end_before_start_does_nothing(self):
original_end = datetime(2016, 9, 5, 23, 35, 40).replace(tzinfo=timezone.utc)
Expand Down

0 comments on commit 7667e84

Please sign in to comment.