javer / influxdb-admin-bundle
提供InfluxDB ODM与SonataAdminBundle的集成
Requires
- php: >=8.1
- javer/influxdb-odm: ^1.0.4
- javer/influxdb-odm-bundle: ^1.0
- sonata-project/admin-bundle: ^4.17
- sonata-project/form-extensions: ^1.15 || ^2.0
- symfony/config: ^5.4 || ^6.0 || ^7.0
- symfony/dependency-injection: ^5.4 || ^6.0 || ^7.0
- symfony/form: ^5.4 || ^6.0 || ^7.0
- symfony/http-kernel: ^5.4 || ^6.0 || ^7.0
- symfony/yaml: ^5.4 || ^6.0 || ^7.0
Requires (Dev)
- phpstan/phpstan: ^1.0
- squizlabs/php_codesniffer: 3.8.*
- swivl/php-coding-standard: ^1.4
Conflicts
- sonata-project/exporter: <2.8.0
README
此扩展包将InfluxDB对象文档映射器(ODM)库集成到SonataAdminBundle中,以便您可以将对象持久化和检索到InfluxDB中。
安装
确保已全局安装Composer,具体请参阅Composer文档的安装章节。
使用Symfony Flex的应用程序
打开命令行,进入项目目录并执行
$ composer require javer/influxdb-admin-bundle
未使用Symfony Flex的应用程序
步骤1:下载扩展包
打开命令行,进入项目目录并执行以下命令以下载此扩展包的最新稳定版本
$ composer require javer/influxdb-admin-bundle
步骤2:启用扩展包
然后,通过将其添加到项目中config/bundles.php文件中注册的扩展包列表中来启用扩展包
// config/bundles.php return [ // ... Javer\InfluxDB\AdminBundle\JaverInfluxDBAdminBundle::class => ['all' => true], ];
配置
完整的配置选项
javer_influx_db_admin: templates: form: - '@SonataAdmin/Form/form_admin_fields.html.twig' filter: - '@SonataAdmin/Form/filter_admin_fields.html.twig' types: list: array: "@SonataAdmin/CRUD/list_array.html.twig" boolean: "@SonataAdmin/CRUD/list_boolean.html.twig" date: "@SonataAdmin/CRUD/list_date.html.twig" time: "@SonataAdmin/CRUD/list_time.html.twig" datetime: "@SonataAdmin/CRUD/list_datetime.html.twig" text: "@SonataAdmin/CRUD/base_list_field.html.twig" trans: "@SonataAdmin/CRUD/list_trans.html.twig" string: "@SonataAdmin/CRUD/base_list_field.html.twig" smallint: "@SonataAdmin/CRUD/base_list_field.html.twig" bigint: "@SonataAdmin/CRUD/base_list_field.html.twig" integer: "@SonataAdmin/CRUD/base_list_field.html.twig" decimal: "@SonataAdmin/CRUD/base_list_field.html.twig" identifier: "@SonataAdmin/CRUD/base_list_field.html.twig" show: array: "@SonataAdmin/CRUD/show_array.html.twig" boolean: "@SonataAdmin/CRUD/show_boolean.html.twig" date: "@SonataAdmin/CRUD/show_date.html.twig" time: "@SonataAdmin/CRUD/show_time.html.twig" datetime: "@SonataAdmin/CRUD/show_datetime.html.twig" text: "@SonataAdmin/CRUD/base_show_field.html.twig" trans: "@SonataAdmin/CRUD/show_trans.html.twig" string: "@SonataAdmin/CRUD/base_show_field.html.twig" smallint: "@SonataAdmin/CRUD/base_show_field.html.twig" bigint: "@SonataAdmin/CRUD/base_show_field.html.twig" integer: "@SonataAdmin/CRUD/base_show_field.html.twig" decimal: "@SonataAdmin/CRUD/base_show_field.html.twig"
管理类定义
CpuLoadAdmin
定义示例
# config/services.yaml services: acme.admin.cpu_load: class: App\Admin\CpuLoadAdmin arguments: [ ~, App\Measurement\CpuLoad, ~ ] tags: - { name: sonata.admin, manager_type: influxdb, label: 'CPU Load', pager_type: simple }
请注意,您必须使用influxdb
作为manager_type
来与InfluxDB测量类一起工作。分页器pager_type
可以是default
或simple
。
测量类CpuLoad
示例
// src/Measurement/CpuLoad.php namespace App\Measurement; use Javer\InfluxDB\ODM\Mapping\Annotations as InfluxDB; /** * @InfluxDB\Measurement(name="cpu_load") */ class CpuLoad { /** * @InfluxDB\Timestamp(precision="u") */ private ?\DateTime $time = null; /** * @InfluxDB\Tag(name="server_id", type="integer") */ private ?int $serverId = null; /** * @InfluxDB\Tag(name="core_number", type="integer") */ private ?int $coreNumber = null; /** * @InfluxDB\Field(name="load", type="float") */ private ?float $load = null; // ...getters and setters }
CpuLoadAdmin
类示例
namespace App\Admin; use Sonata\AdminBundle\Admin\AbstractAdmin; class CpuLoadAdmin extends AbstractAdmin { protected function generateBaseRouteName(bool $isChildAdmin = false): string { return 'cpu_load'; } protected function generateBaseRoutePattern(bool $isChildAdmin = false): string { return 'cpu_load'; } protected function configureListFields(ListMapper $list): void { // ... } protected function configureDatagridFilters(DatagridMapper $filter): void { // ... } protected function configureShowFields(ShowMapper $show): void { // ... } protected function configureFormFields(FormMapper $form): void { // ... } }
请注意,您必须显式实现generateBaseRouteName()
和generateBaseRoutePattern()
,因为它们的结果无法自动从测量类名称中检测。
列表字段定义
这些字段用于在列表表中显示信息。
示例
namespace App\Admin; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface; class CpuLoadAdmin extends AbstractAdmin { protected function configureListFields(ListMapper $list): void { $list ->addIdentifier('time', FieldDescriptionInterface::TYPE_DATETIME, [ 'format' => 'Y-m-d H:i:s.u', ]) ->add('serverId') ->add('load') ->add(ListMapper::NAME_ACTIONS, ListMapper::TYPE_ACTIONS, [ 'actions' => [ 'show' => [], 'edit' => [], 'delete' => [], ], ]); } }
可用类型
每个字段最重要的选项是type
。可用的类型包括
- datetime (
FieldDescriptionInterface::TYPE_DATETIME
) - boolean (
FieldDescriptionInterface::TYPE_BOOLEAN
) - integer (
FieldDescriptionInterface::TYPE_INTEGER
) - float (
FieldDescriptionInterface::TYPE_FLOAT
) - string (
FieldDescriptionInterface::TYPE_STRING
)
如果没有设置类型,则Admin类将使用在doctrine映射定义中定义的类型。
过滤器字段定义
这些字段在过滤器框内显示。它们允许您通过多种不同方法过滤实体列表。
示例
namespace App\Admin; use Javer\InfluxDB\AdminBundle\Filter\DateTimeRangeFilter; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Datagrid\DatagridMapper; class CpuLoadAdmin extends AbstractAdmin { protected function configureDatagridFilters(DatagridMapper $filter): void { $filter ->add('time', DateTimeRangeFilter::class) ->add('serverId'); } }
可用类型
每个过滤器最重要的选项是type
。来自命名空间Javer\InfluxDB\AdminBundle\Filter
的可用的类型包括
- BooleanFilter
- NumberFilter
- StringFilter
- ChoiceFilter
- CallbackFilter
- DateFilter
- DateTimeFilter
- DateRangeFilter
- DateTimeRangeFilter
表单字段定义
这些字段用于在编辑页面上编辑数据。
示例
namespace App\Admin; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Form\FormMapper; class CpuLoadAdmin extends AbstractAdmin { protected function configureFormFields(FormMapper $form): void { $form ->add('serverId') ->add('load'); } }
可用类型
- 复选框
- 整数
- 文本
- 选择
- datetime
如果没有设置类型,则Admin类将使用在doctrine映射定义中设置的类型。
InfluxDB代理查询
ProxyQuery
对象用于添加原始Doctrine Query builder中缺失的功能
use Javer\InfluxDB\AdminBundle\Datagrid\ProxyQuery; $query = $this->measurementManager->createQuery(); $proxyQuery = new ProxyQuery($query); $proxyQuery->setSortBy('time'); $proxyQuery->setMaxResults(10); $results = $proxyQuery->execute();