Skip to content

Commit db892e5

Browse files
authored
Improve payload builder and add more feature test (norbybaru#31)
* Improve payload builder and add more feature test * apply styleci * refactor * refactor * fix enum for php8 * refactor with TimestreamPayloadBuilder * fix test * ally styleci * revert builders * make query builder marcoable * apply styleci
1 parent 9c06334 commit db892e5

16 files changed

+30573
-113
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.DS_Store
12
*.lock
23
*.bak
34
/.env

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"require": {
3333
"php": "^8.0",
3434
"aws/aws-sdk-php": "^3.209",
35-
"illuminate/support": "^8.0|^9.52|^10.0"
35+
"illuminate/support": "^8.0|^9.52|^10.0",
36+
"spatie/enum": "^3.13"
3637
},
3738
"minimum-stability": "dev",
3839
"prefer-stable": true,

phpunit.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
processIsolation="false"
88
stopOnFailure="false"
99
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
10-
cacheDirectory=".phpunit.cache"
11-
backupStaticProperties="false"
1210
>
1311
<testsuites>
1412
<testsuite name="Unit">

src/Builder/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use Closure;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Str;
8+
use Illuminate\Support\Traits\Macroable;
89
use NorbyBaru\AwsTimestream\Concerns\BuildersConcern;
910
use NorbyBaru\AwsTimestream\Contract\QueryBuilderContract;
1011

1112
abstract class Builder implements QueryBuilderContract
1213
{
1314
use BuildersConcern;
15+
use Macroable;
1416

1517
protected string $database = '';
1618
protected string $table = '';

src/Builder/CommonPayloadBuilder.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace NorbyBaru\AwsTimestream\Builder;
4+
5+
use Illuminate\Support\Carbon;
6+
use NorbyBaru\AwsTimestream\Enum\ValueTypeEnum;
7+
8+
class CommonPayloadBuilder
9+
{
10+
protected array $commonDimensions = [];
11+
protected array $commonAttributes = [];
12+
13+
public static function make(): self
14+
{
15+
return new self();
16+
}
17+
18+
public function setCommonDimensions(string $name, mixed $value): self
19+
{
20+
$this->commonDimensions[] = [
21+
'Name' => $name,
22+
'Value' => $value,
23+
'DimensionValueType' => ValueTypeEnum::VARCHAR()->value,
24+
];
25+
26+
return $this;
27+
}
28+
29+
public function setCommonMeasureValueType(ValueTypeEnum $type): self
30+
{
31+
$this->commonAttributes['MeasureValueType'] = $type->value;
32+
33+
return $this;
34+
}
35+
36+
public function setCommonTime(Carbon $time): self
37+
{
38+
$this->commonAttributes['Time'] = $this->getPreciseTime($time);
39+
40+
return $this;
41+
}
42+
43+
public function setCommonVersion(int $version): self
44+
{
45+
$this->commonAttributes['Version'] = $version;
46+
47+
return $this;
48+
}
49+
50+
public function toArray(): array
51+
{
52+
$common = [];
53+
if ($this->commonDimensions) {
54+
$common = [
55+
'Dimensions' => $this->commonDimensions,
56+
];
57+
}
58+
59+
return array_merge(
60+
$this->commonAttributes,
61+
$common
62+
);
63+
}
64+
65+
private function getPreciseTime(Carbon $time): string
66+
{
67+
return (string) $time->getPreciseTimestamp(3);
68+
}
69+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace NorbyBaru\AwsTimestream\Builder;
4+
5+
use Illuminate\Support\Carbon;
6+
use NorbyBaru\AwsTimestream\Enum\ValueTypeEnum;
7+
8+
class TimestreamPayloadBuilder
9+
{
10+
protected array $commonDimensions = [];
11+
protected array $commonAttributes = [];
12+
protected array $dimensions = [];
13+
protected array $measureValues = [];
14+
15+
protected ?int $version = null;
16+
protected ?Carbon $time = null;
17+
18+
public function __construct(
19+
protected string $measureName,
20+
protected mixed $measureValue = null,
21+
protected ?ValueTypeEnum $measureValueType = null
22+
) {
23+
}
24+
25+
public function setMeasureName(string $measureName): self
26+
{
27+
$this->measureName = $measureName;
28+
29+
return $this;
30+
}
31+
32+
public function setMeasureValue(mixed $value): self
33+
{
34+
$this->measureValue = $value;
35+
36+
return $this;
37+
}
38+
39+
public function setMeasureValueType(ValueTypeEnum $type): self
40+
{
41+
$this->measureValueType = $type;
42+
43+
return $this;
44+
}
45+
46+
public function setVersion(int $version): self
47+
{
48+
$this->version = $version;
49+
50+
return $this;
51+
}
52+
53+
public function setMultiMeasuresValues(string $name, mixed $value, ?ValueTypeEnum $type = null): self
54+
{
55+
$this->measureValues[] = [
56+
'Name' => $name,
57+
'Value' => $value,
58+
'Type' => $type?->value ?? ValueTypeEnum::VARCHAR()->value,
59+
];
60+
61+
return $this;
62+
}
63+
64+
public function setDimensions(string $name, mixed $value): self
65+
{
66+
$this->dimensions[] = [
67+
'Name' => $name,
68+
'Value' => $value,
69+
];
70+
71+
return $this;
72+
}
73+
74+
public function setTime(Carbon $carbon): self
75+
{
76+
$this->time = $carbon;
77+
78+
return $this;
79+
}
80+
81+
private function getPreciseTime(Carbon $time): string
82+
{
83+
return (string) $time->getPreciseTimestamp(3);
84+
}
85+
86+
public function toRecords(): array
87+
{
88+
return [$this->toArray()];
89+
}
90+
91+
public static function make(string $measureName): self
92+
{
93+
return new self($measureName);
94+
}
95+
96+
public function toArray(): array
97+
{
98+
$metric = [
99+
'MeasureName' => $this->measureName,
100+
'MeasureValue' => (string) $this->measureValue,
101+
];
102+
103+
if ($this->time) {
104+
$metric['Time'] = $this->getPreciseTime($this->time);
105+
}
106+
107+
if ($this->measureValueType) {
108+
$metric['MeasureValueType'] = $this->measureValueType->value;
109+
}
110+
111+
if ($this->measureValues) {
112+
$metric['MeasureValues'] = $this->measureValues;
113+
$metric['MeasureValueType'] = 'MULTI';
114+
unset($metric['MeasureValue']);
115+
}
116+
117+
if ($this->dimensions) {
118+
$metric['Dimensions'] = $this->dimensions;
119+
}
120+
121+
if ($this->version) {
122+
$metric['Version'] = $this->version;
123+
}
124+
125+
return $metric;
126+
}
127+
}

src/Enum/ValueTypeEnum.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace NorbyBaru\AwsTimestream\Enum;
4+
5+
use Spatie\Enum\Enum;
6+
7+
/**
8+
* @method static self DOUBLE()
9+
* @method static self BIGINT()
10+
* @method static self VARCHAR()
11+
* @method static self BOOLEAN()
12+
* @method static self TIMESTAMP()
13+
*/
14+
class ValueTypeEnum extends Enum
15+
{
16+
}

src/TimestreamBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function batchPayload(array $metrics): array
2020
$metric['measure_value'],
2121
$metric['time'],
2222
$metric['measure_value_type'] ?? 'VARCHAR',
23-
$metric['dimensions']
23+
$metric['dimensions'] ?? []
2424
)->toArray(true)
2525
)->all();
2626
}

src/TimestreamService.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,24 @@ private function ingest(array $payload): \Aws\Result
7171

7272
public function query(TimestreamReaderDto $timestreamReader): Collection
7373
{
74-
return $this->runQuery($timestreamReader);
74+
return $this->runQuery($timestreamReader->toArray());
7575
}
7676

77-
private function runQuery(TimestreamReaderDto $timestreamReader, string $nextToken = null): Collection
77+
private function runQuery(array $params): Collection
7878
{
79-
$params = $timestreamReader->toArray();
80-
if ($nextToken) {
81-
$params['NextToken'] = $nextToken;
82-
}
83-
8479
try {
8580
if ($this->shouldDebugQuery()) {
8681
Log::debug('=== Timestream Query ===', $params);
8782
}
8883

8984
$result = $this->reader->query($params);
90-
if ($token = $result->get('NextToken')) {
91-
return $this->runQuery($timestreamReader, $token);
85+
86+
// fetch everything recursively until the limit has been reached or there is no more data
87+
if ($nextToken = $result->get('NextToken')) {
88+
$parsedRows = $this->parseQueryResult($result);
89+
$params['NextToken'] = $nextToken;
90+
91+
return $this->runQuery($params)->merge($parsedRows);
9292
}
9393
} catch (TimestreamQueryException $e) {
9494
throw new FailTimestreamQueryException($e, $params);

0 commit comments

Comments
 (0)