2017 © Pedro Peláez
 

library laravel-metrics

Managable metrics

image

gurmanalexander/laravel-metrics

Managable metrics

  • Saturday, April 28, 2018
  • by gurmanalexander
  • Repository
  • 2 Watchers
  • 4 Stars
  • 58 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 7 % Grown

The README.md

Laravel Metrics

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

Table of Contents

Installation

Require this package with composer:, (*2)

composer require gurmanalexander/laravel-metrics:1.*

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

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

Laravel 5.x:

GurmanAlexander\Metrics\MetricsServiceProvider::class,

Copy the package config to your local config with the publish command:, (*5)

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

You may use the metrics:table command to generate a migration with the proper table schema:, (*6)

php artisan metrics:table

And then migrate:, (*7)

php artisan migrate

Usage

Create Metrics

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

php artisan make:metrics PageViewCountMetrics

Creating TimeMetrics example:, (*9)

php artisan make:metrics FirstPaymentMetrics

This will create new class in app/Metrics/ folder, (*10)

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, (*11)

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

Start metrics

To start Metrics:, (*12)

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), (*13)

In TimeMetrics only two parameters - $user and $admin, (*14)

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

or, (*15)

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

Stop metrics

To stop Metrics:, (*16)

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

or, (*17)

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

Once metrics

To fire once Metrics:, (*18)

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

Statistics

To get statistics you can use MetricsStatistics class, (*19)

Example:, (*20)

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

Methods

user

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

$user - The user to which the metrics belongs., (*22)

// 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, (*23)

admin - The user who called the metrics., (*24)

startAt

Filter from $start_at date, (*25)

The metrics stats calculating by end_at field (when metrics stops), (*26)

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

endAt

Filter to $end_at date, (*27)

The metrics stats calculating by end_at field (when metrics stops), (*28)

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

betweenAt

Filter from $start_at to $end_at date, (*29)

The metrics stats calculating by end_at field (when metrics stops), (*30)

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

period

Calculating statistics grouped by periods, (*31)

$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, (*32)

Results

to get results, (*33)

count

return Count of all filtered Metrics in DB, (*34)

return Collection of Metrics, (*35)

sum

return Sum of all filtered Metrics in DB, (*36)

if this is TimeMetrics - total seconds for metrics, (*37)

avg

return Average of all filtered Metrics in DB, (*38)

if this is TimeMetrics - average seconds for metrics, (*39)

min

return Min of all filtered Metrics in DB, (*40)

if this is TimeMetrics - min seconds for metrics, (*41)

max

return Max of all filtered Metrics in DB, (*42)

if this is TimeMetrics - max seconds for metrics, (*43)

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
//     ],
// ]

The Versions

28/04 2018

dev-master

9999999-dev

Managable metrics

  Sources   Download

MIT

The Requires

 

by Alexander Gurman

laravel statistics metrics stats

28/04 2018

1.0.4

1.0.4.0

Managable metrics

  Sources   Download

MIT

The Requires

 

by Alexander Gurman

laravel statistics metrics stats

28/04 2018

1.0.3

1.0.3.0

Managable metrics

  Sources   Download

MIT

The Requires

 

by Alexander Gurman

laravel statistics metrics stats

01/07 2017

1.0.2

1.0.2.0

Managable metrics

  Sources   Download

MIT

The Requires

 

by Alexander Gurman

laravel statistics metrics stats

30/06 2017

1.0.1

1.0.1.0

Managable metrics

  Sources   Download

MIT

The Requires

 

by Alexander Gurman

laravel statistics metrics stats

30/06 2017

1.0.0

1.0.0.0

Managable metrics

  Sources   Download

MIT

The Requires

 

by Alexander Gurman

laravel statistics metrics stats