gurmanalexander / laravel-metrics by gurmanalexander

Managable metrics
3,034
14
2
Package Data
Maintainer Username: gurmanalexander
Maintainer Contact: gurmanalexander@gmail.com (Alexander Gurman)
Package Create Date: 2017-06-30
Package Last Update: 2019-04-09
Language: PHP
License: MIT
Last Refreshed: 2024-04-16 15:05:28
Package Statistics
Total Downloads: 3,034
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 14
Total Watchers: 2
Total Forks: 4
Total Open Issues: 2

Laravel Metrics

This package helps you to manage your application metrics (e.g. Time, Count, Money)

Table of Contents

  • Installation
  • Usage
    • Create Metrics
    • Metrics usage
    • Start Metrics
    • Stop Metrics
    • Once Metrics
  • Statistics
    • Methods
      • user()
      • admin()
      • startAt()
      • endAt()
      • betweenAt()
      • period
      • getBuilder()
    • Results
      • count()
      • sum()
      • avg()
      • min()
      • max()

Installation

Require this package with composer:

composer require gurmanalexander/laravel-metrics:1.*

After updating composer, add the ServiceProvider to the providers array in config/app.php

Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider

Laravel 5.x:

GurmanAlexander\Metrics\MetricsServiceProvider::class,

Copy the package config to your local config with the publish command:

php artisan vendor:publish --provider="GurmanAlexander\Metrics\MetricsServiceProvider"

You may use the metrics:table command to generate a migration with the proper table schema:

php artisan metrics:table

And then migrate:

php artisan migrate

Usage

Create Metrics

You can crete new Metrics (default CountMetrics), but you can change it to TimeMetrics with parameter --time

php artisan make:metrics PageViewCountMetrics

Creating TimeMetrics example:

php artisan make:metrics FirstPaymentMetrics

This will create new class in app/Metrics/ folder

class FirstPaymentMetrics extends CountMetrics
{

}

Metrics usage

Now you can start watching your Metrics. You need to add trait Metricable to your Model (e.g. User), that you want watching

use GurmanAlexander\Metrics\Metricable;
class User extends Model
{
    use Metricable;
    ...
}

Start metrics

To start Metrics:

In CountMetrics first parameter - $user (The user to which the metrics belongs, default null), second parameter - admin (The user who called the metrics, default null), third parameter - $count (How much to increase the metrics. For example, you can use money metrics. default 1)

In TimeMetrics only two parameters - $user and $admin

// For example, when creating user start Metrics
$user = User::create(['email', 'password']);
$user->startMetrics(new FirstPaymentMetrics($user));

or

// when user view some news
$user = auth()->user();
$news->startMetrics(new PageViewCountMetrics($user));

Stop metrics

To stop Metrics:

// when user make some payment
$user->paySomething();
$user->closeMetrics(new FirstPaymentMetrics($user));

or

// when user logout
$user = auth()->user();
$news->closeMetrics(new PageViewCountMetrics($user));
auth()->logout();

Once metrics

To fire once Metrics:

$user = auth()->user();
$user->onceMetrics(new SomeOnceMetrics());

Statistics

To get statistics you can use MetricsStatistics class

Example:

// to get total payments
$stats = new MetricsStatistics(new FirstPaymentMetrics());

Methods

user

Filter statistics by $user (user Model, array of users or Collection of users)

$user - The user to which the metrics belongs.

// model
$stats->user(auth()->user());

// array
$stats->user([auth()->user(), User:first()]);

// collection
$stats->user(User::where('is_active', 1)->get());

admin

Filter by admin like by user

admin - The user who called the metrics.

startAt

Filter from $start_at date

The metrics stats calculating by end_at field (when metrics stops)

$start_at = Carbon::now()->startOfMonth();
$stats->startAt($start_at);

endAt

Filter to $end_at date

The metrics stats calculating by end_at field (when metrics stops)

$end_at = Carbon::now()->endOfMonth();
$stats->endAt($end_at);

betweenAt

Filter from $start_at to $end_at date

The metrics stats calculating by end_at field (when metrics stops)

$start_at = Carbon::now()->startOfMonth();
$end_at = Carbon::now()->endOfMonth();
$stats->betweenAt($start_at, $end_at);

period

Calculating statistics grouped by periods

$stats->hourly();
$stats->daily();
$stats->weekly();
$stats->monthly();
$stats->yearly();

// result example
$stats->hourly()->count()->toArray();

// [
//     0 => [
//         "count" => 23,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 9
//     ],
//     1 => [
//         "count" => 15,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 8
//     ],
//     2 => [
//         "count" => 32,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 7
//     ],
// ]

getBuilder

return Builder to your custom calculating

Results

to get results

count

return Count of all filtered Metrics in DB

return Collection of Metrics

sum

return Sum of all filtered Metrics in DB

if this is TimeMetrics - total seconds for metrics

avg

return Average of all filtered Metrics in DB

if this is TimeMetrics - average seconds for metrics

min

return Min of all filtered Metrics in DB

if this is TimeMetrics - min seconds for metrics

max

return Max of all filtered Metrics in DB

if this is TimeMetrics - max seconds for metrics

Example:

// to get total payments
$stats = new MetricsStatistics(new FirstPaymentMetrics());

// to get average time from user registration to first payment (in seconds)
$stats = new MetricsStatistics(new FirstPaymentMetrics())->hourly()->avg()->toArray();

// [
//     0 => [
//         "avg" => 12.13,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 9
//     ],
//     1 => [
//         "avg" => 8.00,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 8
//     ],
//     2 => [
//         "avg" => 5.34,
//         "date" => "2017-06-30",
//         "period" => "hour",
//         "hour" => 7
//     ],
// ]