'fill tools_and_job_state.mako template with
- the name of the tool
- the number of jobs using this tool in state \'ok\'
- the number of jobs using this tool in error'

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

    message = escape(util.restore_text(kwd.get('message', '')))
    user_cutoff = int(kwd.get('user_cutoff', 60))
    sort_by = kwd.get('sorting', 'Tool')
    sorting = (0 if (sort_by == 'Tool') else (1 if (sort_by == 'ok') else 2))
    descending = (1 if (kwd.get('descending', 'desc') == 'desc') else (-1))
    sort_functions = ((lambda first, second: (descending if (first.lower() > second.lower()) else (- descending))), (lambda first, second: ((- descending) if (tools_and_jobs_ok.get(first, 0) > tools_and_jobs_ok.get(second)) else descending)), (lambda first, second: ((- descending) if (tools_and_jobs_error.get(first, 0) > tools_and_jobs_error.get(second, 0)) else descending)))
    data = collections.OrderedDict()
    tools_and_jobs_ok = sa.select((galaxy.model.Job.table.c.tool_id.label('tool'), sa.func.count(galaxy.model.Job.table.c.id).label('job')), from_obj=[galaxy.model.Job.table], whereclause=(galaxy.model.Job.table.c.state == 'ok'), group_by=['tool'])
    tools_and_jobs_error = sa.select((galaxy.model.Job.table.c.tool_id.label('tool'), sa.func.count(galaxy.model.Job.table.c.id).label('job')), from_obj=[galaxy.model.Job.table], whereclause=(galaxy.model.Job.table.c.state == 'error'), group_by=['tool'])
    tools_and_jobs_ok = dict(list(tools_and_jobs_ok.execute()))
    tools_and_jobs_error = dict(list(tools_and_jobs_error.execute()))
    tools = list((set(tools_and_jobs_ok.keys()) | set(tools_and_jobs_error.keys())))
    tools.sort(sort_functions[sorting])
    for tool in tools:
        data[tool] = (str(tools_and_jobs_ok.get(tool, '-')), str(tools_and_jobs_error.get(tool, '-')))
    return trans.fill_template('/webapps/reports/tools_and_job_state.mako', data=data, user_cutoff=user_cutoff, sorting=sorting, descending=descending, message=message)
