-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve payload builder and add more feature test (#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
- Loading branch information
Showing
16 changed files
with
30,573 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
*.lock | ||
*.bak | ||
/.env | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.