'fill history_and_dataset_type.mako template with:
- the name of history
- the number of dataset foreach type'

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

    message = escape(util.restore_text(kwd.get('message', '')))
    user_cutoff = int(kwd.get('user_cutoff', 60))
    descending = (1 if (kwd.get('descending', 'desc') == 'desc') else (-1))
    user_selection = kwd.get('user_selection', None)
    from_obj = [galaxy.model.Dataset.table, galaxy.model.History.table, galaxy.model.HistoryDatasetAssociation.table]
    if (user_selection is not None):
        from_obj.append(galaxy.model.User.table)
        whereclause = and_((galaxy.model.Dataset.table.c.id == galaxy.model.HistoryDatasetAssociation.table.c.dataset_id), (galaxy.model.History.table.c.id == galaxy.model.HistoryDatasetAssociation.table.c.history_id), (galaxy.model.User.table.c.id == galaxy.model.History.table.c.user_id), (galaxy.model.User.table.c.email == user_selection))
    else:
        whereclause = and_((galaxy.model.Dataset.table.c.id == galaxy.model.HistoryDatasetAssociation.table.c.dataset_id), (galaxy.model.History.table.c.id == galaxy.model.HistoryDatasetAssociation.table.c.history_id))
    histories = sa.select((galaxy.model.Dataset.table.c.state.label('state'), galaxy.model.History.table.c.name.label('name')), from_obj=from_obj, whereclause=whereclause, order_by=['name'])
    data = [((_.name if (_.name is not None) else 'NoNamedHistory'), _.state) for _ in histories.execute()]
    data.sort((lambda first, second: (descending if (first[0].lower() > second[0].lower()) else (- descending))))
    if data:
        (names, status) = zip(*tuple(data))
    else:
        (names, status) = ([], [])
    possible_status = {'ok': 0, 'upload': 1, 'paused': 2, 'queued': 3, 'error': 4, 'discarded': 5}
    number_of_possible_status = (len(possible_status) + 1)
    datas = collections.OrderedDict()
    for (no, name) in enumerate(names):
        if (name not in datas):
            if (user_cutoff > 0):
                if (len(datas) == user_cutoff):
                    break
            datas[name] = (['-'] * number_of_possible_status)
        no_status = possible_status.get(status[no], 6)
        if (datas[name][no_status] == '-'):
            datas[name][no_status] = 0
        datas[name][no_status] += 1
    return trans.fill_template('/webapps/reports/history_and_dataset_type.mako', data=datas, user_cutoff=user_cutoff, descending=descending, message=message)
