From 9d5aea83d9ca46ded112d293e95e0fbd7b7fea52 Mon Sep 17 00:00:00 2001
From: Shaunak Kashyap <ycombinator@gmail.com>
Date: Fri, 9 Mar 2018 06:05:38 -0800
Subject: [PATCH 1/6] Add Advanced Settings option to show/hide date histogram
 viz in Discovery

The option is named `discover:showDateHistogramVisualization`. It is a boolean option that defaults to `true`. Setting it to `false` removes the `aggs` part of the `_msearch` request that is made by Discover and hides the time chart panel in Discover.
---
 docs/management/advanced-options.asciidoc            |  1 +
 .../kibana/public/discover/controllers/discover.js   | 12 +++++++-----
 src/core_plugins/kibana/public/discover/index.html   |  6 +++++-
 src/core_plugins/kibana/ui_setting_defaults.js       |  4 ++++
 4 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/docs/management/advanced-options.asciidoc b/docs/management/advanced-options.asciidoc
index 0dc17c57bbeb4..0aac7d9e5d278 100644
--- a/docs/management/advanced-options.asciidoc
+++ b/docs/management/advanced-options.asciidoc
@@ -37,6 +37,7 @@ document.
 `discover:sampleSize`:: The number of rows to show in the Discover table.
 `discover:aggs:terms:size`:: Determines how many terms will be visualized when clicking the "visualize" button, in the field drop downs, in the discover sidebar. The default value is `20`.
 `discover:sort:defaultOrder`:: Controls the default sort direction for time based index patterns in the Discover app.
+`discover:showDateHistogramVisualization`:: Determines whether to show the date histogram visualization in the Discover app.
 `doc_table:highlight`:: Highlight results in Discover and Saved Searches Dashboard. Highlighting makes request slow when
 working on big documents. Set this property to `false` to disable highlighting.
 `courier:maxSegmentCount`:: Kibana splits requests in the Discover app into segments to limit the size of requests sent to
diff --git a/src/core_plugins/kibana/public/discover/controllers/discover.js b/src/core_plugins/kibana/public/discover/controllers/discover.js
index 51a06b3791b9f..8a18928c0d332 100644
--- a/src/core_plugins/kibana/public/discover/controllers/discover.js
+++ b/src/core_plugins/kibana/public/discover/controllers/discover.js
@@ -295,6 +295,7 @@ function discoverController(
   $scope.opts = {
     // number of records to fetch, then paginate through
     sampleSize: config.get('discover:sampleSize'),
+    showViz: config.get('discover:showDateHistogramVisualization'),
     timefield: $scope.indexPattern.timeFieldName,
     savedSearch: savedSearch,
     indexPatternList: $route.current.locals.ip.list,
@@ -354,7 +355,7 @@ function discoverController(
 
         $scope.$watch('vis.aggs', function () {
         // no timefield, no vis, nothing to update
-          if (!$scope.opts.timefield) return;
+          if (!$scope.opts.timefield || !$scope.opts.showViz) return;
 
           const buckets = $scope.vis.getAggConfig().bySchemaGroup.buckets;
 
@@ -409,7 +410,7 @@ function discoverController(
             };
           }()));
 
-        if ($scope.opts.timefield) {
+        if ($scope.opts.timefield && $scope.opts.showViz) {
           setupVisualization();
           $scope.updateTime();
         }
@@ -535,7 +536,7 @@ function discoverController(
     segmented.on('mergedSegment', function (merged) {
       $scope.mergedEsResp = merged;
 
-      if ($scope.opts.timefield) {
+      if ($scope.opts.timefield && $scope.opts.showViz) {
         $scope.searchSource.rawResponse = merged;
         Promise
           .resolve(responseHandler($scope.vis, merged))
@@ -666,8 +667,9 @@ function discoverController(
   };
 
   function setupVisualization() {
-    // If no timefield has been specified we don't create a histogram of messages
-    if (!$scope.opts.timefield) return;
+    // If no timefield has been specified or the visualization is disabled
+    // we don't create a histogram of messages
+    if (!$scope.opts.timefield || !$scope.opts.showViz) return;
 
     const visStateAggs = [
       {
diff --git a/src/core_plugins/kibana/public/discover/index.html b/src/core_plugins/kibana/public/discover/index.html
index 6e7a971cc37ff..487385eb9b60b 100644
--- a/src/core_plugins/kibana/public/discover/index.html
+++ b/src/core_plugins/kibana/public/discover/index.html
@@ -101,7 +101,11 @@ <h2>Searching</h2>
               </span>
             </button>
 
-            <section aria-label="Histogram of found documents" class="discover-timechart" ng-if="opts.timefield">
+            <section
+              ng-if="opts.timefield && opts.showViz"
+              aria-label="Histogram of found documents"
+              class="discover-timechart"
+            >
               <header>
                 <center class="small">
                   <span tooltip="To change the time, click the clock icon in the navigation bar">{{timeRange.from | moment}} - {{timeRange.to | moment}}</span>
diff --git a/src/core_plugins/kibana/ui_setting_defaults.js b/src/core_plugins/kibana/ui_setting_defaults.js
index 88ba519b65137..0d73d76bc0e16 100644
--- a/src/core_plugins/kibana/ui_setting_defaults.js
+++ b/src/core_plugins/kibana/ui_setting_defaults.js
@@ -97,6 +97,10 @@ export function getUiSettingDefaults() {
       type: 'select',
       description: 'Controls the default sort direction for time based index patterns in the Discover app.',
     },
+    'discover:showDateHistogramVisualization': {
+      value: true,
+      description: 'Determines whether to show the date histogram visualization in the Discover app.'
+    },
     'doc_table:highlight': {
       value: true,
       description: 'Highlight results in Discover and Saved Searches Dashboard.' +

From bc62c4cf6e6cb10196467d7958897d8eb5b8ef63 Mon Sep 17 00:00:00 2001
From: Shaunak Kashyap <ycombinator@gmail.com>
Date: Fri, 20 Apr 2018 10:58:53 -0700
Subject: [PATCH 2/6] Add show/hide viz link + flag in app state

---
 .../public/discover/controllers/discover.js   | 17 +++---
 .../kibana/public/discover/index.html         | 54 +++++++++++--------
 2 files changed, 43 insertions(+), 28 deletions(-)

diff --git a/src/core_plugins/kibana/public/discover/controllers/discover.js b/src/core_plugins/kibana/public/discover/controllers/discover.js
index 8a18928c0d332..71d8e1188e74b 100644
--- a/src/core_plugins/kibana/public/discover/controllers/discover.js
+++ b/src/core_plugins/kibana/public/discover/controllers/discover.js
@@ -274,7 +274,8 @@ function discoverController(
       columns: savedSearch.columns.length > 0 ? savedSearch.columns : config.get('defaultColumns').slice(),
       index: $scope.indexPattern.id,
       interval: 'auto',
-      filters: _.cloneDeep($scope.searchSource.getOwn('filter'))
+      filters: _.cloneDeep($scope.searchSource.getOwn('filter')),
+      showViz: true
     };
   }
 
@@ -292,10 +293,11 @@ function discoverController(
     $state.save();
   });
 
+  $scope.$watch('state.showViz', () => $state.save());
+
   $scope.opts = {
     // number of records to fetch, then paginate through
     sampleSize: config.get('discover:sampleSize'),
-    showViz: config.get('discover:showDateHistogramVisualization'),
     timefield: $scope.indexPattern.timeFieldName,
     savedSearch: savedSearch,
     indexPatternList: $route.current.locals.ip.list,
@@ -355,7 +357,7 @@ function discoverController(
 
         $scope.$watch('vis.aggs', function () {
         // no timefield, no vis, nothing to update
-          if (!$scope.opts.timefield || !$scope.opts.showViz) return;
+          if (!$scope.opts.timefield || !$scope.state.showViz) return;
 
           const buckets = $scope.vis.getAggConfig().bySchemaGroup.buckets;
 
@@ -410,7 +412,7 @@ function discoverController(
             };
           }()));
 
-        if ($scope.opts.timefield && $scope.opts.showViz) {
+        if ($scope.opts.timefield && $scope.state.showViz) {
           setupVisualization();
           $scope.updateTime();
         }
@@ -536,7 +538,7 @@ function discoverController(
     segmented.on('mergedSegment', function (merged) {
       $scope.mergedEsResp = merged;
 
-      if ($scope.opts.timefield && $scope.opts.showViz) {
+      if ($scope.opts.timefield && $scope.state.showViz) {
         $scope.searchSource.rawResponse = merged;
         Promise
           .resolve(responseHandler($scope.vis, merged))
@@ -666,10 +668,13 @@ function discoverController(
     $scope.minimumVisibleRows = $scope.hits;
   };
 
+  $scope.showViz = () => $scope.state.showViz = true;
+  $scope.hideViz = () => $scope.state.showViz = false;
+
   function setupVisualization() {
     // If no timefield has been specified or the visualization is disabled
     // we don't create a histogram of messages
-    if (!$scope.opts.timefield || !$scope.opts.showViz) return;
+    if (!$scope.opts.timefield || !$scope.state.showViz) return;
 
     const visStateAggs = [
       {
diff --git a/src/core_plugins/kibana/public/discover/index.html b/src/core_plugins/kibana/public/discover/index.html
index 487385eb9b60b..a4d9eaecdeb33 100644
--- a/src/core_plugins/kibana/public/discover/index.html
+++ b/src/core_plugins/kibana/public/discover/index.html
@@ -102,39 +102,42 @@ <h2>Searching</h2>
             </button>
 
             <section
-              ng-if="opts.timefield && opts.showViz"
+              ng-if="opts.timefield"
               aria-label="Histogram of found documents"
               class="discover-timechart"
             >
               <header>
                 <center class="small">
-                  <span tooltip="To change the time, click the clock icon in the navigation bar">{{timeRange.from | moment}} - {{timeRange.to | moment}}</span>
-
-                  &mdash;
-
-                  <span class="results-interval form-inline">
-                    <select
-                      class="form-control"
-                      ng-model="state.interval"
-                      ng-options="interval.val as interval.display for interval in intervalOptions | filter: intervalEnabled"
-                      ng-blur="toggleInterval()"
-                      data-test-subj="discoverIntervalSelect"
-                      >
-                    </select>
-                    <span ng-if="bucketInterval.scaled">
-                      <icon-tip
-                        content="getBucketIntervalToolTipText()"
-                        position="'top'"
-                      ></icon-tip>
-                      Scaled to {{ bucketInterval.description }}
+                  <div ng-if="state.showViz">
+                    <span tooltip="To change the time, click the clock icon in the navigation bar">{{timeRange.from | moment}} - {{timeRange.to | moment}}</span>
+
+                    &mdash;
+
+                    <span class="results-interval form-inline">
+                      <select
+                        class="form-control"
+                        ng-model="state.interval"
+                        ng-options="interval.val as interval.display for interval in intervalOptions | filter: intervalEnabled"
+                        ng-blur="toggleInterval()"
+                        data-test-subj="discoverIntervalSelect"
+                        >
+                      </select>
+                      <span ng-if="bucketInterval.scaled">
+                        <icon-tip
+                          content="getBucketIntervalToolTipText()"
+                          position="'top'"
+                        ></icon-tip>
+                        Scaled to {{ bucketInterval.description }}
+                      </span>
+
                     </span>
-                  </span>
+                  </div>
                 </center>
 
               </header>
 
               <visualization
-                 ng-if="vis && rows.length != 0"
+                 ng-if="state.showViz && vis && rows.length != 0"
                  vis="vis"
                  ui-state="uiState"
                  vis-data="visData"
@@ -143,6 +146,13 @@ <h2>Searching</h2>
                  style="height: 200px"
                 >
               </visualization>
+
+              <div>
+                  <center class="small">
+                    <a ng-if="!state.showViz" kbn-accessible-click ng-click="showViz()">Show visualization</a>
+                    <a ng-if="state.showViz" kbn-accessible-click ng-click="hideViz()">Hide visualization</a>
+                  </center>
+              </div>
             </section>
 
             <section class="discover-table" fixed-scroll aria-label="Documents">

From 3a280f3ab2d0449967e95ad202a1595a15054dc9 Mon Sep 17 00:00:00 2001
From: Shaunak Kashyap <ycombinator@gmail.com>
Date: Fri, 20 Apr 2018 11:00:45 -0700
Subject: [PATCH 3/6] Remove flag from Advanced Options

---
 docs/management/advanced-options.asciidoc      | 1 -
 src/core_plugins/kibana/ui_setting_defaults.js | 4 ----
 2 files changed, 5 deletions(-)

diff --git a/docs/management/advanced-options.asciidoc b/docs/management/advanced-options.asciidoc
index 0aac7d9e5d278..0dc17c57bbeb4 100644
--- a/docs/management/advanced-options.asciidoc
+++ b/docs/management/advanced-options.asciidoc
@@ -37,7 +37,6 @@ document.
 `discover:sampleSize`:: The number of rows to show in the Discover table.
 `discover:aggs:terms:size`:: Determines how many terms will be visualized when clicking the "visualize" button, in the field drop downs, in the discover sidebar. The default value is `20`.
 `discover:sort:defaultOrder`:: Controls the default sort direction for time based index patterns in the Discover app.
-`discover:showDateHistogramVisualization`:: Determines whether to show the date histogram visualization in the Discover app.
 `doc_table:highlight`:: Highlight results in Discover and Saved Searches Dashboard. Highlighting makes request slow when
 working on big documents. Set this property to `false` to disable highlighting.
 `courier:maxSegmentCount`:: Kibana splits requests in the Discover app into segments to limit the size of requests sent to
diff --git a/src/core_plugins/kibana/ui_setting_defaults.js b/src/core_plugins/kibana/ui_setting_defaults.js
index 0d73d76bc0e16..88ba519b65137 100644
--- a/src/core_plugins/kibana/ui_setting_defaults.js
+++ b/src/core_plugins/kibana/ui_setting_defaults.js
@@ -97,10 +97,6 @@ export function getUiSettingDefaults() {
       type: 'select',
       description: 'Controls the default sort direction for time based index patterns in the Discover app.',
     },
-    'discover:showDateHistogramVisualization': {
-      value: true,
-      description: 'Determines whether to show the date histogram visualization in the Discover app.'
-    },
     'doc_table:highlight': {
       value: true,
       description: 'Highlight results in Discover and Saved Searches Dashboard.' +

From 4156c7ee713b59c84dbe452ab734e6b63acd8c31 Mon Sep 17 00:00:00 2001
From: Shaunak Kashyap <ycombinator@gmail.com>
Date: Fri, 20 Apr 2018 11:03:39 -0700
Subject: [PATCH 4/6] Using .bind instead of =>

---
 src/core_plugins/kibana/public/discover/controllers/discover.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/core_plugins/kibana/public/discover/controllers/discover.js b/src/core_plugins/kibana/public/discover/controllers/discover.js
index 71d8e1188e74b..23401b0762044 100644
--- a/src/core_plugins/kibana/public/discover/controllers/discover.js
+++ b/src/core_plugins/kibana/public/discover/controllers/discover.js
@@ -293,7 +293,7 @@ function discoverController(
     $state.save();
   });
 
-  $scope.$watch('state.showViz', () => $state.save());
+  $scope.$watch('state.showViz', $state.save.bind($state));
 
   $scope.opts = {
     // number of records to fetch, then paginate through

From b31a2f08880f80202f02174274475d4a60628a53 Mon Sep 17 00:00:00 2001
From: Shaunak Kashyap <ycombinator@gmail.com>
Date: Mon, 23 Apr 2018 16:54:14 -0700
Subject: [PATCH 5/6] Do not let show/hide viz state mess with getting data for
 visualization

---
 .../kibana/public/discover/controllers/discover.js     | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/core_plugins/kibana/public/discover/controllers/discover.js b/src/core_plugins/kibana/public/discover/controllers/discover.js
index 23401b0762044..21bc609139836 100644
--- a/src/core_plugins/kibana/public/discover/controllers/discover.js
+++ b/src/core_plugins/kibana/public/discover/controllers/discover.js
@@ -357,7 +357,7 @@ function discoverController(
 
         $scope.$watch('vis.aggs', function () {
         // no timefield, no vis, nothing to update
-          if (!$scope.opts.timefield || !$scope.state.showViz) return;
+          if (!$scope.opts.timefield) return;
 
           const buckets = $scope.vis.getAggConfig().bySchemaGroup.buckets;
 
@@ -412,7 +412,7 @@ function discoverController(
             };
           }()));
 
-        if ($scope.opts.timefield && $scope.state.showViz) {
+        if ($scope.opts.timefield) {
           setupVisualization();
           $scope.updateTime();
         }
@@ -538,7 +538,7 @@ function discoverController(
     segmented.on('mergedSegment', function (merged) {
       $scope.mergedEsResp = merged;
 
-      if ($scope.opts.timefield && $scope.state.showViz) {
+      if ($scope.opts.timefield) {
         $scope.searchSource.rawResponse = merged;
         Promise
           .resolve(responseHandler($scope.vis, merged))
@@ -672,9 +672,9 @@ function discoverController(
   $scope.hideViz = () => $scope.state.showViz = false;
 
   function setupVisualization() {
-    // If no timefield has been specified or the visualization is disabled
+    // If no timefield has been specified
     // we don't create a histogram of messages
-    if (!$scope.opts.timefield || !$scope.state.showViz) return;
+    if (!$scope.opts.timefield) return;
 
     const visStateAggs = [
       {

From ed6bb4a21f9f41f0fa0a6e45d5f67968cf34aae7 Mon Sep 17 00:00:00 2001
From: Shaunak Kashyap <ycombinator@gmail.com>
Date: Tue, 24 Apr 2018 12:46:25 -0700
Subject: [PATCH 6/6] Using more distinct variable name

---
 .../kibana/public/discover/controllers/discover.js        | 8 ++++----
 src/core_plugins/kibana/public/discover/index.html        | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/core_plugins/kibana/public/discover/controllers/discover.js b/src/core_plugins/kibana/public/discover/controllers/discover.js
index 21bc609139836..856c7aa8c1afb 100644
--- a/src/core_plugins/kibana/public/discover/controllers/discover.js
+++ b/src/core_plugins/kibana/public/discover/controllers/discover.js
@@ -275,7 +275,7 @@ function discoverController(
       index: $scope.indexPattern.id,
       interval: 'auto',
       filters: _.cloneDeep($scope.searchSource.getOwn('filter')),
-      showViz: true
+      isVisualizationVisible: true
     };
   }
 
@@ -293,7 +293,7 @@ function discoverController(
     $state.save();
   });
 
-  $scope.$watch('state.showViz', $state.save.bind($state));
+  $scope.$watch('state.isVisualizationVisible', $state.save.bind($state));
 
   $scope.opts = {
     // number of records to fetch, then paginate through
@@ -668,8 +668,8 @@ function discoverController(
     $scope.minimumVisibleRows = $scope.hits;
   };
 
-  $scope.showViz = () => $scope.state.showViz = true;
-  $scope.hideViz = () => $scope.state.showViz = false;
+  $scope.showViz = () => $scope.state.isVisualizationVisible = true;
+  $scope.hideViz = () => $scope.state.isVisualizationVisible = false;
 
   function setupVisualization() {
     // If no timefield has been specified
diff --git a/src/core_plugins/kibana/public/discover/index.html b/src/core_plugins/kibana/public/discover/index.html
index a4d9eaecdeb33..b3cb78569ebb2 100644
--- a/src/core_plugins/kibana/public/discover/index.html
+++ b/src/core_plugins/kibana/public/discover/index.html
@@ -108,7 +108,7 @@ <h2>Searching</h2>
             >
               <header>
                 <center class="small">
-                  <div ng-if="state.showViz">
+                  <div ng-if="state.isVisualizationVisible">
                     <span tooltip="To change the time, click the clock icon in the navigation bar">{{timeRange.from | moment}} - {{timeRange.to | moment}}</span>
 
                     &mdash;
@@ -137,7 +137,7 @@ <h2>Searching</h2>
               </header>
 
               <visualization
-                 ng-if="state.showViz && vis && rows.length != 0"
+                 ng-if="state.isVisualizationVisible && vis && rows.length != 0"
                  vis="vis"
                  ui-state="uiState"
                  vis-data="visData"
@@ -149,8 +149,8 @@ <h2>Searching</h2>
 
               <div>
                   <center class="small">
-                    <a ng-if="!state.showViz" kbn-accessible-click ng-click="showViz()">Show visualization</a>
-                    <a ng-if="state.showViz" kbn-accessible-click ng-click="hideViz()">Hide visualization</a>
+                    <a ng-if="!state.isVisualizationVisible" kbn-accessible-click ng-click="showViz()">Show visualization</a>
+                    <a ng-if="state.isVisualizationVisible" kbn-accessible-click ng-click="hideViz()">Hide visualization</a>
                   </center>
               </div>
             </section>