PHP code example of norbybaru / laravel-aws-timestream

1. Go to this page and download the library: Download norbybaru/laravel-aws-timestream library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

norbybaru / laravel-aws-timestream example snippets




use NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\TimestreamBuilder;
use NorbyBaru\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 NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\TimestreamBuilder;
use NorbyBaru\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)
}



use NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\Dto\TimestreamWriterDto;
use NorbyBaru\AwsTimestream\Builder\TimestreamPayloadBuilder;

public function ingest(TimestreamService $timestreamService)
{
    $payload = TimestreamPayloadBuilder::make(measureName: 'cpu_usage')
        ->setMeasureValue(value: 80.5)
        ->setMeasureValueType(type: ValueTypeEnum::DOUBLE())
        ->setDimensions(name: "mac_address", value: '00:11:22:AA:BB:CC ')
        ->setDimensions(name: "ref", value: 'station a')
        ->setTime(Carbon::now());

    $timestreamWriter = TimestreamWriterDto::make($payload->toRecords())->forTable('table-name');

    return $timestreamService->write($timestreamWriter);
}



use NorbyBaru\AwsTimestream\TimestreamService;
use NorbyBaru\AwsTimestream\Dto\TimestreamWriterDto;
use NorbyBaru\AwsTimestream\Builder\TimestreamPayloadBuilder;

public function ingest(TimestreamService $timestreamService)
{
    $payloads = [
        ...TimestreamPayloadBuilder::make(measureName: 'cpu_usage')
            ->setMeasureValue(value: 80.6)
            ->setDimensions(name: "ref", value: 'station a')
            ->toRecords(),
        ...TimestreamPayloadBuilder::make(measureName: 'memory_usage')
            ->setMeasureValue(value: 45.5)
            ->setDimensions(name: "ref", value: 'station b')
            ->toRecords(),
    ];

    $common = CommonPayloadBuilder::make()
        ->setCommonDimensions(name: 'processor', value: 'unix')
        ->setCommonDimensions(name: 'mac_address', value: '00:11:22:AA:BB:CC')
        ->setCommonDimensions(name: 'device_name', value: 'device_1')
        ->setCommonMeasureValueType(ValueTypeEnum::DOUBLE())
        ->setCommonTime(Carbon::now())
        ->toArray();

    $timestreamWriter = TimestreamWriterDto::make($payloads, $common, 'table-name');

    return $timestreamService->write($timestreamWriter);
}
bash
php artisan vendor:publish --provider="NorbyBaru\AwsTimestream\TimestreamServiceProvider" --tag="timestream-config"