{% macro chart_timeline(timeline, product, period='month') -%}
    <div class="chart-timeline">
        {% if timeline %}
            {% set max_count = timeline.most_common(1)[0][1] %}
            <div class="chart-timeline-bars">
                {% for start_time, count in timeline.items() | sort %}
                    <a href="{{ url_for('overview_page',
                            product_name=product.name,
                            **{
                                'year': start_time.year,
                                'month':start_time.month,
                                'day': start_time.day if period=='day' else None}
                            ) }}"
                       class="chart-timeline-bar {% if start_time[period] == 1 %}new-year{% endif %}"
                       style="height: {{ (count or 0)/(max_count if max_count > 0 else 1)*100 }}%"
                       title="View the {{ count }} datasets for
{{ start_time.day|string + ' ' if period == 'day' else '' }}{{ start_time.month | month_name }} {{ start_time.year }}"
                    ></a>
                {% endfor %}
            </div>
            <div class="chart-timeline-years">
                {% for group in timeline | groupby('year') %}
                    <span class="chart-timeline-year">{{ group.grouper }}</span>
                {% endfor %}
            </div>
        {% endif %}
    </div>
{%- endmacro %}

{% macro query_param_list(d, prefix="", wide=false, show_nulls=true, show_dicts=true, descriptions=None, fallback_dict={}) -%}
    <div class="query-param-list {% if wide %}wide{% endif %}">
        {% for key, value in d | dictsort if (show_dicts or value is not mapping) recursive %}
            {% if show_nulls or (value is not none) or (key in fallback_dict) %}
            <span class="query-param {{ prefix+key | maybe_to_css_class_name(prefix="key-") }}">
                <span class="key"
                      {% if descriptions  and descriptions[key] -%}
                      title="{{ descriptions[key].description}}"
                      {%- endif %}>
                    {{- key -}}
                </span>
                <span class="value">
                    {%- if value is mapping -%}
                        {{- loop(value.items()) -}}
                    {%- elif value is none -%}
                        {%- if key in fallback_dict -%}
                            {% if fallback_dict[key] is not none %}
                                <span class="missing-value" title="Inferred from datasets: not present in product definition">
                                    {{- fallback_dict[key] | query_value -}}
                                </span>
                            {% else %}
                                <span class="missing-value null-value" title="Always empty in dataset samples">•</span>
                            {% endif %}
                        {%- else -%}
                            <span class="null-value" title="Unspecified">•</span>
                        {%- endif -%}
                    {%- else -%}
                        {{- value | query_value -}}
                    {%- endif -%}
                </span>
            </span>
            {% endif %}
        {% endfor %}
    </div>
{%- endmacro %}




{% macro _show_raw_keyval(value) -%}
    {%- if value is string or value is number -%}
        <span class="value">{{ value }}</span>
    {%- elif value is mapping -%}
        {%- for key, val in value.items() %}
            <div class="property">
                <span class="key">{{ key }}:</span>
                {{ _show_raw_keyval(val) }}
            </div>
        {%- endfor %}
    {# A string is an iterable, so we check for it above #}
    {%- elif value is iterable -%}
        {# If it's a long list, make is expandable/collapsable #}
        {% if value | length > 20 %}
            <details class="array">[
                <summary><span>[{{ value | length }} items...]</span></summary>
            {%- for item in value %}
                <div class="array-item">{{ _show_raw_keyval(item) }},</div>
            {% endfor %}
            ]
            </details>
        {% else %}
            <span class="array">[
            {%- for item in value %}
                <div class="array-item">{{ _show_raw_keyval(item) }}</div>
            {% endfor %}
            ]</span>
        {% endif %}
    {%- else -%}
        <span class="value unknown">{{ value | torapidjson}}</span>
    {%- endif -%}
{%- endmacro %}



{% macro show_raw_document(doc) -%}
    <div class="raw-metadata">
        {{ _show_raw_keyval(doc) }}
    </div>
{%- endmacro %}
