DebugAction.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * xunsearch DebugPanel class file
  4. *
  5. * @author hightman
  6. * @link http://www.xunsearch.com/
  7. * @copyright Copyright &copy; 2014 HangZhou YunSheng Network Technology Co., Ltd.
  8. * @license http://www.xunsearch.com/license/
  9. * @version $Id$
  10. */
  11. namespace hightman\xunsearch;
  12. use yii\base\Action;
  13. use yii\base\NotSupportedException;
  14. use yii\helpers\ArrayHelper;
  15. use yii\helpers\Json;
  16. use yii\web\HttpException;
  17. use yii\web\Response;
  18. use Yii;
  19. /**
  20. * Debug Action is used by [[DebugPanel]] to perform xunsearch queries using ajax.
  21. *
  22. * @author hightman <hightman@twomice.net>
  23. * @since 1.4.9
  24. */
  25. class DebugAction extends Action
  26. {
  27. /**
  28. * @var string the xunsearch component name
  29. */
  30. public $com;
  31. /**
  32. * @var DebugPanel
  33. */
  34. public $panel;
  35. /**
  36. * @var \yii\debug\controllers\DefaultController
  37. */
  38. public $controller;
  39. public function run($logId, $tag)
  40. {
  41. $this->controller->loadData($tag);
  42. $timings = $this->panel->calculateTimings();
  43. ArrayHelper::multisort($timings, 3, SORT_DESC);
  44. if (!isset($timings[$logId])) {
  45. throw new HttpException(404, 'Log message not found.');
  46. }
  47. $message = $timings[$logId][1];
  48. if (($pos = mb_strpos($message, "#")) !== false) {
  49. $url = mb_substr($message, 0, $pos);
  50. $body = mb_substr($message, $pos + 1);
  51. } else {
  52. $url = $message;
  53. $body = null;
  54. }
  55. list($dbname, $action) = explode('.', $url);
  56. /* @var $db Database */
  57. $db = Yii::$app->get($this->com)->getDatabase($dbname);
  58. $time = microtime(true);
  59. switch ($action) {
  60. case 'findAll':
  61. $docs = $db->getSearch()->setLimit(3)->setQuery($body)->search();
  62. $result = '<strong>Estimated Matched: </strong>' . $db->getLastCount();
  63. foreach ($docs as $doc) {
  64. $result .= '<br/>' . $doc->rank() . '. (' . $doc->percent() . '%)';
  65. $result .= "<br/>" . Json::encode($doc->getFields(), 448) . "\n";
  66. }
  67. if ($db->getLastCount() > 3) {
  68. $result .= '<br/> ... other ' . ($db->getLastCount() - 3) . ' results ...';
  69. }
  70. break;
  71. case 'findOne':
  72. $docs = $db->getSearch()->setLimit(1)->setQuery($body)->search();
  73. if (count($docs) === 0) {
  74. $result = '<span class="label label-danger">no found</span>';
  75. } else {
  76. $result = "<br/>\n" . Json::encode($docs[0]->getFields(), 448);
  77. }
  78. break;
  79. case 'count':
  80. $count = $db->getSearch()->setQuery($body)->count();
  81. $result = '<strong>Estimated Matched: </strong>' . $count;
  82. break;
  83. default:
  84. throw new NotSupportedException("Action '$action' is not supported by xunsearch.");
  85. }
  86. $result = '<strong>DB Total: </strong>' . $db->getDbTotal() . '<br/>'
  87. . '<strong>Parsed Query: </strong>' . $db->getQuery() . '<br/>' . $result;
  88. Yii::$app->response->format = Response::FORMAT_JSON;
  89. return [
  90. 'time' => sprintf('%.1f ms', (microtime(true) - $time) * 1000),
  91. 'result' => $result,
  92. ];
  93. }
  94. }