Provides integration of InfluxDB ODM with SonataAdminBundle
MIT License
This bundle integrates the InfluxDB Object Document Mapper (ODM) library into SonataAdminBundle so that you can persist and retrieve objects to and from InfluxDB.
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Open a command console, enter your project directory and execute:
$ composer require javer/influxdb-admin-bundle
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require javer/influxdb-admin-bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
// config/bundles.php
return [
// ...
Javer\InfluxDB\AdminBundle\JaverInfluxDBAdminBundle::class => ['all' => true],
];
Full configuration options:
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"
Example of CpuLoadAdmin
definition:
# 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 }
Please note that you must use influxdb
as manager_type
to work with InfluxDB measurement class.
Pager pager_type
can be either default
or simple
.
Example of CpuLoad
measurement class:
// 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
}
Example of CpuLoadAdmin
class:
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
{
// ...
}
}
Please note that you must explicitly implement generateBaseRouteName()
and generateBaseRoutePattern()
because they results cannot detected automatically from the measurement class name.
These fields are used to display the information inside the list table.
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' => [],
],
]);
}
}
The most important option for each field is the type
. The available types include:
FieldDescriptionInterface::TYPE_DATETIME
)FieldDescriptionInterface::TYPE_BOOLEAN
)FieldDescriptionInterface::TYPE_INTEGER
)FieldDescriptionInterface::TYPE_FLOAT
)FieldDescriptionInterface::TYPE_STRING
)If no type is set, the Admin
class will use the type defined in the doctrine mapping definition.
These fields are displayed inside the filter box. They allow you to filter the list of entities by a number of different methods.
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');
}
}
The most important option for each filter is the type
. The available types from namespace Javer\InfluxDB\AdminBundle\Filter
are:
These fields are used to edit data on the edit page.
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');
}
}
If no type is set, the Admin
class will use the one set in the doctrine mapping definition.
The ProxyQuery
object is used to add missing features from the original 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();