|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Level23\Druid\Tasks; |
|
5
|
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Level23\Druid\DruidClient; |
|
8
|
|
|
use Level23\Druid\Interval\IntervalInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class TaskBuilder |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var DruidClient |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $client; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The task ID. If this is not explicitly specified, Druid generates the task ID using task type, |
|
19
|
|
|
* data source name, interval, and date-time stamp. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string|null |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $taskId = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Check if the given interval is valid for the given dataSource. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $dataSource |
|
29
|
|
|
* @param \Level23\Druid\Interval\IntervalInterface $interval |
|
30
|
|
|
* |
|
31
|
|
|
* @throws \Level23\Druid\Exceptions\QueryResponseException |
|
32
|
|
|
*/ |
|
33
|
5 |
|
protected function validateInterval(string $dataSource, IntervalInterface $interval): void |
|
34
|
|
|
{ |
|
35
|
5 |
|
$fromStr = $interval->getStart()->format('Y-m-d\TH:i:s.000\Z'); |
|
36
|
5 |
|
$toStr = $interval->getStop()->format('Y-m-d\TH:i:s.000\Z'); |
|
37
|
|
|
|
|
38
|
5 |
|
$foundFrom = false; |
|
39
|
5 |
|
$foundTo = false; |
|
40
|
|
|
|
|
41
|
|
|
// Get all intervals and check if our interval is among them. |
|
42
|
5 |
|
$intervals = array_keys($this->client->metadata()->intervals($dataSource)); |
|
43
|
|
|
|
|
44
|
5 |
|
foreach ($intervals as $dateStr) { |
|
45
|
|
|
|
|
46
|
5 |
|
if (!$foundFrom && substr($dateStr, 0, strlen($fromStr)) === $fromStr) { |
|
47
|
3 |
|
$foundFrom = true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
5 |
|
if (!$foundTo && substr($dateStr, -strlen($toStr)) === $toStr) { |
|
51
|
3 |
|
$foundTo = true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
if ($foundFrom && $foundTo) { |
|
55
|
5 |
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
throw new InvalidArgumentException( |
|
60
|
3 |
|
'Error, invalid interval given. The given dates do not match a complete interval!' . PHP_EOL . |
|
61
|
3 |
|
'Given interval: ' . $interval->getInterval() . PHP_EOL . |
|
62
|
3 |
|
'Valid intervals: ' . implode(', ', $intervals) |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Execute the index task. We will return the task identifier. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Level23\Druid\Context\TaskContext|array $context |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
* @throws \Level23\Druid\Exceptions\QueryResponseException |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function execute($context = []): string |
|
75
|
|
|
{ |
|
76
|
1 |
|
$task = $this->buildTask($context); |
|
77
|
|
|
|
|
78
|
1 |
|
return $this->client->executeTask($task); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Return the task in Json format. |
|
83
|
|
|
* |
|
84
|
|
|
* @param \Level23\Druid\Context\TaskContext|array $context |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
* @throws \Level23\Druid\Exceptions\QueryResponseException |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function toJson($context = []): string |
|
90
|
|
|
{ |
|
91
|
1 |
|
$task = $this->buildTask($context); |
|
92
|
|
|
|
|
93
|
1 |
|
$json = \json_encode($task->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
|
94
|
1 |
|
if (\JSON_ERROR_NONE !== \json_last_error()) { |
|
95
|
|
|
throw new InvalidArgumentException( |
|
96
|
|
|
'json_encode error: ' . \json_last_error_msg() |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return (string)$json; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return the task as array |
|
105
|
|
|
* |
|
106
|
|
|
* @param \Level23\Druid\Context\TaskContext|array $context |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
* @throws \Level23\Druid\Exceptions\QueryResponseException |
|
110
|
|
|
*/ |
|
111
|
4 |
|
public function toArray($context = []): array |
|
112
|
|
|
{ |
|
113
|
4 |
|
$task = $this->buildTask($context); |
|
114
|
|
|
|
|
115
|
3 |
|
return $task->toArray(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* The task ID. If this is not explicitly specified, Druid generates the task ID using task type, |
|
120
|
|
|
* data source name, interval, and date-time stamp. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $taskId |
|
123
|
|
|
* |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
3 |
|
public function taskId(string $taskId) |
|
127
|
|
|
{ |
|
128
|
3 |
|
$this->taskId = $taskId; |
|
129
|
|
|
|
|
130
|
3 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param \Level23\Druid\Context\TaskContext|array $context |
|
135
|
|
|
* |
|
136
|
|
|
* @return \Level23\Druid\Tasks\TaskInterface |
|
137
|
|
|
* @throws \Level23\Druid\Exceptions\QueryResponseException |
|
138
|
|
|
*/ |
|
139
|
|
|
abstract protected function buildTask($context): TaskInterface; |
|
140
|
|
|
} |