{#
 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 'admin/master.html' %}
{% import 'admin/lib.html' as lib with context %}
{% import 'admin/static.html' as admin_static with context%}
{% import 'admin/model/layout.html' as model_layout with context %}
{% import 'admin/actions.html' as actionlib with context %}

{% block head %}
    {{ super() }}
    {{ lib.form_css() }}
{% endblock %}

{% block body %}
    <h2>DAGs</h2>
    {% block model_menu_bar %}
    <ul class="nav nav-tabs">
        <li class="active">
            <a href="javascript:void(0)">{{ _gettext('List') }} ({{ count }})</a>
        </li>
        {% if admin_view.can_create %}
        <li>
            <a href="{{ get_url('.create_view', url=return_url) }}" title="{{ _gettext('Create new record') }}">{{ _gettext('Create') }}</a>
        </li>
        {% endif %}

        {% if filters %}
        <li class="dropdown">
            {{ model_layout.filter_options() }}
        </li>
        {% endif %}

        {% if actions %}
        <li class="dropdown">
            {{ actionlib.dropdown(actions) }}
        </li>
        {% endif %}

        {% if search_supported %}
        <li>
            {{ model_layout.search_form() }}
        </li>
        {% endif %}
    </ul>
    {% endblock %}

    {% if filters %}
        {{ model_layout.filter_form() }}
        <div class="clearfix"></div>
    {% endif %}

    {% block model_list_table %}
    <table class="table table-striped table-bordered table-hover model-list">
        <thead>
            <tr>
                {% block list_header scoped %}
                    {% if actions %}
                    <th class="list-checkbox-column">
                        <input type="checkbox" name="rowtoggle" class="action-rowtoggle" title="{{ _gettext('Select all records') }}" />
                    </th>
                    {% endif %}
                    {% block list_row_actions_header %}
                    <th class="col-md-1">&nbsp;</th>
                    {% endblock %}
                    {% set column = 0 %}
                    {% for c, name in list_columns %}
                    <th class="column-header">
                        {% if admin_view.is_sortable(c) %}
                            {% if sort_column == column %}
                                <a href="{{ sort_url(column, True) }}" title="{{ _gettext('Sort by %(name)s', name=name) }}">
                                    {{ name }}
                                    {% if sort_desc %}
                                        <span class="glyphicon glyphicon-chevron-up"></span>
                                    {% else %}
                                        <span class="glyphicon glyphicon-chevron-down"></span>
                                    {% endif %}
                                </a>
                            {% else %}
                                <a href="{{ sort_url(column) }}" title="{{ _gettext('Sort by %(name)s', name=name) }}">{{ name }}</a>
                            {% endif %}
                        {% else %}
                            {{ name }}
                        {% endif %}
                        {% if admin_view.column_descriptions.get(c) %}
                            <a class="glyphicon glyphicon-question-sign"
                               title="{{ admin_view.column_descriptions[c] }}"
                               href="javascript:void(0)" data-role="tooltip"
                            ></a>
                        {% endif %}
                    </th>
                    {% set column = column + 1 %}
                    {% endfor %}
                {% endblock %}
                <th style="width:80px;">Statuses</th>
                <th style="width:160px;">Links</th>
            </tr>
        </thead>
        {% for row in data %}
        <tr>
            {% block list_row scoped %}
                {% if actions %}
                <td>
                    <input type="checkbox" name="rowid" class="action-checkbox" value="{{ get_pk_value(row) }}" title="{{ _gettext('Select record') }}" />
                </td>
                {% endif %}
                {% block list_row_actions_column scoped %}
                <td>
                    {% block list_row_actions scoped %}
                        {%- if admin_view.can_edit -%}
                        <a class="icon" href="{{ get_url('.edit_view', id=get_pk_value(row), url=return_url) }}" title="{{ _gettext('Edit record') }}">
                            <span class="glyphicon glyphicon-pencil"></span>
                        </a>
                        {%- endif -%}
                        {%- if admin_view.can_delete -%}
                        <form class="icon" method="POST" action="{{ get_url('.delete_view', id=get_pk_value(row), url=return_url) }}">
                            {% if csrf_token %}
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                            {% endif %}
                                <button onclick="return confirm('{{ _gettext('Are you sure you want to delete this record?') }}');" title="Delete record">
                                <span class="glyphicon glyphicon-trash"></span>
                            </button>
                        </form>
                        {%- endif -%}
                    {% endblock %}
                </td>
                {% endblock %}
                {% for c, name in list_columns %}
                <td>{{ get_value(row, c) }}</td>
                {% endfor %}
            {% endblock %}
            <td style="padding:0px; width:90px; height:10px;">
                <svg height="10" width="10" id='dag-{{ row.dag_id }}' style="display: block;"></svg>
            </td>
            <td>
              <a href="{{ url_for("airflow.tree", dag_id=row.dag_id, num_runs=num_runs) }}" title="Tree View">
                  <span class="glyphicon glyphicon-tree-deciduous" aria-hidden="true"></span>
              </a>
              <a href="{{ url_for("airflow.graph", dag_id=row.dag_id) }}" title="Graph View">
                  <span class="glyphicon glyphicon-certificate" aria-hidden="true"></span>
                </a>
              <a href="{{ url_for("airflow.duration", dag_id=row.dag_id) }}" title="Task Tries">
                  <span class="glyphicon glyphicon-duplicate" aria-hidden="true"></span>
              </a>
              <a href="{{ url_for("airflow.tries", dag_id=row.dag_id) }}" title="Tasks Duration">
                  <span class="glyphicon glyphicon-stats" aria-hidden="true"></span>
              </a>
              <a href="{{ url_for("airflow.landing_times", dag_id=row.dag_id) }}" title="Landing Times">
                <span class="glyphicon glyphicon-plane" aria-hidden="true"></span>
              </a>
              <a href="{{ url_for("airflow.gantt", dag_id=row.dag_id) }}" title="Gantt View">
                <span class="glyphicon glyphicon-align-left" aria-hidden="true"></span>
                <i class="icon-align-left"></i>
              </a>
              <a href="{{ url_for("airflow.code", dag_id=row.dag_id) }}" title="Code View">
                <span class="glyphicon glyphicon-file" aria-hidden="true"></span>
              </a>
              <a href="{{ url_for("airflow.refresh", dag_id=row.dag_id) }}" title="Refresh">
                <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>
              </a>
              <a href="{{ url_for('log.index_view') }}?sort=1&desc=1&flt1_dag_id_equals={{ row.dag_id }}" title="Logs">
                 <i class="icon-list"></i>
                 <span class="glyphicon glyphicon-align-justify" aria-hidden="true"></span>
              </a>
            </td>
        </tr>
        {% else %}
        <tr>
            <td colspan="999">
                {% block empty_list_message %}
                <div class="text-center">
                    {{ admin_view.get_empty_list_message() }}
                </div>
                {% endblock %}
            </td>
        </tr>
        {% endfor %}
    </table>
    {{ lib.pager(page, num_pages, pager_url) }}
    {% endblock %}

    {{ actionlib.form(actions, get_url('.action_view')) }}
{% endblock %}

{% block tail %}
    {{ super() }}
    <script src="{{ admin_static.url(filename='admin/js/filters.js') }}"></script>
    {{ lib.form_js() }}

    {{ actionlib.script(_gettext('Please select at least one record.'),
                      actions,
                      actions_confirmation) }}

    <script language="javascript">
        (function($) {
            $('[data-role=tooltip]').tooltip({
                html: true,
                placement: 'bottom'
            });
            {% if filter_groups %}
                var filter = new AdminFilters(
                    '#filter_form', '.field-filters',
                    {{ filter_groups|tojson }},
                    {{ active_filters|tojson }}
                );
            {% endif %}
        })(jQuery);
    </script>

  <script src="{{ url_for('static', filename='d3.v3.min.js') }}"></script>
  <script>
      d3.selectAll("span.glyphicon-pencil").attr("class", "glyphicon glyphicon-search");
      diameter = 25;
      circle_margin = 4;
      stroke_width = 2;
      stroke_width_hover = 6;
      d3.json("{{ url_for('airflow.dag_stats') }}", function(error, json) {
        for(var dag_id in json) {
            states = json[dag_id];
            g = d3.select('svg#dag-' + dag_id)
              .attr('height', diameter + (stroke_width_hover * 2))
              .attr('width', '90px')
              .selectAll("g")
              .data(states)
              .enter()
              .append('g')
              .attr('transform', function(d, i) {
                x = (i * (diameter + circle_margin)) + (diameter/2 + circle_margin);
                y = (diameter/2) + stroke_width_hover;
                return 'translate(' + x + ',' + y + ')';
              });

            g.append('text')
              .attr('fill', 'black')
              .attr('text-anchor', 'middle')
              .attr('vertical-align', 'middle')
              .attr('font-size', 8)
              .attr('y', 3)
              .text(function(d){ return d.count; });

            g.append('circle')
              .attr('stroke-width', function(d) {
                  if (d.count > 0)
                    return stroke_width;
                  else {
                    return 1;
                  }
              })
              .attr('stroke', function(d) {
                  if (d.count > 0)
                    return d.color;
                  else {
                    return 'grey';
                  }
              })
              .attr('fill-opacity', 0)
              .attr('r', diameter/2)
              .attr('title', function(d) {return d.state})
              .attr('style', function(d) {
                if (d.count > 0)
                    return"cursor:pointer;"
              })
              .on('click', function(d, i) {
                  if (d.count > 0)
                    window.location = "{{ url_for('taskinstance.index_view') }}?flt1_dag_id_equals=" + d.dag_id + "&flt2_state_equals=" + d.state;
              })
              .on('mouseover', function(d, i) {
                if (d.count > 0) {
                    d3.select(this).transition().duration(400)
                      .attr('fill-opacity', 0.3)
                      .style("stroke-width", stroke_width_hover);
                }
              })
              .on('mouseout', function(d, i) {
                if (d.count > 0) {
                    d3.select(this).transition().duration(400)
                      .attr('fill-opacity', 0)
                      .style("stroke-width", stroke_width);
                }
              });
        }

        $("circle").tooltip({
          html: true,
          container: "body",
        });
    });
  </script>
{% endblock %}
