javer/ influxdb-odm-bundle
提供InfluxDB ODM与Symfony的集成
Requires
- php: >=8.1
- javer/influxdb-odm: ^1.3
- symfony/config: ^5.4 || ^6.0 || ^7.0
- symfony/dependency-injection: ^5.4 || ^6.0 || ^7.0
- symfony/http-foundation: ^5.4 || ^6.0 || ^7.0
- symfony/http-kernel: ^5.4 || ^6.0 || ^7.0
- symfony/service-contracts: ^1.0 || ^2.0 || ^3.0
- symfony/yaml: ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- phpstan/phpstan: ^1.3
- squizlabs/php_codesniffer: 3.8.*
- swivl/php-coding-standard: ^1.4
README
此包将InfluxDB对象文档映射器(ODM)库集成到Symfony中,以便您可以将对象持久化到InfluxDB并从中检索。
兼容性
此包当前版本的以下要求
- InfluxDB 1.x
- PHP 8.1+
- Symfony 5.4+
安装
确保全局已安装Composer,如Composer文档中的安装章节所述。
使用Symfony Flex的应用程序
打开命令行控制台,进入您的项目目录并执行
$ composer require javer/influxdb-odm-bundle
未使用Symfony Flex的应用程序
步骤1:下载包
打开命令行控制台,进入您的项目目录并执行以下命令以下载此包的最新稳定版本
$ composer require javer/influxdb-odm-bundle
步骤2:启用包
然后,通过将其添加到项目中config/bundles.php
文件中注册的包列表中,启用该包
// config/bundles.php return [ // ... Javer\InfluxDB\Bundle\JaverInfluxDBBundle::class => ['all' => true], ];
配置
# config/packages/javer_influx_db.yaml javer_influx_db: url: '%env(INFLUXDB_URL)%' mapping_dir: '%kernel.project_dir%/src/Measurement' mapping_type: attribute
为了使用注解,您必须安装一个名为doctrine/annotations
的额外包。
# .env INFLUXDB_URL=influxdb://:8086/metrics
映射配置
ODM的唯一必要配置是显式定义所有映射的度量。以下是对映射的配置选项
mapping_dir
- 度量文件路径
为了避免为您的映射配置大量信息,您应将所有度量放入项目中的Measurement/
目录中。例如 src/Measurement/
。
自定义类型
当您缺少特定的映射类型或想要替换现有映射类型的实现时,自定义类型
非常有用。
# config/packages/javer_influx_db.yaml javer_influx_db: types: custom_type: Fully\Qualified\Class\Name
用法
有关声明、创建、更新和删除度量的信息,请参阅influxdb-odm库的文档。
MeasurementManager
将Javer\InfluxDB\ODM\MeasurementManager
注入任何需要创建、更新或删除度量的服务
use App\Measurement\CpuLoad; use Javer\InfluxDB\ODM\MeasurementManager; public function demoAction(MeasurementManager $measurementManager) { $now = new DateTime(); // Create $cpuLoad = new CpuLoad(); $cpuLoad->setTime($now); $cpuLoad->setServerId(42); $cpuLoad->setCoreNumber(0); $cpuLoad->setLoad(3.14); $measurementManager->persist($cpuLoad); // Fetch $cpuLoad = $measurementManager->getRepository(CpuLoad::class)->find($now); // Update $cpuLoad->setLoad(2.54); $measurementManager->persist($cpuLoad); // Remove $measurementManager->remove($cpuLoad); }
服务存储库
此包提供了一种获取存储库实例的新方法:将存储库用作服务,并将其作为依赖项注入到其他服务中。
// src/Repository/CpuLoadRepository.php namespace App\Repository; use App\Measurement\CpuLoad; use Javer\InfluxDB\Bundle\Repository\ServiceMeasurementRepository; use Javer\InfluxDB\ODM\MeasurementManager; /** * Remember to map this repository in the corresponding measurement repositoryClass. */ class CpuLoadRepository extends ServiceMeasurementRepository { public function __construct(MeasurementManager $measurementManager) { parent::__construct($measurementManager, CpuLoad::class); } }
您的自定义存储库所扩展的ServiceMeasurementRepository
类允许您利用Symfony的autowiring
和autoconfiguration
。要注册所有存储库作为服务,可以使用以下服务配置
# config/services.yaml services: _defaults: autowire: true autoconfigure: true App\Repository\: resource: '../src/Repository/*'
Symfony Profiler数据收集器
此包向Symfony Profiler工具栏和新的Symfony Profiler页面添加了新的图标,以使您能够监控所有查询和写入到InfluxDB。默认情况下,仅在开发环境中启用。