* @since 1.4.9
*/
class DebugAction extends Action
{
/**
* @var string the xunsearch component name
*/
public $com;
/**
* @var DebugPanel
*/
public $panel;
/**
* @var \yii\debug\controllers\DefaultController
*/
public $controller;
public function run($logId, $tag)
{
$this->controller->loadData($tag);
$timings = $this->panel->calculateTimings();
ArrayHelper::multisort($timings, 3, SORT_DESC);
if (!isset($timings[$logId])) {
throw new HttpException(404, 'Log message not found.');
}
$message = $timings[$logId][1];
if (($pos = mb_strpos($message, "#")) !== false) {
$url = mb_substr($message, 0, $pos);
$body = mb_substr($message, $pos + 1);
} else {
$url = $message;
$body = null;
}
list($dbname, $action) = explode('.', $url);
/* @var $db Database */
$db = Yii::$app->get($this->com)->getDatabase($dbname);
$time = microtime(true);
switch ($action) {
case 'findAll':
$docs = $db->getSearch()->setLimit(3)->setQuery($body)->search();
$result = 'Estimated Matched: ' . $db->getLastCount();
foreach ($docs as $doc) {
$result .= '
' . $doc->rank() . '. (' . $doc->percent() . '%)';
$result .= "
" . Json::encode($doc->getFields(), 448) . "\n";
}
if ($db->getLastCount() > 3) {
$result .= '
... other ' . ($db->getLastCount() - 3) . ' results ...';
}
break;
case 'findOne':
$docs = $db->getSearch()->setLimit(1)->setQuery($body)->search();
if (count($docs) === 0) {
$result = 'no found';
} else {
$result = "
\n" . Json::encode($docs[0]->getFields(), 448);
}
break;
case 'count':
$count = $db->getSearch()->setQuery($body)->count();
$result = 'Estimated Matched: ' . $count;
break;
default:
throw new NotSupportedException("Action '$action' is not supported by xunsearch.");
}
$result = 'DB Total: ' . $db->getDbTotal() . '
'
. 'Parsed Query: ' . $db->getQuery() . '
' . $result;
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'time' => sprintf('%.1f ms', (microtime(true) - $time) * 1000),
'result' => $result,
];
}
}