emilhorlyck/laravel-poly-metrics

This is my package laravel-poly-metrics

0.1.0 2024-10-18 17:57 UTC

This package is auto-updated.

Last update: 2024-11-21 15:37:45 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.