예제 #1
0
    def setup(self):
        s1 = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 2,
            'type': 'end'
        }])  # 1-2
        s2 = Intervals([{
            'time': 2,
            'type': 'start'
        }, {
            'time': 4,
            'type': 'end'
        }])  # --2--4
        s3 = copy.copy(s1)
        s4 = copy.copy(s1)
        s5 = copy.copy(s2)

        self.r1 = Reservation_v3(1, 1, {'foo': s1})
        self.r2 = Reservation_v3(2, 2, {'bar': s2})
        self.r3 = Reservation_v3(1, 1, {'foo': s3})
        self.r4 = Reservation_v3(1, 1, {'foo': s4})
        self.r5 = Reservation_v3(2, 2, {'bar': s5})

        self.cr1 = CompoundReservation_v2([self.r1])
        self.cr2 = CompoundReservation_v2([self.r3, self.r2], 'and')
        self.cr3 = CompoundReservation_v2([self.r4])
        self.cr4 = CompoundReservation_v2([self.r5])
        self.cr5 = CompoundReservation_v2([self.r4, self.r5], 'oneof')

        self.gpw = {}
        self.gpw['foo'] = [{
            'time': 1,
            'type': 'start'
        }, {
            'time': 5,
            'type': 'end'
        }]

        self.gpw2 = {}
        self.gpw2['foo'] = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 5,
            'type': 'end'
        }], 'free')
        self.gpw2['bar'] = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 5,
            'type': 'end'
        }], 'free')

        self.sched = Scheduler([self.cr1, self.cr2, self.cr3], self.gpw2, [])
        self.sched2 = Scheduler([self.cr1, self.cr4], self.gpw2, [])

        self.sched3 = Scheduler([self.cr5], self.gpw2, [])
 def setup(self):
     s1 = Intervals([{
         'time': 1,
         'type': 'start'
     }, {
         'time': 2,
         'type': 'end'
     }])
     s2 = Intervals([{
         'time': 2,
         'type': 'start'
     }, {
         'time': 4,
         'type': 'end'
     }])
     s3 = Intervals([{
         'time': 2,
         'type': 'start'
     }, {
         'time': 6,
         'type': 'end'
     }])
     self.r1 = Reservation_v3(1, 1, {'foo': s1})
     self.r2 = Reservation_v3(1, 2, {'bar': s2})
     self.r3 = Reservation_v3(2, 1, {'foo': s3})
     self.r4 = Reservation_v3(1, 1, {'foo': s1, 'bar': s2})
     self.cr1 = CompoundReservation_v2([self.r1])
     self.cr2 = CompoundReservation_v2([self.r1, self.r2], 'and')
     self.cr3 = CompoundReservation_v2([self.r1, self.r3], 'oneof')
    def test_schedule_no_available_windows(self):
        s1 = Intervals([{
            'time': 0,
            'type': 'start'
        }, {
            'time': 1000,
            'type': 'end'
        }])
        s2 = Intervals([{
            'time': 0,
            'type': 'start'
        }, {
            'time': 1000,
            'type': 'end'
        }])
        r1 = Reservation_v3(1, 30, {'foo': s1, 'goo': s2})
        cr = CompoundReservation_v2([r1], 'single')
        gpw = {}
        gpw['goo'] = Intervals([{
            'time': 250,
            'type': 'start'
        }, {
            'time': 750,
            'type': 'end'
        }])

        fs = FullScheduler_ortoolkit('GUROBI', [cr], gpw, [], 60, 0.01, False)
        fs.schedule_all()
    def test_construct_global_availability(self):
        tel_name = '1m0a.doma.lsc'
        sem_start = datetime(2012, 10, 1)

        # Resource is available from 3-7
        dt0 = datetime(2013, 3, 22, 3)
        dt1 = datetime(2013, 3, 22, 7)

        dt_resource_int = Intervals([
            {
                'time': dt0,
                'type': 'start'
            },
            {
                'time': dt1,
                'type': 'end'
            },
        ])
        epoch_resource_int = normalise_dt_intervals(dt_resource_int, sem_start)
        resource_windows = {tel_name: epoch_resource_int}

        # Resource is unavailable from 4-5
        dt2 = datetime(2013, 3, 22, 4)
        dt3 = datetime(2013, 3, 22, 5)
        masked_inervals = {
            '1m0a.doma.lsc':
            Intervals([{
                'time': dt2,
                'type': 'start'
            }, {
                'time': dt3,
                'type': 'end'
            }])
        }

        # Expected available intervals after masking are
        # 3-4, 5-7
        received = construct_global_availability(masked_inervals, sem_start,
                                                 resource_windows)
        received_int = received[tel_name]
        timepoints = received_int.toDictList()
        assert_equal(len(timepoints), 4)
        r0 = normalised_epoch_to_datetime(timepoints[0]['time'],
                                          datetime_to_epoch(sem_start))
        r1 = normalised_epoch_to_datetime(timepoints[1]['time'],
                                          datetime_to_epoch(sem_start))
        # r2 = normalised_epoch_to_datetime(timepoints[2]['time'],
        #                                   datetime_to_epoch(sem_start))
        r3 = normalised_epoch_to_datetime(timepoints[3]['time'],
                                          datetime_to_epoch(sem_start))
        assert_equal(r0, dt0)
        assert_equal(r1, dt2)
        assert_equal(r3, dt1)
 def test_running_intervals(self):
     start = datetime.utcnow()
     end = start + timedelta(minutes=10)
     running_r = RunningRequest('1m0a.doma.elp', 1, start, end)
     running_ur = RunningRequestGroup(1, running_r)
     snapshot = ResourceUsageSnapshot(datetime.utcnow(), [running_ur], [])
     assert_equal(Intervals([]),
                  snapshot.running_intervals('1m0a.doma.lsc'))
     assert_equal(
         Intervals([{
             'time': start,
             'type': 'start'
         }, {
             'time': end,
             'type': 'end'
         }]), snapshot.running_intervals('1m0a.doma.elp'))
def get_intervals(observations):
    ''' Create Intervals from given observations '''
    intervals = []
    for observation in observations:
        intervals.append((observation['start'], observation['end']))

    return Intervals(intervals)
    def test_request_intervals_for_multiple_targets_intersected(self):
        request_dict = self.request.as_dict()
        intervals = get_filtered_rise_set_intervals_by_site(request_dict).get('tst', [])
        truth_intervals = [
            (datetime(2016, 10, 1, 0, 0, tzinfo=timezone.utc),
            datetime(2016, 10, 1, 3, 20, 31, 366820, tzinfo=timezone.utc)),
            (datetime(2016, 10, 1, 19, 13, 14, 944205, tzinfo=timezone.utc),
            datetime(2016, 10, 2, 3, 19, 9, 181040, tzinfo=timezone.utc)),
            (datetime(2016, 10, 2, 19, 9, 19, 241762, tzinfo=timezone.utc),
            datetime(2016, 10, 3, 3, 17, 47, 117329, tzinfo=timezone.utc)),
            (datetime(2016, 10, 3, 19, 5, 23, 539011, tzinfo=timezone.utc),
            datetime(2016, 10, 4, 3, 16, 25, 202612, tzinfo=timezone.utc)),
            (datetime(2016, 10, 4, 19, 1, 27, 835928, tzinfo=timezone.utc),
            datetime(2016, 10, 5, 3, 15, 3, 464340, tzinfo=timezone.utc)),
            (datetime(2016, 10, 5, 18, 57, 32, 132481, tzinfo=timezone.utc),
            datetime(2016, 10, 6, 3, 12, 5, 895932, tzinfo=timezone.utc)),
            (datetime(2016, 10, 6, 18, 53, 36, 428629, tzinfo=timezone.utc),
            datetime(2016, 10, 7, 3, 8, 10, 183626, tzinfo=timezone.utc)),
            (datetime(2016, 10, 7, 18, 49, 40, 724307, tzinfo=timezone.utc),
            datetime(2016, 10, 8, 0, 0, tzinfo=timezone.utc))
        ]

        self.assertEqual(intervals, truth_intervals)
        # now create get the intervals for a request with the second target
        configuration2 = deepcopy(request_dict['configurations'][0])
        configuration2['target']['ra'] = 85.0  # change the RA so the target has different visibility
        request_dict2 = deepcopy(request_dict)
        request_dict2['configurations'][0] = configuration2
        intervals2 = get_filtered_rise_set_intervals_by_site(request_dict2).get('tst', [])
        truth_intervals2 = [
            (datetime(2016, 10, 1, 0, 0, tzinfo=timezone.utc), datetime(2016, 10, 1, 3, 20, 31, 366820, tzinfo=timezone.utc)),
            (datetime(2016, 10, 1, 23, 24, 4, 392218, tzinfo=timezone.utc), datetime(2016, 10, 2, 3, 19, 9, 181040, tzinfo=timezone.utc)),
            (datetime(2016, 10, 2, 23, 20, 8, 717423, tzinfo=timezone.utc), datetime(2016, 10, 3, 3, 17, 47, 117329, tzinfo=timezone.utc)),
            (datetime(2016, 10, 3, 23, 16, 13, 42308, tzinfo=timezone.utc), datetime(2016, 10, 4, 3, 16, 25, 202612, tzinfo=timezone.utc)),
            (datetime(2016, 10, 4, 23, 12, 17, 366627, tzinfo=timezone.utc), datetime(2016, 10, 5, 3, 15, 3, 464340, tzinfo=timezone.utc)),
            (datetime(2016, 10, 5, 23, 8, 21, 690204, tzinfo=timezone.utc), datetime(2016, 10, 6, 3, 13, 41, 930536, tzinfo=timezone.utc)),
            (datetime(2016, 10, 6, 23, 4, 26, 12943, tzinfo=timezone.utc), datetime(2016, 10, 7, 3, 12, 20, 629833, tzinfo=timezone.utc)),
            (datetime(2016, 10, 7, 23, 0, 30, 334810, tzinfo=timezone.utc), datetime(2016, 10, 8, 0, 0, tzinfo=timezone.utc)),
        ]
        self.assertEqual(intervals2, truth_intervals2)

        # now get the intervals for both targets combined in the request and verify they are intersected
        request_dict3 = deepcopy(request_dict)
        request_dict3['configurations'].append(configuration2)
        intervals3 = get_filtered_rise_set_intervals_by_site(request_dict3).get('tst', [])
        truth_intervals_combined = Intervals(truth_intervals).intersect([Intervals(truth_intervals2)]).toTupleList()
        self.assertEqual(intervals3, truth_intervals_combined)
 def uncommit_reservation_from_schedule(self, r):
     resource = r.scheduled_resource
     self.schedule_dict[resource].remove(r)
     # remove interval & add back free time
     self.schedule_dict_free[resource].add(r.scheduled_timepoints)
     self.schedule_dict_busy[resource].subtract(Intervals(r.scheduled_timepoints, 'free'))
     self.unscheduled_reservation_list.append(r)
     r.unschedule()
예제 #9
0
def req_window_to_kernel_intervals(windows):
    '''Convert rise_set intervals (a list of (start, end) datetime tuples) to
       kernel Intervals (an object that stores Timepoints).'''

    intervals = []
    for window in windows:
        intervals.append((window.start, window.end))

    return Intervals(intervals)
예제 #10
0
    def test_schedule_5_7_2012(self):
        s1 = Intervals([{'time': 93710, 'type': 'start'},
                        {'time': 114484, 'type': 'end'},
                        {'time': 180058, 'type': 'start'},
                        {'time': 200648, 'type': 'end'}])
        r1 = Reservation_v3(1, 30, {'foo': s1})
        s2 = copy.copy(s1)
        r2 = Reservation_v3(1, 30, {'goo': s2})

        cr = CompoundReservation_v2([r1, r2], 'oneof')
        gpw = {}
        gpw['foo'] = Intervals([{'time': 90000, 'type': 'start'},
                                {'time': 201000, 'type': 'end'}])
        gpw['goo'] = Intervals([{'time': 90000, 'type': 'start'},
                                {'time': 201000, 'type': 'end'}])

        fs = FullScheduler_v6([cr], gpw, [], 60)
        fs.schedule_all()
 def test_remove_from_free_windows_2(self):
     self.r2.remove_from_free_windows(
         Intervals([{
             'time': 3,
             'type': 'start'
         }, {
             'time': 4,
             'type': 'end'
         }]), 'bar')
     assert_equal(self.r2.free_windows_dict['bar'].timepoints, [])
 def test_remove_from_free_windows_1(self):
     self.r1.remove_from_free_windows(
         Intervals([{
             'time': 1,
             'type': 'start'
         }, {
             'time': 2,
             'type': 'end'
         }]), 'foo')
     assert_equal(self.r1.free_windows_dict['foo'].timepoints, [])
    def _running_intervals(self, resource):
        intervals_list = []
        running_rgs = self.running_request_groups_by_resource.get(resource, [])
        for running_rg in running_rgs:
            for running_r in running_rg.running_requests:
                # Only consider the interval running if the request should continue running
                if running_r.should_continue():
                    intervals_list.append((running_r.start, running_r.end))
        intervals = Intervals(intervals_list)

        return intervals
예제 #14
0
def normalise_dt_intervals(dt_intervals, dt_earliest):
    '''Convert datetime Intervals into normalised kernel Intervals.'''

    epoch_earliest = datetime_to_epoch(dt_earliest)

    epoch_timepoints = []
    for tp in dt_intervals.toDictList():
        epoch_time = normalise(datetime_to_epoch(tp['time']), epoch_earliest)
        epoch_timepoints.append({'time': epoch_time, 'type': tp['type']})

    return Intervals(epoch_timepoints)
    def test_schedule_5_7_2012(self):
        s1 = Intervals([{
            'time': 93710,
            'type': 'start'
        }, {
            'time': 114484,
            'type': 'end'
        }, {
            'time': 180058,
            'type': 'start'
        }, {
            'time': 200648,
            'type': 'end'
        }])
        r1 = Reservation_v3(1, 30, {'foo': s1})
        s2 = copy.copy(s1)
        r2 = Reservation_v3(1, 30, {'goo': s2})

        cr = CompoundReservation_v2([r1, r2], 'oneof')
        gpw = {}
        gpw['foo'] = Intervals([{
            'time': 90000,
            'type': 'start'
        }, {
            'time': 201000,
            'type': 'end'
        }])
        gpw['goo'] = Intervals([{
            'time': 90000,
            'type': 'start'
        }, {
            'time': 201000,
            'type': 'end'
        }])
        slice_size_seconds = 300
        fs = FullScheduler_ortoolkit('GUROBI', [cr], gpw, [],
                                     slice_size_seconds, 0.01, False)
        fs.schedule_all()
예제 #16
0
    def commit_reservation_to_schedule(self, r):
        if not r.scheduled:
            print("error: trying to commit unscheduled reservation")
            return
        else:
            interval = Intervals(r.scheduled_timepoints)
            self.schedule_dict[r.scheduled_resource].append(r)

            # add interval & remove free time
            self.schedule_dict_busy[r.scheduled_resource].add(r.scheduled_timepoints)
            self.schedule_dict_free[r.scheduled_resource] = self.schedule_dict_free[r.scheduled_resource].subtract(
                interval)
            # remove from list of unscheduled reservations
            self.unscheduled_reservation_list.remove(r)
 def test_remove_from_free_windows_3(self):
     self.r3.remove_from_free_windows(
         Intervals([{
             'time': 2,
             'type': 'start'
         }, {
             'time': 3,
             'type': 'end'
         }]), 'foo')
     assert_equal(self.r3.free_windows_dict['foo'].timepoints[0]['time'], 3)
     assert_equal(self.r3.free_windows_dict['foo'].timepoints[0]['type'],
                  'start')
     assert_equal(self.r3.free_windows_dict['foo'].timepoints[1]['time'], 6)
     assert_equal(self.r3.free_windows_dict['foo'].timepoints[1]['type'],
                  'end')
예제 #18
0
    def __init__(self, compound_reservation_list,
                 globally_possible_windows_dict,
                 contractual_obligation_list):
        self.compound_reservation_list = compound_reservation_list
        self.contractual_obligation_list = contractual_obligation_list
        # globally_possible_windows_dict is a dictionary mapping:
        # resource -> globally possible windows (Intervals) on that resource. 
        self.globally_possible_windows_dict = globally_possible_windows_dict
        # these dictionaries hold:
        # scheduled reservations
        self.schedule_dict = {}
        # busy intervals
        self.schedule_dict_busy = {}
        # free intervals
        self.schedule_dict_free = {}

        # sanity check: walk through and make sure none of the globally
        # possible windows are empty. Do the iteration over keys, because
        # we're modifying the dict as we go.
        for r in list(globally_possible_windows_dict.keys()):
            if globally_possible_windows_dict[r].is_empty():
                del globally_possible_windows_dict[r]
        # resource_list holds the schedulable resources.
        # possible windows specified by reservations may include
        # resources not on this list, but we cannot schedule them because
        # we do not know their globally possible windows.

        self.resource_list = list(globally_possible_windows_dict.keys())

        for resource in self.resource_list:
            # reservation list
            self.schedule_dict[resource] = []
            # busy intervals
            self.schedule_dict_busy[resource] = Intervals([], 'busy')
        # free intervals
        self.schedule_dict_free = copy.deepcopy(globally_possible_windows_dict)

        self.and_constraints = []
        self.oneof_constraints = []
        self.reservation_list, self.reservation_dict = self.convert_compound_to_simple()
        self.unscheduled_reservation_list = copy.copy(self.reservation_list)

        self.reservations_by_resource_dict = {}
        for resource in self.resource_list:
            self.reservations_by_resource_dict[resource] = []
        for reservation in self.reservation_list:
            for resource in reservation.free_windows_dict.keys():
                self.reservations_by_resource_dict[resource].append(reservation)
    def make_intersection_dict(self):
        timepoints = [
            {
                'time': datetime(2011, 11, 1, 6, 0, 0),
                'type': 'start'
            },
            {
                'time': datetime(2011, 11, 1, 7, 0, 0),
                'type': 'end'
            },
        ]
        intervals = Intervals(timepoints)

        intersection_dict = {'1m0a.doma.coj': intervals}

        return intersection_dict
예제 #20
0
 def check_against_gpw(self, reservation):
     # intersect the free_windows of reservation with the 
     # globally_possible_windows at each resource.
     # if there are no more free_windows in a specific resource, then 
     # remove that resource from the free_windows_dict.
     # if there are NO MORE resources, then return False.
     for resource in list(reservation.free_windows_dict.keys()):
         reservation.free_windows_dict[resource] = reservation.free_windows_dict[resource].intersect(
             [self.globally_possible_windows_dict.get(resource, Intervals([]))])
         reservation.clean_up_free_windows(resource)
         if reservation.free_windows_dict[resource].is_empty():
             del (reservation.free_windows_dict[resource])
     if reservation.free_windows_dict:
         return True
     else:
         return False
예제 #21
0
def intervals_by_site_to_intervalsets_by_telescope(intervals_by_site,
                                                   telescopes):
    ''' Takes in a dictionary of rise_set intervals by sites and a dictionary of telescope details for the request.
        Returns a dictionary by telescopes of rise_set intervals for the request
    '''
    intervalsets_by_telescope = {}
    for telescope in telescopes:
        if telescope not in intervalsets_by_telescope:
            site = telescope.split('.')[2]
            datetime_intervals = []
            for start, end in intervals_by_site[site]:
                datetime_intervals.append({'type': 'start', 'time': start})
                datetime_intervals.append({'type': 'end', 'time': end})
            intervalsets_by_telescope[telescope] = Intervals(
                datetime_intervals)

    return intervalsets_by_telescope
예제 #22
0
    def _order_downtime_by_resource(raw_downtime_intervals):
        ''' Puts the raw downtime interval sets into a dictionary by resource
        '''
        downtime_intervals = {}
        for interval in raw_downtime_intervals:
            resource = '.'.join([interval['telescope'], interval['observatory'], interval['site']])
            if resource not in downtime_intervals:
                downtime_intervals[resource] = []
            start = datetime.strptime(interval['start'], DOWNTIME_DATE_FORMAT).replace(tzinfo=timezone.utc)
            end = datetime.strptime(interval['end'], DOWNTIME_DATE_FORMAT).replace(tzinfo=timezone.utc)
            downtime_intervals[resource].append({'type': 'start', 'time': start})
            downtime_intervals[resource].append({'type': 'end', 'time': end})

        for resource in downtime_intervals:
            downtime_intervals[resource] = Intervals(downtime_intervals[resource])

        return downtime_intervals
예제 #23
0
def get_rise_set_intervals(request_dict, site=''):
    intervals = []
    site = site if site else request_dict['location'].get('site', '')
    telescope_details = configdb.get_telescopes_with_instrument_type_and_location(
        request_dict['molecules'][0]['instrument_name'], site,
        request_dict['location'].get('observatory', ''),
        request_dict['location'].get('telescope', ''))
    if not telescope_details:
        return intervals

    intervals_by_site = get_rise_set_intervals_by_site(request_dict)
    intervalsets_by_telescope = intervals_by_site_to_intervalsets_by_telescope(
        intervals_by_site, telescope_details.keys())
    filtered_intervalsets_by_telescope = filter_out_downtime_from_intervalsets(
        intervalsets_by_telescope)
    filtered_intervalset = Intervals().union(
        filtered_intervalsets_by_telescope.values())
    filtered_intervals = filtered_intervalset.toTupleList()

    return filtered_intervals
예제 #24
0
    def _order_downtime_by_resource_and_instrument_type(
            raw_downtime_intervals):
        ''' Puts the raw downtime interval sets into a dictionary by resource and then by instrument_type or "all"
        '''
        downtime_intervals = {}
        for interval in raw_downtime_intervals:
            resource = '.'.join([
                interval['telescope'], interval['enclosure'], interval['site']
            ])
            if resource not in downtime_intervals:
                downtime_intervals[resource] = {}
            instrument_type = interval['instrument_type'] if interval[
                'instrument_type'] else 'all'
            if instrument_type not in downtime_intervals[resource]:
                downtime_intervals[resource][instrument_type] = []
            start = datetime.strptime(
                interval['start'],
                DOWNTIME_DATE_FORMAT).replace(tzinfo=timezone.utc)
            end = datetime.strptime(
                interval['end'],
                DOWNTIME_DATE_FORMAT).replace(tzinfo=timezone.utc)
            downtime_intervals[resource][instrument_type].append({
                'type': 'start',
                'time': start
            })
            downtime_intervals[resource][instrument_type].append({
                'type': 'end',
                'time': end
            })

        for resource in downtime_intervals:
            for instrument_type, intervals in downtime_intervals[
                    resource].items():
                downtime_intervals[resource][instrument_type] = Intervals(
                    intervals)

        return downtime_intervals
예제 #25
0
def intervals_by_site_to_intervalsets_by_telescope(intervals_by_site: dict,
                                                   telescopes: list) -> dict:
    """Convert rise_set intervals ordered by site to be ordered by telescope

     `telescopes` must be telescope details for the request that the `intervals_by_site` were
     calculated for.

    Parameters:
        intervals_by_site: rise_set intervals ordered by site
        telescopes: Available telescope details for the request
    Returns:
        rise_set intervals ordered by telescope
    """
    intervalsets_by_telescope = {}
    for telescope in telescopes:
        if telescope not in intervalsets_by_telescope:
            site = telescope.split('.')[2]
            datetime_intervals = []
            for start, end in intervals_by_site[site]:
                datetime_intervals.append({'type': 'start', 'time': start})
                datetime_intervals.append({'type': 'end', 'time': end})
            intervalsets_by_telescope[telescope] = Intervals(
                datetime_intervals)
    return intervalsets_by_telescope
def get_rise_set_intervals_by_site(request: dict,
                                   only_schedulable: bool = False) -> dict:
    """Get rise_set intervals by site for a request

    Computes the intervals only if they do not already exist in cache.

    Parameters:
        request: The request for which to get the intervals
    Returns:
        rise_set intervals by site
    """
    site_details = configdb.get_sites_with_instrument_type_and_location(
        instrument_type=request['configurations'][0]['instrument_type'],
        only_schedulable=only_schedulable)
    intervals_by_site = {}
    for site in site_details:
        intervals_by_site[site] = None
        if request.get('id'):
            cache_key = '{}.{}.rsi'.format(request['id'], site)
            intervals_by_site[site] = cache.get(cache_key, None)
        else:
            cache_key = 'rise_set_intervals_' + site + '_' + str(
                hashlib.sha1(
                    json.dumps(request, sort_keys=True,
                               cls=DjangoJSONEncoder).encode()).hexdigest())
            intervals_by_site[site] = caches['locmem'].get(cache_key, None)

        if intervals_by_site[site] is None:
            # There is no cached rise_set intervals for this request and site, so recalculate it now
            intervals_by_site[site] = []
            rise_set_site = get_rise_set_site(site_details[site])
            unique_targets_constraints = set([
                json.dumps(
                    (configuration['target'], configuration['constraints']))
                for configuration in request['configurations']
            ])

            for window in request['windows']:
                visibility = get_rise_set_visibility(rise_set_site,
                                                     window['start'],
                                                     window['end'],
                                                     site_details[site])
                target_intervals = Intervals()
                first_target = True
                for target_constraints in unique_targets_constraints:
                    (target, constraints) = json.loads(target_constraints)
                    rise_set_target = get_rise_set_target(target)
                    try:
                        rs_interval = visibility.get_observable_intervals(
                            rise_set_target,
                            airmass=constraints['max_airmass'],
                            moon_distance=Angle(
                                degrees=constraints['min_lunar_distance']))
                        # We only want times when all targets are visible to keep things simple
                        if first_target:
                            first_target = False
                            target_intervals = Intervals(rs_interval)
                        else:
                            target_intervals = Intervals(
                                rs_interval).intersect([target_intervals])
                    except MovingViolation:
                        pass
                intervals_by_site[site].extend(target_intervals.toTupleList())

            if request.get('id'):
                cache.set(cache_key, intervals_by_site[site],
                          86400 * 30)  # cache for 30 days
            else:
                caches['locmem'].set(cache_key, intervals_by_site[site],
                                     300)  # cache for 5 minutes
    return intervals_by_site
    def setup(self):
        s1 = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 2,
            'type': 'end'
        }])  # 1-2
        s2 = Intervals([{
            'time': 2,
            'type': 'start'
        }, {
            'time': 4,
            'type': 'end'
        }])  # --2--4
        s3 = copy.copy(s1)
        s4 = copy.copy(s1)
        s5 = copy.copy(s2)
        s6 = copy.copy(s1)
        s7 = copy.copy(s1)
        s8 = copy.copy(s1)
        s9 = copy.copy(s2)
        s10 = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }])
        s11 = copy.copy(s10)
        s12 = copy.copy(s10)
        s13 = copy.copy(s10)

        self.r1 = Reservation_v3(1, 1, {'foo': s1})
        self.r2 = Reservation_v3(2, 2, {'bar': s2})
        self.r3 = Reservation_v3(1, 1, {'foo': s3})
        self.r4 = Reservation_v3(1, 1, {'foo': s4})
        self.r5 = Reservation_v3(2, 2, {'bar': s5})
        self.r6 = Reservation_v3(1, 2, {'bar': s5})
        self.r7 = Reservation_v3(1, 1, {'bar': s6, 'foo': s5})
        self.r8 = Reservation_v3(1, 1, {'foo': s6, 'bar': s7})
        self.r9 = Reservation_v3(1, 1, {'foo': s8})
        self.r10 = Reservation_v3(2, 2, {'bar': s9})
        self.r11 = Reservation_v3(1, 1, {'bar': s10})
        self.r12 = Reservation_v3(1, 1, {'bar': s11})
        self.r13 = Reservation_v3(1, 1, {'bar': s12})
        self.r14 = Reservation_v3(1, 1, {'bar': s13})

        self.cr1 = CompoundReservation_v2([self.r1])
        self.cr2 = CompoundReservation_v2([self.r3, self.r2], 'and')
        self.cr3 = CompoundReservation_v2([self.r4])
        self.cr4 = CompoundReservation_v2([self.r5])
        self.cr5 = CompoundReservation_v2([self.r4, self.r5], 'oneof')
        self.cr6 = CompoundReservation_v2([self.r3])
        self.cr7 = CompoundReservation_v2([self.r2])
        self.cr8 = CompoundReservation_v2([self.r4, self.r6], 'oneof')
        self.cr9 = CompoundReservation_v2([self.r4, self.r1, self.r3], 'oneof')
        self.cr10 = CompoundReservation_v2([self.r7])
        self.cr11 = CompoundReservation_v2([self.r8])
        self.cr12 = CompoundReservation_v2([self.r9, self.r10], 'oneof')
        self.cr13 = CompoundReservation_v2([self.r11])
        self.cr14 = CompoundReservation_v2([self.r12])
        self.cr15 = CompoundReservation_v2([self.r13])
        self.cr16 = CompoundReservation_v2([self.r14])

        self.gpw2 = {}
        self.gpw2['foo'] = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')
        self.gpw2['bar'] = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')

        self.gpw3 = {}
        self.gpw3['foo'] = Intervals([{
            'time': 5,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')
        self.gpw3['bar'] = Intervals([{
            'time': 5,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')

        self.gpw4 = {}
        self.gpw4['bar'] = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')

        self.fs1 = FullScheduler_v5([self.cr1, self.cr2, self.cr3], self.gpw2,
                                    [], 1)
        self.fs2 = FullScheduler_v5([self.cr1, self.cr4], self.gpw2, [], 1)
        self.fs3 = FullScheduler_v5([self.cr5], self.gpw2, [], 1)
        self.fs4 = FullScheduler_v5([self.cr8, self.cr6, self.cr7], self.gpw2,
                                    [], 1)
        self.fs5 = FullScheduler_v5([self.cr10, self.cr2, self.cr3], self.gpw2,
                                    [], 1)
        self.fs6 = FullScheduler_v5([self.cr11, self.cr2, self.cr3], self.gpw2,
                                    [], 1)
        self.fs7 = FullScheduler_v5([self.cr12], self.gpw3, [], 1)
        self.fs8 = FullScheduler_v5(
            [self.cr13, self.cr14, self.cr15, self.cr16], self.gpw4, [], 1)
예제 #28
0
def rise_set_to_kernel_intervals(intervals):
    '''Convert rise_set intervals (a list of (start, end) datetime tuples) to
       kernel Intervals (an object that stores Timepoints).'''
    return Intervals(intervals)
예제 #29
0
    def setup(self, algorithm):
        s1 = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 2,
            'type': 'end'
        }])  # 1-2
        s2 = Intervals([{
            'time': 2,
            'type': 'start'
        }, {
            'time': 4,
            'type': 'end'
        }])  # --2--4
        s3 = copy.copy(s1)
        s4 = copy.copy(s1)
        s5 = copy.copy(s2)
        s6 = copy.copy(s1)
        s7 = copy.copy(s1)
        s8 = copy.copy(s1)
        s9 = copy.copy(s2)
        s10 = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }])
        s11 = copy.copy(s10)
        s12 = copy.copy(s10)
        s13 = copy.copy(s10)

        # Priority, Duration, possible_windows_dict
        self.r1 = Reservation_v3(1, 1, {'foo': s1})
        self.r2 = Reservation_v3(2, 2, {'bar': s2})
        self.r3 = Reservation_v3(1, 1, {'foo': s3})
        self.r4 = Reservation_v3(1, 1, {'foo': s4})
        self.r5 = Reservation_v3(2, 2, {'bar': s5})
        self.r6 = Reservation_v3(1, 2, {'bar': s5})
        self.r7 = Reservation_v3(1, 1, {'bar': s6, 'foo': s5})
        self.r8 = Reservation_v3(1, 1, {'foo': s6, 'bar': s7})
        self.r9 = Reservation_v3(1, 1, {'foo': s8})
        self.r10 = Reservation_v3(2, 2, {'bar': s9})
        self.r11 = Reservation_v3(1, 1, {'bar': s10})
        self.r12 = Reservation_v3(1, 1, {'bar': s11})
        self.r13 = Reservation_v3(1, 1, {'bar': s12})
        self.r14 = Reservation_v3(1, 1, {'bar': s13})

        self.r15 = Reservation_v3(1, 9, {'bar': s13})
        self.r16 = Reservation_v3(1, 9, {'foo': s13})
        self.r17 = Reservation_v3(2, 9, {'bar': s13})
        self.r18 = Reservation_v3(2, 9, {'foo': s13})

        self.r19 = Reservation_v3(1, 1, {'bar': s10})
        self.r20 = Reservation_v3(1, 1, {'bar': s10})
        self.r21 = Reservation_v3(1, 1, {'bar': s10})

        self.cr1 = CompoundReservation_v2([self.r1])
        self.cr2 = CompoundReservation_v2([self.r3, self.r2], 'and')
        self.cr3 = CompoundReservation_v2([self.r4])
        self.cr4 = CompoundReservation_v2([self.r5])
        self.cr5 = CompoundReservation_v2([self.r4, self.r5], 'oneof')
        self.cr6 = CompoundReservation_v2([self.r3])
        self.cr7 = CompoundReservation_v2([self.r2])
        self.cr8 = CompoundReservation_v2([self.r4, self.r6], 'oneof')
        self.cr9 = CompoundReservation_v2([self.r4, self.r1, self.r3], 'oneof')
        self.cr10 = CompoundReservation_v2([self.r7])
        self.cr11 = CompoundReservation_v2([self.r8])
        self.cr12 = CompoundReservation_v2([self.r9, self.r10], 'oneof')
        self.cr13 = CompoundReservation_v2([self.r11])
        self.cr14 = CompoundReservation_v2([self.r12])
        self.cr15 = CompoundReservation_v2([self.r13])
        self.cr16 = CompoundReservation_v2([self.r14])

        self.cr17 = CompoundReservation_v2([self.r15, self.r16], 'and')
        self.cr18 = CompoundReservation_v2([self.r17])
        self.cr19 = CompoundReservation_v2([self.r18])

        self.cr20 = CompoundReservation_v2([self.r19])
        self.cr21 = CompoundReservation_v2([self.r20])
        self.cr22 = CompoundReservation_v2([self.r21])

        self.gpw2 = {}
        self.gpw2['foo'] = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')
        self.gpw2['bar'] = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')

        self.gpw3 = {}
        self.gpw3['foo'] = Intervals([{
            'time': 5,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')
        self.gpw3['bar'] = Intervals([{
            'time': 5,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')

        self.gpw4 = {}
        self.gpw4['bar'] = Intervals([{
            'time': 1,
            'type': 'start'
        }, {
            'time': 10,
            'type': 'end'
        }], 'free')

        slice_dict = {}
        slice_dict['foo'] = [0, 1]
        slice_dict['bar'] = [0, 1]
        slice_size_seconds = 1

        self.fs1 = FullScheduler_ortoolkit(algorithm,
                                           [self.cr1, self.cr2, self.cr3],
                                           self.gpw2, [], slice_size_seconds,
                                           0.01, False)
        self.fs2 = FullScheduler_ortoolkit(algorithm, [self.cr1, self.cr4],
                                           self.gpw2, [], slice_size_seconds,
                                           0.01, False)
        self.fs3 = FullScheduler_ortoolkit(algorithm, [self.cr5], self.gpw2,
                                           [], slice_size_seconds, 0.01, False)
        self.fs4 = FullScheduler_ortoolkit(algorithm,
                                           [self.cr8, self.cr6, self.cr7],
                                           self.gpw2, [], slice_size_seconds,
                                           0.01, False)
        self.fs5 = FullScheduler_ortoolkit(algorithm,
                                           [self.cr10, self.cr2, self.cr3],
                                           self.gpw2, [], slice_size_seconds,
                                           0.01, False)
        self.fs6 = FullScheduler_ortoolkit(algorithm,
                                           [self.cr11, self.cr2, self.cr3],
                                           self.gpw2, [], slice_size_seconds,
                                           0.01, False)
        self.fs7 = FullScheduler_ortoolkit(algorithm, [self.cr12], self.gpw3,
                                           [], slice_size_seconds, 0.01, False)
        self.fs8 = FullScheduler_ortoolkit(
            algorithm, [self.cr13, self.cr14, self.cr15, self.cr16], self.gpw4,
            [], slice_size_seconds, 0.01, False)
        self.fs9 = FullScheduler_ortoolkit(algorithm,
                                           [self.cr17, self.cr18, self.cr19],
                                           self.gpw2, [], slice_size_seconds,
                                           0.01, False)
        self.fs10 = FullScheduler_ortoolkit(algorithm,
                                            [self.cr20, self.cr21, self.cr22],
                                            self.gpw2, [], slice_size_seconds,
                                            0.01, False)
 def blocked_intervals(self, resource):
     return self.extra_blocked_intervals.get(resource, Intervals([]))