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 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    public function __construct( $description, $queryType, $extra = [], $namespaces = null ) {
45        parent::__construct( $description, $queryType, $extra );
46        $this->namespaces = $namespaces;
47    }
48
49    /**
50     * @param SearchSuggestion[] $result The set of suggestion results that
51     *  will be returned to the user.
52     * @param string[][] $suggestionMetadataByDocId A map from elasticsearch
53     *  document id to the completion profile that provided the highest score
54     *  for that document id.
55     */
56    public function setResult( array $result, array $suggestionMetadataByDocId ) {
57        $maxScore = $this->maxScore;
58        foreach ( $result as $docId => $suggestion ) {
59            $index = $suggestionMetadataByDocId[$docId]['index'] ?? '';
60            $title = $suggestion->getSuggestedTitle();
61            $pageId = $suggestion->getSuggestedTitleID() ?: -1;
62            $maxScore = $maxScore !== null ? max( $maxScore, $suggestion->getScore() ) : $suggestion->getScore();
63            $this->hits[] = [
64                'title' => $title ? $title->getPrefixedText() : $suggestion->getText(),
65                'index' => $index,
66                'pageId' => (int)$pageId,
67                'score' => $suggestion->getScore(),
68                'profileName' => $suggestionMetadataByDocId[$docId]['profile'] ?? '',
69            ];
70        }
71        $this->maxScore = $maxScore !== null ? (float)$maxScore : null;
72    }
73
74    /**
75     * @return int
76     */
77    public function getElasticTookMs() {
78        return $this->suggestTookMs;
79    }
80
81    /**
82     * @return bool
83     */
84    public function isCachedResponse() {
85        return false;
86    }
87
88    /**
89     * @return array
90     */
91    public function getLogVariables() {
92        // Note this intentionally extracts data from $this->extra, rather than
93        // using it directly. The use case is small enough for this class we can
94        // be more explicit about returned variables.
95        return [
96            'query' => $this->extra['query'] ?? '',
97            'queryType' => $this->getQueryType(),
98            'index' => implode( ',', $this->indices ),
99            'elasticTookMs' => $this->getElasticTookMs(),
100            'hitsTotal' => $this->hitsTotal,
101            'maxScore' => $this->maxScore ?? 0.0,
102            'hitsReturned' => count( $this->hits ),
103            'hitsOffset' => $this->extra['offset'] ?? 0,
104            'tookMs' => $this->getTookMs(),
105        ];
106    }
107
108    /**
109     * @return array[]
110     */
111    public function getRequests() {
112        $vars = $this->getLogVariables() + [
113            'hits' => $this->hits,
114            'namespaces' => $this->namespaces,
115        ];
116        return [ $vars ];
117    }
118
119    /**
120     * @param int $totalHits
121     */
122    public function setTotalHits( $totalHits ) {
123        $this->hitsTotal = $totalHits;
124    }
125
126    /**
127     * @param int $suggestTookMs
128     */
129    public function setSuggestTookMs( $suggestTookMs ) {
130        $this->suggestTookMs = $suggestTookMs;
131    }
132
133    /**
134     * @param int $prefixTookMs
135     */
136    public function setPrefixTookMs( $prefixTookMs ) {
137        $this->prefixTookMs = $prefixTookMs;
138    }
139
140    /**
141     * Add an index used by this request
142     * @param string $indexName
143     */
144    public function addIndex( $indexName ) {
145        $this->indices[$indexName] = $indexName;
146    }
147}