github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/static/js/minion-searches.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  
    18  'use strict';
    19  
    20  let gridDiv = null;
    21  let resultRowData=[];
    22  
    23  $(document).ready(function () {
    24      if (Cookies.get('theme')) {
    25          theme = Cookies.get('theme');
    26          $('body').attr('data-theme', theme);
    27      }
    28      $('.theme-btn').on('click', themePickerHandler);
    29      getAllMinionSearches()
    30  });
    31  
    32  function getAllMinionSearches(){
    33    fetch("/api/minionsearch/allMinionSearches")
    34    .then(res => res.json())
    35    .then(data=> displayLogsResults(data.minionSearches));
    36  }
    37  
    38  const columnDefs=[
    39      { headerName: "Log Statement", field: "log_text", initialWidth:400,autoHeight: true,},
    40      { headerName: "Filename", field: "filename",initialWidth:400,autoHeight: true,},
    41      { headerName: "Line Number", field: "line_number",initialWidth:140},
    42      { headerName: "Level", field: "log_level",initialWidth: 140 },
    43      { headerName: "State", field: "state",initialWidth: 140 },
    44  ];
    45  
    46  const gridOptions = {
    47      defaultColDef: {
    48        cellStyle: { 'text-align': "left" },
    49        resizable: true,
    50        sortable: true,
    51        minWidth: 120,
    52        animateRows: true,
    53        readOnlyEdit: true,
    54        autoHeight: true,
    55      },
    56      headerHeight:32,
    57      columnDefs:columnDefs,
    58      pagination: true,
    59      paginationAutoPageSize: true,
    60      onRowClicked: onRowClicked,
    61  };
    62  
    63  function displayLogsResults(res){
    64      if (gridDiv === null) {
    65          gridDiv = document.querySelector('#ag-grid');
    66          new agGrid.Grid(gridDiv, gridOptions);
    67      }
    68      gridOptions.api.setColumnDefs(columnDefs);
    69      let newRow = new Map();
    70      $.each(res, function (key, value) {
    71          const details = value.minionSearchDetails;
    72          //later implement from backend
    73          const state = value.alertInfo.state;
    74          const stateLabel = state === 0 ? "Normal" : state === 1 ? "Firing" : null;
    75          
    76          newRow.set("log_text", details.log_text);
    77          newRow.set("filename", details.filename);
    78          newRow.set("line_number", details.line_number);
    79          newRow.set("log_level", details.log_level);
    80          newRow.set("state", stateLabel);
    81          
    82          resultRowData.push(Object.fromEntries(newRow));
    83      })
    84      gridOptions.api.setRowData(resultRowData);
    85      gridOptions.api.sizeColumnsToFit();
    86  }
    87  
    88  function onRowClicked(event) {
    89    const resultBody = $('.result-pane .result-body');
    90    resultBody.empty();
    91    const rowData = event.data;
    92    const logText = rowData.log_text;
    93    const filename = rowData.filename;
    94    const lineNumber = rowData.line_number;
    95    const logLevel = rowData.log_level;
    96  
    97    const rowDetailsDiv = document.createElement('div');
    98    rowDetailsDiv.classList.add('row-details');
    99  
   100    const content = `
   101        <p><strong>Log Text:</strong> ${logText}</p>
   102        <p><strong>Filename:</strong> ${filename}</p>
   103        <p><strong>Line Number:</strong> ${lineNumber}</p>
   104        <p><strong>Log Level:</strong> ${logLevel}</p>
   105    `;
   106  
   107    rowDetailsDiv.innerHTML = content;
   108    resultBody.append(rowDetailsDiv);
   109  }