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