-
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.
refactor with TimestreamPayloadBuilder
- Loading branch information
Showing
3 changed files
with
295 additions
and
25 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 |
---|---|---|
@@ -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
Oops, something went wrong.