github.com/jenkins-x/test-infra@v0.0.7/testgrid/extension/testgrid_alerter/js/updater.js (about)

     1  /**
     2   * @fileoverview Polls and receives alerts from TestGrid server for data on
     3   * dashboards, tabs, and alerts and stores user config data.
     4   *
     5   * TODO(carolinemoore) switch to Tango notifications.
     6   */
     7  
     8  /**
     9   * Holds information about alerts for a single dashboard.
    10   */
    11  var Alert = class {
    12    /**
    13     * Creates a new Alert object.
    14     * @param {string} dashboard The name of the dashboard.
    15     * @param {Array} tabs A list of DashboardTabSummary objects for the tabs in
    16     *   the given dashboard.
    17     */
    18    constructor(dashboard, tabs) {
    19      this.dashboard = dashboard;
    20      this.tabs = tabs;
    21    }
    22  };
    23  
    24  /**
    25   * Holds public functions in updater.js.
    26   */
    27  var Updater = (function() {
    28  
    29    // Mapping from dashboard name to list of tab names for all dashboards.
    30    let dashboards = {};
    31    // Mapping from dashboard name to list of tab names for selected dashboards
    32    // and tabs.
    33    let selectedDashboards = {};
    34    // List of Alerts based on information requested from TestGrid. There will be
    35    // one alert object for each dashboard.
    36    let alerts = [];
    37    // A boolean indicating whether the user is able to request data from
    38    // TestGrid.
    39    let hasPermission = false;
    40  
    41    // The base url to use to make requests.
    42    let URL_BASE = 'https://testgrid.k8s.io/';
    43  
    44    /**
    45     * Makes a request to the TestGrid server for a list of dashboards and tabs.
    46     */
    47    function updateDashboardListFromServer() {
    48      // URL to make the request to.
    49      let url = URL_BASE + 'q/list';
    50      let xhttp = new XMLHttpRequest();
    51      xhttp.onreadystatechange = function() {
    52        if (this.readyState == 4) {
    53          if (this.status == 200) {
    54            dashboards = JSON.parse(this.responseText);
    55            hasPermission = true;
    56            console.log('finished getting dashboards');
    57          } else {
    58            hasPermission = false;
    59          }
    60        }
    61      };
    62      xhttp.open('GET', url, true);
    63      xhttp.send();
    64    }
    65  
    66    /**
    67     * Requests the list of selected dashboards from chrome.storage.
    68     *
    69     * Calls updateAlerts() if the selected dashboards have changed.
    70     */
    71    function updateSelectedDashboards() {
    72      try {
    73        chrome.storage.sync.get('selectedDashboards', function(items) {
    74          if ('selectedDashboards' in items &&
    75              selectedDashboards != items.selectedDashboards) {
    76            selectedDashboards = items.selectedDashboards;
    77            updateAlerts();
    78            console.log('selected dashboards have changed');
    79          }
    80          console.log('finished getting selected dashboards');
    81        });
    82      } catch (err) {
    83        console.log('caught an error requesting data from chrome.storage');
    84      }
    85    }
    86  
    87    /**
    88     * Creates a DashboardTabSummary object with the given dashboardName and
    89     * dashboardTabName and with other necessary fields with no real data.
    90     *
    91     * For testing only.
    92     * TODO(carolinemoore) remove.
    93     *
    94     * @param {string} dashboardName the name of the dashboard
    95     * @return {Object} a DashboardTabSummary object
    96     */
    97    function createEmptyDashboardTabSummary(dashboardName) {
    98      return {dashboard_name: dashboardName, alert: '', tests: []};
    99    }
   100  
   101    /**
   102     * Requests current alert information from TestGrid.
   103     *
   104     * Currently fills in fake data with no failures for testing.
   105     * TODO(carolinemoore) implement.
   106     */
   107    function updateAlerts() {
   108      console.log('updating alerts');
   109      alerts = [];
   110      for (dashboardName in selectedDashboards) {
   111        if (selectedDashboards[dashboardName].length > 0) {
   112          tabs = [];
   113          for (i in selectedDashboards[dashboardName]) {
   114            tabs[selectedDashboards[dashboardName][i]] =
   115                (createEmptyDashboardTabSummary(dashboardName));
   116          }
   117          alerts.push(new Alert(dashboardName, tabs));
   118        }
   119      }
   120    }
   121  
   122    /**
   123     * Load data on startup.
   124     */
   125    (function() {
   126      updateDashboardListFromServer();
   127      updateSelectedDashboards();
   128      chrome.browserAction.setBadgeBackgroundColor({color: [200, 55, 55, 255]});
   129    })();
   130  
   131    /**
   132     * Updates selectedDashboards if selectedDashboards changes elsewhere.
   133     *
   134     * Calls updateAlerts() if selectedDashboards chagnes from what is currently
   135     * stored.
   136     */
   137    chrome.storage.onChanged.addListener(function(changes, areaName) {
   138      console.log(changes);
   139      if (changes.hasOwnProperty('selectedDashboards') &&
   140          (changes.selectedDashboards.newValue != selectedDashboards)) {
   141        selectedDashboards = changes.selectedDashboards.newValue;
   142        updateAlerts();
   143      }
   144    });
   145  
   146    return {
   147      /**
   148       * Getter function for alerts.
   149       * @return {Array} A list of alerts.
   150       */
   151      getAlerts: function() {
   152        return alerts;
   153      },
   154  
   155      /**
   156       * Provides values used by the settings page.
   157       *
   158       * @return {Array} array containing the list of dashboards, the list of
   159       *   selected dashboards, and the boolean indicating if the extension
   160       *   successfully requested data from TestGrid.
   161       */
   162      openSettings: function() {
   163        return [dashboards, selectedDashboards, hasPermission];
   164      },
   165  
   166      /**
   167       * Updates and saves selectedDashboards to the given value and calls
   168       * updateAlerts() if it's value changed.
   169       *
   170       * @param {Object} newDashboards Mapping from dashboard name to list of tab
   171       * names for dashboards and tabs selected by the user.
   172       */
   173      saveSettings: function(newDashboards) {
   174        if (selectedDashboards != newDashboards) {
   175          selectedDashboards = newDashboards;
   176          chrome.storage.sync.set(
   177              {'selectedDashboards': newDashboards}, function() {
   178                console.log('finished setting selected dashboards');
   179              });
   180          updateAlerts();
   181        }
   182      }
   183    };
   184  })();