actengage / metrics
A abstraction for generating metrics in Laravel.
Installs: 1 828
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- laravel/framework: ^11.0
Requires (Dev)
- laravel/pint: ^1.8
- mockery/mockery: ^1.5
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^10.0
README
This package provides a standalone metrics API similar to Laravel Nova.
Requirements
- Laravel 9.x+
- PHP 8.1+
Getting Started
Install via Composer
composer require actengage/metrics
Value Metric
use Actengage\Metrics\Contracts\Result; use Actengage\Metrics\Value; use App\Models\User; use Illuminate\Http\Request; $metric = new class extends Value { public function ranges(): array { return [ '5' => '5 Days', '10' => '10 Days', '15' => '15 Days', 'P21D' => '21 Days', 'today' => 'Today', 'yesterday' => 'Yesterday', 'MTD' => 'Month To Date', 'QTD' => 'Quarter To Date', 'YTD' => 'Year To Date', 'ALL' => 'All Time' ]; } public function calculate(Request $request): Result { return $this ->title('Total Users') ->description('The total number of users.') ->timezone('EST') ->range('P21D') ->count(User::class) ->prefix('#') ->suffix('users'); } }; $result = $metric->resolve(request()->merge([ 'range' => 'MTD' ]));
Trend Metric
use Actengage\Metrics\Contracts\Result; use Actengage\Metrics\Trend; use App\Models\User; use Illuminate\Http\Request; $metric = new class extends Trend { public function calculate(Request $request): Result { return $this ->title('User Registrations') ->description('User registrations trending by day.') ->twelveHourTime() ->range($request->range) ->count(User::class, Trend::BY_DAYS) ->prefix('#') ->suffix('users'); } }; $result = $metric->resolve(request()->merge([ 'range' => 30 ]));