github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/static/js/test-data.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  let selectedLogSource = "";
    18  
    19  $(document).ready(function () {
    20      $('#data-ingestion,#test-data-btn').hide();
    21      if (Cookies.get('theme')) {
    22          theme = Cookies.get('theme');
    23          $('body').attr('data-theme', theme);
    24      }
    25      $('.theme-btn').on('click', themePickerHandler);
    26      let org_name;
    27  
    28      $.ajax({
    29          method: 'get',
    30          url: 'api/config',
    31          crossDomain: true,
    32          dataType: 'json',
    33          credentials: 'include'
    34      })
    35          .then((res) => {
    36              let token;
    37              {{ if .TestDataSendData }}
    38                  {{ .TestDataSendData }}
    39              {{ else }}
    40                  myOrgSendTestData(token);
    41              {{ end }}
    42          })
    43          .catch((err) => {
    44              console.log(err)
    45          });
    46          var currentUrl = window.location.href;
    47          var url = new URL(currentUrl);
    48          var baseUrl = url.protocol + '//' + url.hostname;
    49  
    50          $('#source-options').on('click', '.source-option', function() {
    51              selectedLogSource = $(this).text().trim();
    52              $('.source-option').removeClass("active");
    53              $(this).addClass("active");
    54              $('#source-selection span').html(selectedLogSource);
    55          
    56              var showDataIngestion = ['Vector', 'Logstash', 'Fluentd', 'Filebeat', 'Promtail','Elastic Bulk','Splunk HEC'].includes(selectedLogSource);
    57              $('#data-ingestion').toggle(showDataIngestion);
    58              $('#test-data-btn').toggle(!showDataIngestion);
    59          
    60              var curlCommand = 'curl -X POST "' + baseUrl + ':8081/elastic/_bulk" \\\n' +
    61                              '-H \'Content-Type: application/json\' \\\n' +
    62                              '-d \'{ "index" : { "_index" : "test" } }\n' +
    63                              '{ "name" : "john", "age":"23" }\'';
    64              $('#verify-command').text(curlCommand);
    65          
    66              switch (selectedLogSource) {
    67                  case 'Vector':
    68                  case 'Logstash':
    69                  case 'Fluentd':
    70                  case 'Filebeat':
    71                  case 'Promtail':
    72                      $('#platform-input').val(selectedLogSource);
    73                      $('#setup-instructions-link').attr('href', 'https://www.siglens.com/siglens-docs/log-ingestion/' + selectedLogSource.toLowerCase());
    74                      break;
    75                  case 'Elastic Bulk':
    76                      $('#platform-input').val(selectedLogSource);
    77                      $('#setup-instructions-link').attr('href', 'https://www.siglens.com/siglens-docs/migration/elasticsearch/fluentd');
    78                      break;
    79                  case 'Splunk HEC':
    80                      $('#platform-input').val(selectedLogSource);
    81                      $('#setup-instructions-link').attr('href', 'https://www.siglens.com/siglens-docs/migration/splunk/fluentd');
    82                      curlCommand = 'curl -X POST "' + baseUrl + ':8081/splunk/services/collector/event" \\\n' +
    83                              '-H "Authorization: A94A8FE5CCB19BA61C4C08"  \\\n' +
    84                              '-d \'{ "index" : { "_index" : "test" } }\n' +
    85                              '{ "name" : "john", "age":"23" }\'';
    86                      $('#verify-command').text(curlCommand);
    87                      break;
    88                  case 'Send Test Data':
    89                      $('#test-data-btn').show();
    90                      break;
    91                  default:
    92                      break;
    93              }
    94          });
    95          
    96  
    97      //Copy Handler
    98      $('.copyable').each(function() {
    99          var copyIcon = $('<span class="copy-icon"></span>');
   100          $(this).after(copyIcon);
   101      });
   102  
   103      $('.copy-icon').on('click', function(event) {
   104          var copyIcon = $(this);
   105          var inputOrTextarea = copyIcon.prev('.copyable');
   106          var inputValue = inputOrTextarea.val();
   107      
   108          var tempInput = document.createElement("textarea");
   109          tempInput.value = inputValue;
   110          document.body.appendChild(tempInput);
   111          tempInput.select();
   112          document.execCommand("copy");
   113          document.body.removeChild(tempInput);
   114      
   115          copyIcon.addClass('success');
   116          setTimeout(function() {
   117              copyIcon.removeClass('success'); 
   118          }, 1000);
   119      });
   120      
   121      {{ .Button1Function }}
   122  })
   123  
   124  function dropdown() {
   125      $('.dropdown-option').toggleClass('active');
   126  }
   127  
   128  function sendTestData(e, token) {
   129      
   130      if (token) {
   131          sendTestDataWithBearerToken(token).then((res) => {
   132              showSendTestDataUpdateToast('Sent Test Data Successfully');
   133              let testDataBtn = document.getElementById("test-data-btn");
   134              testDataBtn.disabled = false;
   135          })
   136              .catch((err) => {
   137                  console.log(err)
   138                  showSendTestDataUpdateToast('Error Sending Test Data');
   139                  let testDataBtn = document.getElementById("test-data-btn");
   140                  testDataBtn.disabled = false;
   141              });
   142      } else {
   143          sendTestDataWithoutBearerToken().then((res) => {
   144              showSendTestDataUpdateToast('Sent Test Data Successfully');
   145              let testDataBtn = document.getElementById("test-data-btn");
   146              testDataBtn.disabled = false;
   147          })
   148              .catch((err) => {
   149                  console.log(err)
   150                  showSendTestDataUpdateToast('Error Sending Test Data');
   151                  let testDataBtn = document.getElementById("test-data-btn");
   152                  testDataBtn.disabled = false;
   153              });
   154      }
   155  
   156          
   157  
   158      function sendTestDataWithBearerToken( token) {
   159          return new Promise((resolve, reject) => {
   160              $.ajax({
   161                  method: 'post',
   162                  url: '/api/sampledataset_bulk',
   163                  headers: {
   164                      'Authorization': `Bearer ${token}`,
   165                  },
   166                  crossDomain: true,
   167                  dataType: 'json',
   168                  credentials: 'include'
   169              }).then((res) => {
   170                  resolve(res);
   171              })
   172                  .catch((err) => {
   173                      console.log(err);
   174                      reject(err);
   175                  });
   176          });
   177      }
   178  
   179      function sendTestDataWithoutBearerToken() {
   180          return new Promise((resolve, reject) => {
   181              $.ajax({
   182                  method: 'post',
   183                  url:  '/api/sampledataset_bulk',
   184                  crossDomain: true,
   185                  dataType: 'json',
   186                  credentials: 'include'
   187              }).then((res) => {
   188                  resolve(res);
   189              })
   190                  .catch((err) => {
   191                      console.log(err);
   192                      reject(err);
   193                  });
   194          });
   195      }
   196  }