github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/static/js/settings.js (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  $(document).ready(function () {
    18      if (Cookies.get('theme')) {
    19          theme = Cookies.get('theme');
    20          $('body').attr('data-theme', theme);
    21      }
    22      $('.theme-btn').on('click', themePickerHandler);
    23      getRetentionDataFromConfig();
    24      getPersistentQueriesSetting();
    25      getSystemInfo();
    26      {{ .SettingsExtraOnReadySetup }}
    27      {{ .Button1Function }}
    28  });
    29  
    30  function getRetentionDataFromConfig() {
    31      $.ajax({
    32          method: 'get',
    33          url: 'api/config',
    34          crossDomain: true,
    35          dataType: 'json',
    36          credentials: 'include'
    37      })
    38      {{ if .SettingsRetentionDataThenBlock }}
    39          {{ .SettingsRetentionDataThenBlock }}
    40      {{ else }}
    41          .then((res) => {
    42              let ret_days = res.RetentionHours / 24;
    43              $('#retention-days-value').html(`${ret_days} days`);
    44          })
    45      {{ end }}
    46      .catch((err) => {
    47          console.log(err)
    48      });
    49  }
    50  
    51  function getPersistentQueriesSetting(){
    52      $.ajax({
    53          method: "GET",
    54          url: "/api/pqs/get",
    55          headers: {
    56              "Content-Type": "application/json; charset=utf-8",
    57              Accept: "*/*",
    58          },
    59          dataType: "json",
    60          crossDomain: true,
    61          success: function (res) {
    62              console.log("Update successful:", res);
    63              setPersistentQueries(res.pqsEnabled);
    64          },
    65          error: function (xhr, status, error) {
    66              console.error("Update failed:", xhr, status, error);
    67          },
    68      });
    69  }
    70  function updatePersistentQueriesSetting(pqsEnabled) {
    71      $.ajax({
    72          method: "POST",
    73          url: "/api/pqs/update",
    74          headers: {
    75              "Content-Type": "application/json; charset=utf-8",
    76              Accept: "*/*",
    77          },
    78          dataType: "json",
    79          crossDomain: true,
    80          data: JSON.stringify({ pqsEnabled: pqsEnabled }),
    81          success: function (res) {
    82              console.log("Update successful:", res);
    83          },
    84          error: function (xhr, status, error) {
    85              console.error("Update failed:", xhr, status, error);
    86          },
    87      });
    88  }
    89  
    90  $(document).on('click', '.contact-option', updatePQS);
    91  
    92  function updatePQS() {
    93      var selectedOption = $(this).text();
    94      $('.contact-option').removeClass('active');
    95  
    96      if (selectedOption.toLowerCase() === 'disabled') {
    97          $('.popupOverlay, .popupContent').addClass('active');
    98          $('#cancel-disable-pqs').on('click', function() {
    99              $('.popupOverlay, .popupContent').removeClass('active');
   100              $(`.contact-option:contains("Enabled")`).addClass('active');
   101          });
   102          
   103          $('#disable-pqs').on('click', function() {
   104              $('#contact-types span').text(selectedOption); 
   105              $('.popupOverlay, .popupContent').removeClass('active');
   106              $(`.contact-option:contains("Disabled")`).addClass('active');
   107              updatePersistentQueriesSetting(false);
   108          });
   109      }
   110      if(selectedOption.toLowerCase() === 'enabled') {
   111          updatePersistentQueriesSetting(true);
   112          $('#contact-types span').text(selectedOption); 
   113          $(`.contact-option:contains("Enabled")`).addClass('active');
   114      }
   115  }
   116  
   117  function setPersistentQueries(pqsEnabled) {
   118      $('.contact-option').removeClass('active');
   119      $('#contact-types span').text(pqsEnabled ? "Enabled" : "Disabled");
   120      $('.contact-option:contains("' + (pqsEnabled ? "Enabled" : "Disabled") + '")').addClass('active');
   121  }
   122  
   123  {{ .SettingsExtraFunctions }}
   124  
   125  function getSystemInfo(){
   126      $.ajax({
   127          method: "GET",
   128          url: "/api/system-info",
   129          headers: {
   130              "Content-Type": "application/json; charset=utf-8",
   131              Accept: "*/*",
   132          },
   133          dataType: "json",
   134          crossDomain: true,
   135          success: function (res) {
   136              addSystemInfoTable(res);
   137          },
   138          error: function (xhr, status, error) {
   139              console.error("Update failed:", xhr, status, error);
   140          },
   141      });
   142  }
   143  
   144  function addSystemInfoTable(systemInfo) {
   145      var table = $("#system-info-table");
   146      
   147      function createRow(header, value) {
   148          return `<tr><th>${header}</th><td>${value}</td></tr>`;
   149      }
   150  
   151      var osRow = createRow("Operating System", systemInfo.os);
   152      var cpuRow = createRow("vCPU Count", systemInfo.v_cpu);
   153      var memoryUsage = systemInfo.memory.used_percent.toFixed(2);
   154      var totalMemoryGB = (systemInfo.memory.total / Math.pow(1024, 3)).toFixed(2);
   155      var availableMemoryGB = (systemInfo.memory.free / Math.pow(1024, 3)).toFixed(2);
   156      var memoryInfo = `<div><b>Total:</b> ${totalMemoryGB} GB</div>
   157                      <div><b>Available:</b> ${availableMemoryGB} GB</div>
   158                      <div><b>Used:</b> ${memoryUsage}%</div>`;
   159      var memoryRow = createRow("Memory Usage", memoryInfo);
   160      var diskUsage = systemInfo.disk.used_percent.toFixed(2);
   161      var totalDiskGB = (systemInfo.disk.total / Math.pow(1024, 3)).toFixed(2);
   162      var availableDiskGB = (systemInfo.disk.free / Math.pow(1024, 3)).toFixed(2);
   163      var diskInfo = `<div><b>Total:</b> ${totalDiskGB} GB</div>
   164                      <div><b>Available:</b> ${availableDiskGB} GB</div>
   165                      <div><b>Used:</b> ${diskUsage}%</div>`;
   166      var diskRow = createRow("Disk Usage", diskInfo);
   167      var uptime = createRow("Process Uptime", formatUptime(systemInfo.uptime));
   168  
   169      table.append(uptime, cpuRow, memoryRow, osRow, diskRow);
   170  }
   171  
   172  function formatUptime(uptimeMinutes) {
   173      if (uptimeMinutes < 60) {
   174          return uptimeMinutes + " mins";
   175      } else if (uptimeMinutes < 24*60) {
   176          return Math.floor(uptimeMinutes / 60) + " hours";
   177      } else if (uptimeMinutes < 7*24*60) {
   178          return Math.floor(uptimeMinutes / (24*60)) + " days";
   179      } else if (uptimeMinutes < 30*24*60) {
   180          return Math.floor(uptimeMinutes / (7*24*60)) + " weeks";
   181      } else {
   182          return Math.floor(uptimeMinutes / (30*24*60)) + " months";
   183      }
   184  }
   185