<commit_before>import { addElement } from '../../../assets/src/utils';

/**
 * Admin Elasticsearch configuration
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at https://mozilla.org/MPL/2.0/.
 *
 * @package   phpMyFAQ
 * @author    <NAME>
 * @copyright 2022 phpMyFAQ Team
 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      https://www.phpmyfaq.de
 * @since     2022-03-20
 */

export const handleElasticsearch = () => {
  const buttons = document.querySelectorAll('button.pmf-elasticsearch');

  if (buttons) {
    buttons.forEach((element) => {
      element.addEventListener('click', (event) => {
        event.preventDefault();

        const action = event.target.getAttribute('data-action');

        fetch(`index.php?action=ajax&ajax=elasticsearch&ajaxaction=${action}`)
          .then(async (response) => {
            if (response.status === 200) {
              return response.json();
            }
            throw new Error('Network response was not ok.');
          })
          .then((response) => {
            const stats = document.getElementById('pmf-elasticsearch-result');
            stats.insertAdjacentElement(
              'afterend',
              addElement('div', { classList: 'alert alert-success', innerText: response.success })
            );

            setInterval(elasticsearchStats, 5000);
          })
          .catch((error) => {
            const stats = document.getElementById('pmf-elasticsearch-result');
            stats.insertAdjacentElement(
              'afterend',
              addElement('div', { classList: 'alert alert-danger', innerText: error })
            );
          });
      });
    });

    const elasticsearchStats = () => {
      const div = document.getElementById('pmf-elasticsearch-stats');
      div.innerHTML = '';
      fetch(`index.php?action=ajax&ajax=elasticsearch&ajaxaction=stats`)
        .then((response) => {
          return response.json();
        })
        .then((stats) => {
          const count = stats?.indices?.phpmyfaq?.total?.docs?.count;
          const sizeInBytes = stats?.indices?.phpmyfaq?.total?.store?.size_in_bytes;
          let html = '<dl class="row">';
          html += `<dt class="col-sm-3">Documents</dt><dd class="col-sm-9">${count}</dd>`;
          html += `<dt class="col-sm-3">Storage size</dt><dd class="col-sm-9">${sizeInBytes}</dd>`;
          html += '</dl>';
          div.innerHTML = html;
        });
    };

    elasticsearchStats();<commit_msg>fix: added check if variable is not null<commit_after>import { addElement } from '../../../assets/src/utils';

/**
 * Admin Elasticsearch configuration
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at https://mozilla.org/MPL/2.0/.
 *
 * @package   phpMyFAQ
 * @author    <NAME>
 * @copyright 2022 phpMyFAQ Team
 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      https://www.phpmyfaq.de
 * @since     2022-03-20
 */

export const handleElasticsearch = () => {
  const buttons = document.querySelectorAll('button.pmf-elasticsearch');

  if (buttons) {
    buttons.forEach((element) => {
      element.addEventListener('click', (event) => {
        event.preventDefault();

        const action = event.target.getAttribute('data-action');

        fetch(`index.php?action=ajax&ajax=elasticsearch&ajaxaction=${action}`)
          .then(async (response) => {
            if (response.status === 200) {
              return response.json();
            }
            throw new Error('Network response was not ok.');
          })
          .then((response) => {
            const stats = document.getElementById('pmf-elasticsearch-result');
            stats.insertAdjacentElement(
              'afterend',
              addElement('div', {
                classList: 'alert alert-success',
                innerText: response.success,
              })
            );

            setInterval(elasticsearchStats, 5000);
          })
          .catch((error) => {
            const stats = document.getElementById('pmf-elasticsearch-result');
            stats.insertAdjacentElement(
              'afterend',
              addElement('div', { classList: 'alert alert-danger', innerText: error })
            );
          });
      });
    });

    const elasticsearchStats = () => {
      const div = document.getElementById('pmf-elasticsearch-stats');
      if (div) {
        div.innerHTML = '';
        fetch(`index.php?action=ajax&ajax=elasticsearch&ajaxaction=stats`)
          .then((response) => {
            return response.json();
          })
          .then((stats) => {
            const count = stats?.indices?.phpmyfaq?.total?.docs?.count;
            const sizeInBytes = stats?.indices?.phpmyfaq?.total?.store?.size_in_bytes;
            let html = '<dl class="row">';
            html += `<dt class="col-sm-3">Documents</dt><dd class="col-sm-9">${count}</dd>`;
            html += `<dt class="col-sm-3">Storage size</dt><dd class="col-sm-9">${sizeInBytes}</dd>`;
            html += '</dl>';
            div.innerHTML = html;
          });
      }
    };

    elasticsearchStats();
{'fraction_non_alphanumeric': 0.1533049435289761, 'fraction_numerical': 0.011479355674875022, 'mean_word_length': 2.2002369668246446, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 43, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3301818', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1491, 'n_words': 310}