1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Level23\Druid\Aggregations; |
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Level23\Druid\Types\DataType; |
8
|
|
|
|
9
|
|
|
class AnyAggregator extends MethodAggregator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var int|null |
13
|
|
|
*/ |
14
|
|
|
protected $maxStringBytes; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* constructor. |
18
|
|
|
* |
19
|
|
|
* @param string $metricName |
20
|
|
|
* @param string $outputName When not given, we will use the same name as the metric. |
21
|
|
|
* @param string $type The type of field. This can either be "long", "float" or |
22
|
|
|
* "double" |
23
|
|
|
* @param int|null $maxStringBytes optional, defaults to 1024 |
24
|
|
|
*/ |
25
|
4 |
|
public function __construct( |
26
|
|
|
string $metricName, |
27
|
|
|
string $outputName = '', |
28
|
|
|
string $type = 'long', |
29
|
|
|
int $maxStringBytes = null |
30
|
|
|
) { |
31
|
4 |
|
$type = strtolower($type); |
32
|
4 |
|
if (!in_array($type, ['long', 'float', 'double'])) { |
33
|
1 |
|
throw new InvalidArgumentException( |
34
|
1 |
|
'Incorrect type given: ' . $type . '. This can either be "long", "float" or "double"' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
$this->type = $type; |
39
|
3 |
|
$this->metricName = $metricName; |
40
|
3 |
|
$this->outputName = $outputName ?: $metricName; |
41
|
3 |
|
$this->maxStringBytes = $maxStringBytes; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return the aggregator as it can be used in a druid query. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
3 |
|
public function toArray(): array |
50
|
|
|
{ |
51
|
3 |
|
$response = [ |
52
|
3 |
|
'type' => $this->type . ucfirst($this->getMethod()), |
53
|
3 |
|
'name' => $this->outputName, |
54
|
3 |
|
'fieldName' => $this->metricName, |
55
|
|
|
]; |
56
|
|
|
|
57
|
3 |
|
if ($this->type == DataType::STRING && $this->maxStringBytes !== null) { |
58
|
|
|
$response['maxStringBytes'] = $this->maxStringBytes; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
return $response; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the method for the type aggregation |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
3 |
|
protected function getMethod(): string |
70
|
|
|
{ |
71
|
3 |
|
return 'any'; |
72
|
|
|
} |
73
|
|
|
} |