[添加]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\Widgets\Widget;
use Illuminate\Support\Facades\DB;
use Spatie\Activitylog\ActivityLogger;
use Spatie\Activitylog\ActivityLogStatus;
class UpdateConfigWidget extends Widget
{
@@ -63,6 +65,13 @@ class UpdateConfigWidget extends Widget
->success() // 设置为成功类型
->send(); // 发送通知
app(ActivityLogger::class)
->useLog('导出')
->setLogStatus(app(ActivityLogStatus::class))
->withProperties($metrics)
->event('settings updated')
->log('settings updated');
} catch (\Exception $e) {
// 失败通知
Notification::make()
@@ -70,6 +79,12 @@ class UpdateConfigWidget extends Widget
->danger() // 设置为错误类型
->body($e->getMessage()) // 显示异常信息
->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')
->widgets([
Widgets\AccountWidget::class,
Widgets\FilamentInfoWidget::class,
// Widgets\FilamentInfoWidget::class,
UpdateConfigWidget::class,
])
->middleware([
@@ -56,6 +56,8 @@ class AdminPanelProvider extends PanelProvider
])
->authMiddleware([
Authenticate::class,
])->resources([
config('filament-logger.activity_resource')
]);
}
}