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

     1  'use strict';
     2  let redMetrics ={
     3      "indexName":  "red-traces",
     4      "queryLanguage": "Splunk QL"
     5     
     6  }
     7  var RateCountChart;
     8  var ErrCountChart;
     9  var LatenciesChart;
    10  $(document).ready(() => {
    11      setupEventHandlers();
    12      $(".theme-btn").on("click", themePickerHandler);
    13      if (Cookies.get("theme")) {
    14          theme = Cookies.get("theme");
    15          $("body").attr("data-theme", theme);
    16      }
    17      $('.theme-btn').on('click', getOneServiceOverview);
    18  
    19     
    20      const serviceName = getParameterFromUrl('service');
    21      redMetrics['searchText']="service="  + serviceName + "";
    22      $('.service-name').text(serviceName);
    23      
    24      let stDate = "now-1h";
    25      let endDate = "now";
    26      datePickerHandler(stDate, endDate, stDate);
    27      $('.range-item').on('click', isGraphsDatePickerHandler);
    28      let data = getTimeRange();
    29      
    30      redMetrics = {... redMetrics, ... data}
    31      getOneServiceOverview()
    32     
    33      $(".service-health-text").click(function () {
    34          window.location.href = "../service-health.html";
    35      })
    36  });
    37  
    38  function isGraphsDatePickerHandler(evt) {
    39      evt.preventDefault();
    40      getOneServiceOverview()
    41      $('#daterangepicker').hide();
    42  }
    43  
    44  function getTimeRange() {
    45      return {
    46          'startEpoch': filterStartDate || "now-1h",
    47          'endEpoch': filterEndDate || "now",
    48      };
    49  }
    50  function getParameterFromUrl(param) {
    51      const urlParams = new URLSearchParams(window.location.search);
    52      return urlParams.get(param);
    53  }
    54  let gridLineColor;
    55  let tickColor;
    56  
    57  function getOneServiceOverview(){
    58      let endDate = filterEndDate || "now";
    59      let stDate = filterStartDate || "now-1h";
    60      let data = getTimeRange();
    61      redMetrics = {... redMetrics, ... data}
    62      $.ajax({
    63          method: "POST",
    64          url: "api/search",
    65          headers: {
    66              'Content-Type': 'application/json; charset=utf-8',
    67              'Accept': '*/*'
    68          },
    69          data: JSON.stringify(redMetrics),
    70          dataType: 'json',
    71          crossDomain: true,
    72      }).then(function (res) {
    73          if ($('body').attr('data-theme') == "light") {
    74              gridLineColor = "#DCDBDF";
    75              tickColor = "#160F29";
    76          }
    77          else {
    78              gridLineColor = "#383148";
    79              tickColor = "#FFFFFF"
    80          }
    81          if (RateCountChart !== undefined) {
    82              RateCountChart.destroy();
    83          }
    84          if (ErrCountChart !== undefined) {
    85              ErrCountChart.destroy();
    86          }
    87          if (LatenciesChart!==undefined){
    88              LatenciesChart.destroy();
    89          }
    90          rateChart(res.hits.records,gridLineColor,tickColor);
    91          errorChart(res.hits.records,gridLineColor,tickColor);
    92          latenciesChart(res.hits.records,gridLineColor,tickColor);
    93      })
    94  }
    95  
    96  function rateChart(rateData,gridLineColor,tickColor) {
    97      let graph_data = []
    98      for(let data of rateData){
    99          graph_data.push({
   100              x : new Date(data.timestamp).toISOString().split('T').join(" "),
   101              y: data.rate
   102          })
   103      }
   104      var RateCountChartCanvas = $("#ServiceHealthChart").get(0).getContext("2d");
   105      RateCountChart = new Chart(RateCountChartCanvas, {
   106          type: 'line',
   107          data: {
   108              datasets: [
   109                  {
   110                      label: 'Rate',
   111                      data: graph_data,
   112                      borderColor: ['rgb(99,71,217)'],
   113                      yAxisID: 'y',
   114                      pointStyle: 'circle',
   115                      pointRadius: 5,
   116                      pointBorderColor: ['rgb(99,71,217)'],
   117                      fill: false,
   118                  },
   119  
   120              ]
   121          },
   122          options: {
   123              responsive: true,
   124              interaction: {
   125                  intersect: false,
   126                  mode: 'index',
   127              },
   128              scales: {
   129                  y: {
   130                      beginAtZero: true,
   131                      ticks: {
   132                          color: tickColor,
   133                      },
   134                      grid: {
   135                          color: gridLineColor,
   136                      },
   137                  },
   138                  x: {
   139                      ticks: {
   140                          color: tickColor,
   141                      },
   142                      grid: {
   143                          color: gridLineColor,
   144                      },
   145                  }
   146              },
   147              plugins: {
   148                  legend: {
   149                      display: false
   150                  },
   151              }
   152          }
   153      });
   154      return RateCountChart;
   155  }
   156  
   157  function errorChart(errorData,gridLineColor,tickColor) {
   158      let graph_data_err = []
   159      for(let data of errorData){
   160              let formatted_date = new Date(data.timestamp).toISOString().split('T').join(" ")
   161              graph_data_err.push({
   162                  x : formatted_date,
   163                  y: data.error_rate
   164              }) 
   165      }
   166      var ErrorCountChartCanvas = $("#ServiceHealthChartErr").get(0).getContext("2d");
   167      ErrCountChart = new Chart(ErrorCountChartCanvas, {
   168          type: 'line',
   169          data: {
   170              datasets: [
   171                  {
   172                      label: 'Error Rate',
   173                      data: graph_data_err,
   174                      borderColor: ['rgb(99,71,217)'],
   175                      yAxisID: 'y',
   176                      pointStyle: 'circle',
   177                      pointRadius: 5,
   178                      pointBorderColor: ['rgb(99,71,217)'],
   179                      fill: false,
   180                  },
   181  
   182              ]
   183          },
   184          options: {
   185              responsive: true,
   186              interaction: {
   187                  intersect: false,
   188                  mode: 'index',
   189              },
   190              scales: {
   191                  y: {
   192                      beginAtZero: true,
   193                      ticks: {
   194                          color: tickColor,
   195                      },
   196                      grid: {
   197                          color: gridLineColor,
   198                      },
   199                  },
   200                  x: {
   201                      ticks: {
   202                          color: tickColor,
   203                      },
   204                      grid: {
   205                          color: gridLineColor,
   206                      },
   207                  }
   208              },
   209              plugins: {
   210                  legend: {
   211                      display: false
   212                  },
   213              }
   214          }
   215      });
   216      return ErrCountChart;
   217  }
   218  
   219  
   220  function latenciesChart(latenciesData,gridLineColor,tickColor) {
   221      let graph_data_latencies = {
   222          p50: [],
   223          p90: [],
   224          p99: [],
   225      };
   226  
   227      for (let data of latenciesData) {
   228          const timestamp = new Date(data.timestamp).toISOString().split('T').join(" ");
   229          graph_data_latencies.p50.push({ x: timestamp, y: data.p50 });
   230          graph_data_latencies.p90.push({ x: timestamp, y: data.p90 });
   231          graph_data_latencies.p99.push({ x: timestamp, y: data.p99 });
   232      }
   233      var LatenciesChartCanvas = $("#ServiceHealthChart2").get(0).getContext("2d");
   234      LatenciesChart = new Chart(LatenciesChartCanvas, {
   235          type: 'line',
   236          data: {
   237              datasets: [
   238                  {
   239                      label: 'P50 Latency',
   240                      data: graph_data_latencies.p50,
   241                      borderColor: '#FF6484',
   242                      yAxisID: 'y',
   243                      pointStyle: 'circle',
   244                      pointRadius: 5,
   245                      pointBorderColor: ['#FF6484'],
   246                      fill: false,
   247                  },
   248                  {
   249                      label: 'P90 Latency',
   250                      data: graph_data_latencies.p90,
   251                      borderColor: '#36A2EB',
   252                      yAxisID: 'y',
   253                      pointStyle: 'circle',
   254                      pointRadius: 5,
   255                      pointBorderColor: '#36A2EB',
   256                      fill: false,
   257                  },
   258                  {
   259                      label: 'P99 Latency',
   260                      data: graph_data_latencies.p99,
   261                      yAxisID: 'y',
   262                      pointStyle: 'circle',
   263                      pointRadius: 5,
   264                      pointBorderColor: "#4BC0C0",
   265                      borderColor: "#4BC0C0",
   266                      fill: false,
   267                  },
   268              ]
   269          },
   270          options: {
   271              responsive: true,
   272              interaction: {
   273                  intersect: false,
   274                  mode: 'index',
   275              },
   276              scales: {
   277                  y: {
   278                      beginAtZero: true,
   279                      ticks: {
   280                          color: tickColor,
   281                      },
   282                      grid: {
   283                          color: gridLineColor,
   284                      },
   285                  },
   286                  x: {
   287                      ticks: {
   288                          color: tickColor,
   289                      },
   290                      grid: {
   291                          color: gridLineColor,
   292                      },
   293                  }
   294              },
   295              plugins:{
   296                  legend: {
   297                      position: 'bottom',
   298                      labels: {
   299                          boxHeight: 10,
   300                          padding: 20,
   301                      }
   302                  },
   303              }
   304          }
   305      });
   306  
   307      return LatenciesChart;
   308  }