Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
The allocation availability calculation is now faster and accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Aug 28, 2015
1 parent 88fb94b commit c09eb96
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
Unreleased
~~~~~~~~~~

- The allocation availability calculation is now faster and accurate.
[href]

- Expired reservation sessions are now automatically removed.
[href]

Expand Down
6 changes: 3 additions & 3 deletions onegov/town/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class AllocationEventInfo(object):

__slots__ = ['allocation', 'availability', 'request', 'translate']

def __init__(self, allocation, request):
def __init__(self, allocation, availability, request):
self.allocation = allocation
self.availability = allocation.availability
self.availability = availability
self.request = request
self.translate = request.translate

Expand Down Expand Up @@ -118,7 +118,7 @@ def event_title(self):
}))
else:
quota = self.allocation.quota
quota_left = self.allocation.quota_left
quota_left = int(quota * self.availability / 100)

if quota == 1:
if quota_left:
Expand Down
29 changes: 25 additions & 4 deletions onegov/town/views/allocation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import morepath

from itertools import groupby
from libres.db.models import Allocation
from libres.modules.errors import LibresError
from operator import attrgetter
from onegov.core.security import Public, Private
from onegov.libres import Resource, ResourceCollection
from onegov.town import TownApp, _, utils
Expand Down Expand Up @@ -30,13 +32,32 @@ def view_allocations_json(self, request):
return []

scheduler = self.get_scheduler(request.app.libres_context)
queries = scheduler.queries

query = scheduler.allocations_in_range(start, end)
# get all allocations (including mirrors), for the availability calculation
query = scheduler.allocations_in_range(start, end, masters_only=False)
query = query.order_by(Allocation._start)

return [
utils.AllocationEventInfo(a, request).as_dict() for a in query.all()
]
allocations = query.all()

# put only return the master allocations
events = []

for key, group in groupby(allocations, key=attrgetter('_start')):
grouped = tuple(group)
availability = queries.availability_by_allocations(grouped)

for allocation in grouped:
if allocation.is_master:
events.append(
utils.AllocationEventInfo(
allocation,
availability,
request
)
)

return [e.as_dict() for e in events]


def get_new_allocation_form_class(resource, request):
Expand Down

0 comments on commit c09eb96

Please sign in to comment.