PHP code example of javer / influxdb-odm

1. Go to this page and download the library: Download javer/influxdb-odm 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/ */

    

javer / influxdb-odm example snippets


// 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", countable=true)
     */
    private ?float $load = null;
}

// 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', countable: true)]
    private ?float $load = null;
}

use Doctrine\Common\Annotations\AnnotationReader;
use Javer\InfluxDB\ODM\Connection\ConnectionFactory;
use Javer\InfluxDB\ODM\Mapping\Driver\AnnotationDriver;
use Javer\InfluxDB\ODM\MeasurementManager;
use Javer\InfluxDB\ODM\Repository\RepositoryFactory;

$dsn = 'influxdb://localhost:8086/metrics';
$mappingDir = 'src/Measurements';
$mappingDriver = new AnnotationDriver(new AnnotationReader(), $mappingDir);
$connectionFactory = new ConnectionFactory();
$repositoryFactory = new RepositoryFactory();
$measurementManager = new MeasurementManager($mappingDriver, $connectionFactory, $repositoryFactory, $dsn);

use Javer\InfluxDB\ODM\Connection\ConnectionFactory;
use Javer\InfluxDB\ODM\Mapping\Driver\AttributeDriver;
use Javer\InfluxDB\ODM\MeasurementManager;
use Javer\InfluxDB\ODM\Repository\RepositoryFactory;

$dsn = 'influxdb://localhost:8086/metrics';
$mappingDir = 'src/Measurements';
$mappingDriver = new AttributeDriver($mappingDir);
$connectionFactory = new ConnectionFactory();
$repositoryFactory = new RepositoryFactory();
$measurementManager = new MeasurementManager($mappingDriver, $connectionFactory, $repositoryFactory, $dsn);

use App\Measurement\CpuLoad;
use Javer\InfluxDB\ODM\MeasurementManager;

$cpuLoad = new CpuLoad();
$cpuLoad->setTime(new DateTime());
$cpuLoad->setServerId(42);
$cpuLoad->setCoreNumber(0);
$cpuLoad->setLoad(3.14);

/** @var MeasurementManager $measurementManager */
$measurementManager->persist($cpuLoad);

$cpuLoad = $measurementManager->getRepository(CpuLoad::class)->find($time);

$repository = $measurementManager->getRepository(CpuLoad::class);

// query by the time
$cpuLoad = $repository->find($time);

// find *all* CPU metrics
$cpuLoads = $repository->findAll();

// find a group of CPU metrics for the particular server
$cpuLoads = $repository->findBy(['serverId' => 42]);

// query for one cpuLoad matching by serverId and coreNumber
$cpuLoad = $repository->findOneBy(['serverId' => 42, 'coreNumber' => 3]);

// query for all cpuLoads matching the serverId, ordered by time desc
$cpuLoads = $repository->findBy(
    ['serverId' => 42],
    ['time' => 'DESC']
);

$cpuLoad->setLoad(2.54);

$measurementManager->persist($cpuLoad);

$measurementManager->remove($cpuLoad);

$cpuLoads = $measurementManager->createQuery(CpuLoad::class)
    ->where('serverId', 42)
    ->orderBy('time', 'ASC')
    ->limit(10)
    ->getResult();

// src/Measurement/CpuLoad.php
namespace App\Measurement;

use App\Repository\CpuLoadRepository;
use Javer\InfluxDB\ODM\Mapping\Annotations as InfluxDB;

/**
 * @InfluxDB\Measurement(name="cpu_load", repositoryClass=CpuLoadRepository::class)
 */
class CpuLoad
{
    // ...
}

// src/Repository/CpuLoadRepository.php
namespace App\Repository;

use Javer\InfluxDB\ODM\Repository\MeasurementRepository;

class CpuLoadRepository extends MeasurementRepository
{
    public function findAllOrderedByTimeDesc(): array
    {
        return $this->createQuery()
            ->orderBy('time', 'DESC')
            ->getResult();
    }
}

$cpuLoads = $measurementManager->getRepository(CpuLoad::class)
    ->findAllOrderedByTimeDesc();