<!--
@license
Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed 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.
-->

<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../tf-graph-loader/tf-graph-loader.html">
<link rel="import" href="../tf-graph-board/tf-graph-board.html">
<link rel="import" href="../tf-graph/tf-graph-controls.html">
<link rel="import" href="../tf-dashboard-common/tf-dashboard.html">
<link rel="import" href="../tf-backend/tf-backend.html">

<!--
tf-graph-dashboard displays a graph from a TensorFlow run.

It has simple behavior: Creates a url-generator and run-generator
to talk to the backend, and then passes the runsWithGraph (list of runs with
associated graphs) along with the url generator into tf-graph-board for display.

If there are multiple runs with graphs, the first run's graph is shown
by default. The user can select a different run from a dropdown menu.
-->
<dom-module id="tf-graph-dashboard">
<template>
<tf-no-data-warning
  data-type="graph"
  show-warning="[[_datasetsEmpty(_datasets)]]"
></tf-no-data-warning>
<template is="dom-if" if="[[!_datasetsEmpty(_datasets)]]">
<tf-dashboard-layout>
<div class="sidebar">
  <tf-graph-controls id="controls"
        devices-for-stats="{{_devicesForStats}}"
        color-by-params="[[_colorByParams]]"
        stats="[[_stats]]"
        color-by="{{_colorBy}}"
        datasets="[[_datasets]]"
        render-hierarchy="[[_renderHierarchy]]"
        selected-dataset="{{_selectedDataset}}"
        selected-file="{{_selectedFile}}"
        selected-metadata-tag="{{_selectedMetadataTag}}"
  ></tf-graph-controls>
  <tf-graph-loader id="loader"
        datasets="[[_datasets]]"
        selected-dataset="[[_selectedDataset]]"
        selected-metadata-tag="[[_selectedMetadataTag]]"
        selected-file="[[_selectedFile]]"
        out-graph-hierarchy="{{_graphHierarchy}}"
        out-graph="{{_graph}}"
        out-stats="{{_stats}}"
        progress="{{_progress}}"
out-hierarchy-params="{{_hierarchyParams}}"
  ></tf-graph-loader>
</div>
<div class="center">
    <tf-graph-board id="graphboard"
        devices-for-stats="[[_devicesForStats]]"
        color-by="[[_colorBy]]"
        color-by-params="{{_colorByParams}}"
        graph-hierarchy="[[_graphHierarchy]]"
        graph="[[_graph]]"
        hierarchy-params="[[_hierarchyParams]]"
        progress="[[_progress]]"
        render-hierarchy="{{_renderHierarchy}}"
        stats="[[_stats]]"
    ></tf-graph-board>
</div>
</tf-dashboard-layout>
</template>
<style>

:host /deep/ {
  font-family: 'Roboto', sans-serif;
}

.center {
  position: relative;
  height: 100%;
}

</style>
</template>
</dom-module>

<script>
(function() {
Polymer({
  is: 'tf-graph-dashboard',
  properties: {
    _datasets: Object,
    _renderHierarchy: Object,
    backend: {type: Object, observer: 'reload'},
    runs: Array
  },
  reload: function() {
    Promise.all([this.backend.graphRuns(), this.backend.runMetadataRuns()])
    .then(function(result) {
      var runsWithGraph = result[0];
      var runToMetadata = result[1];
      var datasets = _.map(runsWithGraph, function(runName) {
        return {
          name: runName,
          path: this.backend.router.graph(runName, tf.graph.LIMIT_ATTR_SIZE,
            tf.graph.LARGE_ATTRS_KEY),
          runMetadata: runToMetadata[runName] ? _.map(runToMetadata[runName].sort(), function(tag) {
            return {
              tag: tag,
              path: this.backend.router.runMetadata(tag, runName)
            };
          }, this) : []
        };
      }, this);
      this.set('_datasets', datasets);
    }.bind(this));
  },
  _datasetsEmpty: function(datasets) {
    return !datasets || !datasets.length;
  }
});
})();
</script>
