def test_telescope_availability_combine(self, mock_intervals): mock_intervals.return_value = [(datetime(2016, 9, 30, 18, 30, 0, tzinfo=timezone.utc), datetime(2016, 9, 30, 21, 0, 0, tzinfo=timezone.utc)), (datetime(2016, 10, 1, 18, 30, 0, tzinfo=timezone.utc), datetime(2016, 10, 1, 21, 0, 0, tzinfo=timezone.utc)), (datetime(2016, 10, 2, 18, 30, 0, tzinfo=timezone.utc), datetime(2016, 10, 2, 21, 0, 0, tzinfo=timezone.utc))] start = datetime(2016, 9, 30, tzinfo=timezone.utc) end = datetime(2016, 10, 2, tzinfo=timezone.utc) telescope_availability = get_telescope_availability_per_day(start, end) self.assertIn(self.tk1, telescope_availability) self.assertIn(self.tk2, telescope_availability) combined_telescope_availability = combine_telescope_availabilities_by_site_and_class(telescope_availability) combined_key = TelescopeKey(self.tk1.site, '', self.tk1.telescope[:-1]) self.assertIn(combined_key, combined_telescope_availability) doma_available_time = (datetime(2016, 10, 1, 20, 44, 58) - datetime(2016, 10, 1, 18, 30, 0)).total_seconds() doma_total_time = (datetime(2016, 10, 1, 21, 0, 0) - datetime(2016, 10, 1, 18, 30, 0)).total_seconds() doma_expected_availability = doma_available_time / doma_total_time domb_available_time = (datetime(2016, 10, 1, 19, 24, 59) - datetime(2016, 10, 1, 18, 30, 0)).total_seconds() domb_available_time += (datetime(2016, 10, 1, 20, 44, 58) - datetime(2016, 10, 1, 20, 24, 59)).total_seconds() domb_total_time = (datetime(2016, 10, 1, 21, 0, 0) - datetime(2016, 10, 1, 18, 30, 0)).total_seconds() domb_expected_availability = domb_available_time / domb_total_time total_expected_availability = (doma_expected_availability + domb_expected_availability) / 2.0 self.assertAlmostEqual(total_expected_availability, combined_telescope_availability[combined_key][0][1])
def combine_telescope_availabilities_by_site_and_class( telescope_availabilities): combined_keys = { TelescopeKey(tk.site, '', tk.telescope[:-1]) for tk in telescope_availabilities.keys() } combined_availabilities = {} for key in combined_keys: num_groups = 0 total_availability = [] for telescope_key, availabilities in telescope_availabilities.items(): if telescope_key.site == key.site and telescope_key.telescope[: -1] == key.telescope: num_groups += 1 if not total_availability: total_availability = availabilities else: for i, availability in enumerate(availabilities): total_availability[i][1] += availability[1] for i, availability in enumerate(total_availability): total_availability[i][1] /= num_groups combined_availabilities[key] = total_availability return combined_availabilities
def _telescope(event_source): return TelescopeKey( site=event_source['site'], enclosure=event_source['observatory'], telescope=event_source['telescope'] )
def test_one_telescope_correctness(self): telescope_states = TelescopeStates(self.start, self.end).get() tak = TelescopeKey(site='lsc', enclosure='domb', telescope='1m0a') expected_events = [{'end': datetime(2016, 10, 3, 10, 25, 5, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 1, 0, 0, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 3, 10, 31, 20, tzinfo=timezone.utc), 'event_reason': 'Sequencer: Sequencer unavailable for scheduling', 'event_type': 'SEQUENCER_DISABLED', 'start': datetime(2016, 10, 3, 10, 25, 5, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 3, 16, 47, 42, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 3, 10, 31, 20, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 3, 17, 7, 49, tzinfo=timezone.utc), 'event_reason': 'Site Agent: No update since 2016-10-03T16:37:35', 'event_type': 'SITE_AGENT_UNRESPONSIVE', 'start': datetime(2016, 10, 3, 16, 47, 42, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 3, 23, 35, 58, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 3, 17, 7, 49, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 4, 1, 3, tzinfo=timezone.utc), 'event_reason': 'Weather: Sky transparency too low', 'event_type': 'NOT_OK_TO_OPEN', 'start': datetime(2016, 10, 3, 23, 35, 58, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 4, 1, 20, 46, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 4, 1, 3, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 4, 10, 30, 55, tzinfo=timezone.utc), 'event_reason': 'Weather: Sky transparency too low', 'event_type': 'NOT_OK_TO_OPEN', 'start': datetime(2016, 10, 4, 1, 20, 46, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 4, 21, 47, 6, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 4, 10, 30, 55, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 5, 0, 58, 26, tzinfo=timezone.utc), 'event_reason': 'Sequencer: Sequencer in MANUAL state', 'event_type': 'SEQUENCER_DISABLED', 'start': datetime(2016, 10, 4, 21, 47, 6, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 6, 16, 48, 6, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 5, 0, 58, 26, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 6, 16, 57, 19, tzinfo=timezone.utc), 'event_reason': 'Site Agent: No update since 2016-10-06T16:12:10', 'event_type': 'SITE_AGENT_UNRESPONSIVE', 'start': datetime(2016, 10, 6, 16, 48, 6, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 7, 10, 20, 44, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 6, 16, 57, 19, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 7, 10, 28, 58, tzinfo=timezone.utc), 'event_reason': 'Sequencer: Sequencer unavailable for scheduling', 'event_type': 'SEQUENCER_DISABLED', 'start': datetime(2016, 10, 7, 10, 20, 44, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 8, 10, 20, 25, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 7, 10, 28, 58, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 8, 10, 28, 36, tzinfo=timezone.utc), 'event_reason': 'Sequencer: Sequencer unavailable for scheduling', 'event_type': 'SEQUENCER_DISABLED', 'start': datetime(2016, 10, 8, 10, 20, 25, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}, {'end': datetime(2016, 10, 10, 0, 0, tzinfo=timezone.utc), 'event_reason': 'Available for scheduling', 'event_type': 'AVAILABLE', 'start': datetime(2016, 10, 8, 10, 28, 36, tzinfo=timezone.utc), 'telescope': 'lsc.domb.1m0a'}] # looked in depth at lsc.domb.1m0a in the date range to verify correctness of this # data is available on the telescope_events index of elasticsearch self.assertEqual(telescope_states[tak], expected_events)
def setUp(self): self.configdb_null_patcher = patch('observation_portal.common.configdb.ConfigDB._get_configdb_data') mock_configdb_null = self.configdb_null_patcher.start() mock_configdb_null.return_value = {} self.configdb_patcher = patch('observation_portal.common.configdb.ConfigDB.get_instrument_types_per_telescope') self.mock_configdb = self.configdb_patcher.start() self.mock_configdb.return_value = { TelescopeKey(site='coj', enclosure='clma', telescope='2m0a'): ['2M0-FLOYDS-SCICAM', '2M0-SCICAM-SPECTRAL'], TelescopeKey(site='coj', enclosure='doma', telescope='1m0a'): ['1M0-SCICAM-SINISTRO'], TelescopeKey(site='coj', enclosure='domb', telescope='1m0a'): ['1M0-SCICAM-SINISTRO'], TelescopeKey(site='cpt', enclosure='domb', telescope='1m0a'): ['1M0-SCICAM-SINISTRO'], TelescopeKey(site='cpt', enclosure='domc', telescope='1m0a'): ['1M0-SCICAM-SINISTRO'], TelescopeKey(site='elp', enclosure='doma', telescope='1m0a'): ['1M0-SCICAM-SINISTRO'], TelescopeKey(site='lsc', enclosure='domb', telescope='1m0a'): ['1M0-SCICAM-SINISTRO'], TelescopeKey(site='lsc', enclosure='domc', telescope='1m0a'): ['1M0-SCICAM-SINISTRO'], TelescopeKey(site='ogg', enclosure='clma', telescope='0m4b'): ['0M4-SCICAM-SBIG'], TelescopeKey(site='ogg', enclosure='clma', telescope='2m0a'): ['2M0-FLOYDS-SCICAM'], TelescopeKey(site='sqa', enclosure='doma', telescope='0m8a'): ['0M8-SCICAM-SBIG', '0M8-NRES-SCICAM']} with open('observation_portal/common/test_data/es_telescope_states_data.txt', 'r') as input_file: self.es_output = json.loads(input_file.read()) self.start = datetime(2016, 10, 1, tzinfo=timezone.utc) self.end = datetime(2016, 10, 10, tzinfo=timezone.utc) self.short_end = datetime(2016, 10, 4, tzinfo=timezone.utc) self.es_patcher = patch('observation_portal.common.telescope_states.TelescopeStates._get_es_data') self.mock_es = self.es_patcher.start() self.mock_es.return_value = self.es_output
def setUp(self): super().setUp() self.es_output = [ { '_source': { 'timestamp': '2016-10-01 18:24:58', 'site': 'tst', 'telescope': '1m0a', 'value_string': "", 'observatory': 'doma', } }, { '_source': { 'timestamp': '2016-10-01 19:24:58', 'site': 'tst', 'telescope': '1m0a', 'value_string': "", 'observatory': 'doma', } }, { '_source': { 'timestamp': '2016-10-01 20:24:58', 'site': 'tst', 'telescope': '1m0a', 'value_string': "", 'observatory': 'doma', } }, { '_source': { 'timestamp': '2016-10-01 20:44:58', 'site': 'tst', 'telescope': '1m0a', 'value_string': "Site Agent: Bad Bug", 'observatory': 'doma', } }, { '_source': { 'timestamp': '2016-10-01 18:30:00', 'site': 'tst', 'telescope': '1m0a', 'value_string': "", 'observatory': 'domb', } }, { '_source': { 'timestamp': '2016-10-01 19:24:59', 'site': 'tst', 'telescope': '1m0a', 'value_string': "Sequencer: Unavailable. Enclosure: Interlocked (Power)", 'observatory': 'domb', } }, { '_source': { 'timestamp': '2016-10-01 20:24:59', 'site': 'tst', 'telescope': '1m0a', 'value_string': "", 'observatory': 'domb', } }, { '_source': { 'timestamp': '2016-10-01 20:44:58', 'site': 'tst', 'telescope': '1m0a', 'value_string': "Site Agent: Bad Bug", 'observatory': 'domb', } }, ] self.tk1 = TelescopeKey('tst', 'doma', '1m0a') self.tk2 = TelescopeKey('tst', 'domb', '1m0a') self.es_patcher = patch('observation_portal.common.telescope_states.TelescopeStates._get_es_data') self.mock_es = self.es_patcher.start() self.mock_es.return_value = self.es_output