Passed
Pull Request — master (#35)
by Teye
04:28
created

DoublesSketchAggregator::toArray()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.9666
cc 3
nc 4
nop 0
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Aggregations;
5
6
class DoublesSketchAggregator implements AggregatorInterface
7
{
8
    /**
9
     * A String for the name of the input field (can contain sketches or raw numeric values).
10
     *
11
     * @var string
12
     */
13
    protected $metricName;
14
15
    /**
16
     * A String for the output (result) name of the calculation.
17
     *
18
     * @var string
19
     */
20
    protected $outputName;
21
22
    /**
23
     * Parameter that determines the accuracy and size of the sketch. Higher k means higher accuracy but more space to
24
     * store sketches. Must be a power of 2 from 2 to 32768. See accuracy information in the DataSketches documentation
25
     * for details.
26
     *
27
     * @var int|null
28
     */
29
    protected $sizeAndAccuracy;
30
31
    /**
32
     * This parameter is a temporary solution to avoid a known issue. It may be removed in a future release after the
33
     * bug is fixed. This parameter defines the maximum number of items to store in each sketch. If a sketch reaches
34
     * the limit, the query can throw IllegalStateException. To workaround this issue, increase the maximum stream
35
     * length. See accuracy information in the DataSketches documentation for how many bytes are required per stream
36
     * length.
37
     *
38
     * @var int|null
39
     */
40
    protected $maxStreamLength;
41
42
    /**
43
     * @param string   $metricName      A String for the name of the input field (can contain sketches or raw numeric
44
     *                                  values).
45
     * @param string   $outputName      A String for the output (result) name of the calculation.
46
     * @param int|null $sizeAndAccuracy Parameter that determines the accuracy and size of the sketch. Higher k means
47
     *                                  higher accuracy but more space to store sketches. Must be a power of 2 from 2
48
     *                                  to 32768. See accuracy information in the DataSketches documentation for
49
     *                                  details.
50
     * @param int|null $maxStreamLength This parameter is a temporary solution to avoid a known issue. It may be
51
     *                                  removed in a future release after the bug is fixed. This parameter defines the
52
     *                                  maximum number of items to store in each sketch. If a sketch reaches the limit,
53
     *                                  the query can throw IllegalStateException. To workaround this issue, increase
54
     *                                  the maximum stream length. See accuracy information in the DataSketches
55
     *                                  documentation for how many bytes are required per stream length.
56
     *
57
     * @see https://druid.apache.org/docs/latest/development/extensions-core/datasketches-quantiles.html
58
     */
59 4
    public function __construct(
60
        string $metricName,
61
        string $outputName = '',
62
        ?int $sizeAndAccuracy = null,
63
        ?int $maxStreamLength = null
64
    ) {
65 4
        $this->metricName      = $metricName;
66 4
        $this->outputName      = $outputName ?: $metricName;
67 4
        $this->sizeAndAccuracy = $sizeAndAccuracy;
68 4
        $this->maxStreamLength = $maxStreamLength;
69
    }
70
71 4
    public function toArray(): array
72
    {
73 4
        $result = [
74
            'type'      => 'quantilesDoublesSketch',
75 4
            'name'      => $this->outputName,
76 4
            'fieldName' => $this->metricName,
77
        ];
78
79 4
        if ($this->sizeAndAccuracy !== null) {
80 2
            $result['k'] = $this->sizeAndAccuracy;
81
        }
82
83 4
        if ($this->maxStreamLength !== null) {
84 2
            $result['maxStreamLength'] = $this->maxStreamLength;
85
        }
86
87 4
        return $result;
88
    }
89
}