Passed
Push — master ( 4d62c3...e2457a )
by Teye
05:24
created

IndexTask::toArray()   B

Complexity

Conditions 9
Paths 128

Size

Total Lines 44
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 44
ccs 33
cts 33
cp 1
rs 7.8222
c 1
b 0
f 0
cc 9
nc 128
nop 0
crap 9
1
<?php
2
declare(strict_types=1);
3
4
namespace Level23\Druid\Tasks;
5
6
use Level23\Druid\Context\TaskContext;
7
use Level23\Druid\Transforms\TransformSpec;
8
use Level23\Druid\Dimensions\TimestampSpec;
9
use Level23\Druid\TuningConfig\TuningConfig;
10
use Level23\Druid\Collections\AggregationCollection;
11
use Level23\Druid\InputSources\InputSourceInterface;
12
use Level23\Druid\InputFormats\InputFormatInterface;
13
use Level23\Druid\Granularities\GranularityInterface;
14
use Level23\Druid\Collections\SpatialDimensionCollection;
15
16
class IndexTask implements TaskInterface
17
{
18
    protected ?TuningConfig $tuningConfig;
19
20
    protected ?TaskContext $context;
21
22
    protected bool $appendToExisting = false;
23
24
    protected InputSourceInterface $inputSource;
25
26
    protected string $dateSource;
27
28
    protected GranularityInterface $granularity;
29
30
    protected ?TransformSpec $transformSpec;
31
32
    protected ?AggregationCollection $aggregations;
33
34
    /**
35
     * @var array<array<string,string|bool>>
36
     */
37
    protected array $dimensions = [];
38
39
    /**
40
     * Whether this task should be executed parallel.
41
     *
42
     * @var bool
43
     */
44
    protected bool $parallel = false;
45
46
    protected ?string $taskId;
47
48
    protected ?InputFormatInterface $inputFormat;
49
50
    protected TimestampSpec $timestampSpec;
51
52
    protected ?SpatialDimensionCollection $spatialDimensions = null;
53
54
    /**
55
     * IndexTask constructor.
56
     *
57
     * @param string                                                     $dateSource
58
     * @param \Level23\Druid\InputSources\InputSourceInterface           $inputSource
59
     * @param \Level23\Druid\Granularities\GranularityInterface          $granularity
60
     * @param \Level23\Druid\Dimensions\TimestampSpec                    $timestampSpec
61
     * @param \Level23\Druid\Transforms\TransformSpec|null               $transformSpec
62
     * @param \Level23\Druid\TuningConfig\TuningConfig|null              $tuningConfig
63
     * @param \Level23\Druid\Context\TaskContext|null                    $context
64
     * @param \Level23\Druid\Collections\AggregationCollection|null      $aggregations
65
     * @param array<array<string,string|bool>>                           $dimensions
66
     * @param string|null                                                $taskId
67
     * @param \Level23\Druid\InputFormats\InputFormatInterface|null      $inputFormat
68
     * @param \Level23\Druid\Collections\SpatialDimensionCollection|null $spatialDimensions
69
     */
70 3
    public function __construct(
71
        string $dateSource,
72
        InputSourceInterface $inputSource,
73
        GranularityInterface $granularity,
74
        TimestampSpec $timestampSpec,
75
        ?TransformSpec $transformSpec = null,
76
        ?TuningConfig $tuningConfig = null,
77
        ?TaskContext $context = null,
78
        ?AggregationCollection $aggregations = null,
79
        array $dimensions = [],
80
        ?string $taskId = null,
81
        ?InputFormatInterface $inputFormat = null,
82
        ?SpatialDimensionCollection $spatialDimensions = null
83
    ) {
84 3
        $this->tuningConfig      = $tuningConfig;
85 3
        $this->context           = $context;
86 3
        $this->inputSource       = $inputSource;
87 3
        $this->dateSource        = $dateSource;
88 3
        $this->granularity       = $granularity;
89 3
        $this->transformSpec     = $transformSpec;
90 3
        $this->aggregations      = $aggregations;
91 3
        $this->dimensions        = $dimensions;
92 3
        $this->taskId            = $taskId;
93 3
        $this->inputFormat       = $inputFormat;
94 3
        $this->timestampSpec     = $timestampSpec;
95 3
        $this->spatialDimensions = $spatialDimensions;
96
    }
97
98
    /**
99
     * Return the task in a format so that we can send it to druid.
100
     *
101
     * @return array<string,array<string,array<string,array<int|string,array<mixed>|bool|int|string>|bool|int|string|null>|bool|int|string>|string>
102
     */
103 2
    public function toArray(): array
104
    {
105 2
        $result = [
106 2
            'type' => $this->parallel ? 'index_parallel' : 'index',
107 2
            'spec' => [
108 2
                'dataSchema' => [
109 2
                    'dataSource'      => $this->dateSource,
110 2
                    'timestampSpec'   => $this->timestampSpec->toArray(),
111 2
                    'dimensionsSpec'  => [
112 2
                        'dimensions' => $this->dimensions,
113 2
                    ],
114 2
                    'metricsSpec'     => ($this->aggregations?->toArray()),
115 2
                    'granularitySpec' => $this->granularity->toArray(),
116 2
                    'transformSpec'   => ($this->transformSpec?->toArray()),
117 2
                ],
118 2
                'ioConfig'   => [
119 2
                    'type'             => $this->parallel ? 'index_parallel' : 'index',
120 2
                    'inputSource'      => $this->inputSource->toArray(),
121 2
                    'inputFormat'      => $this->inputFormat?->toArray(),
122 2
                    'appendToExisting' => $this->appendToExisting,
123 2
                ],
124 2
            ],
125 2
        ];
126
127
        // Add our spatial dimensions if supplied.
128 2
        if (!empty($this->spatialDimensions)) {
129 1
            $result['spec']['dataSchema']['dimensionsSpec']['spatialDimensions'] = $this->spatialDimensions->toArray();
130
        }
131
132 2
        $context = $this->context ? $this->context->toArray() : [];
133 2
        if (count($context) > 0) {
134 1
            $result['context'] = $context;
135
        }
136
137 2
        if ($this->tuningConfig) {
138 1
            $this->tuningConfig->setType($this->parallel ? 'index_parallel' : 'index');
139 1
            $result['spec']['tuningConfig'] = $this->tuningConfig->toArray();
140
        }
141
142 2
        if ($this->taskId) {
143 1
            $result['id'] = $this->taskId;
144
        }
145
146 2
        return $result;
147
    }
148
149
    /**
150
     * @param bool $appendToExisting
151
     */
152 2
    public function setAppendToExisting(bool $appendToExisting): void
153
    {
154 2
        $this->appendToExisting = $appendToExisting;
155
    }
156
157
    /**
158
     * Whether this task should be executed parallel.
159
     *
160
     * @param bool $parallel
161
     */
162 2
    public function setParallel(bool $parallel): void
163
    {
164 2
        $this->parallel = $parallel;
165
    }
166
167
    /**
168
     * @param InputFormatInterface $inputFormat
169
     *
170
     * @return IndexTask
171
     */
172 2
    public function setInputFormat(InputFormatInterface $inputFormat): IndexTask
173
    {
174 2
        $this->inputFormat = $inputFormat;
175
176 2
        return $this;
177
    }
178
}