eliseekn/laravel-metrics 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eliseekn/laravel-metrics

Composer 安装命令:

composer require eliseekn/laravel-metrics

包简介

Generate easily metrics and trends data of your models for your dashboards.

README 文档

README

Latest Version on Packagist Total Downloads

Generate easily metrics and trends data of your models for your dashboards.

Requirements

PHP ^8.2.x
Laravel ^11.x, ^12.x

Note : For Laravel 13.x support check 4.x branch.

Installation

composer require eliseekn/laravel-metrics

Features

  • MySQL, PostgreSQL and SQLite support
  • Verbose query builder
  • Custom columns and table definition
  • Days and months translation with Carbon

Usage

With Eloquent Query

Import the Eliseekn\LaravelMetrics\LaravelMetrics class in your controller and use it as follows :

  • Basic usage
// generate trends of products amount's sum for the current year
LaravelMetrics::query(Product::query())
    ->count()
    ->byMonth()
    ->trends();

// generate trends of orders amount's sum for the last 6 months of the current year including current month
LaravelMetrics::query(Order::query())
    ->sum('amount')
    ->byMonth(6)
    ->trends();

// generate total orders amount's sum
LaravelMetrics::query(Order::query())
    ->sum('amount')
    ->byYear()
    ->metrics();

// generate total product count for the current day
LaravelMetrics::query(Product::query())
    ->count()
    ->byDay(1)
    ->metrics();
  • Using custom query
LaravelMetrics::query(
    Post::query()->where('user_id', auth()->id())
)
    ->count()
    ->byDay()
    ->trends();
  • Using custom date column
LaravelMetrics::query(Post::query())
    ->count()
    ->byDay()
    ->dateColumn('published_at')
    ->trends();
  • Using date range
LaravelMetrics::query(Post::query()))
    ->count()
    ->between('2020-05-01', '2022-08-21')
    ->trends();
  • Using custom label column
LaravelMetrics::query(Order::query())
    ->count()
    ->byMonth(12)
    ->labelColumn('status')
    ->trends();
  • Using custom table
LaravelMetrics::query(
    Order::query()->join('users', 'orders.id', 'users.order_id')
)
    ->count()
    ->table('users')
    ->labelColumn('name')
    ->trends();

With Query Builder

LaravelMetrics::query(
    DB::table('orders')
)
    ->sum('amount')
    ->byMonth()
    ->trends();

With traits

Add HasMetrics trait to your models and use it as follows :

Order::metrics()
    ->sum('amount')
    ->byMonth()
    ->trends();

Types of periods

LaravelMetrics::query(...)
    ->byDay(int $count = 0) //or
    ->byWeek(int $count = 0) //or
    ->byMonth(int $count = 0) //or
    ->byYear(int $count = 0) //or
    ->between(string $startDate, string $endDate, string $dateIsoFormat) //or
    ->from(string $date, string $dateIsoFormat)

Note : Periods are defined for the current day, week, month or year by default. However, you can define a specific value using dedicated methods. For example:

// generate trends of orders count for the current year
LaravelMetrics::query(Order::query())
    ->count()
    ->byMonth(12)
    ->forYear(now()->year)
    ->labelColumn('status')
    ->trends();

// generate total orders amount's sum for the third month only
LaravelMetrics::query(Product::query())
    ->sum('amount')
    ->byMonth(1)
    ->forMonth(3)
    ->metrics();
LaravelMetrics::query(...)
    ->forDay(int $day)
    ->forWeek(int $week)
    ->forMonth(int $month)
    ->forYear(int $year)

Types of aggregates

LaravelMetrics::query(...)
    ->count(string $column = 'id') //or
    ->average(string $column) //or
    ->sum(string $column) //or
    ->max(string $column) //or
    ->min(string $column)

Types of data

LaravelMetrics::query(...)
    ->trends(bool $inPercent = false) //or
    ->metrics() //or
    ->metricsWithVariations(int $previousCount, string $previousPeriod, bool $inPercent = false)

Note 1 : The trends method can generate data in percentage format when the $inPercent parameter is set to true.

Note 2 : The metricsWithVariations method generates metrics with variations from the $previousPeriod period (day, week, month, or year). The $previousCount parameter specifies the count for the past period. Set $inPercent parameter to true to get variations result in percent.

Combining periods and aggregates

Combining different time periods and data aggregates can enhance your overall experience. For example :

LaravelMetrics::query(...)
    ->sumByYear()
    ->trends();

LaravelMetrics::query(...)
    ->countByMonth(count: 12)
    ->forYear(now()->year)
    ->labelColumn('status')
    ->trends();

LaravelMetrics::query(...)
    ->countBetween([Carbon::now()->subDays(10)->format('Y-m-d'), Carbon::now()->format('Y-m-d')])
    ->trends();

LaravelMetrics::query(...)
    ->averageFrom(Carbon::now()->subDays(10)->format('Y-m-d'))
    ->trends();
...

Possible combinations :

LaravelMetrics::query(...)
    ->countByMonth(...) //or
    ->countByYear(...) //or
    ->countByDay(...) //or
    ->countByWeek(...) //or
    ->sumByMonth(...) //or
    ->sumByYear(...) //or
    ->sumByDay(...) //or
    ->sumByWeek(...) //or
    ->averageByMonth(...) //or
    ->averageByYear(...) //or
    ->averageByDay(...) //or
    ->averageByWeek(...) //or
    ->maxByMonth(...) //or
    ->maxByYear(...) //or
    ->maxByDay(...) //or
    ->maxByWeek(...) //or
    ->minByMonth(...) //or
    ->minByYear(...) //or
    ->minByDay(...) //or
    ->minByWeek(...) //or
    ->countBetween(...) //or
    ->sumBetween(...) //or
    ->averageBetween(...) //or
    ->maxBetween(...) //or
    ->minBetween(...) //or
    ->countFrom(...) //or
    ->sumFrom(...) //or
    ->averageFrom(...) //or
    ->maxFrom(...) //or
    ->minFrom(...)

Fill missing data with default value

You can fill missing data with default value with the global method fillMissingData, especially for trends. For example :

LaravelMetrics::query(...)
    ->countBetween([Carbon::now()->subDays(10)->format('Y-m-d'), Carbon::now()->format('Y-m-d')])
    ->fillMissingData()
    ->trends();

LaravelMetrics::query(...)
    ->sumByYear(count: 5)
    ->fillMissingData()
    ->trends();
...

Note : The fillMissingData method automatically discovers all labels, ensuring that data is filled for all available labels without the need for explicit label specification.

Group period (only when using between method)

You can group period by days, months, weeks or years when using the between method (default is day). For example :

LaravelMetrics::query(...)
    ->countBetween([Carbon::now()->subDays(10)->format('Y-m-d'), Carbon::now()->format('Y-m-d')])
    ->groupByMonth()
    ->fillMissingData()
    ->trends();
LaravelMetrics::query(...)
    ->groupByYear() //or
    ->groupByMonth() //or
    ->groupByWeek() //or
    ->groupByDay()

Group data (only for trends)

You can group data of a column with multiple values to use it in a dataset for your charts. For example :

Order::metrics()
    ->countByMonth(column: 'status')
    ->groupData(['pending', 'delivered', 'cancelled'], Aggregate::SUM->value)
    ->fillMissingData()
    ->trends();

Note : Follow same order in the example to avoid false data.

Translations

Days and months names are automatically translated using config(app.locale) except 'week' period.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email eliseekn@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Demo

You can find a demo project here.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

eliseekn/laravel-metrics 适用场景与选型建议

eliseekn/laravel-metrics 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.93k 次下载、GitHub Stars 达 132, 最近一次更新时间为 2021 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「charts」 「laravel」 「dashboard」 「laravel-package」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 eliseekn/laravel-metrics 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 eliseekn/laravel-metrics 我们能提供哪些服务?
定制开发 / 二次开发

基于 eliseekn/laravel-metrics 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 132
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 132
  • Watchers: 2
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-25