'Fill the template tool_execution_time.mako with informations:
- Tool name
- Tool average execution time
- last job execution time
- min and max execution time'

@web.expose
def tool_execution_time(self, trans, **kwd):

    user_cutoff = int(kwd.get('user_cutoff', 60))
    sort_by = kwd.get('sort_by', 'tool')
    descending = (1 if (kwd.get('descending', 'desc') == 'desc') else (-1))
    sort_by = (0 if (sort_by == 'tool') else (1 if (sort_by == 'avg') else (2 if (sort_by == 'min') else 3)))
    color = (True if (kwd.get('color', '') == 'True') else False)
    data = {}
    ordered_data = collections.OrderedDict()
    def field_sort(first, second, field):
        (descending if (data[first][field] < data[second][field]) else (- descending))
    sort_functions = [(lambda first, second: ((- descending) if (first.lower() < second.lower()) else descending)), (lambda first, second: field_sort(first, second, 'avg')), (lambda first, second: field_sort(first, second, 'min')), (lambda first, second: field_sort(first, second, 'max'))]
    jobs_times = sa.select((galaxy.model.Job.table.c.tool_id.label('name'), galaxy.model.Job.table.c.create_time.label('create_time'), galaxy.model.Job.table.c.update_time.label('update_time'), (galaxy.model.Job.table.c.update_time - galaxy.model.Job.table.c.create_time)), from_obj=[galaxy.model.Job.table])
    jobs_times = [(name, (create, update, time)) for (name, create, update, time) in jobs_times.execute()]
    for (tool, attr) in jobs_times:
        if (tool not in data):
            data[tool] = {'last': [(attr[1], attr[0])], 'avg': [attr[2]]}
        else:
            data[tool]['last'].append((attr[1], attr[0]))
            data[tool]['avg'].append(attr[2])
    for tool in data:
        data[tool]['min'] = min(data[tool]['avg'])
        data[tool]['max'] = max(data[tool]['avg'])
        last = max(data[tool]['last'])
        data[tool]['last'] = (last[0] - last[1])
        data[tool]['avg'] = (sum(data[tool]['avg'], timedelta()) / len(data[tool]['avg']))
    tools = data.keys()
    if user_cutoff:
        tools = tools[:user_cutoff]
    tools.sort(sort_functions[sort_by])
    for tool in tools:
        ordered_data[tool] = {'min': self.formatted(data[tool]['min'], color), 'max': self.formatted(data[tool]['max'], color), 'avg': self.formatted(data[tool]['avg'], color), 'last': self.formatted(data[tool]['last'], color)}
    return trans.fill_template('/webapps/reports/tool_execution_time.mako', data=ordered_data, descending=descending, user_cutoff=user_cutoff, sort_by=sort_by)
