Find the average duration time for the last couple of builds.

    :param connection: txkoji.Connection
    :param package: package name
    :returns: deferred that when fired returns a datetime.timedelta object, or
              None if there were no previous builds for this package.

def average_last_builds(connection, package, limit=5):
    """
    Find the average duration time for the last couple of builds.

    :param connection: txkoji.Connection
    :param package: package name
    :returns: deferred that when fired returns a datetime.timedelta object, or
              None if there were no previous builds for this package.
    """
    # TODO: take branches (targets, or tags, etc) into account when estimating
    # a package's build time.
    state = build_states.COMPLETE
    opts = {'limit': limit, 'order': '-completion_time'}
    builds = yield connection.listBuilds(package, state=state, queryOpts=opts)
    if not builds:
        defer.returnValue(None)
    durations = [build.duration for build in builds]
    average = sum(durations, timedelta()) / len(durations)
    # print('average duration for %s is %s' % (package, average))
    defer.returnValue(average)