text: /*
   * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
   * or more contributor license agreements. Licensed under the Elastic License;
   * you may not use this file except in compliance with the Elastic License.
   */
  
  /**
   * Controller for Advanced Node Detail
   */
  import React from 'react';
  import { render } from 'react-dom';
  import { find } from 'lodash';
  import uiRoutes from 'ui/routes';
  import { ajaxErrorHandlersProvider } from 'plugins/monitoring/lib/ajax_error_handler';
  import { routeInitProvider } from 'plugins/monitoring/lib/route_init';
  import template from './index.html';
  import { timefilter } from 'ui/timefilter';
  import { I18nProvider } from '@kbn/i18n/react';
  import { AdvancedNode } from '../../../../components/elasticsearch/node/advanced';
  import moment from 'moment';
  
  function getPageData($injector) {
    const $http = $injector.get('$http');
    const globalState = $injector.get('globalState');
    const $route = $injector.get('$route');
    const timeBounds = timefilter.getBounds();
    const url = `../api/monitoring/v1/clusters/${globalState.cluster_uuid}/elasticsearch/nodes/${$route.current.params.node}`;
  
    return $http.post(url, {
      ccs: globalState.ccs,
      timeRange: {
        min: timeBounds.min.toISOString(),
        max: timeBounds.max.toISOString()
      },
      is_advanced: true,
    })
      .then(response => response.data)
      .catch((err) => {
        const Private = $injector.get('Private');
        const ajaxErrorHandlers = Private(ajaxErrorHandlersProvider);
        return ajaxErrorHandlers(err);
      });
  }
  
  uiRoutes.when('/elasticsearch/nodes/:node/advanced', {
    template,
    resolve: {
      clusters: function (Private) {
        const routeInit = Private(routeInitProvider);
        return routeInit();
      },
      pageData: getPageData
    },
    controller($injector, $scope, i18n) {
      timefilter.enableTimeRangeSelector();
      timefilter.enableAutoRefreshSelector();
  
      const $route = $injector.get('$route');
      const globalState = $injector.get('globalState');
      $scope.cluster = find($route.current.locals.clusters, { cluster_uuid: globalState.cluster_uuid });
      $scope.pageData = $route.current.locals.pageData;
  
      const title = $injector.get('title');
      const routeTitle = i18n('xpack.monitoring.elasticsearch.node.advanced.routeTitle', {
        defaultMessage: 'Elasticsearch - Nodes - {nodeSummaryName} - Advanced',
        values: {
          nodeSummaryName: $scope.pageData.nodeSummary.name
        }
      });
  
      title($scope.cluster, routeTitle);
  
      const $executor = $injector.get('$executor');
      $executor.register({
        execute: () => getPageData($injector),
        handleResponse: (response) => {
          $scope.pageData = response;
        }
      });
  
      $executor.start($scope);
  
      $scope.$on('$destroy', $executor.destroy);
  
      function onBrush({ xaxis }) {
        timefilter.setTime({
          from: moment(xaxis.from),
          to: moment(xaxis.to),
          mode: 'absolute',
        });
      }
  
      this.renderReact = () => {
        render(
          <I18nProvider>
            <AdvancedNode
              nodeSummary={$scope.pageData.nodeSummary}
              metrics={$scope.pageData.metrics}
              onBrush={onBrush}
            />
          </I18nProvider>,
          document.getElementById('monitoringElasticsearchAdvancedNodeApp')
        );
      };
  
      $scope.$watch('pageData', this.renderReact);
    }
  });
  