PHP code example of azaharizaman / nexus-metric-engine
1. Go to this page and download the library: Download azaharizaman/nexus-metric-engine 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/ */
azaharizaman / nexus-metric-engine example snippets
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
$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
$batch = $batchEvaluator->evaluate([
new FormulaDefinition('metric.total', AggregationType::SUM, ['revenue', 'cost'], PrecisionPolicy::default()),
], [
'revenue' => new MetricInput('revenue', 100),
'cost' => new MetricInput('cost', 60),
]);