emilhorlyck / laravel-poly-metrics
This is my package laravel-poly-metrics
Fund package maintenance!
emilhorlyck
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
This package allows you to log metrics for multiple models, and even without relation to a models in laravel.
Features
- Log metrics without relation to a model
- Daily resolution
- Monthly resolution
- Log metrics with relation to a model
- Daily resolution
- Monthly resolution
- Offer traits to easily log metrics on models
- Offer command to purge old metrics
- Offer command to purge old metrics for a specific model
- Deafult scheduled command to purge old metrics
Installation
You can install the package via composer:
composer require emilhorlyck/laravel-poly-metrics
You can publish all the necessary files with:
php artisan vendor:publish --provider="EmilHorlyck\PolyMetric\PolyMetricServiceProvider"
You can them run the migrations with:
php artisan migrate
Usage
Log metrics without relation to a model
// Modify metrics with daily and monthly resolution PolyMetric::set('my-metric', 42); // This will set the metric to 42 PolyMetric::increment('my-metric'); // This will increment the metric by 1 PolyMetric::decrement('my-metric'); // This will decrement the metric by 1 // Modify metrics with only daily resolution PolyMetric::setDaily('my-metric', 42); // This will set the metric to 42 PolyMetric::incrementDaily('my-metric'); // This will increment the metric by 1 PolyMetric::decrementDaily('my-metric'); // This will decrement the metric by 1 // Modify metrics with only monthly resolution PolyMetric::setMonthly('my-metric', 42); // This will set the metric to 42 PolyMetric::incrementMonthly('my-metric'); // This will increment the metric by 1 PolyMetric::decrementMonthly('my-metric'); // This will decrement the metric by 1
Log metrics with relation to a model
Add the HasMetrics
trait to your model.
use EmilHorlyck\PolyMetric\Traits\HasMetrics; class User extends Model { use HasMetrics; }
Log metrics on the model
$user = User::find(1); // Modify metrics with daily and monthly resolution $user->setMetric('my-metric', 42); // This will set the metric to 42 $user->incrementMetric('my-metric'); // This will increment the metric by 1 $user->decrementMetric('my-metric'); // This will decrement the metric by 1 // Modify metrics with only daily resolution $user->setDailyMetric('my-metric', 42); // This will set the metric to 42 $user->incrementDailyMetric('my-metric'); // This will increment the metric by 1 $user->decrementDailyMetric('my-metric'); // This will decrement the metric by 1 // Modify metrics with only monthly resolution $user->setMonthlyMetric('my-metric', 42); // This will set the metric to 42 $user->incrementMonthlyMetric('my-metric'); // This will increment the metric by 1 $user->decrementMonthlyMetric('my-metric'); // This will decrement the metric by 1
Credits
License
The MIT License (MIT). Please see License File for more information.