Better redirect to kibana dashboard

Redirect the user specifically to the kibana dashboard rather
than to the list of dashboards: the relevant dashboard id is
retrieved by querying elasticsearch directly (kibana configuration
is kept under the .kibana index). To allow this, CORS has been enabled
from all origins in elasticsearch.

Also, setting the time range in kibana according to the ETL
time range itself.

Feature: Issue 9966
Change-Id: Ifaadc43ff45d6959a8fbd511f23ec352f01abf33
diff --git a/src/main/resources/static/js/analyticswizard.js b/src/main/resources/static/js/analyticswizard.js
index c46d73a..c7b02d2 100644
--- a/src/main/resources/static/js/analyticswizard.js
+++ b/src/main/resources/static/js/analyticswizard.js
@@ -44,10 +44,50 @@
     setTimeout(function () { checkStatusRequest()}, 5000);
 }
 
+function baseURL() {
+    return location.protocol + "//" + location.hostname;
+}
+
+function getKibanaIndex(success) {
+    var elasticSearchURL = baseURL() + ":9200/.kibana/_search?q=_type:dashboard&stored_fields=_id";
+
+    $.ajax({
+        type : "GET",
+        crossDomain: true,
+        url : elasticSearchURL,
+        dataType: 'json',
+        contentType:"application/json; charset=utf-8"
+    })
+    .done(function ( json ) {
+        var kibanaIndex = json["hits"]["hits"][0]["_id"];
+        success("dashboard/" + kibanaIndex);
+    })
+    .fail(function ( jqxhr, textStatus, error ) {
+        console.error("Error extracting kibana index: " + textStatus + ", " + error);
+        console.log("Defaulting to all dashboards");
+        success("dashboards");
+    });
+}
+
 function wizardGoToDashboard() {
     waitingDialog.hide();
-    var redirectLocation = location.protocol + "//" + location.hostname + ":5601/app/kibana#/dashboards";
-    window.location.replace(redirectLocation);
+
+    getKibanaIndex( function (kibanaIndex) {
+        var redirectLocation = baseURL() + ":5601/app/kibana#/" + kibanaIndex + "?" + kibanaTimeframeParams();
+        window.location.replace(redirectLocation);
+    });
+}
+
+function kibanaTimeframeParams() {
+    var from = $("#since").val()
+        || new Date(new Date().setFullYear(new Date().getFullYear() - 10)).toISOString();
+
+    var to = $("#until").val()
+        || new Date(Date.now()).toISOString();
+
+    var timeFrame = "_g=(time:(from:'"+from+"',to:'"+to+"',mode:absolute))";
+
+    return timeFrame;
 }
 
 function getRequestBody() {
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/analytics/wizard/AnalyticDashboardSetup.scala b/src/main/scala/com/googlesource/gerrit/plugins/analytics/wizard/AnalyticDashboardSetup.scala
index 10a0f52..9ad690d 100644
--- a/src/main/scala/com/googlesource/gerrit/plugins/analytics/wizard/AnalyticDashboardSetup.scala
+++ b/src/main/scala/com/googlesource/gerrit/plugins/analytics/wizard/AnalyticDashboardSetup.scala
@@ -99,6 +99,9 @@
        |      - http.host=0.0.0.0
        |      - network.host=_site_
        |      - http.publish_host=_site_
+       |      - http.cors.allow-origin=*
+       |      - http.cors.enabled=true
+       |
        |    ports:
        |      - "9200:9200"
        |      - "9300:9300"