alexvergara/laravel-aws-timestream

由 Norbybaru 分支 - 用于通过 API 与 AWS Timestream 服务交互的库 - 支持降级到 Laravel6 + PHP7 - AWS Config 修复

v0.0.1 2022-09-29 21:58 UTC

This package is auto-updated.

Last update: 2024-08-29 06:07:33 UTC


README

PHPUnit Tests PHPStan

AWS Timestream 是一个快速、可扩展和无服务器的时序数据库服务。此包是对查询 timestream 并将数据摄入 timestream 的有见地性实现。

它提供了一个具有常见时序 SQL 函数的查询构建器类。这灵感来自 Laravel Eloquent ORM。请参阅支持的查询函数 AlexVergara\AwsTimestream\Contract\QueryBuilderContract

它还提供了一个有效载荷构建器类,用于正确格式化数据以摄入到 timestream。请参阅 AlexVergara\AwsTimestream\Contract\PayloadBuilderContract

安装

composer require norbybaru/laravel-aws-timestream

配置

  • 发布配置
php artisan vendor:publish --provider="AlexVergara\AwsTimestream\TimestreamServiceProvider" --tag="timestream-config"
  • 打开 timestream.php 配置文件并设置你的数据库名和表
  • 使用以下环境变量设置你的 AWS Timestream 密钥和权限
AWS_TIMESTREAM_KEY=
AWS_TIMESTREAM_SECRET=
AWS_TIMESTREAM_PROFILE=
AWS_TIMESTREAM_VERSION=
AWS_TIMESTREAM_REGION=

基本用法

查询 Timestream

使用 TimestreamBuilder::query() 将提供所有可用函数的自动完成

  1. 使用 TimestreamBuilder 构建查询并将其传递给 TimestreamReaderDto,它生成一个对象,该对象可以由 TimestreamService 查询函数消费
<?php

use AlexVergara\AwsTimestream\TimestreamService;
use AlexVergara\AwsTimestream\TimestreamBuilder;
use AlexVergara\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)
}
  1. 使用 TimestreamReaderDto 注入带有默认 database 名称和按需 table 名称的 from 查询。 注意。无需在查询构建器上添加 ->from() 查询。
<?php

use AlexVergara\AwsTimestream\TimestreamService;
use AlexVergara\AwsTimestream\TimestreamBuilder;
use AlexVergara\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 摄入

我们需要构建 Timestream 将接受的摄入有效载荷。

  1. 使用 TimestreamBuilder 构建摄入有效载荷
<?php

use AlexVergara\AwsTimestream\TimestreamService;
use AlexVergara\AwsTimestream\Dto\TimestreamWriterDto;
use AlexVergara\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);
}
  1. 使用通用属性批量摄入数据以降低 Timestream 摄入成本
<?php

use AlexVergara\AwsTimestream\TimestreamService;
use AlexVergara\AwsTimestream\Dto\TimestreamWriterDto;
use AlexVergara\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);
}

在线资源