Splunk - Javascript с XML - PullRequest
       13

Splunk - Javascript с XML

1 голос
/ 16 апреля 2020

Я разработал приборную панель, используя xml кодирование. В XML я ссылался на javascript.

XML: я передаю слово, введенное из поля, как выделенный знак.

Javascript: я получаю слово и обработка поиска во всех таблицах, указанных в XML. Я могу искать по одному слову. Но я хочу искать как несколько слов строки с запятой. Найдите приведенную ниже кодировку и помогите мне с javascript.

Пример. Если я ищу ключевое слово как «Splunk», оно будет выделяться, но если я буду искать как «Splunk, web, access», оно не будет отображаться. что угодно.

XML:

<form script="highlightToken.js">
  <label>Highlight_text</label>
  <fieldset>
    <input type="time" token="field1">
      <label></label>
      <default>
        <earliest>-5m@m</earliest>
        <latest>now</latest>
      </default>
    </input>
    <input type="text" token="highlightToken" searchWhenChanged="true">
      <label>Search Word</label>
      <default>splunk*</default>
      <initialValue>searchword</initialValue>
    </input>
  </fieldset>
  <row>
    <panel>
      <title>$highlightToken$</title>
      <table id="highlightTable1">
        <search id="highlightSearch1">
          <query>index=_internal
| stats count by sourcetype</query>

          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>

        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
    <panel>
      <table id="highlightTable2">
        <search id="highlightSearch2">
          <query>index=_internal 
| stats count by sourcetype
| head 5</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
  </row>
  <row>
    <panel>
      <table id="highlightTable3">
        <search id="highlightSearch3">
          <query>index=_internal 
| stats count by sourcetype
| head 5</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
    <panel>
      <table id="highlightTable4">
        <search id="highlightSearch4">
          <query>index=_internal sourcetype="splunkd"
| stats count by sourcetype
| head 5</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
  </row>
  <row>
    <panel>
      <table id="highlightTable5">
        <search id="highlightSearch5">
          <query>index=_internal 
| stats count by sourcetype
| head 5</query>
          <earliest>$field1.earliest$</earliest>
          <latest>$field1.latest$</latest>
        </search>
        <option name="drilldown">none</option>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
  </row>
</form>

Javascript |:

require([
    "underscore",
    "jquery",
    "splunkjs/mvc",
    "splunkjs/mvc/searchmanager",
    "splunkjs/mvc/tableview",
    "splunkjs/mvc/simplexml/ready!"
], function (_, $, mvc, SearchManager, TableView) {
    var defaultTokenModel = mvc.Components.get("default");
    // var strHighlightText = ""
    var strHighlightText = defaultTokenModel.get("highlightToken");
    var customCellRenderer = TableView.BaseCellRenderer.extend({
        canRender: function (cell) {
            return cell.field !== "_time";
        },
        render: function ($td, cell) {
            var strText = cell.value;
            if (strHighlightText !== "*" && strHighlightText !== "") {
                var regEx = new RegExp(strHighlightText, "gi");
                strText = strText.replace(regEx, '<b style="background:#4dff4d;">$&</b>');
            }
            $td.addClass('string').html(_.template(strText));
        }
    });
    function highlightTable() {
        var maxCount = 6;
        for (i = 0; i < maxCount; i++) {
            var tableItem = mvc.Components.get("highlightTable" + i);
            if (typeof (strHighlightText) !== "undefined" && typeof (tableItem) !== "undefined") {
                var search = mvc.Components.get("highlightSearch" + i);
                if (search !== undefined) {
                    console.log("highlightSearch:", i)
                    search.startSearch();
                }
                console.log("highlightToken:", strHighlightText, " tableId:", i)
                tableItem.getVisualization(function (tableView) {
                    tableView.addCellRenderer(new customCellRenderer());
                });
            }
        }
    }
    defaultTokenModel.on("change:highlightToken", function (model, value, options) {
        if (typeof (value) !== "undefined" || value !== "$value$") {
            strHighlightText = value;
            highlightTable();
        }
    });
    highlightTable();
});

1 Ответ

0 голосов
/ 16 апреля 2020

Вы используете регулярные выражения в JavaScript для соответствия выделенному тексту, поэтому вы можете поставить (Splunk)|(web)|(access) для соответствия нескольким фразам.

В качестве альтернативы, если вы действительно хотите go с запятой Вы должны будете изменить следующий раздел кода. Сначала вам нужно разделить strHighlightText на запятые, а затем l oop на следующий код с каждым разделенным словом

var regEx = new RegExp(strHighlightText, "gi");
strText = strText.replace(regEx, '<b style="background:#4dff4d;">$&</b>');
...