Skip to content

Commit

Permalink
replaces sources with aliases (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru authored Jul 8, 2022
1 parent 3136fad commit 51b8c03
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 25 deletions.
17 changes: 6 additions & 11 deletions config/timestream.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'database' => env('AWS_TIMESTREAM_DATABASE'),

/**
* Enable query and metedata logging on server
* Enable query and metadata logging on server
*/
'debug_query' => env('TIMESTREAM_DEBUG_QUERY', false),

Expand All @@ -33,16 +33,11 @@
*/
'tables' => [
/**
* Default table table name
* To handle multiple tables access, you can map them below using key value pair.
* The `value` should represent the table name that you want to access
* The `key` is an alias to the table name that can be anything meaningful.
* eg. ['listing' => 'listing-kpi', 'github' => 'ingestion-github', 'prod' => 'prod']
*/
'default' => null,

/**
* To handle multiple tables acess, you can map them below and access them.
* The `value` of the `key` should represent the table name that you want to access
* and they `key` can be anything meaningful.
* eg. ['listing' => 'listing-kpi', ['github' => 'ingestion-github']]
*/
'sources' => [],
'aliases' => [],
],
];
4 changes: 2 additions & 2 deletions src/Dto/AbstractTimestreamDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function onDataBase(string $databaseName): self
return $this;
}

public function forTable(string $source): self
public function forTable(string $alias): self
{
$this->table = $this->tables[$source];
$this->table = $this->tables[$alias];

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Dto/TimestreamReaderDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class TimestreamReaderDto extends AbstractTimestreamDto
public function __construct(protected Builder $builder, string $forTable = null)
{
$this->database = config('timestream.database');
$this->tables = config('timestream.tables.sources');
$this->tables = config('timestream.tables.aliases');

if ($forTable) {
$this->forTable($forTable);
Expand Down
2 changes: 1 addition & 1 deletion src/Dto/TimestreamWriterDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final class TimestreamWriterDto extends AbstractTimestreamDto
public function __construct(protected array $records, protected array $commonAttributes = [], string $forTable = null)
{
$this->database = config('timestream.database');
$this->tables = config('timestream.tables.sources');
$this->tables = config('timestream.tables.aliases');

if ($forTable) {
$this->forTable($forTable);
Expand Down
3 changes: 1 addition & 2 deletions tests/Fixtures/Traits/ConfigurableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public function getTimestreamConfig(): array
'secret' => env('AWS_TIMESTREAM_SECRET'),
'database' => 'test-db',
'tables' => [
'default' => 'default',
'sources' => [
'aliases' => [
'test' => 'default',
],
],
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/ReaderUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function test_reader_dto_can_inject_database_and_table_for_sql_query()
->whereIn('measure_value::varchar', ['reviewer', 'open', 'closed'])
->orderBy('time', 'desc');

$tableSource = 'test';
$dto = TimestreamReaderDto::make($queryBuilder, $tableSource);
$alias = 'test';
$dto = TimestreamReaderDto::make($queryBuilder, $alias);

$database = config('timestream.database');
$table = config("timestream.tables.sources.{$tableSource}");
$table = config("timestream.tables.aliases.{$alias}");
$this->assertStringContainsString("FROM \"{$database}\".\"{$table}\"", $dto->toArray()['QueryString']);
}
}
10 changes: 5 additions & 5 deletions tests/Unit/WriterUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ public function test_it_should_ingest_to_correct_database_name_and_table_name()
$metrics['dimensions']
)->toArray();

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

$this->assertEquals(config('timestream.database'), $payload['DatabaseName']);
$this->assertEquals(config("timestream.tables.sources.{$table}"), $payload['TableName']);
$this->assertEquals(config("timestream.tables.aliases.{$alias}"), $payload['TableName']);

$timestreamWriter = TimestreamWriterDto::make($payload, [], $table);
$timestreamWriter = TimestreamWriterDto::make($payload, [], $alias);
$payload = $timestreamWriter->toArray();

$this->assertEquals(config('timestream.database'), $payload['DatabaseName']);
$this->assertEquals(config("timestream.tables.sources.{$table}"), $payload['TableName']);
$this->assertEquals(config("timestream.tables.aliases.{$alias}"), $payload['TableName']);
}

private function generateMetrics(): array
Expand Down

0 comments on commit 51b8c03

Please sign in to comment.