{% extends "crashstats_base.html" %}
{% block page_title %}Query Results - Mozilla Crash Reports{% endblock %}

{% block content %}
<div id="mainbody">

    {% if error %}
    <div id="message" class="error">
        <h2>Uh oh! Something went wrong...</h2>
        <ul>
            {% if error.type == 'exceeded_maximum_date_range' %}
            <li>The maximum query date range you can perform is
                {{ error.data.maximum }} days. Admins may log in to increase
                query date range limits. Query results have been narrowed to
                the default range of {{ error.data.default }} days.
            </li>
            {% elif error and error.type == 'exceeded_maximum_date_range_admin' %}
            <li>The maximum query date range you can perform is
                {{ error.data.maximum }} days. Query results have been narrowed to
                the default range of {{ error.data.default }} days.
            </li>
            {% endif %}
        </ul>
    </div>
    {% endif %}

    <div class="version-nav query_removemargin">
        <form id="searchform" action="{{ url('crashstats:query') }}">
        <fieldset>

        <ul>
        <li><label class="basic" for="product">Product</label>
            <select id="product" name="product" size="8" multiple="multiple" class="primary">
            {% for product in releases %}
                <option value="{{ product }}"{% if (product in params.products) %} selected{% endif %}>{{ product }}</option>
            {% endfor %}
            </select>
        </li>
        <li><label class="basic" for="version">Version: </label>
            <select id="version" name="version" size="8" multiple="multiple" class="primary">
                <option value='ALL:ALL'>All</option>
                <optgroup label="Featured Versions"></optgroup>
                <optgroup label="Older Versions"></optgroup>
            </select>
        </li>
        <li><label class="basic" for="platform">Operating System</label>
            <select id="platform" name="platform" size="8" multiple="multiple" class="primary">
            {% for platform in platforms %}
                {% if platform.display %}
                <option value="{{ platform.code }}"{% if (platform.code in params.platforms) %} selected{% endif %}>{{ platform.name }}</option>
                {% endif %}
            {% endfor %}
            </select>
        </li>
        <li>
            <span class="basic label">
                <a href="#" id="advfiltertoggle" class="not-toggled">Advanced Filters</a>
            </span>

            <div id="advfilter">
                <p class="advanced">
                    <label for="range_value">For the period of </label>
                    <input type="text" id="range_value" name="range_value" value="{{ params.date_range_value }}" size="2">
                    <select id="range_unit" name="range_unit">
                        <option value="hours"{% if params.date_range_unit == 'hours' %} selected{% endif %}>Hours</option>
                        <option value="days"{% if params.date_range_unit == 'days' %} selected{% endif %}>Days</option>
                        <option value="weeks"{% if params.date_range_unit == 'weeks' %} selected{% endif %}>Weeks</option>
                    </select>
                    <span id="dateHelp">
                        <label for="date"> before </label>
                        <input type="text" name="date" id="date" size="20" title="This field must be formatted as MM/DD/YYYY HH:MM:SS" value="{{ params.end_date.strftime('%m/%d/%Y %H:%M:%S') }}">
                        (UTC)
                    </span>
                </p>

                <p class="advanced">
                    <label for="query_type">Stack signature</label>
                    <input type="hidden" name="query_search" value="signature">
                    <select id="query_type" name="query_type">
                        <option value="is_exactly"{% if params.query_type == 'is_exactly' %} selected{% endif%}>is exactly</option>
                        <option value="contains"{% if params.query_type == 'contains' %} selected{% endif%}>contains</option>
                        <option value="starts_with"{% if params.query_type == 'starts_with' %} selected{% endif%}>starts with</option>
                    </select>
                    <input type="text" id="query" name="query" value="{{ params.query or params.signature }}" size="25">
                </p>

                <p class="advanced">
                    <label for="reason">Crash Reason</label>
                    <input type="text" id="reason" name="reason" value="{{ params.reason }}" size="25">
                </p>

                <p class="advanced">
                    <label for="release_channels">Release Channel</label>
                    <input type="text" id="release_channels" name="release_channels" value="{{ params.release_channels }}" size="25">
                </p>

                <p class="advanced">
                    <label for="build_id">Build IDs</label>
                    <input type="text" id="build_id" name="build_id" value="{{ ', '.join(params.build_id or []) }}" size="25">
                </p>

                <p class="advanced">
                    <span class="label">Report Process:</span>
                    <span class="radio-item">
                        <label>
                            <input type="radio" name="process_type" value="any"{% if params.process_type == 'any' %} checked{% endif %}>
                            Any
                        </label>
                    </span>
                    <span class="radio-item">
                        <label>
                            <input type="radio" name="process_type" value="browser"{% if params.process_type == 'browser' %} checked{% endif %}>
                            Browser
                        </label>
                    </span>
                    <span class="radio-item">
                        <label>
                            <input type="radio" name="process_type" value="plugin"{% if params.process_type == 'plugin' %} checked{% endif %}>
                            Plugins
                        </label>
                    </span>
                    <span class="radio-item">
                        <label>
                            <input type="radio" name="process_type" value="content"{% if params.process_type == 'content' %} checked{% endif %}>
                            Content
                        </label>
                    </span>
                </p>

                <p class="advanced">
                    <span class="label">Report Type:</span>
                    <span class="radio-item">
                        <label>
                            <input type="radio" name="hang_type" value="any"{% if params.hang_type == 'any' %} checked{% endif %}>
                            Any
                        </label>
                    </span>
                    <span class="radio-item">
                        <label>
                            <input type="radio" name="hang_type" value="crash"{% if params.hang_type == 'crash' %} checked{% endif %}>
                            Crash
                        </label>
                    </span>
                    <span class="radio-item">
                        <label>
                            <input type="radio" name="hang_type" value="hang"{% if params.hang_type == 'hang' %} checked{% endif %}>
                            Hang
                        </label>
                    </span>
                </p>

                <p id="plugin-inputs" class="advanced">
                    <label for="plugin_field" >Search By Plugin</label>
                    <select id="plugin_field" name="plugin_field" >
                        <option value="filename"{% if params.plugin_field == 'filename' %} selected{% endif %}>Filename</option>
                        <option value="name"{% if params.plugin_field == 'name' %} selected{% endif %}>Name</option>
                    </select>
                    <select id="plugin_query_type" name="plugin_query_type" >
                        <option value="is_exactly"{% if params.plugin_query_type == 'is_exactly' %} selected{% endif%}>is exactly</option>
                        <option value="contains"{% if params.plugin_query_type == 'contains' %} selected{% endif%}>contains</option>
                        <option value="starts_with"{% if params.plugin_query_type == 'starts_with' %} selected{% endif%}>starts with</option>
                    </select>
                    <input type="text" id="plugin_query" name="plugin_query" value="{{ params.plugin_query }}" size="25">
                </p>
            </div><!-- /advfilter -->

            <button id="query_submit" type="submit">Filter Crash Reports</button>

            <div id="query_waiting" class="hidden">
                <p class="advanced">
                    <img src="{{ static('img/loading.png') }}" alt="Loading animation" />
                    <i>processing query, please wait...</i>
                </p>
            </div>
            </li>

            </ul>
            </fieldset>
        </form>
    </div>

    <div id="formspacer"> </div>

    {% if 'hits' in query %}
    <div class="page-heading">
        <h2>Query Results</h2>
    </div>

    <div class="panel">
        <div class="body notitle">
    <p>
    Results within {{ params.date_range_value }} {{ params.date_range_unit }} of {{ params.end_date }}
{%- if params.query %}
    where the crash signature {{ params.query_type|replace('_', ' ') }} '{{ params.query }}'
{%- endif -%}
{% for field in ['product', 'version'] %}
    {%- if params[field + 's'] -%}
        , and the {{ field }} is one of {{ ', '.join(params[field + 's']) }}
    {%- endif -%}
{% endfor %}
{%- if params.platforms_names -%}
    , and the platform is one of {{ ', '.join(params.platforms_names) }}
{%- endif -%}
{%- if params.build_id %}
    for build {{ ', '.join(params.build_id) }}
{%- endif -%}
{%- if params.process_type == 'any' -%}
    , and the crashing process was of any type (including unofficial release channels)
{%- else -%}
    , and the crashing process was a {{ params.process_type }}
    {% if params.process_type == 'plugin' and params.plugin_query %}
        and its {{ params.plugin_field }} {{ params.plugin_query_type|replace('_', ' ') }} {{ params.plugin_query }}
    {%- endif -%}
{%- endif -%}.</p>

    {% if query.total > 0 %}
    {{ pagination(query, current_url, current_page) }}

    <table id="signatureList" class="tablesorter data-table">
        <thead>
            <tr>
                <th scope="col">Rank</th>
                <th scope="col">Signature</th>
                {% if params.process_type == 'plugin' %}
                <th scope="col">Plugin Name/Ver</th>
                <th scope="col">Plugin Filename</th>
                {% endif %}
                <th title="Total number of crashes" scope="col">#</th>
                <th scope="col">Win</th>
                <th scope="col">Mac</th>
                <th scope="col">Lin</th>
                <th class="bugzilla_numbers">Bugzilla IDs</th>
            </tr>
        </thead>
        <tbody>
            {% for hit in query.hits %}
            <tr>
                <td>{{ results_offset + loop.index }}</td>
                <td>
                    {% if hit.signature %}
                    <a href="{{ url('crashstats:report_list') }}?signature={{ hit.signature|urlencode }}&amp;{{ report_list_query_string }}#reports">
                        {{ hit.signature }}
                    </a>
                    {% else %}
                        (null signature)
                        <a href='http://code.google.com/p/socorro/wiki/NullOrEmptySignatures' class='inline-help'>Learn More</a>
                    {% endif %}
                    <div class="signature-icons">
                        {% if hit.numhang > 0 %}
                        <img src="{{ static('img/3rdparty/fatcow/stop16x16.png') }}" width="16" height="16" alt="Hanged Crash" title="Hanged Crash" class="hang" />
                        {% endif %}
                        {% if hit.numplugin > 0 %}
                        <img src="{{ static('img/3rdparty/fatcow/brick16x16.png') }}" width="16" height="16" alt="Plugin Crash" title="Plugin Crash" class="plugin" />
                        {% endif %}
                        {% if hit.numcontent > 0 %}
                        <img src="{{ static('img/3rdparty/fatcow/content16x16.png') }}" width="16" height="16" alt="Content Crash" title="Content Crash" class="content" />
                        {% endif %}
                    </div>
                </td>
                {% if params.process_type == 'plugin' %}
                <td>{{ hit.pluginname }} {{ hit.pluginversion }}</td>
                <td>{{ hit.pluginfilename }}</td>
                {% endif %}
                <td>{{ hit.count }}</td>
                <td>{{ hit.is_windows }}</td>
                <td>{{ hit.is_mac }}</td>
                <td>{{ hit.is_linux }}</td>
                <td>
                    {% for bug in hit.bugs %}
                    {{ show_bug_link(bug) }}
                    {% endfor %}
                    <div class="bug_ids_expanded_list">
                        <h3>Bugs for <code>{{ hit.signature }}</code></h3>
                        <ul class="bug_ids_expanded full_bug_ids popup">
                            {% for bug in hit.bugs %}
                            <li>{{ show_bug_link(bug) }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    {{ pagination(query, current_url, current_page) }}

    {% else %}
    <p>No results were found.</p>
    {% endif %}
        </div>
    </div>
    {% endif %}
</div>

{% endblock %}

{% block site_css %}
    {{ super() }}
{% endblock %}

{% block site_js %}
    {{ super() }}

<script type="text/javascript">
var SEARCH_FORM_PARAMS = {{ params_json|safe }};
var PRODUCTS_VERSIONS_MAP = {{ products_json|safe }};
</script>

    {% compress js %}
    <script src="{{ static('crashstats/js/jquery/plugins/jquery.tablesorter.js') }}"></script>
    {% endcompress %}

    {% compress js %}
<script type="text/javascript" src="{{ static('crashstats/js/socorro/query.js') }}"></script>
<script type="text/javascript" src="{{ static('crashstats/js/socorro/bugzilla.js') }}"></script>

    {% endcompress %}
{% endblock %}
