harout-itology / laravel-aws-timestream
Library to interact with AWS Timestream service via API
Requires
- php: ^8.0
- aws/aws-sdk-php: ^3.209
- illuminate/support: ^8.0|^9.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.4
- nunomaduro/larastan: ^1.0|^2.0
- orchestra/testbench: ^6.24|^7.0
- phpunit/phpunit: ^9.5
- vlucas/phpdotenv: ^5.4
README
AWS Timestream is a fast, scalable, and serverless time series database service. This package is an opinionated implementation to query timestream and ingest data into timestream.
It provides a query builder class which has common timeseries sql function. This was inspired by Laravel Eloquent ORM.
See supported query functions HaroutItology\AwsTimestream\Contract\QueryBuilderContract
It also provide a payload builder class to format your data correctly to ingest into timestream.
See HaroutItology\AwsTimestream\Contract\PayloadBuilderContract
Install
composer require harout-itology/laravel-aws-timestream
Configuration
- Publish config
php artisan vendor:publish --provider="HaroutItology\AwsTimestream\TimestreamServiceProvider" --tag="timestream-config"
- Open
timestream.php
config file and setup your databse name and tables - Setup you AWS Timestream keys and permissions with the following enviroment variable
AWS_TIMESTREAM_KEY=
AWS_TIMESTREAM_SECRET=
AWS_TIMESTREAM_PROFILE=
AWS_TIMESTREAM_REGION=
Basic Usage
Query Timestream
Using TimestreamBuilder::query()
will give autocomplete of all available functions
- Using
TimestreamBuilder
to build query to be passed ontoTimestreamReaderDto
which generate am object that can be consumed byTimestreamService
query function
<?php use HaroutItology\AwsTimestream\TimestreamService; use HaroutItology\AwsTimestream\TimestreamBuilder; use HaroutItology\AwsTimestream\Dto\TimestreamReaderDto; public function overview(TimestreamService $timestreamService) { $queryBuilder = TimestreamBuilder::query() ->select('*') ->from("database-name", 'table-name') ->whereAgo('time', '24h', '>=') ->whereNotIn('measure_value::varchar', ['reviewer', 'open', 'closed']) ->orderBy('time', 'desc'); TimestreamReaderDto::make($queryBuilder); // response from Aws timestream return $timestreamService->query($timestreamReader) }
- Use
TimestreamReaderDto
to injectfrom
query with defaultdatabase
name and on demandtable
name. NB. No need to add->from()
query on your query builder.
<?php use HaroutItology\AwsTimestream\TimestreamService; use HaroutItology\AwsTimestream\TimestreamBuilder; use HaroutItology\AwsTimestream\Dto\TimestreamReaderDto; public function overview(TimestreamService $timestreamService) { $queryBuilder = TimestreamBuilder::query() ->select('*') ->whereAgo('time', '24h', '>=') ->whereNotIn('measure_value::varchar', ['reviewer', 'open', 'closed']) ->orderBy('time', 'desc'); TimestreamReaderDto::make($queryBuilder, 'table-name'); // response from Aws timestream return $timestreamService->query($timestreamReader) }
Timestream Ingestion
We need to build our payload that Timestream will accept for ingestion.
- Use
TimestreamBuilder
to build ingestion payload
<?php use HaroutItology\AwsTimestream\TimestreamService; use HaroutItology\AwsTimestream\Dto\TimestreamWriterDto; use HaroutItology\AwsTimestream\TimestreamBuilder; public function ingest(TimestreamService $timestreamService) { $metrics = [ 'measure_name' => 'cpu_usage', 'measure_value' => 80, 'time' => Carbon::now(), 'dimensions' => [ 'mac_address' => 'randomstring', 'ref' => 'refs', ], ]; $payload = TimestreamBuilder::payload( $metrics['measure_name'], $metrics['measure_value'], $metrics['time'], 'VARCHAR', $metrics['dimensions'], )->toArray(); $timestreamWriter = TimestreamWriterDto::make($payload)->forTable('table-name'); return $timestreamService->write($timestreamWriter); }
- Ingestion data in batch using Common Attributes to reduce ingestion cost with Timestream
<?php use HaroutItology\AwsTimestream\TimestreamService; use HaroutItology\AwsTimestream\Dto\TimestreamWriterDto; use HaroutItology\AwsTimestream\Support\TimestreamPayloadBuilder; public function ingest(TimestreamService $timestreamService) { $metrics = [ [ 'measure_name' => 'cpu_usage', 'measure_value' => 80, 'time' => Carbon::now(), 'dimensions' => [ 'ref' => 'ref_1', ], ], [ 'measure_name' => 'memory_usage', 'measure_value' => 20, 'time' => Carbon::now(), 'dimensions' => [ 'ref' => 'ref_2', ], ] ]; $commonAttributes['device_name'] = 'device_1'; $commonAttributes['mac_address'] = 'randomstring'; $payload = TimestreamBuilder::batchPayload($metrics); $common = TimestreamBuilder::commonAttributes($commonAttributes); $timestreamWriter = TimestreamWriterDto::make($payload, $common, 'table-name'); return $timestreamService->write($timestreamWriter); }