{#
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
#}

{% extends "airflow/dag.html" %}
{% block title %}Dag Audit Log{% endblock %}

{%- macro sortable_column(display_name, attribute_name) -%}
  {% set curr_ordering_direction = (request.args.get('sorting_direction', 'desc')) %}
  {% set new_ordering_direction = ('asc' if (request.args.get('sorting_key') != attribute_name or curr_ordering_direction == 'desc') else 'desc') %}
  <a href="{{ url_for('Airflow.audit_log',
                       dag_id=dag_id,
                       sorting_key=attribute_name,
                       sorting_direction=new_ordering_direction
                       ) }}"
     class="js-tooltip"
     role="tooltip"
     title="Sort by {{ new_ordering_direction }} {{ attribute_name }}."
  >
    {{ display_name }}

    <span class="material-icons" aria-hidden="true" aria-describedby="sorting-tip-{{ display_name }}">
      {% if curr_ordering_direction == 'desc' and request.args.get('sorting_key') == attribute_name %}
        expand_more
      {% elif curr_ordering_direction == 'asc' and request.args.get('sorting_key') == attribute_name %}
        expand_less
      {% else %}
        unfold_more
      {% endif %}
    </span>
  </a>
{%- endmacro -%}

{% block head_css %}
{{ super() }}
<link href="{{ url_for_asset('dataTables.bootstrap.min.css') }}" rel="stylesheet" type="text/css" >
<link href="{{ url_for_asset('bootstrap-toggle.min.css') }}" rel="stylesheet" type="text/css">
{% endblock %}

{% block content %}
  {{ super() }}
  <h2>Dag Audit Log</h2>
    <h4 style="display: block; padding-top: 10px; padding-bottom: 4px">
      <p>
        This view displays selected events and operations that have been taken on this dag.
        The included and excluded events are set in the Airflow configuration,
        which by default remove view only actions. For a full list of events regarding this DAG, click
        <a href="{{ url_for('LogModelView.list', _flt_3_dag_id=dag_id) }}">here</a>.
      </p>
    </h4>
    <table id="dag_audit_log_table" class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>{{ sortable_column("Time", "dttm") }}</th>
                <th>{{ sortable_column("Task ID", "task_id") }}</th>
                <th>{{ sortable_column("Event", "event") }}</th>
                <th>{{ sortable_column("Logical Date", "execution_date") }}</th>
                <th>Owner
                  <span class="material-icons text-muted js-tooltip" aria-hidden="true" data-original-title="This is the user who triggered the event.">info</span>
                </th>
                <th>Details</th>
            </tr>
        </thead>
        <tbody>
        {% for log in dag_logs %}
            <tr>
              <!--     Time     -->
              <td><time datetime="{{ log.dttm }}">{{ log.dttm }}</time></td>
              <!--     Task Id: task id is None for Dag level events    -->
              <td>{{ log.task_id if log.task_id else None }}</td>
              <!--     Event    -->
              <td>{{ log.event if log.event else None }}</td>
              <!--     Execution Date     -->
              <td>{{ log.execution_date if log.execution_date else None }}</td>
              <!--     By User     -->
              <td>{{ log.owner if log.owner else None }}</td>
              <!--     Details     -->
              <td>{{ log.extra if log.extra else None }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
  <div class="row">
    <div class="col-sm-6">
      {{ paging }}
    </div>
    <div class="col-sm-6 text-right">
      Showing <strong>{{ num_log_from }}-{{ num_log_to }}</strong> of <strong>{{ audit_logs_count }}</strong> Dag Audit Log
    </div>
  </div>
  </div>
  <div id="svg-tooltip" class="tooltip top" style="position: fixed; display: none; opacity: 1; pointer-events: none;">
    <div class="tooltip-arrow"></div>
    <div class="tooltip-inner"></div>
  </div>
{% endblock %}

{% block tail %}
  {{ super() }}
  <script src="{{ url_for_asset('jquery.dataTables.min.js') }}"></script>
  <script src="{{ url_for_asset('dataTables.bootstrap.min.js') }}"></script>
{% endblock %}
