def latest_reservations(resources, daterange, reservations='*'): query = Session().query(Reservation) query = query.filter(Reservation.resource.in_(resources.keys())) query = query.filter(Reservation.created > daterange[0]) query = query.filter(Reservation.created <= daterange[1]) query = query.order_by(desc(Reservation.created)) if reservations != '*': query = query.filter(Reservation.token.in_(reservations)) result = utils.OrderedDict() for reservation in query.all(): if reservation.token in result: result[reservation.token].append(reservation) else: result[reservation.token] = [reservation] return result
def fetch_records(resources, year, month): """ Returns the records used for the dataset. """ if not resources.keys(): return [] query = Session().query(Reservation) query = query.filter(Reservation.resource.in_(resources.keys())) if year != 'all': query = query.filter(extract('year', Reservation.start) == int(year)) if month != 'all': query = query.filter(extract('month', Reservation.start) == int(month)) query = query.order_by( Reservation.resource, Reservation.status, Reservation.start, Reservation.email, Reservation.token, ) return query.all()
def monthly_report(year, month, resources, reservations='*'): titles = dict() for uuid in resources.keys(): titles[uuid] = utils.get_resource_title(resources[uuid]) # timezone of the report timezone = settings.timezone() # this order is used for every day in the month ordered_uuids = [i[0] for i in sorted(titles.items(), key=lambda i: i[1])] # build the hierarchical structure of the report data report = utils.OrderedDict() last_day = 28 for d in sorted((d for d in calendar.itermonthdates(year, month))): if not d.month == month: continue day = d.day last_day = max(last_day, day) report[day] = utils.OrderedDict() for uuid in ordered_uuids: report[day][uuid] = dict() report[day][uuid][u'title'] = titles[uuid] report[day][uuid][u'approved'] = list() report[day][uuid][u'pending'] = list() report[day][uuid][u'url'] = resources[uuid].absolute_url() report[day][uuid][u'lists'] = { u'approved': _(u'Approved'), u'pending': _(u'Pending'), } # gather the reservations with as much bulk loading as possible period_start = datetime(year, month, 1, tzinfo=timezone) period_end = datetime(year, month, last_day, tzinfo=timezone) period_end += timedelta(days=1, microseconds=-1) # get a list of relevant allocations in the given period query = Session().query(Allocation) query = query.filter(period_start <= Allocation._start) query = query.filter(Allocation._start <= period_end) query = query.filter(Allocation.resource == Allocation.mirror_of) query = query.filter(Allocation.resource.in_(resources.keys())) allocations = query.all() # quit if there are no allocations at this point if not allocations: return {} # store by group as it will be needed multiple times over later groups = dict() for allocation in allocations: groups.setdefault(allocation.group, list()).append(allocation) # using the groups get the relevant reservations query = Session().query(Reservation) query = query.filter(Reservation.target.in_(groups.keys())) if reservations != '*': query = query.filter(Reservation.token.in_(reservations)) query = query.order_by(Reservation.status) reservations = query.all() @utils.memoize def json_timespans(start, end): return json.dumps([dict(start=start, end=end)]) used_days = dict([(i, False) for i in range(1, 32)]) timezone = settings.timezone() def add_reservation(start, end, reservation): day = start.day used_days[day] = True end += timedelta(microseconds=1) start = sedate.to_timezone(start, timezone=timezone) end = sedate.to_timezone(end, timezone=timezone) start = utils.localize_date(start, time_only=True) end = utils.localize_date(end, time_only=True) context = resources[utils.string_uuid(reservation.resource)] reservation_lists = report[day][utils.string_uuid( reservation.resource )] reservation_lists[reservation.status].append( dict( start=start, end=end, email=reservation.email, data=reservation.data, timespans=json_timespans(start, end), id=reservation.id, token=reservation.token, quota=utils.get_reservation_quota_statement(reservation.quota), resource=context, ) ) for reservation in reservations: if reservation.target_type == u'allocation': add_reservation(reservation.start, reservation.end, reservation) else: for allocation in groups[reservation.target]: add_reservation(allocation.start, allocation.end, reservation) # remove unused days for day in report: if not used_days[day]: del report[day] return report
def monthly_report(year, month, resources, reservations='*'): titles = dict() for uuid in resources.keys(): titles[uuid] = utils.get_resource_title(resources[uuid]) # timezone of the report timezone = settings.timezone() # this order is used for every day in the month ordered_uuids = [i[0] for i in sorted(titles.items(), key=lambda i: i[1])] # build the hierarchical structure of the report data report = utils.OrderedDict() last_day = 28 for d in sorted((d for d in calendar.itermonthdates(year, month))): if not d.month == month: continue day = d.day last_day = max(last_day, day) report[day] = utils.OrderedDict() for uuid in ordered_uuids: report[day][uuid] = dict() report[day][uuid][u'title'] = titles[uuid] report[day][uuid][u'approved'] = list() report[day][uuid][u'pending'] = list() report[day][uuid][u'url'] = resources[uuid].absolute_url() report[day][uuid][u'lists'] = { u'approved': _(u'Approved'), u'pending': _(u'Pending'), } # gather the reservations with as much bulk loading as possible period_start = datetime(year, month, 1, tzinfo=timezone) period_end = datetime(year, month, last_day, tzinfo=timezone) period_end += timedelta(days=1, microseconds=-1) # get a list of relevant allocations in the given period query = Session().query(Allocation) query = query.filter(period_start <= Allocation._start) query = query.filter(Allocation._start <= period_end) query = query.filter(Allocation.resource == Allocation.mirror_of) query = query.filter(Allocation.resource.in_(resources.keys())) allocations = query.all() # quit if there are no allocations at this point if not allocations: return {} # store by group as it will be needed multiple times over later groups = dict() for allocation in allocations: groups.setdefault(allocation.group, list()).append(allocation) # using the groups get the relevant reservations query = Session().query(Reservation) query = query.filter(Reservation.target.in_(groups.keys())) if reservations != '*': query = query.filter(Reservation.token.in_(reservations)) query = query.order_by(Reservation.status) reservations = query.all() @utils.memoize def json_timespans(start, end): return json.dumps([dict(start=start, end=end)]) used_days = dict([(i, False) for i in range(1, 32)]) timezone = settings.timezone() def add_reservation(start, end, reservation): day = start.day used_days[day] = True end += timedelta(microseconds=1) start = modules.calendar.to_timezone(start, timezone=timezone) end = modules.calendar.to_timezone(end, timezone=timezone) start = utils.localize_date(start, time_only=True) end = utils.localize_date(end, time_only=True) context = resources[utils.string_uuid(reservation.resource)] reservation_lists = report[day][utils.string_uuid( reservation.resource )] reservation_lists[reservation.status].append( dict( start=start, end=end, email=reservation.email, data=reservation.data, timespans=json_timespans(start, end), id=reservation.id, token=reservation.token, quota=utils.get_reservation_quota_statement(reservation.quota), resource=context, ) ) for reservation in reservations: if reservation.target_type == u'allocation': add_reservation(reservation.start, reservation.end, reservation) else: for allocation in groups[reservation.target]: add_reservation(allocation.start, allocation.end, reservation) # remove unused days for day in report: if not used_days[day]: del report[day] return report