Skip to content

Commit

Permalink
Improve payload builder and add more feature test (#31)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
norbybaru authored Nov 21, 2023
1 parent 9c06334 commit db892e5
Show file tree
Hide file tree
Showing 16 changed files with 30,573 additions and 113 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.lock
*.bak
/.env
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"require": {
"php": "^8.0",
"aws/aws-sdk-php": "^3.209",
"illuminate/support": "^8.0|^9.52|^10.0"
"illuminate/support": "^8.0|^9.52|^10.0",
"spatie/enum": "^3.13"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
2 changes: 0 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Unit">
Expand Down
2 changes: 2 additions & 0 deletions src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use NorbyBaru\AwsTimestream\Concerns\BuildersConcern;
use NorbyBaru\AwsTimestream\Contract\QueryBuilderContract;

abstract class Builder implements QueryBuilderContract
{
use BuildersConcern;
use Macroable;

protected string $database = '';
protected string $table = '';
Expand Down
69 changes: 69 additions & 0 deletions src/Builder/CommonPayloadBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace NorbyBaru\AwsTimestream\Builder;

use Illuminate\Support\Carbon;
use NorbyBaru\AwsTimestream\Enum\ValueTypeEnum;

class CommonPayloadBuilder
{
protected array $commonDimensions = [];
protected array $commonAttributes = [];

public static function make(): self
{
return new self();
}

public function setCommonDimensions(string $name, mixed $value): self
{
$this->commonDimensions[] = [
'Name' => $name,
'Value' => $value,
'DimensionValueType' => ValueTypeEnum::VARCHAR()->value,
];

return $this;
}

public function setCommonMeasureValueType(ValueTypeEnum $type): self
{
$this->commonAttributes['MeasureValueType'] = $type->value;

return $this;
}

public function setCommonTime(Carbon $time): self
{
$this->commonAttributes['Time'] = $this->getPreciseTime($time);

return $this;
}

public function setCommonVersion(int $version): self
{
$this->commonAttributes['Version'] = $version;

return $this;
}

public function toArray(): array
{
$common = [];
if ($this->commonDimensions) {
$common = [
'Dimensions' => $this->commonDimensions,
];
}

return array_merge(
$this->commonAttributes,
$common
);
}

private function getPreciseTime(Carbon $time): string
{
return (string) $time->getPreciseTimestamp(3);
}
}
127 changes: 127 additions & 0 deletions src/Builder/TimestreamPayloadBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace NorbyBaru\AwsTimestream\Builder;

use Illuminate\Support\Carbon;
use NorbyBaru\AwsTimestream\Enum\ValueTypeEnum;

class TimestreamPayloadBuilder
{
protected array $commonDimensions = [];
protected array $commonAttributes = [];
protected array $dimensions = [];
protected array $measureValues = [];

protected ?int $version = null;
protected ?Carbon $time = null;

public function __construct(
protected string $measureName,
protected mixed $measureValue = null,
protected ?ValueTypeEnum $measureValueType = null
) {
}

public function setMeasureName(string $measureName): self
{
$this->measureName = $measureName;

return $this;
}

public function setMeasureValue(mixed $value): self
{
$this->measureValue = $value;

return $this;
}

public function setMeasureValueType(ValueTypeEnum $type): self
{
$this->measureValueType = $type;

return $this;
}

public function setVersion(int $version): self
{
$this->version = $version;

return $this;
}

public function setMultiMeasuresValues(string $name, mixed $value, ?ValueTypeEnum $type = null): self
{
$this->measureValues[] = [
'Name' => $name,
'Value' => $value,
'Type' => $type?->value ?? ValueTypeEnum::VARCHAR()->value,
];

return $this;
}

public function setDimensions(string $name, mixed $value): self
{
$this->dimensions[] = [
'Name' => $name,
'Value' => $value,
];

return $this;
}

public function setTime(Carbon $carbon): self
{
$this->time = $carbon;

return $this;
}

private function getPreciseTime(Carbon $time): string
{
return (string) $time->getPreciseTimestamp(3);
}

public function toRecords(): array
{
return [$this->toArray()];
}

public static function make(string $measureName): self
{
return new self($measureName);
}

public function toArray(): array
{
$metric = [
'MeasureName' => $this->measureName,
'MeasureValue' => (string) $this->measureValue,
];

if ($this->time) {
$metric['Time'] = $this->getPreciseTime($this->time);
}

if ($this->measureValueType) {
$metric['MeasureValueType'] = $this->measureValueType->value;
}

if ($this->measureValues) {
$metric['MeasureValues'] = $this->measureValues;
$metric['MeasureValueType'] = 'MULTI';
unset($metric['MeasureValue']);
}

if ($this->dimensions) {
$metric['Dimensions'] = $this->dimensions;
}

if ($this->version) {
$metric['Version'] = $this->version;
}

return $metric;
}
}
16 changes: 16 additions & 0 deletions src/Enum/ValueTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NorbyBaru\AwsTimestream\Enum;

use Spatie\Enum\Enum;

/**
* @method static self DOUBLE()
* @method static self BIGINT()
* @method static self VARCHAR()
* @method static self BOOLEAN()
* @method static self TIMESTAMP()
*/
class ValueTypeEnum extends Enum
{
}
2 changes: 1 addition & 1 deletion src/TimestreamBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function batchPayload(array $metrics): array
$metric['measure_value'],
$metric['time'],
$metric['measure_value_type'] ?? 'VARCHAR',
$metric['dimensions']
$metric['dimensions'] ?? []
)->toArray(true)
)->all();
}
Expand Down
18 changes: 9 additions & 9 deletions src/TimestreamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,24 @@ private function ingest(array $payload): \Aws\Result

public function query(TimestreamReaderDto $timestreamReader): Collection
{
return $this->runQuery($timestreamReader);
return $this->runQuery($timestreamReader->toArray());
}

private function runQuery(TimestreamReaderDto $timestreamReader, string $nextToken = null): Collection
private function runQuery(array $params): Collection
{
$params = $timestreamReader->toArray();
if ($nextToken) {
$params['NextToken'] = $nextToken;
}

try {
if ($this->shouldDebugQuery()) {
Log::debug('=== Timestream Query ===', $params);
}

$result = $this->reader->query($params);
if ($token = $result->get('NextToken')) {
return $this->runQuery($timestreamReader, $token);

// fetch everything recursively until the limit has been reached or there is no more data
if ($nextToken = $result->get('NextToken')) {
$parsedRows = $this->parseQueryResult($result);
$params['NextToken'] = $nextToken;

return $this->runQuery($params)->merge($parsedRows);
}
} catch (TimestreamQueryException $e) {
throw new FailTimestreamQueryException($e, $params);
Expand Down
Loading

0 comments on commit db892e5

Please sign in to comment.