azaharizaman/nexus-metric-engine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

azaharizaman/nexus-metric-engine

Composer 安装命令:

composer require azaharizaman/nexus-metric-engine

包简介

Deterministic Layer 1 metric calculation engine for Nexus packages.

README 文档

README

Deterministic Layer 1 metric calculation engine for Nexus packages.

Nexus\MetricEngine evaluates prepared scalar and time-series inputs against neutral formula definitions. It does not fetch data, persist state, render reports, or attach domain meaning to metrics.

Installation

composer require azaharizaman/nexus-metric-engine

Requirements

  • PHP 8.3 or newer

Basic Usage

<?php

declare(strict_types=1);

use Nexus\MetricEngine\Enums\AggregationType;
use Nexus\MetricEngine\Services\ComparisonService;
use Nexus\MetricEngine\Services\FormulaEvaluatorService;
use Nexus\MetricEngine\Services\NumericValueService;
use Nexus\MetricEngine\Services\PeriodComparatorService;
use Nexus\MetricEngine\Services\ScalarMetricCalculatorService;
use Nexus\MetricEngine\Services\TimeSeriesMetricCalculatorService;
use Nexus\MetricEngine\Services\WindowResolverService;
use Nexus\MetricEngine\ValueObjects\FormulaDefinition;
use Nexus\MetricEngine\ValueObjects\MetricInput;
use Nexus\MetricEngine\ValueObjects\PrecisionPolicy;

$numeric = new NumericValueService();
$evaluator = new FormulaEvaluatorService(
    new ScalarMetricCalculatorService($numeric),
    new TimeSeriesMetricCalculatorService(
        $numeric,
        new WindowResolverService(new PeriodComparatorService()),
        new ComparisonService($numeric)
    )
);

$formula = new FormulaDefinition(
    identifier: 'example.ratio',
    operation: AggregationType::RATIO,
    operands: ['actual', 'target'],
    precisionPolicy: new PrecisionPolicy(2)
);

$result = $evaluator->evaluate($formula, [
    'actual' => new MetricInput('actual', 75),
    'target' => new MetricInput('target', 100),
]);

$result->value(); // 0.75

Supported Calculations

Scalar primitives:

  • sum
  • avg
  • min
  • max
  • count
  • ratio
  • delta
  • absolute_delta
  • pct_change
  • weighted_avg
  • weighted_score

Time-series primitives:

  • rolling_sum
  • rolling_avg
  • period_compare

Failure Semantics

MetricEngine fails loudly with package-specific exceptions for invalid formula structure, missing inputs, incompatible input types, invalid numeric values, divide-by-zero operations, invalid windows, and insufficient time-series data.

It does not return fallback values or synthetic zeros unless a caller-defined formula explicitly models that behavior.

Batch Evaluation

Use FormulaCatalog and BatchFormulaEvaluatorService when an application needs status-aware metric runs.

$catalog = new FormulaCatalog([
    new FormulaDefinition('metric.delta', AggregationType::DELTA, ['revenue', 'cost'], PrecisionPolicy::default()),
    new FormulaDefinition('metric.ratio', AggregationType::RATIO, [new FormulaReference('metric.delta'), 'revenue'], PrecisionPolicy::default()),
]);

$batch = $batchEvaluator->evaluate($catalog, [
    'revenue' => new MetricInput('revenue', 100),
    'cost' => new MetricInput('cost', 60),
]);

$batch->get('metric.ratio')->status; // MetricResultStatus::AVAILABLE

The batch evaluator also accepts a formula list directly:

$batch = $batchEvaluator->evaluate([
    new FormulaDefinition('metric.total', AggregationType::SUM, ['revenue', 'cost'], PrecisionPolicy::default()),
], [
    'revenue' => new MetricInput('revenue', 100),
    'cost' => new MetricInput('cost', 60),
]);

Enable audit traces when callers need deterministic calculation evidence. Traces include original operands, resolved operands, used input values, dependency results, status, result values, and failure details when present.

$batch = $batchEvaluator->evaluate(
    $catalog,
    [
        'revenue' => new MetricInput('revenue', 100),
        'cost' => new MetricInput('cost', 60),
    ],
    MetricEvaluationOptions::withAuditTrace()
);

$batch->get('metric.ratio')->auditTrace?->resolvedOperands; // [40.0, 100]

Fingerprints

MetricRunFingerprintService returns stable hashes for reproducible runs. The hash is based on canonical serialized formulas, prepared inputs, caller metadata, and the formula dependency graph.

$fingerprint = $fingerprintService->fingerprint($catalog, [
    'revenue' => new MetricInput('revenue', 100),
    'cost' => new MetricInput('cost', 60),
]);

$fingerprint->algorithm; // sha256

Boundaries

MetricEngine is framework-agnostic. Laravel service providers belong in Laravel adapters or applications.

MetricEngine supports neutral units and precision policies, but currency-specific rules and money value objects belong in finance or accounting packages.

Development

composer install
./vendor/bin/phpunit
composer validate --strict

azaharizaman/nexus-metric-engine 适用场景与选型建议

azaharizaman/nexus-metric-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 azaharizaman/nexus-metric-engine 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 45
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-05