Comment 2 for bug 1000787

Revision history for this message
James Tunnicliffe (dooferlad) wrote : Re: upcomingwork page fails when there are no work items

Having trouble recreating this in a test or just a local dev instance. It is clear that we could avoid the issue by checking if total_items was 0 and not doing the divide, but it should never be 0 in the first place.

https://staging.launchpad.net/~linaro/+upcomingwork works (currently) so I can't gather extra information from there.

A review of the code seems to show that total_items will always be at least 1, since they are array lengths and those arrays are only created just before the first item is put in them (there is no way out of the function (getWorkItemsDueBefore) that creates those arrays where they can be empty.

Clearly I can just protect the section of code that has the divide by 0 in, probably something like the code below, but with no test and no logical reason for this happening, it feels like the bug lies elsewhere...

class PersonUpcomingWorkView(LaunchpadView):
    """This view displays work items and bugtasks that are due within 60 days
    and are assigned to a person (or participants of of a team).
    """

    # We'll show bugs and work items targeted to milestones with a due date up
    # to DAYS from now.
    DAYS = 180

    def initialize(self):
        super(PersonUpcomingWorkView, self).initialize()
        self.workitem_counts = {}
        self.bugtask_counts = {}
        self.milestones_per_date = {}
        self.progress_per_date = {}
        for date, containers in self.work_item_containers:
            total_items = 0
            total_done = 0
            milestones = set()
            self.bugtask_counts[date] = 0
            self.workitem_counts[date] = 0
            for container in containers:
                total_items += len(container.items)
                if total_items == 0:
                    continue

                total_done += len(container.done_items)
                if isinstance(container, AggregatedBugsContainer):
                    self.bugtask_counts[date] += len(container.items)
                else:
                    self.workitem_counts[date] += len(container.items)
                for item in container.items:
                    milestones.add(item.milestone)
            self.milestones_per_date[date] = sorted(
                milestones, key=attrgetter('displayname'))
            self.progress_per_date[date] = '{0:.0f}'.format(
                100.0 * total_done / float(total_items))