sakanjo/laravel-easy-metrics
Composer 安装命令:
composer require sakanjo/laravel-easy-metrics
包简介
Generate metrics with ease and precision.
README 文档
README
🔥 Easy metrics
Easily create metrics for your application.
✨ Help support the maintenance of this package by sponsoring me.
Designed to work with Laravel, Filament, Easy enum, 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; [$labels, $data] = Doughnut::make(User::class) ->count('gender');
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 TrendLine extends TrendDoughnut extends PiePolar 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::TODAYRange::YESTERDAYRange::MTDRange::QTDRange::YTDRange::ALL
Growth rates
Growth rate, expressed as both a value and a percentage, measures the change in a quantity over time, showing the speed of its expansion or contraction in both absolute and relative terms.
Using Value metric
use SaKanjo\EasyMetrics\Metrics\Value; use SaKanjo\EasyMetrics\Enums\GrowthRateType; use App\Models\User; [$value, $growth] = Value::make(User::class) ->withGrowthRate() ->growthRateType(GrowthRateType::Value) // default is `Percentage` ->count();
Using Trend metric
use SaKanjo\EasyMetrics\Metrics\Trend; use App\Models\User; [$labels, $data, $growth] = Trend::make(User::class) ->withGrowthRate() ->countByYears();
Using Doughnut metric
use SaKanjo\EasyMetrics\Metrics\Doughnut; use App\Models\User; [$labels, $data, $growth] = Doughnut::make(User::class) ->withGrowthRate() ->count('gender');
Available growth rate types
GrowthRateType::ValueGrowthRateType::Percentage
🔥 Tips
Using getLabel
You can use the getLabel method to customize the retreived data labels, for example:
<?php namespace App\Enums; use SaKanjo\EasyEnum; enum ExampleEnum: int { use EasyEnum; // Includes getLabel method case Active = 0; case Disabled = 1; }
🔥 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 Months', 30 => '30 Months', 60 => '60 Months', ]; } }
💖 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/laravel-easy-metrics 适用场景与选型建议
sakanjo/laravel-easy-metrics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45.54k 次下载、GitHub Stars 达 318, 最近一次更新时间为 2023 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Metrics」 「charts」 「laravel」 「reports」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sakanjo/laravel-easy-metrics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sakanjo/laravel-easy-metrics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sakanjo/laravel-easy-metrics 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The "View Counter" bundle
A Versatile and Expandable jQuery Plotting Plugin
PHP library for c3js
Draw charts with a simple API on Laravel
Laravel Service Provider for morris.js
A abstraction for generating metrics in Laravel.
统计信息
- 总下载量: 45.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 318
- 点击次数: 17
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-06

