Skip to content

Commit

Permalink
refactor with TimestreamPayloadBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru committed Oct 18, 2023
1 parent 67e58bc commit d895ff9
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 25 deletions.
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;
}
}
28 changes: 14 additions & 14 deletions tests/Feature/PayloadWriterFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use NorbyBaru\AwsTimestream\Builder\CommonPayloadBuilder;
use NorbyBaru\AwsTimestream\Builder\PayloadBuilder;
use NorbyBaru\AwsTimestream\Dto\TimestreamWriterDto;
use NorbyBaru\AwsTimestream\Enum\ValueTypeEnum;
use NorbyBaru\AwsTimestream\Tests\TestCase;
use NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\Enum\ValueTypeEnum;
use NorbyBaru\AwsTimestream\Dto\TimestreamWriterDto;
use NorbyBaru\AwsTimestream\Builder\CommonPayloadBuilder;
use NorbyBaru\AwsTimestream\Builder\TimestreamPayloadBuilder;

class PayloadWriterFeatureTest extends TestCase
{
/**
* Writing of Multi-measure attributes
*/
public function test_it_should_ingest_multi_measure_records()
public function it_should_ingest_multi_measure_records()
{
$filePath = __DIR__ . "/../Fixtures/data/sample.csv";
$records = [];
foreach ($this->readCSV($filePath) as $index => $row) {
$data = explode(";", $row[0]);
$payload = PayloadBuilder::make(measureName: 'metric');
$payload = TimestreamPayloadBuilder::make(measureName: 'metric');

$payload
->setDimensions(name: $data[0], value: $data[1])
Expand All @@ -37,7 +37,7 @@ public function test_it_should_ingest_multi_measure_records()
$payload->setTime(Carbon::now()->subMilliseconds($index * 50));
$records = [
...$records,
...$payload->toArray(),
...$payload->toRecords(),
];

if (count($records) === 100) {
Expand All @@ -57,19 +57,19 @@ public function test_it_should_ingest_multi_measure_records()
*/
public function test_it_should_ingest_single_measure_record()
{
$payload = PayloadBuilder::make(measureName: 'device')
$payload = TimestreamPayloadBuilder::make(measureName: 'device')
->setMeasureValue(value: $this->faker->randomDigit)
->setDimensions(name: "mac_address", value: $this->faker->macAddress)
->setDimensions(name: "ref", value: $this->faker->uuid)
->setTime(Carbon::now());

$timestreamWriter = TimestreamWriterDto::make($payload->toArray())->forTable('test');
$timestreamWriter = TimestreamWriterDto::make($payload->toRecords())->forTable('test');

/** @var TimestreamService */
$timestreamService = app(TimestreamService::class);
$result = $timestreamService->write($timestreamWriter);

$this->assertAwsResults($result, count($payload->toArray()));
$this->assertAwsResults($result, count($payload->toRecords()));
}

/**
Expand All @@ -78,14 +78,14 @@ public function test_it_should_ingest_single_measure_record()
public function test_it_should_batch_ingest_data()
{
$payloads = [
...PayloadBuilder::make(measureName: 'cpu_usage')
...TimestreamPayloadBuilder::make(measureName: 'cpu_usage')
->setMeasureValue(value: $this->faker->randomFloat(5, 1, 100))
->setDimensions(name: "ref", value: $this->faker->uuid)
->toArray(),
...PayloadBuilder::make(measureName: 'memory_usage')
->toRecords(),
...TimestreamPayloadBuilder::make(measureName: 'memory_usage')
->setMeasureValue(value: $this->faker->randomFloat(5, 1, 100))
->setDimensions(name: "ref", value: $this->faker->uuid)
->toArray(),
->toRecords(),
];

$common = CommonPayloadBuilder::make()
Expand Down
Loading

0 comments on commit d895ff9

Please sign in to comment.