Process the return from Salt

def returner(ret):
    '''
    Process the return from Salt
    '''

    job_fun = ret['fun']
    job_fun_escaped = job_fun.replace('.', '_')
    job_id = ret['jid']
    job_retcode = ret.get('retcode', 1)
    job_success = True if not job_retcode else False

    options = _get_options(ret)

    if job_fun in options['functions_blacklist']:
        log.info(
            'Won\'t push new data to Elasticsearch, job with jid=%s and '
            'function=%s which is in the user-defined list of ignored '
            'functions', job_id, job_fun
        )
        return
    if ret.get('data', None) is None and ret.get('return') is None:
        log.info(
            'Won\'t push new data to Elasticsearch, job with jid=%s was '
            'not successful', job_id
        )
        return

    # Build the index name
    if options['states_single_index'] and job_fun in STATE_FUNCTIONS:
        index = 'salt-{0}'.format(STATE_FUNCTIONS[job_fun])
    else:
        index = 'salt-{0}'.format(job_fun_escaped)

    if options['index_date']:
        index = '{0}-{1}'.format(index,
            datetime.date.today().strftime('%Y.%m.%d'))

    counts = {}

    # Do some special processing for state returns
    if job_fun in STATE_FUNCTIONS:
        # Init the state counts
        if options['states_count']:
            counts = {
                'suceeded': 0,
                'failed':   0,
            }

        # Prepend each state execution key in ret['data'] with a zero-padded
        # version of the '__run_num__' field allowing the states to be ordered
        # more easily. Change the index to be
        # index to be '<index>-ordered' so as not to clash with the unsorted
        # index data format
        if options['states_order_output'] and isinstance(ret['data'], dict):
            index = '{0}-ordered'.format(index)
            max_chars = len(six.text_type(len(ret['data'])))

            for uid, data in six.iteritems(ret['data']):
                # Skip keys we've already prefixed
                if uid.startswith(tuple('0123456789')):
                    continue

                # Store the function being called as it's a useful key to search
                decoded_uid = uid.split('_|-')
                ret['data'][uid]['_func'] = '{0}.{1}'.format(
                    decoded_uid[0],
                    decoded_uid[-1]
                )

                # Prefix the key with the run order so it can be sorted
                new_uid = '{0}_|-{1}'.format(
                    six.text_type(data['__run_num__']).zfill(max_chars),
                    uid,
                )

                ret['data'][new_uid] = ret['data'].pop(uid)

        # Catch a state output that has failed and where the error message is
        # not in a dict as expected. This prevents elasticsearch from
        # complaining about a mapping error
        elif not isinstance(ret['data'], dict):
            ret['data'] = {job_fun_escaped: {'return': ret['data']}}

        # Need to count state successes and failures
        if options['states_count']:
            for state_data in ret['data'].values():
                if state_data['result'] is False:
                    counts['failed'] += 1
                else:
                    counts['suceeded'] += 1

    # Ensure the index exists
    _ensure_index(index)

    # Build the payload
    class UTC(tzinfo):
        def utcoffset(self, dt):
            return timedelta(0)

        def tzname(self, dt):
            return 'UTC'

        def dst(self, dt):
            return timedelta(0)

    utc = UTC()
    data = {
        '@timestamp': datetime.datetime.now(utc).isoformat(),
        'success': job_success,
        'retcode': job_retcode,
        'minion': ret['id'],
        'fun': job_fun,
        'jid': job_id,
        'counts': counts,
        'data': _convert_keys(ret['data'])
    }

    if options['debug_returner_payload']:
        log.debug('elasicsearch payload: %s', data)

    # Post the payload
    ret = __salt__['elasticsearch.document_create'](index=index,
                                                    doc_type=options['doc_type'],
                                                    body=salt.utils.json.dumps(data))