'Return a dictionary keyed to possible dataset states and valued with the number
of datasets in this history that have those states.'

def serialize_state_counts(self, history, key, exclude_deleted=True, exclude_hidden=False, **context):

    state_counts = {}
    for state in model.Dataset.states.values():
        state_counts[state] = 0
    for hda in history.datasets:
        if (exclude_deleted and hda.deleted):
            continue
        if (exclude_hidden and (not hda.visible)):
            continue
        state_counts[hda.state] = (state_counts[hda.state] + 1)
    return state_counts
