Commit 1665c091 authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3173655 by baldwinlouie, yas: Refactor the usage of the...

Issue #3173655 by baldwinlouie, yas: Refactor the usage of the /clouds/k8s/node/all_allocated_resources in K8s Node Allocated Resources and K8s Node Heatmap
parent 662e9ca9
Loading
Loading
Loading
Loading
+17 −5
Changes for modules/cloud_service_providers/k8s/js/k8s_node_allocated_resources.js: 17 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -259,8 +259,7 @@
    svgs.push(createSvg(selector));
  });

  let updateNodeAllocatedResources = function () {
    d3.json(drupalSettings.k8s.resource_url).then(function (nodes) {
  let updateGraph = function (nodes) {
    let cpu_capacity = 0;
    let cpu_request = 0;
    let memory_capacity = 0;
@@ -342,18 +341,31 @@
      resourceAllocationRatio: (pods_allocation_ratio * 100).toFixed(1) + '%',
      title: 'Pods Allocation'
    });
    });
  };

  updateNodeAllocatedResources();
  updateGraph(drupalSettings.k8s.initial_data);
  drawLegend();

  let updateNodeAllocatedResources = function() {
    d3.json(drupalSettings.k8s.resource_url).then(
      function (nodes) {
        updateGraph(nodes);
      }
    );
  };

  d3.select('body')
      .style('position', 'static');

  if (drupalSettings.k8s.use_event_listener == true) {
    $(document).on('updatedNodes', function (e, eventInfo) {
      updateGraph(eventInfo);
    });
  }
  else {
    let interval = drupalSettings.k8s.k8s_js_refresh_interval || 10;
    setInterval(function () {
      updateNodeAllocatedResources();
    }, interval * 1000);

  }
}(jQuery, drupalSettings));
+8 −2
Changes for modules/cloud_service_providers/k8s/js/k8s_node_heatmap.js: 8 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -244,11 +244,17 @@
  // Update the graph with the initial data set.
  updateGraph(null, drupalSettings.k8s.initial_data);

  // updateNodeHeatmap();
  if (drupalSettings.k8s.use_event_listener == true) {
    $(document).on('updatedNodes', function (e, eventInfo) {
      let pods_count = $('#pods_count').val();
      updateGraph(pods_count, eventInfo);
    });
  }
  else {
    let interval = drupalSettings.k8s.k8s_js_refresh_interval || 10;
    setInterval(function () {
      let pods_count = $('#pods_count').val();
      updateNodeHeatmap(pods_count);
    }, interval * 1000);

  }
}(jQuery, drupalSettings));
+19
Changes for modules/cloud_service_providers/k8s/js/k8s_refresh_resource_trigger.js: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
(function ($, drupalSettings) {

  'use strict';

  let fetchAllocatedResources = function() {
    d3.json(drupalSettings.k8s.resource_url).then(
      function (nodes) {
        var event = jQuery.Event("updatedNodes");
        $(document).trigger(event, [nodes]);
      }
    );
  };

  let interval = drupalSettings.k8s.k8s_js_refresh_interval || 10;
  setInterval(function () {
    fetchAllocatedResources();
  }, interval * 1000);

}(jQuery, drupalSettings));
+5
Changes for modules/cloud_service_providers/k8s/k8s.libraries.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -77,6 +77,11 @@ k8s_all_resources:
  dependencies:
    - core/jquery

k8s_refresh_resource_trigger:
  version: 8.x-2.x
  js:
    js/k8s_refresh_resource_trigger.js: {}

k8s_node_allocated_resources_chart:
  version: 8.x-2.x
  js:
+43 −2
Changes for modules/cloud_service_providers/k8s/src/Plugin/Block/K8sNodeAllocatedResourcesBlock.php: 43 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,8 +2,9 @@

namespace Drupal\k8s\Plugin\Block;

use Drupal\Core\Form\FormStateInterface;
use Drupal\cloud\Entity\CloudConfig;
use Drupal\cloud\Entity\CloudProjectInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a block of the allocated resources at a Node.
@@ -22,6 +23,7 @@ class K8sNodeAllocatedResourcesBlock extends K8sBaseBlock {
  public function defaultConfiguration() {
    return [
      'display_view_k8s_node_list_only' => 1,
      'use_event_listener' => 1,
    ];
  }

@@ -34,6 +36,14 @@ class K8sNodeAllocatedResourcesBlock extends K8sBaseBlock {
      '#title' => $this->t('Display this block in K8s Node list page and K8s Project Content page only'),
      '#default_value' => $this->configuration['display_view_k8s_node_list_only'],
    ];
    $form['use_event_listener'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Use a common JS resource url.'),
      '#description' => $this->t('Check to use a common JS resource url to update this block on dashboard.
      Useful if this block is placed on a page along with K8s Node Heatmap block.  Reduces duplicate AJAX calls to
      K8s Resource Url.'),
      '#default_value' => $this->configuration['use_event_listener'],
    ];
    return $form;
  }

@@ -43,6 +53,8 @@ class K8sNodeAllocatedResourcesBlock extends K8sBaseBlock {
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['display_view_k8s_node_list_only']
      = $form_state->getValue('display_view_k8s_node_list_only');
    $this->configuration['use_event_listener']
      = $form_state->getValue('use_event_listener');
  }

  /**
@@ -57,7 +69,7 @@ class K8sNodeAllocatedResourcesBlock extends K8sBaseBlock {
    }

    $cloud_context = $this->routeMatch->getParameter('cloud_context');
    if (!CloudConfig::checkCloudContext($cloud_context, 'k8s')) {
    if (!empty($cloud_context) && !CloudConfig::checkCloudContext($cloud_context, 'k8s')) {
      return [];
    }

@@ -132,16 +144,45 @@ class K8sNodeAllocatedResourcesBlock extends K8sBaseBlock {
              ->get('k8s_js_refresh_interval'),
            'metrics_enabled' => $metrics_enabled,
            'resource_url' => k8s_get_resource_url(),
            'initial_data' => $this->buildInitialDataSet($cloud_context, !empty($cloud_project) ? $cloud_project : NULL),
            'use_event_listener' => FALSE,
          ],
        ],
      ],
    ];

    // Add refresh js that uses Event Trigger method of updating the block.
    if ($this->configuration['use_event_listener']) {
      $build['k8s_node_allocated_resources']['#attached']['library'][] = 'k8s/k8s_refresh_resource_trigger';
      $build['k8s_node_allocated_resources']['#attached']['drupalSettings']['k8s']['use_event_listener'] = TRUE;
    }

    $this->cloudService->reorderForm($build, $fieldset_defs);

    return $build;
  }

  /**
   * Build the initial data set for the allocated resource block.
   *
   * @param string $cloud_context
   *   The cloud context to use.
   * @param \Drupal\cloud\Entity\CloudProjectInterface $cloud_project
   *   The cloud project.
   *
   * @return array
   *   Array of nodes.
   */
  private function buildInitialDataSet($cloud_context, CloudProjectInterface $cloud_project = NULL): array {
    if (empty($cloud_context)) {
      $response = $this->loadAllNodeAllocatedResources();
    }
    else {
      $response = $this->loadNodeAllocatedResources($cloud_context, $cloud_project);
    }
    return $response;
  }

  /**
   * {@inheritdoc}
   */
Loading