{% extends 'nautobot_ssot/sync_header.html' %}
{% load buttons %}
{% load plugins %}
{% load shorter_timedelta %}
{% load render_diff %}
{% load humanize_bytes %}


{% block content %}
    <style>
        li.diff-added { list-style-type: "+ "; color: #3c763d; background-color: #dff0d8;}
        li.diff-subtracted { list-style-type: "- "; color: #a94442; background-color: #f2dede;}
        li.diff-changed { list-style-type: "! "; color: #8a6d3b; background-color: #fcf8e3;}
        li.diff-unchanged { list-style-type: circle; color: #333; background-color: white;}
    </style>
    <div class="row">
        <div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Sync</strong>
                </div>
                <table class="table table-hover panel-body attr-table">
                    <tr>
                        <td>Data Source</td>
                        <td>
                            {% if object.get_source_url %}
                                <a href="{{ object.get_source_url }}">{{ object.source }}</a>
                            {% else %}
                                {{ object.source }}
                            {% endif %}
                        </td>
                    </tr>
                    <tr>
                        <td>Data Target</td>
                        <td>
                            {% if object.get_target_url %}
                                <a href="{{ object.get_target_url }}">{{ object.target }}</a>
                            {% else %}
                                {{ object.target }}
                            {% endif %}
                        </td>
                    </tr>
                    <tr>
                        <td>Type</td>
                        <td>
                            {% if object.dry_run %}
                                <span class="dry_run label label-default">Dry Run</span>
                            {% else %}
                                <span class="dry_run label label-info">Sync</span>
                            {% endif %}
                        </td>
                    </tr>
                    <tr>
                        <td>Start Time</td>
                        <td>{{ object.start_time }} <span class="text-muted">({{ object.start_time | timesince }} ago)</span></td>
                    </tr>
                    <tr>
                        <td>End Time</td>
                        <td>{{ object.job_result.completed }} <span class="text-muted">({{ object.job_result.completed | timesince }} ago)</span></td>
                    </tr>
                    <tr>
                        <td>Duration</td>
                        <td>
                            {{ object.duration | shorter_timedelta }} total
                            <ul>
                                <li>{{ object.source_load_time | shorter_timedelta }} loading from {{ object.source }}</li>
                                <li>{{ object.target_load_time | shorter_timedelta }} loading from {{ object.target }}</li>
                                <li>{{ object.diff_time | shorter_timedelta }} calculating diffs</li>
                                <li>{{ object.sync_time | shorter_timedelta }} performing sync</li>
                            </ul>
                        </td>
                    </tr>
                    <tr>
                        <td>Status</td>
                        <td>{% include 'extras/inc/job_label.html' with result=object.job_result %}</td>
                    </tr>
                    <tr>
                        <td>Job result</td>
                        <td><a href="{{ object.job_result.get_absolute_url }}">{{ object.job_result.pk }}</a></td>
                    </tr>
                </table>
            </div>
            {% plugin_left_page object %}
        </div>
        <div class="col-md-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Statistics</strong>
                </div>
                <table class="table table-hover panel-body attr-table">
                    <tr>
                        <td>Creates</td>
                        <td>
                            <a class="label label-success" href="{% url 'plugins:nautobot_ssot:sync_logentries' pk=object.pk %}?action=create">
                                {{ object.num_created }}
                            </a>
                        </td>
                    </tr>
                    <tr>
                        <td>Updates</td>
                        <td>
                            <a class="label label-warning" href="{% url 'plugins:nautobot_ssot:sync_logentries' pk=object.pk %}?action=update">
                                {{ object.num_updated }}
                            </a>
                        </td>
                    </tr>
                    <tr>
                        <td>Deletes</td>
                        <td>
                            <a class="label label-danger" href="{% url 'plugins:nautobot_ssot:sync_logentries' pk=object.pk %}?action=delete">
                                {{ object.num_deleted }}
                            </a>
                        </td>
                    </tr>
                    <tr>
                        <td>Failures</td>
                        <td>
                            <a href="{% url 'plugins:nautobot_ssot:sync_logentries' pk=object.pk %}?status=failure">
                                {{ object.num_failed }}
                            </a>
                        </td>
                    </tr>
                    <tr>
                        <td>Errors</td>
                        <td>
                            <a href="{% url 'plugins:nautobot_ssot:sync_logentries' pk=object.pk %}?status=error">
                                {{ object.num_errored }}
                            </a>
                        </td>
                    </tr>
                </table>
            </div>
            {% if object.source_load_memory_final %}
            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Memory Usage Stats</strong>
                </div>
                <table class="table table-hover panel-body attr-table">
                    <tr>
                        <td rowspan="2">Memory used loading {{ object.source }}</td>
                        <td>Final</td>
                        <td>{{ object.source_load_memory_final | humanize_bytes }}</td>
                    </tr>
                    <tr>
                        <td>Peak</td>
                        <td>{{ object.source_load_memory_peak | humanize_bytes }}</td>
                    </tr>
                    <tr>
                        <td rowspan="2">Memory used loading {{ object.target }}</td>
                        <td>Final</td>
                        <td>{{ object.target_load_memory_final | humanize_bytes }}</td>
                    </tr>
                    <tr>
                        <td>Peak</td>
                        <td>{{ object.target_load_memory_peak | humanize_bytes }}</td>
                    </tr>
                    <tr>
                        <td rowspan="2">Memory used calculating diff</td>
                        <td>Final</td>
                        <td>{{ object.diff_memory_final | humanize_bytes }}</td>
                    </tr>
                    <tr>
                        <td>Peak</td>
                        <td>{{ object.diff_memory_peak | humanize_bytes }}</td>
                    </tr>
                    {% if object.sync_memory_final %}
                    <tr>
                        <td rowspan="2">Memory used syncing</td>
                        <td>Final</td>
                        <td>{{ object.sync_memory_final | humanize_bytes }}</td>
                    </tr>
                    <tr>
                        <td>Peak</td>
                        <td>{{ object.sync_memory_peak | humanize_bytes }}</td>
                    </tr>
                    {% endif %}
                </table>
            </div>
            {% endif %}
            {% include 'inc/custom_fields_panel.html' %}
            {% include 'inc/relationships_panel.html' %}
            {% plugin_right_page object %}
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <strong>Diff</strong>
                </div>
                <div class="panel-body">
                    {% render_diff object.diff %}
                </div>
            </div>
        </div>
        {% plugin_full_width_page object %}
    </div>
{% endblock %}
