insowe/datalogger

将数据日志保存到云存储。

v0.2.0-beta 2020-09-11 04:56 UTC

This package is auto-updated.

Last update: 2024-09-11 13:06:27 UTC


README

此软件包帮助在每次更新后记录数据,并将其上传到云存储而不是数据库,以减少数据库的负载。

安装

通过 Composer

$ composer require insowe/datalogger

执行 php artisan vendor:publish,选择 Provider: Insowe\DataLogger\DataLoggerServiceProvider 以发布数据库迁移到 ~/database/migrations/2020_01_22_023910_create_data_logs_table.php

您应将所有需要设置为枚举列的数据类型放入设置中。

Schema::create('data_logs', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->enum('data_type', [

    // Put data types of the app here!

    ])->comment('資料類型');

例如

Schema::create('data_logs', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->enum('data_type', [
        Model::getDataLogType(),
        Product::getDataLogType(),
        CleaningService::getDataLogType(),
    ])->comment('資料類型');

此软件包使用 队列,请确保队列环境已准备好,或者只需将 QUEUE_CONNECTION=sync

使用方法

如果需要记录 Eloquent Model,让它实现接口 Insowe\DataLogger\Models\IData

class Model extends BaseModel implements IData
{
    public function getDataLogId()
    {
        return $this->id;
    }
    
    public static function getDataLogType()
    {
        return 'model';
    }
}

在数据更新后,在控制器中创建一个 createLog 方法。

public function createOrUpdate(Request $request)
{
    if (intval($request->id) === 0) {
        $item = $this->create($request);
    }
    else {
        $item = $this->update($request);
    }
    $this->createLog($item->id, $request->user()->id);
}

createLog 方法中,获取最新数据并触发事件 Updated,监听器将为数据库添加一行日志并创建一个队列以上传日志文件到云存储。

public function createLog($modelId, $userId)
{
    $item = Model::with('brand')
            ->with('type')
            ->with('age')
            ->with('minAge')
            ->with('usages')
            ->with('detail')
            ->with('accessories')
            ->where('id', $modelId)
            ->first();
    
    $item->setHidden([
        'product_quantity',
        'product_in_stock',
        'updated_at',
        'deleted_at',
    ]);
    
    event(new Updated($item, $userId));
}

注意:应隐藏如 机器更新列(updated_at、deleted_at)和其他 统计列

变更日志

请参阅变更日志了解最近的变化。

贡献

请参阅contributing.md获取详细信息及待办事项列表。

安全

如果您发现任何安全相关的问题,请通过作者电子邮件而不是使用问题跟踪器来报告。

许可

许可。请参阅许可文件以获取更多信息。