[添加]settings监听,ActivityLogger

This commit is contained in:
2024-08-29 09:06:10 +08:00
parent 6cfc882294
commit abb8f830c3
6 changed files with 221 additions and 87 deletions

View File

@@ -1,84 +0,0 @@
<?php
namespace App\Filament\Pages;
use App\Services\EtcdService;
use Filament\Pages\Page;
use Illuminate\Support\Facades\DB;
use Filament\Notifications\Notification;
class UpdateToEtcd extends Page
{
protected static ?string $navigationIcon = 'heroicon-o-document-text';
protected static string $view = 'filament.pages.update-to-etcd';
protected static ?string $navigationLabel = '导出至ETCD'; // 设置侧边栏的中文名称
protected static ?string $title = '导出至ETCD'; // 自定义页面标题
protected static ?int $navigationSort = 99999;
public function updateSettingsToEtcd()
{
try {
$etcd = new EtcdService();
// $etcd->deleteByPrefix("datasource");
$settings = DB::table('settings')
->where('group', 'datasource')
->get(); // 获取 group 为 datasource 的所有记录
foreach ($settings as $setting) {
$key = '/' . $setting->group . '/' . $setting->name;
$etcd->put($key, $setting->payload);
}
$metrics = DB::table('metrics')->get(); // 获取所有 metrics 数据
foreach ($metrics as $index => $metric) {
$baseKey = '/metrics/' . $index;
// 存储 name 到 etcd
$etcd->put($baseKey . '/name', $metric->name);
// 存储 identifier_type 到 etcd
$etcd->put($baseKey . '/identifier_type', $metric->identifier_type);
// 根据 identifier_type 生成 nodeid 并存储到 etcd
switch ($metric->identifier_type) {
case 1: // Numeric
$nodeId = 'ns=' . $metric->namespace_index . ';i=' . $metric->numeric_id;
break;
case 2: // String
$nodeId = 'ns=' . $metric->namespace_index . ';s=' . $metric->string_id;
break;
case 3: // GUID
$nodeId = 'ns=' . $metric->namespace_index . ';g=' . $metric->guid_id;
break;
default:
$nodeId = ''; // 若 identifier_type 不符合预期,则 nodeId 为空
break;
}
$etcd->put($baseKey . '/nodeid', $nodeId);
// 存储 help 到 etcd
$etcd->put($baseKey . '/help', $metric->help ?? '');
}
Notification::make()
->title('配置更新成功!')
->success() // 设置为成功类型
->send(); // 发送通知
} catch (\Exception $e) {
// 失败通知
Notification::make()
->title('配置更新失败!')
->danger() // 设置为错误类型
->body($e->getMessage()) // 显示异常信息
->send(); // 发送通知
}
}
}

View File

@@ -6,6 +6,8 @@ use App\Services\EtcdService;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Filament\Widgets\Widget; use Filament\Widgets\Widget;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Spatie\Activitylog\ActivityLogger;
use Spatie\Activitylog\ActivityLogStatus;
class UpdateConfigWidget extends Widget class UpdateConfigWidget extends Widget
{ {
@@ -63,6 +65,13 @@ class UpdateConfigWidget extends Widget
->success() // 设置为成功类型 ->success() // 设置为成功类型
->send(); // 发送通知 ->send(); // 发送通知
app(ActivityLogger::class)
->useLog('导出')
->setLogStatus(app(ActivityLogStatus::class))
->withProperties($metrics)
->event('settings updated')
->log('settings updated');
} catch (\Exception $e) { } catch (\Exception $e) {
// 失败通知 // 失败通知
Notification::make() Notification::make()
@@ -70,6 +79,12 @@ class UpdateConfigWidget extends Widget
->danger() // 设置为错误类型 ->danger() // 设置为错误类型
->body($e->getMessage()) // 显示异常信息 ->body($e->getMessage()) // 显示异常信息
->send(); // 发送通知 ->send(); // 发送通知
app(ActivityLogger::class)
->useLog('导出失败')
->setLogStatus(app(ActivityLogStatus::class))
->event('Failed')
->log($e->getMessage());
} }
} }
} }

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Listeners;
use Spatie\Activitylog\ActivityLogger;
use Spatie\Activitylog\ActivityLogStatus;
use Spatie\LaravelSettings\Events\SavingSettings;
use Spatie\LaravelSettings\Events\SettingsSaved;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class SavingSettingsListener
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*/
public function handle(SavingSettings $event): void
{
$properties = $event->properties;
app(ActivityLogger::class)
->useLog('配置')
->setLogStatus(app(ActivityLogStatus::class))
->withProperties($properties)
->event('settings updated')
->log('settings updated');
}
}

View File

@@ -40,7 +40,7 @@ class AdminPanelProvider extends PanelProvider
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets') ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([ ->widgets([
Widgets\AccountWidget::class, Widgets\AccountWidget::class,
Widgets\FilamentInfoWidget::class, // Widgets\FilamentInfoWidget::class,
UpdateConfigWidget::class, UpdateConfigWidget::class,
]) ])
->middleware([ ->middleware([
@@ -56,6 +56,8 @@ class AdminPanelProvider extends PanelProvider
]) ])
->authMiddleware([ ->authMiddleware([
Authenticate::class, Authenticate::class,
])->resources([
config('filament-logger.activity_resource')
]); ]);
} }
} }

View File

@@ -11,7 +11,8 @@
"filament/spatie-laravel-settings-plugin": "^3.2", "filament/spatie-laravel-settings-plugin": "^3.2",
"laravel/framework": "^11.9", "laravel/framework": "^11.9",
"laravel/octane": "^2.5", "laravel/octane": "^2.5",
"laravel/tinker": "^2.9" "laravel/tinker": "^2.9",
"z3d0x/filament-logger": "^0.7.2"
}, },
"require-dev": { "require-dev": {
"laravel/pint": "^1.13", "laravel/pint": "^1.13",

View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "6d9e01b0da7ef17659cce2c1a99d0763", "content-hash": "0384a2e68cafacbc7a470648824ea732",
"packages": [ "packages": [
{ {
"name": "anourvalar/eloquent-serialize", "name": "anourvalar/eloquent-serialize",
@@ -5265,6 +5265,97 @@
], ],
"time": "2024-05-17T09:06:10+00:00" "time": "2024-05-17T09:06:10+00:00"
}, },
{
"name": "spatie/laravel-activitylog",
"version": "4.8.0",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-activitylog.git",
"reference": "eb6f37dd40af950ce10cf5280f0acfa3e08c3bff"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-activitylog/zipball/eb6f37dd40af950ce10cf5280f0acfa3e08c3bff",
"reference": "eb6f37dd40af950ce10cf5280f0acfa3e08c3bff",
"shasum": ""
},
"require": {
"illuminate/config": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"illuminate/database": "^8.69 || ^9.27 || ^10.0 || ^11.0",
"illuminate/support": "^8.0 || ^9.0 || ^10.0 || ^11.0",
"php": "^8.1",
"spatie/laravel-package-tools": "^1.6.3"
},
"require-dev": {
"ext-json": "*",
"orchestra/testbench": "^6.23 || ^7.0 || ^8.0 || ^9.0",
"pestphp/pest": "^1.20 || ^2.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\Activitylog\\ActivitylogServiceProvider"
]
}
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Spatie\\Activitylog\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
},
{
"name": "Sebastian De Deyne",
"email": "sebastian@spatie.be",
"homepage": "https://spatie.be",
"role": "Developer"
},
{
"name": "Tom Witkowski",
"email": "dev.gummibeer@gmail.com",
"homepage": "https://gummibeer.de",
"role": "Developer"
}
],
"description": "A very simple activity logger to monitor the users of your website or application",
"homepage": "https://github.com/spatie/activitylog",
"keywords": [
"activity",
"laravel",
"log",
"spatie",
"user"
],
"support": {
"issues": "https://github.com/spatie/laravel-activitylog/issues",
"source": "https://github.com/spatie/laravel-activitylog/tree/4.8.0"
},
"funding": [
{
"url": "https://spatie.be/open-source/support-us",
"type": "custom"
},
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2024-03-08T22:28:17+00:00"
},
{ {
"name": "spatie/laravel-package-tools", "name": "spatie/laravel-package-tools",
"version": "1.16.4", "version": "1.16.4",
@@ -8193,6 +8284,80 @@
"source": "https://github.com/webmozarts/assert/tree/1.11.0" "source": "https://github.com/webmozarts/assert/tree/1.11.0"
}, },
"time": "2022-06-03T18:03:27+00:00" "time": "2022-06-03T18:03:27+00:00"
},
{
"name": "z3d0x/filament-logger",
"version": "v0.7.2",
"source": {
"type": "git",
"url": "https://github.com/Z3d0X/filament-logger.git",
"reference": "8a8a20b9921d1b37bb2d32bdf1c06b598685182b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Z3d0X/filament-logger/zipball/8a8a20b9921d1b37bb2d32bdf1c06b598685182b",
"reference": "8a8a20b9921d1b37bb2d32bdf1c06b598685182b",
"shasum": ""
},
"require": {
"filament/filament": "^3.0",
"illuminate/contracts": "^8.0 | ^9.0 | ^10.0 | ^11.0",
"php": "^8.0 | ^8.1",
"spatie/laravel-activitylog": "^4.5",
"spatie/laravel-package-tools": "^1.13.5"
},
"require-dev": {
"nunomaduro/collision": "^6.0 | ^8.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^7.0 | ^9.0",
"pestphp/pest": "^1.21 | ^2.34",
"pestphp/pest-plugin-laravel": "^1.1 | ^2.3",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5 | ^10.5",
"spatie/laravel-ray": "^1.26"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Z3d0X\\FilamentLogger\\FilamentLoggerServiceProvider"
],
"aliases": {
"FilamentLogger": "Z3d0X\\FilamentLogger\\Facades\\FilamentLogger"
}
}
},
"autoload": {
"psr-4": {
"Z3d0X\\FilamentLogger\\": "src",
"Z3d0X\\FilamentLogger\\Database\\Factories\\": "database/factories"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ziyaan Hassan",
"email": "ziyaan2010@gmail.com",
"role": "Developer"
}
],
"description": "Activity logger for filament",
"homepage": "https://github.com/z3d0x/filament-logger",
"keywords": [
"Z3d0X",
"filament-logger",
"laravel"
],
"support": {
"issues": "https://github.com/Z3d0X/filament-logger/issues",
"source": "https://github.com/Z3d0X/filament-logger/tree/v0.7.2"
},
"time": "2024-06-09T12:05:25+00:00"
} }
], ],
"packages-dev": [ "packages-dev": [