This package helps you to manage your application metrics (e.g. Time, Count, Money)
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
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
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
{
}
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;
...
}
To start Metrics:
In CountMetrics first parameter -
$user
(The user to which the metrics belongs, defaultnull
), second parameter -admin
(The user who called the metrics, defaultnull
), third parameter -$count
(How much to increase the metrics. For example, you can use money metrics. default1
)
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));
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();
To fire once Metrics:
$user = auth()->user();
$user->onceMetrics(new SomeOnceMetrics());
To get statistics you can use MetricsStatistics
class
Example:
// to get total payments
$stats = new MetricsStatistics(new FirstPaymentMetrics());
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());
Filter by admin like by user
admin
- The user who called the metrics.
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);
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);
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);
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
// ],
// ]
return Builder to your custom calculating
to get results
return Count of all filtered Metrics in DB
return Collection of Metrics
return Sum of all filtered Metrics in DB
if this is TimeMetrics - total seconds for metrics
return Average of all filtered Metrics in DB
if this is TimeMetrics - average seconds for metrics
return Min of all filtered Metrics in DB
if this is TimeMetrics - min seconds for metrics
return Max of all filtered Metrics in DB
if this is TimeMetrics - max seconds for metrics
// 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
// ],
// ]