<%! 
import string
def html_trim(text):
    return "".join([x.strip() for x in text.splitlines()])
%>\
<%page args="job, event, request, options" />\
<%
xslt = '''\
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*" />
    <xsl:preserve-space elements="v sg" />
    <xsl:output method="html" indent="no" />
    <xsl:template match="/">
        <xsl:apply-templates select="v" />
    </xsl:template>
    <xsl:template match="v">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="sg">
        <span>
            <xsl:attribute name="class">
                <xsl:text>t</xsl:text>
                <xsl:if test="@h">
                    <xsl:text> a</xsl:text>
                </xsl:if>
            </xsl:attribute>
            <xsl:apply-templates />
        </span>
    </xsl:template>
</xsl:stylesheet>
'''
%>\

<%def name="event_layout(job, event, request, options, xslt)">
<table>
    <tbody>
        <tr>
            % if options.get('display_row_numbers', False):
            <td class="col1"><%self:event_row_number job="${job}" event="${event}" request="${request}" options="${options}" /></td>
            % endif
            % if options.get('enable_event_actions', False):
            <td class="col2"><%self:event_actions job="${job}" event="${event}" request="${request}" options="${options}" /></td>
            % endif
            <td class="col3">
                <%self:event_time job="${job}" event="${event}" request="${request}" options="${options}" />
            </td>
            % if '_icon' in event:
                <td class="col3"><%self:event_icon job="${job}" event="${event}" request="${request}" options="${options}" /></td>
            % endif
            <td class="col4">\
<%self:event_audit job="${job}" event="${event}" request="${request}" options="${options}" />
                % if not event.raw:
                    <!-- no raw -->
                % else:
<%self:event_raw job="${job}" event="${event}" request="${request}" options="${options}" xslt="${xslt}" />\
<%self:event_show_all job="${job}" event="${event}" request="${request}" options="${options}" />\
                % endif
<%self:event_fields job="${job}" event="${event}" request="${request}" options="${options}" />
            </td>
        </tr>
    </tbody>
</table>
</%def>\
<%def name="event_row_number(job, event, request, options)" filter="html_trim">
    % if options['display_row_numbers']:
        <em class="pos">${event.offset+1}</em>
    % endif
</%def>\
\
<%def name="event_time(job, event, request, options)" filter="html_trim">
    % if hasattr(event,'epoch'):
        <em class="time" s:epoch="${event.epoch}">
        % if event.time:
            % if i18n.current_lang_url_component()=="en-US":
                ${i18n.format_datetime_microseconds(event.get('_time', event.time)).replace(' ', '<br/>', 1).replace(' ', '&nbsp;')}
            % else:
                ${i18n.format_datetime_microseconds(event.get('_time', event.time))}
            % endif
        % endif
        </em>
    % endif
</%def>\
\
<%def name="event_icon(job, event, request, options)" filter="html_trim">
    <em class="icon"><img src="${'/static/app/search/identicons/%d.png' % (int(str(event['_icon']),16) % 1000)}" /></em>
</%def>\
\
<%def name="event_audit(job, event, request, options)" filter="html_trim">\
    <% decorations = {'decoration_audit_valid': _('Valid.'), 'decoration_audit_gap': _('Gap.'), 'decoration_audit_tampered': _('Tampered!'), 'decoration_audit_cantvalidate': _("Can't validate!")} %>
    % for decoration in event.fields.get('_decoration', []):
        % if decorations.has_key(str(decoration)):
            <div><em class="audit ${decoration|h}">${decorations[str(decoration)]|h}</em></div>
        % endif
    % endfor
</%def>\
\
<%def name="event_raw(job, event, request, options, xslt)">\
    % if event.pretty_printed:
<pre class="event">${event.pretty_printed}</pre>
    % elif event.raw.toXml() is None:
<pre class="event">${event.raw | h}</pre>
    % else:
        <% transform = event.raw.toXml(xslt) %>
<pre class="event">${transform.replace('\n', '<br />') if options['replace_newlines'] else transform}</pre>
    % endif
    % if event.json:
<%
lineCountIsInt = str(event.get('linecount','')).isdigit()
qs = dict(count=1, field_list=",".join(request['field_list']), has_layout=0, offset=event.offset, sid=job.sid, segmentation=request['segmentation'])
qs['replace_newlines'] = 1 if options['replace_newlines'] else 0
if request['earliest_time']:
    qs['earliest_time'] = request['earliest_time']
if request['latest_time']:
    qs['latest_time'] = request['latest_time']
qs['display_row_numbers'] = 1 if options['display_row_numbers'] else 0
qs['pretty_print'] = not options['pretty_print']
%>
<a href="${ make_route("/module", host_app=request['host_app'], module='Splunk.Module.EventsViewer', action='render', _qs=qs) }" class="showinline">        
${options['pretty_print'] and _("Show as raw text") or _("Show syntax highlighted")}
    % endif
</a>

</%def>\
\
<%def name="event_show_all(job, event, request, options)" filter="html_trim">
    <%
    lineCountIsInt = str(event.get('linecount','')).isdigit()
    qs = dict(count=1, field_list=",".join(request['field_list']), has_layout=0, offset=event.offset, sid=job.sid, segmentation=request['segmentation'])
    qs['replace_newlines'] = 1 if options['replace_newlines'] else 0
    if request['earliest_time']:
        qs['earliest_time'] = request['earliest_time']
    if request['latest_time']:
        qs['latest_time'] = request['latest_time']
    if request['post_process']:
        qs['post_process'] = request['post_process']
    qs['display_row_numbers'] = 1 if options['display_row_numbers'] else 0
    %>
    % if event.raw.isTruncated is True and request['max_lines'] < max_lines_constraint:
        <%
        qs['max_lines'] = 0
        qs['min_lines'] = request['min_lines']
        qs['max_lines_constraint'] = request['max_lines_constraint']
        qs['truncation_mode'] = 'truncate'
        %>
        <a href="${ make_route("/module", host_app=request['host_app'], module='Splunk.Module.EventsViewer', action='render', _qs=qs) }" class="showinline">        
        % if lineCountIsInt:
            % if int(str(event.get('linecount', max_lines_constraint)))< (max_lines_constraint+1):
                ${_("Show all %s lines") %  event.get('linecount', '')}
            % else:
                ${_("Show most relevant lines (Exceeds %s limit)") % max_lines_constraint}
            % endif:
        % else:
            Show All Lines
        % endif
        </a>
    % elif lineCountIsInt and int(str(event.get('linecount', 0))) > request['min_lines']:
        <%
        qs['max_lines'] = request['min_lines']
        qs['min_lines'] = request['min_lines']
        qs['max_lines_constraint'] = request['max_lines_constraint']
        %>
        <a href="${ make_route("/module", host_app=request['host_app'], module='Splunk.Module.EventsViewer', action='render', _qs=qs) }" class="showinline">${_("Collapse back to %s lines") % request['min_lines']}</a>
    % endif

</%def>\
\
<%def name="event_actions(job, event, request, options)" filter="html_trim">
    <a href="#" class="actions splIconicButton splButton-tertiary" title="${_('Event options menu')}"><span class="splButtonIcon splIconicButtonIcon">&raquo;</span></span></a>
</%def>\
\
<%def name="event_fields(job, event, request, options)" filter="html_trim">
    <ul class="fields">
        <% field_list_items = [x for x in request['field_list'] if event.fields.has_key(x) and x[0]!='_'] %>
        % for field_list_item_index, field_list_item in enumerate(field_list_items):
            % for field_index, field in enumerate(event.fields[field_list_item]):
                <li>
                    <%self:event_field_value_pair field="${field}" name="${field_list_item}" value="${field}" is_highlighted="${field.isHighlighted}" tags="${field.tags}", enable_field_actions="${options.get('enable_field_actions', False)}"/>
                    % if field_list_item_index+1!=len(field_list_items) or field_index+1!=len(event.fields[field_list_item]):
                        |&nbsp;&nbsp;
                    % endif
                </li>
            % endfor
        % endfor
    </ul>
</%def>\
\
<%def name="event_field_value_pair(field, name, value, is_highlighted, tags, enable_field_actions)" filter="html_trim">
    <em class="k">${name | h}</em>=<em class="${'v h' if is_highlighted else 'v'}">${value | h}</em>&nbsp;
    <%self:event_field_value_pair_tags tags="${tags}"/>\
    % if enable_field_actions:    
    <%self:event_field_value_pair_actions field="${field}"/>\
    % endif
</%def>\
\
<%def name="event_field_value_pair_tags(tags)" filter="html_trim">
    % for tag in tags:
        <em class="tg">${tag | h}</em>&nbsp;
    % endfor
</%def>\
\
<%def name="event_field_value_pair_actions(field)" filter="html_trim">
    <a href="#" class="fm">${_("Options")}</a>
</%def>\
\
<%self:event_layout job="${job}" event="${event}" request="${request}" options="${options}" xslt="${xslt}"/>
