hms/statistics

This is a statistics expansion package based on laravel

v1.0 2020-11-18 03:37 UTC

This package is auto-updated.

Last update: 2024-04-18 11:52:49 UTC


README

基于laravel的数据统计存储扩展包

环境要求

  • Laravel >= 5.5
  • Jenssegers/mongodb >= 3.3

安装

composer require hms/statistics

配置

创建配置文件:

php artisan vendor:publish --tag=statistics-config

基本用法

在你的model中定义:

use Hms\Statistics\Eloquents\Statistics;
use Jenssegers\Mongodb\Eloquent\Model;

class Example extends Model
{
    use Statistics;

    public function __construct() 
    {
        parent::__construct();
        
        
        $this->statisticsConditionFields = [
            'day'
        ];
        
        $this->incrementFields = [
            'number'
        ];
    }
}

你的统计表中必须包含至少一个条件字段和一个统计字段,并且你需要在statisticsConditionFields数组中定义你的条件字段,在incrementFields中定义你的统计字段。

存入统计业务表

use App\Example;

$example = new Example();
$example->statistics([
    'day'       => Carbon::now()->toDateString(),
    'number'    => 1
]);

如上所示,statistics方法会接收两个参数,第一个参数是需要存储的数据数组,该方法会默认将数据按model中定义的规则进行判断是新增或是累加操作,如果你期望数据按定义的条件进行强制覆盖操作则可传入第二个参数。 第二个参数是可选参数默认为null,当传入force时数据将以覆盖的形式存入collection中,当传入new时表示该数据以新增方式存入collection中。

使用redis将数据进行缓存

$example->cache([
    'day'       => Carbon::now()->toDateString(),
    'number'    => 3
]);

如果你需要在高并发的业务中进行数据统计,那么强烈推荐你使用此方法存储数据,cache方法也会将数据按model中定义的规则用hash表保存在你的redis中。

将redis中存储的数据同步到mongodb

在使用前你需要开启Laravel提供的任务调度 功能

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

并且在config/statistics.phpnamespaces数组中定义你需要进行同步的 model,如下所示:

'namespaces' => [
    App\Example::class
]

然后在App\Console\Kernelschedule 方法中加入statisticsArtisan 命令

protected function schedule(Schedule $schedule)
{
    $schedule->command('statistics:sync')->daily();
}

至此statistics为你提供了一套非常便利的数据统计

注:statistics提供了数据redis的存储与查询方法的封装,但是并未实现在查询mongodb时默认将缓存中的数据读取的功能,此时需要自行将缓存数据读出进行合并操作。

其他用法

setDistinct

如果你需要统计UV值那么你可以使用该方法:

$example->setDistinct([
    'client' => 'ios',
    'device_id' => 'balabala...' 
]);

or

use Hms\Statistics\Models\StatisticsLog;

// 设置去重条件
$example->setDistinct(function () {
    return Example::query()->where('client', 'ios')->whereIn('user_id', 1234)->first();
});

// 此时应该这样调用cache
$example->cache([
    'day'       => Carbon::now()->toDateString(),
    'number'    => 3
], function () {
    StatisticsLog::create([
        'client'    => 'ios',
        'user_id'   => 1234
    ]);
});

通过setDistinct方法定义你的去重条件,支持数组或闭包形式。如果传入闭包,调用 cache 方法则第二个参数需传入一个闭包函数来记录你的去重条件。

注意:去重条件不推荐用or语句,如果这样做就失去了去重的意义。