<commit_before><?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\elasticsearch;

use yii\debug\Panel;
use yii\helpers\ArrayHelper;
use yii\log\Logger;
use yii\helpers\Html;
use yii\web\View;

/**
 * Debugger panel that collects and displays elasticsearch queries performed.
 *
 * @author <NAME> <<EMAIL>>
 * @since 2.0
 */
class DebugPanel extends Panel
{
	public $db = 'elasticsearch';

	public function init()
	{
		$this->actions['elasticsearch-query'] = [
			'class' => 'yii\\elasticsearch\\DebugAction',
			'panel' => $this,
			'db' => $this->db,
		];
	}

	public function getName()
	{
		return 'Elasticsearch';
	}

	public function getSummary()
	{
		$timings = $this->calculateTimings();
		$queryCount = count($timings);
		$queryTime = 0;
		foreach ($timings as $timing) {
			$queryTime += $timing[3];
		}
		$queryTime = number_format($queryTime * 1000) . ' ms';
		$url = $this->getUrl();
		$output = <<<EOD
<div class="yii-debug-toolbar-block">
	<a href="$url" title="Executed $queryCount elasticsearch queries which took $queryTime.">
		ES <span class="label">$queryCount</span> <span class="label">$queryTime</span>
	</a>
</div>
EOD;
		return $queryCount > 0 ? $output : '';
	}

	public function getDetail()
	{
		$timings = $this->calculateTimings();
		ArrayHelper::multisort($timings, 3, SORT_DESC);
		$rows = [];
		$i = 0;
		foreach ($timings as $logId => $timing) {
			$duration = sprintf('%.1f ms', $timing[3] * 1000);
			$message = $timing[1];
			$traces = $timing[4];
			if (($pos = mb_strpos($message, "#")) !== false) {
				$url = mb_substr($message, 0, $pos);
				$body = mb_substr($message, $pos + 1);
			} else {
				$url = $message;
				$body = null;
			}
			$traceString = '';
			if (!empty($traces)) {
				$traceString .= Html::ul($traces, [
					'class' => 'trace',
					'item' => function ($trace) {
						return "<li>{$trace['file']}({$trace['line']})</li>";
					},
				]);
			}
			$ajaxUrl = Html::url(['elasticsearch-query', 'logId' => $logId, 'tag' => $this->tag]);
			\Yii::$app->view->registerJs(<<<JS
$('#elastic-link-$i').on('click', function() {
	var result = $('#elastic-result-$i');<commit_msg>#2630: `yii\heplers\Html::url` moved to new `yii\helpers\Url::create`<commit_after><?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\elasticsearch;

use yii\debug\Panel;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\log\Logger;
use yii\helpers\Html;
use yii\web\View;

/**
 * Debugger panel that collects and displays elasticsearch queries performed.
 *
 * @author <NAME> <<EMAIL>>
 * @since 2.0
 */
class DebugPanel extends Panel
{
	public $db = 'elasticsearch';

	public function init()
	{
		$this->actions['elasticsearch-query'] = [
			'class' => 'yii\\elasticsearch\\DebugAction',
			'panel' => $this,
			'db' => $this->db,
		];
	}

	public function getName()
	{
		return 'Elasticsearch';
	}

	public function getSummary()
	{
		$timings = $this->calculateTimings();
		$queryCount = count($timings);
		$queryTime = 0;
		foreach ($timings as $timing) {
			$queryTime += $timing[3];
		}
		$queryTime = number_format($queryTime * 1000) . ' ms';
		$url = $this->getUrl();
		$output = <<<EOD
<div class="yii-debug-toolbar-block">
	<a href="$url" title="Executed $queryCount elasticsearch queries which took $queryTime.">
		ES <span class="label">$queryCount</span> <span class="label">$queryTime</span>
	</a>
</div>
EOD;
		return $queryCount > 0 ? $output : '';
	}

	public function getDetail()
	{
		$timings = $this->calculateTimings();
		ArrayHelper::multisort($timings, 3, SORT_DESC);
		$rows = [];
		$i = 0;
		foreach ($timings as $logId => $timing) {
			$duration = sprintf('%.1f ms', $timing[3] * 1000);
			$message = $timing[1];
			$traces = $timing[4];
			if (($pos = mb_strpos($message, "#")) !== false) {
				$url = mb_substr($message, 0, $pos);
				$body = mb_substr($message, $pos + 1);
			} else {
				$url = $message;
				$body = null;
			}
			$traceString = '';
			if (!empty($traces)) {
				$traceString .= Html::ul($traces, [
					'class' => 'trace',
					'item' => function ($trace) {
						return "<li>{$trace['file']}({$trace['line']})</li>";
					},
				]);
			}
			$ajaxUrl = Url::create(['elasticsearch-query', 'logId' => $logId, 'tag' => $this->tag]);
			\Yii::$app->view->registerJs(<<<JS
$('#elastic-link-$i').on('click', function() {
	var result = $('#elastic-result-$i');
{'fraction_non_alphanumeric': 0.20892583679719973, 'fraction_numerical': 0.011813607525705535, 'mean_word_length': 3.8126315789473684, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 63, 'https://': 0, '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': '5645714', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1669, 'n_words': 350}