Laravel Easy Metrics

sakanjo/laravel-easy-metrics image

Laravel Easy Metrics stats

Downloads
36
Stars
37
Open Issues
0
Forks
0

View on GitHub →

Generate metrics with ease and precision.

Laravel v10.x PHP 8.0

đŸ”Ĩ Easy metrics

Easily create metrics for your application.

✨ Help support the maintenance of this package by sponsoring me.

Designed to work with Laravel, Filament, and more.

🚀 Supported metrics

  • Bar metric
  • Doughnut metric
  • Line metric
  • Pie metric
  • Polar metric
  • Trend metric
  • Value metric

Table of contents

đŸ“Ļ Install

composer require sakanjo/laravel-easy-metrics

đŸĻ„ Usage

Value metric

use SaKanjo\EasyMetrics\Metrics\Value;
use App\Models\User;
 
$data = Value::make(User::class)
->count();

Query types

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

Min
Value::make(User::class)
->min('age');
Max
Value::make(User::class)
->max('age');
Sum
Value::make(User::class)
->sum('age');
Average
Value::make(User::class)
->average('age');
Count
Value::make(User::class)
->count();

Doughnut metric

use SaKanjo\EasyMetrics\Metrics\Doughnut;
use App\Models\User;
use App\Enums\Gender;
 
[$labels, $data] = Doughnut::make(User::class)
->options(Gender::class)
->count('gender');

It's always better to use the options method even though it's optional, since the retrieved data may not include all enum options.

Query types

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

Min
Doughnut::make(User::class)
->min('age', 'gender');
Max
Doughnut::make(User::class)
->max('age', 'gender');
Sum
Doughnut::make(User::class)
->sum('age', 'gender');
Average
Doughnut::make(User::class)
->average('age', 'gender');
Count
Doughnut::make(User::class)
->count('gender');

Trend metric

use SaKanjo\EasyMetrics\Metrics\Trend;
use App\Models\User;
 
[$labels, $data] = Trend::make(User::class)
->countByMonths();

Query types

The currently supported aggregate functions to calculate a given column compared to the previous time interval / range

Min
$trend->minByYears('age');
$trend->minByMonths('age');
$trend->minByWeeks('age');
$trend->minByDays('age');
$trend->minByHours('age');
$trend->minByMinutes('age');
Max
$trend->maxByYears('age');
$trend->maxByMonths('age');
$trend->maxByWeeks('age');
$trend->maxByDays('age');
$trend->maxByHours('age');
$trend->maxByMinutes('age');
Sum
$trend->sumByYears('age');
$trend->sumByMonths('age');
$trend->sumByWeeks('age');
$trend->sumByDays('age');
$trend->sumByHours('age');
$trend->sumByMinutes('age');
Average
$trend->averageByYears('age');
$trend->averageByMonths('age');
$trend->averageByWeeks('age');
$trend->averageByDays('age');
$trend->averageByHours('age');
$trend->averageByMinutes('age');
Count
$trend->countByYears();
$trend->countByMonths();
$trend->countByWeeks();
$trend->countByDays();
$trend->countByHours();
$trend->countByMinutes();

Other metrics

  • Bar extends Trend
  • Line extends Trend
  • Doughnut extends Pie
  • Polar extends Pie

Ranges

Every metric class contains a ranges method, that will determine the range of the results based on it's date column.

use SaKanjo\EasyMetrics\Metrics\Trend;
use SaKanjo\EasyMetrics\Metrics\Enums\Range;
use App\Models\User;
 
Value::make(User::class)
->range(30)
->ranges([
15, 30, 365,
Range::TODAY, // Or 'TODAY'
]);

Available custom ranges

  • Range::TODAY
  • Range::YESTERDAY
  • Range::MTD
  • Range::QTD
  • Range::YTD
  • Range::ALL

đŸ”Ĩ Practical examples

Filamentphp v3 widgets

<?php
 
namespace App\Filament\Widgets\Admin;
 
use App\Models\User;
use Filament\Widgets\ChartWidget;
use SaKanjo\EasyMetrics\Metrics\Trend;
 
class UsersCountChart extends ChartWidget
{
protected static ?string $heading = 'Users count trend';
 
protected function getData(): array
{
[$labels, $data] = Trend::make(User::class)
->range($this->filter)
->rangesFromOptions($this->getFilters())
->countByMonths();
 
return [
'datasets' => [
[
'label' => 'Users',
'data' => $data,
],
],
'labels' => $labels,
];
}
 
protected function getType(): string
{
return 'line';
}
 
protected function getFilters(): ?array
{
return [
15 => '15 Days',
30 => '30 Days',
60 => '60 Days',
365 => '365 Days',
];
}
}

💖 Support the development

Do you like this project? Support it by donating

Click the "💖 Sponsor" at the top of this repo.

ÂŠī¸ Credits

📄 License

MIT License Š 2023-PRESENT Salah Kanjo

sakanjo photo

Full Stack Developer â€ĸ Love OpenSource, Interested in @vuejs / @laravel

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Sakanjo Laravel Easy Metrics Related Articles

Generate Secure Temporary Share Links for Files in Laravel image

Generate Secure Temporary Share Links for Files in Laravel

Read article
Laravel Performance Testing With Volt-Test PHP image

Laravel Performance Testing With Volt-Test PHP

Read article
Top 6 MySQL Database Management Struggles for Laravel Developers (And Smart Fixes) image

Top 6 MySQL Database Management Struggles for Laravel Developers (And Smart Fixes)

Read article
Laravel performance monitoring in Honeybadger image

Laravel performance monitoring in Honeybadger

Read article
Laravel Horizon Prometheus Exporter image

Laravel Horizon Prometheus Exporter

Read article
Laravel Easy Metrics image

Laravel Easy Metrics

Read article
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec