1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Level23\Druid\Queries; |
5
|
|
|
|
6
|
|
|
use Level23\Druid\Types\Granularity; |
7
|
|
|
use Level23\Druid\Filters\FilterInterface; |
8
|
|
|
use Level23\Druid\Context\ContextInterface; |
9
|
|
|
use Level23\Druid\Responses\TopNQueryResponse; |
10
|
|
|
use Level23\Druid\Dimensions\DimensionInterface; |
11
|
|
|
use Level23\Druid\Collections\IntervalCollection; |
12
|
|
|
use Level23\Druid\DataSources\DataSourceInterface; |
13
|
|
|
use Level23\Druid\Collections\AggregationCollection; |
14
|
|
|
use Level23\Druid\Collections\VirtualColumnCollection; |
15
|
|
|
use Level23\Druid\Collections\PostAggregationCollection; |
16
|
|
|
|
17
|
|
|
class TopNQuery implements QueryInterface |
18
|
|
|
{ |
19
|
|
|
protected DataSourceInterface $dataSource; |
20
|
|
|
|
21
|
|
|
protected IntervalCollection $intervals; |
22
|
|
|
|
23
|
|
|
protected Granularity $granularity; |
24
|
|
|
|
25
|
|
|
protected DimensionInterface $dimension; |
26
|
|
|
|
27
|
|
|
protected ?VirtualColumnCollection $virtualColumns = null; |
28
|
|
|
|
29
|
|
|
protected int $threshold; |
30
|
|
|
|
31
|
|
|
protected string $metric; |
32
|
|
|
|
33
|
|
|
protected ?FilterInterface $filter = null; |
34
|
|
|
|
35
|
|
|
protected ?AggregationCollection $aggregations = null; |
36
|
|
|
|
37
|
|
|
protected ?PostAggregationCollection $postAggregations = null; |
38
|
|
|
|
39
|
|
|
protected ?ContextInterface $context = null; |
40
|
|
|
|
41
|
|
|
protected bool $descending = true; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* TopNQuery constructor. |
45
|
|
|
* |
46
|
|
|
* @param DataSourceInterface $dataSource |
47
|
|
|
* @param IntervalCollection $intervals |
48
|
|
|
* @param DimensionInterface $dimension |
49
|
|
|
* @param int $threshold |
50
|
|
|
* @param string $metric |
51
|
|
|
* @param string|Granularity $granularity |
52
|
|
|
*/ |
53
|
3 |
|
public function __construct( |
54
|
|
|
DataSourceInterface $dataSource, |
55
|
|
|
IntervalCollection $intervals, |
56
|
|
|
DimensionInterface $dimension, |
57
|
|
|
int $threshold, |
58
|
|
|
string $metric, |
59
|
|
|
string|Granularity $granularity = Granularity::ALL |
60
|
|
|
) { |
61
|
3 |
|
$this->dataSource = $dataSource; |
62
|
3 |
|
$this->intervals = $intervals; |
63
|
3 |
|
$this->granularity = is_string($granularity) ? Granularity::from(strtolower($granularity)) : $granularity; |
64
|
3 |
|
$this->dimension = $dimension; |
65
|
3 |
|
$this->threshold = $threshold; |
66
|
3 |
|
$this->metric = $metric; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Return the query in array format, so we can fire it to druid. |
71
|
|
|
* |
72
|
|
|
* @return array<string,string|int|array<mixed>> |
73
|
|
|
*/ |
74
|
1 |
|
public function toArray(): array |
75
|
|
|
{ |
76
|
1 |
|
$metricSpec = [ |
77
|
1 |
|
'type' => 'numeric', |
78
|
1 |
|
'metric' => $this->metric, |
79
|
1 |
|
]; |
80
|
|
|
|
81
|
1 |
|
if (!$this->descending) { |
82
|
1 |
|
$metricSpec = [ |
83
|
1 |
|
'type' => 'inverted', |
84
|
1 |
|
'metric' => $metricSpec, |
85
|
1 |
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$result = [ |
89
|
1 |
|
'queryType' => 'topN', |
90
|
1 |
|
'dataSource' => $this->dataSource->toArray(), |
91
|
1 |
|
'intervals' => $this->intervals->toArray(), |
92
|
1 |
|
'granularity' => $this->granularity->value, |
93
|
1 |
|
'dimension' => $this->dimension->toArray(), |
94
|
1 |
|
'threshold' => $this->threshold, |
95
|
1 |
|
'metric' => $metricSpec, |
96
|
1 |
|
]; |
97
|
|
|
|
98
|
1 |
|
if ($this->filter) { |
99
|
1 |
|
$result['filter'] = $this->filter->toArray(); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
if ($this->virtualColumns) { |
103
|
1 |
|
$result['virtualColumns'] = $this->virtualColumns->toArray(); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
if ($this->aggregations) { |
107
|
1 |
|
$result['aggregations'] = $this->aggregations->toArray(); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
if ($this->postAggregations) { |
111
|
1 |
|
$result['postAggregations'] = $this->postAggregations->toArray(); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
if (isset($this->context)) { |
115
|
1 |
|
$context = $this->context->toArray(); |
|
|
|
|
116
|
1 |
|
if (sizeof($context) > 0) { |
117
|
1 |
|
$result['context'] = $context; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $result; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param \Level23\Druid\Filters\FilterInterface $filter |
126
|
|
|
*/ |
127
|
1 |
|
public function setFilter(FilterInterface $filter): void |
128
|
|
|
{ |
129
|
1 |
|
$this->filter = $filter; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param \Level23\Druid\Collections\AggregationCollection $aggregations |
134
|
|
|
*/ |
135
|
1 |
|
public function setAggregations(AggregationCollection $aggregations): void |
136
|
|
|
{ |
137
|
1 |
|
$this->aggregations = $aggregations; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param \Level23\Druid\Collections\PostAggregationCollection $postAggregations |
142
|
|
|
*/ |
143
|
1 |
|
public function setPostAggregations(PostAggregationCollection $postAggregations): void |
144
|
|
|
{ |
145
|
1 |
|
$this->postAggregations = $postAggregations; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param \Level23\Druid\Context\ContextInterface $context |
150
|
|
|
*/ |
151
|
1 |
|
public function setContext(ContextInterface $context): void |
152
|
|
|
{ |
153
|
1 |
|
$this->context = $context; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Parse the response into something we can return to the user. |
158
|
|
|
* |
159
|
|
|
* @param array<string|int,array<mixed>|int|string> $response |
160
|
|
|
* |
161
|
|
|
* @return TopNQueryResponse |
162
|
|
|
*/ |
163
|
1 |
|
public function parseResponse(array $response): TopNQueryResponse |
164
|
|
|
{ |
165
|
1 |
|
return new TopNQueryResponse($response); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param \Level23\Druid\Collections\VirtualColumnCollection $virtualColumns |
170
|
|
|
*/ |
171
|
1 |
|
public function setVirtualColumns(VirtualColumnCollection $virtualColumns): void |
172
|
|
|
{ |
173
|
1 |
|
$this->virtualColumns = $virtualColumns; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param bool $descending |
178
|
|
|
*/ |
179
|
1 |
|
public function setDescending(bool $descending): void |
180
|
|
|
{ |
181
|
1 |
|
$this->descending = $descending; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.