예제 #1
0
def get_week_overlaps(date, resources_order):
    """get a data structure which summarizes the overlaping if events in a resource group. This is used for rendering initially and should be returned as json in order to dynamically re-render 
    """
    week_starts = date - timedelta(days=date.weekday())
    day = week_starts
    week_ends = week_starts + timedelta(days=7)
    rusages = list(RUsage.select(AND(IN(RUsage.q.resourceID, resources_order),
                                     RUsage.q.start>=week_starts,
                                     RUsage.q.end_time<=week_ends)).orderBy('start'))
    conflicting_rusages = []
    for res_id in resources_order:
        conflicting_rusages += [AttrDict(id=str(conflict.id)+'-'+str(res_id),
                                         start = conflict.start,
                                         end_time = conflict.end_time) for conflict in unavailable_for_booking(res_id, week_starts, week_ends, ignore_current_res=True)]
        
        
    rusages += conflicting_rusages
    rusages.sort(lambda x,y:cmp(x.start, y.start))
    rusages = list(rusages)
    overlaps_and_index = {}
    current = 0
    while day<=week_ends:
        day = day + timedelta(days=1)
        position_indices = []
        while current<len(rusages) and rusages[current].start<=day:
            for rusage in position_indices:
                if rusage and rusage.end_time <= rusages[current].start:
                    position_indices[position_indices.index(rusage)] = None
            try:
                position_indices[position_indices.index(None)] = rusages[current]
            except ValueError:
                position_indices.append(rusages[current])

            index = position_indices.index(rusages[current])
            look_ahead = 0
            overlaps = 1
            current_overlaps = []
            while look_ahead<len(rusages) and rusages[look_ahead].start<rusages[current].end_time:
                if rusages[look_ahead].end_time<=rusages[current].start:
                    look_ahead += 1
                    continue
                if look_ahead==current:
                    look_ahead += 1
                    continue
                #the look_ahead  current and ends after the current starts => we must add it to the current_overlaps list
                current_overlaps.append(rusages[look_ahead])
                to_remove = []
                for overlap in current_overlaps:
                    if overlap.end_time <= rusages[look_ahead].start:
                        to_remove.append(overlap)
                for remove in to_remove:
                    current_overlaps.remove(remove)
                overlaps = max(overlaps, len(current_overlaps)+1)
                look_ahead += 1
            overlaps_and_index[rusages[current].id] = (index, overlaps)
            current += 1
    return overlaps_and_index
예제 #2
0
 def update(self, wait_secs=0):
     if self.is_updating():
         print "quitting: update already in progress"
         return
     self.updating = True
     time.sleep(wait_secs)
     print "updating"
     try:
         self.storage = dict ( \
          (loc, list(usages)) for loc, usages in sortAndGrpby((ru for ru in (RUsageCache(ru) for ru in RUsage.select())), lambda ru: ru.resource.place.id) )
         self.last_updated = datetime.datetime.now()
     except Exception, err:
         applogger.error("reportutils: error updating cache %s" % err)