承接 javer/influxdb-odm 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

javer/influxdb-odm

Composer 安装命令:

composer require javer/influxdb-odm

包简介

Provides a PHP object mapping functionality for InfluxDB

README 文档

README

The InfluxDB ODM is a library that provides a PHP object mapping functionality for InfluxDB.

Build Status

Installation

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-odm

Usage

The best way to understand the InfluxDB ODM is to see it in action. In this section, you'll walk through each step needed to start persisting measurements to and from InfluxDB.

Create a Measurement Class with Mapping Information

Doctrine allows you to work with InfluxDB in a much more interesting way than just fetching data back and forth as an array. Instead, Doctrine allows you to persist entire objects to InfluxDB and fetch entire objects out of InfluxDB. This works by mapping a PHP class and its properties to entries of a InfluxDB Measurement.

For Doctrine to be able to do this, you have to create "metadata", or configuration that tells Doctrine exactly how the CpuLoad class and its properties should be mapped to InfluxDB. This metadata can be specified directly inside the CpuLoad class via annotations:

// 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;
}

or using PHP 8 Attributes:

// 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;
}

Create Measurement Manager

If you are not using JaverInfluxDBBundle, you have to create an instance of MeasurementManager:

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);

To be able to use annotations, you will have to install an extra package called doctrine/annotations.

Using PHP 8 Attributes:

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);

Persisting Objects to InfluxDB

Now that you have a mapped CpuLoad measurement complete with getter and setter methods, you're ready to persist data to InfluxDB:

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);

Fetching Objects from InfluxDB

Fetching an object back out of InfluxDB is also possible:

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

When you query for a particular type of object, you always use what's known as its "repository". You can think of a repository as a PHP class whose only job is to help you fetch objects of a certain class. You can access the repository object for a measurement class via:

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

Once you have your repository, you have access to all sorts of helpful methods:

// 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]);

You can also take advantage of the useful findBy() and findOneBy() methods to easily fetch objects based on multiple conditions:

// 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']
);

Updating an Object

Once you've fetched an object from Doctrine, let's try to update it.

$cpuLoad->setLoad(2.54);

$measurementManager->persist($cpuLoad);

Please note that you cannot update value of Timestamp or Tag field, only simple Field can be updated.

Deleting an Object

Deleting an object is very similar, but requires a call to the remove() method of the measurement manager:

$measurementManager->remove($cpuLoad);

Querying for Objects

As you saw above, the built-in repository class allows you to query for one or many objects based on any number of different parameters. When this is enough, this is the easiest way to query for measurements. You can also create more complex queries.

Using the Query Builder

InfluxDB ODM ships with a query "Builder" object, which allows you to construct a query for exactly which measurements you want to return. If you use an IDE, you can also take advantage of auto-completion as you type the method names. From inside a controller:

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

Custom Repository Classes

In the previous section, you began constructing and using more complex queries from inside a controller. In order to isolate, test and reuse these queries, it's a good idea to create a custom repository class for your measurement and add methods with your query logic there.

To do this, add the name of the repository class to your mapping definition.

// 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
{
    // ...
}

You have to create the repository in the namespace indicated above. Make sure it extends the default MeasurementRepository. Next, add a new method - findAllOrderedByTimeDesc() - to the new repository class. This method will query for all of the CpuLoad measurements, ordered by time desc.

// 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();
    }
}

You can use this new method like the default finder methods of the repository:

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

When using a custom repository class, you still have access to the default finder methods such as find() and findAll().

javer/influxdb-odm 适用场景与选型建议

javer/influxdb-odm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.86k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「doctrine」 「odm」 「influxdb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 javer/influxdb-odm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 javer/influxdb-odm 我们能提供哪些服务?
定制开发 / 二次开发

基于 javer/influxdb-odm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21.86k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 17
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-18