Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
CompletionRequestLog
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 10
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setResult
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 getElasticTookMs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isCachedResponse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLogVariables
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 getRequests
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 setTotalHits
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSuggestTookMs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPrefixTookMs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addIndex
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch;
4
5use MediaWiki\Search\SearchSuggestion;
6
7class CompletionRequestLog extends BaseRequestLog {
8
9    /**
10     * @var array
11     */
12    private $hits = [];
13
14    /**
15     * @var float|null
16     */
17    private $maxScore = null;
18
19    /**
20     * @var string[]
21     */
22    private $indices = [];
23
24    /**
25     * @var int
26     */
27    private $suggestTookMs = 0;
28
29    /**
30     * @var int
31     */
32    private $prefixTookMs = 0;
33
34    /**
35     * @var int
36     */
37    private $hitsTotal;
38
39    /**
40     * @var int[]|null
41     */
42    private $namespaces;
43
44    /**
45     * @param string $description
46     * @param string $queryType
47     * @param array $extra
48     * @param int[]|null $namespaces
49     */
50    public function __construct( $description, $queryType, $extra = [], $namespaces = null ) {
51        parent::__construct( $description, $queryType, $extra );
52        $this->namespaces = $namespaces;
53    }
54
55    /**
56     * @param SearchSuggestion[] $result The set of suggestion results that
57     *  will be returned to the user.
58     * @param string[][] $suggestionMetadataByDocId A map from elasticsearch
59     *  document id to the completion profile that provided the highest score
60     *  for that document id.
61     */
62    public function setResult( array $result, array $suggestionMetadataByDocId ) {
63        $maxScore = $this->maxScore;
64        foreach ( $result as $docId => $suggestion ) {
65            $index = $suggestionMetadataByDocId[$docId]['index'] ?? '';
66            $title = $suggestion->getSuggestedTitle();
67            $pageId = $suggestion->getSuggestedTitleID() ?: -1;
68            $maxScore = $maxScore !== null ? max( $maxScore, $suggestion->getScore() ) : $suggestion->getScore();
69            $this->hits[] = [
70                'title' => $title ? $title->getPrefixedText() : $suggestion->getText(),
71                'index' => $index,
72                'pageId' => (int)$pageId,
73                'score' => $suggestion->getScore(),
74                'profileName' => $suggestionMetadataByDocId[$docId]['profile'] ?? '',
75            ];
76        }
77        $this->maxScore = $maxScore !== null ? (float)$maxScore : null;
78    }
79
80    /**
81     * @return int
82     */
83    public function getElasticTookMs() {
84        return $this->suggestTookMs;
85    }
86
87    /**
88     * @return bool
89     */
90    public function isCachedResponse() {
91        return false;
92    }
93
94    /**
95     * @return array
96     */
97    public function getLogVariables() {
98        // Note this intentionally extracts data from $this->extra, rather than
99        // using it directly. The use case is small enough for this class we can
100        // be more explicit about returned variables.
101        return [
102            'query' => $this->extra['query'] ?? '',
103            'queryType' => $this->getQueryType(),
104            'index' => implode( ',', $this->indices ),
105            'elasticTookMs' => $this->getElasticTookMs(),
106            'hitsTotal' => $this->hitsTotal,
107            'maxScore' => $this->maxScore ?? 0.0,
108            'hitsReturned' => count( $this->hits ),
109            'hitsOffset' => $this->extra['offset'] ?? 0,
110            'tookMs' => $this->getTookMs(),
111        ];
112    }
113
114    /**
115     * @return array[]
116     */
117    public function getRequests() {
118        $vars = $this->getLogVariables() + [
119            'hits' => $this->hits,
120            'namespaces' => $this->namespaces,
121        ];
122        return [ $vars ];
123    }
124
125    /**
126     * @param int $totalHits
127     */
128    public function setTotalHits( $totalHits ) {
129        $this->hitsTotal = $totalHits;
130    }
131
132    /**
133     * @param int $suggestTookMs
134     */
135    public function setSuggestTookMs( $suggestTookMs ) {
136        $this->suggestTookMs = $suggestTookMs;
137    }
138
139    /**
140     * @param int $prefixTookMs
141     */
142    public function setPrefixTookMs( $prefixTookMs ) {
143        $this->prefixTookMs = $prefixTookMs;
144    }
145
146    /**
147     * Add an index used by this request
148     * @param string $indexName
149     */
150    public function addIndex( $indexName ) {
151        $this->indices[$indexName] = $indexName;
152    }
153}