Skip to content

Commit

Permalink
Improve payload builder and add more feature test
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru committed Oct 4, 2023
1 parent 9c06334 commit 88eaf33
Show file tree
Hide file tree
Showing 12 changed files with 30,369 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
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,

Check failure on line 23 in src/Builder/CommonPayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to constant VARCHAR on an unknown class NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.
];

return $this;
}

public function setCommonMeasureValueType(ValueTypeEnum $type): self

Check failure on line 29 in src/Builder/CommonPayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $type of method NorbyBaru\AwsTimestream\Builder\CommonPayloadBuilder::setCommonMeasureValueType() has invalid type NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.
{
$this->commonAttributes['MeasureValueType'] = $type->value;

Check failure on line 31 in src/Builder/CommonPayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $value on an unknown class NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.

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 [
...$this->commonAttributes,
...$common,
];
}

private function getPreciseTime(Carbon $time): string
{
return (string) $time->getPreciseTimestamp(3);
}
}
118 changes: 107 additions & 11 deletions src/Builder/PayloadBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

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

final class PayloadBuilder implements PayloadBuilderContract
{
protected array $dimensions;
protected array $commonDimensions = [];
protected array $commonAttributes = [];
protected array $dimensions = [];
protected array $measureValues = [];
protected ?int $version = null;

public function __construct(
protected string $measureName,
protected $measureValue,
protected Carbon $time,
protected string $measureValueType = 'DOUBLE',
protected mixed $measureValue = null,
protected ?Carbon $time = null,
protected ?ValueTypeEnum $measureValueType = null,

Check failure on line 21 in src/Builder/PayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $measureValueType of method NorbyBaru\AwsTimestream\Builder\PayloadBuilder::__construct() has invalid type NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.

Check failure on line 21 in src/Builder/PayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Property NorbyBaru\AwsTimestream\Builder\PayloadBuilder::$measureValueType has unknown class NorbyBaru\AwsTimestream\Enum\ValueTypeEnum as its type.
array $dimensions = []
) {
if ($dimensions) {
Expand All @@ -23,12 +28,57 @@ public function __construct(

public static function make(
string $measureName,
$measureValue,
Carbon $time,
string $measureValueType = 'DOUBLE',
mixed $measureValue = null,
?Carbon $time = null,
?string $measureValueType = null,
array $dimensions = []
): self {
return new self($measureName, $measureValue, $time, $measureValueType, $dimensions);
return new self(
measureName: $measureName,
measureValue: $measureValue,
time: $time,
measureValueType: $measureValueType ? ValueTypeEnum::from($measureValueType) : null,

Check failure on line 40 in src/Builder/PayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to static method from() on an unknown class NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.
dimensions: $dimensions
);
}

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

Check failure on line 59 in src/Builder/PayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $type of method NorbyBaru\AwsTimestream\Builder\PayloadBuilder::setMeasureValueType() has invalid type NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.
{
$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 = ValueTypeEnum::VARCHAR): self

Check failure on line 73 in src/Builder/PayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to constant VARCHAR on an unknown class NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.

Check failure on line 73 in src/Builder/PayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $type of method NorbyBaru\AwsTimestream\Builder\PayloadBuilder::setMultiMeasuresValues() has invalid type NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.
{
$this->measureValues[] = [
'Name' => $name,
'Value' => $value,
'Type' => $type->value

Check failure on line 78 in src/Builder/PayloadBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $value on an unknown class NorbyBaru\AwsTimestream\Enum\ValueTypeEnum.
];

return $this;
}

private function buildDimensions(string $name, $value)
Expand All @@ -39,6 +89,23 @@ private function buildDimensions(string $name, $value)
];
}

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;
}

public static function buildCommonAttributes(array $attributes): array
{
$metrics = collect($attributes)
Expand All @@ -57,16 +124,45 @@ public static function buildCommonAttributes(array $attributes): array
];
}

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

public function getRecords(bool $batch = false): array
{
return $this->toArray($batch);
}

public function toArray(bool $batch = false): array
{
$metric = [
'Dimensions' => $this->dimensions,
'MeasureName' => $this->measureName,
'MeasureValue' => (string) $this->measureValue,
'MeasureValueType' => $this->measureValueType,
'Time' => (string) $this->time->getPreciseTimestamp(3),
];

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

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

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;
}

if (!$batch) {
return [$metric];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/PayloadBuilderContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

interface PayloadBuilderContract
{
public static function make(string $measureName, $measureValue, Carbon $time, string $measureValueType = 'DOUBLE', array $dimensions = []): self;
public static function make(string $measureName): self;

public static function buildCommonAttributes(array $attributes): array;

Expand Down
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
17 changes: 8 additions & 9 deletions src/TimestreamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,23 @@ 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
12 changes: 12 additions & 0 deletions src/enum/ValueTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace NorbyBaru\AwsTimestream\Enum;

enum ValueTypeEnum: string
{
case DOUBLE = 'DOUBLE';
case BIGINT = 'BIGINT';
case VARCHAR = 'VARCHAR';
case BOOLEAN = 'BOOLEAN';
case TIMESTAMP = 'TIMESTAMP';
}

0 comments on commit 88eaf33

Please sign in to comment.