From 64da72579195864e2264a6e143c3a81157ad9884 Mon Sep 17 00:00:00 2001 From: jihaofeng <1343277221@qq.com> Date: Tue, 3 Sep 2024 10:11:33 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=8A=A0]Metrics=E5=9C=A8=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E4=B8=AD=E6=95=B0=E6=8D=AE=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/deployment.xml | 15 + .idea/php.xml | 291 ++++++++++ .idea/workspace.xml | 541 ++++++++++++++++++ confd/templates/opcua.tmpl | 10 +- docker/prometheus/prometheus.yml | 6 +- .../app/Filament/Resources/MetricResource.php | 13 +- .../app/Livewire/MetricWidgetChart.php | 61 ++ management-panel/composer.json | 1 + management-panel/composer.lock | 250 +++----- 9 files changed, 1014 insertions(+), 174 deletions(-) create mode 100644 .idea/deployment.xml create mode 100644 .idea/php.xml create mode 100644 .idea/workspace.xml create mode 100644 management-panel/app/Livewire/MetricWidgetChart.php diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..369313d --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..aff9fa3 --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,291 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..982d5e8 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,541 @@ + + + + + + + + + + + + + + + + + + + + + + $PROJECT_DIR$/composer.json + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 4 +} + + + + { + "keyToString": { + "RunOnceActivity.ShowReadmeOnStart": "true", + "git-widget-placeholder": "master", + "last_opened_file_path": "F:/Postgraduate/work_project/data-collection-terminal/management-panel", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1724720814509 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/confd/templates/opcua.tmpl b/confd/templates/opcua.tmpl index 948cd7a..c915978 100644 --- a/confd/templates/opcua.tmpl +++ b/confd/templates/opcua.tmpl @@ -8,19 +8,19 @@ datasource: key_authentication_file: {{ getv "/datasource/key_authentication_file" }} certificate_authentication_file: {{ getv "/datasource/certificate_authentication_file" }} trusted_list: {{ getv "/datasource/trusted_list" }} - measurement_point_address: {{ getv "/datasource/measurement_point_address" }} - interface_address: {{ getv "/datasource/interface_address" }} username: {{ getv "/datasource/username" }} password: {{ getv "/datasource/password" }} certificate_identity_file: {{ getv "/datasource/certificate_identity_file" }} key_identity_file: {{ getv "/datasource/key_identity_file" }} - state: {{ getv "/datasource/state" }} + read_mode: {{ getv "/datasource/read_mode" }} + cache_update: {{ getv "/datasource/cache_update" }} + cache_timeout: {{ getv "/datasource/cache_timeout" }} metrics: {{ range $index, $element := ls "/metrics" }} - name: "{{ getv (printf "/metrics/%d/name" $index) }}" - identifier_type: {{ getv (printf "/metrics/%d/identifier_type" $index) }}" - nodeid: {{ getv (printf "/metrics/%d/nodeid" $index) }} + identifier_type: {{ getv (printf "/metrics/%d/identifier_type" $index) }} + nodeid: "{{ getv (printf "/metrics/%d/nodeid" $index) }}" help: "{{ getv (printf "/metrics/%d/help" $index) }}" {{ end }} diff --git a/docker/prometheus/prometheus.yml b/docker/prometheus/prometheus.yml index 1b933cb..2dd6465 100644 --- a/docker/prometheus/prometheus.yml +++ b/docker/prometheus/prometheus.yml @@ -4,4 +4,8 @@ global: scrape_configs: - job_name: 'node_exporter' static_configs: - - targets: ['node-exporter:9100'] \ No newline at end of file + - targets: [ 'node-exporter:9100' ] + + - job_name: 'opcua_exporter' + static_configs: + - targets: [ 'localhost:8191' ] \ No newline at end of file diff --git a/management-panel/app/Filament/Resources/MetricResource.php b/management-panel/app/Filament/Resources/MetricResource.php index 709ec4a..1701b7e 100644 --- a/management-panel/app/Filament/Resources/MetricResource.php +++ b/management-panel/app/Filament/Resources/MetricResource.php @@ -3,7 +3,7 @@ namespace App\Filament\Resources; use App\Filament\Resources\MetricResource\Pages; -use App\Filament\Resources\MetricResource\RelationManagers; +use App\Livewire\MetricWidgetChart; use App\Models\Metric; use Filament\Forms; use Filament\Forms\Form; @@ -16,6 +16,7 @@ use Filament\Forms\Components\TextInput; use Filament\Forms\Components\Section; use Filament\Forms\Components\Select; use Filament\Tables\Columns\TextColumn; +use LaraZeus\InlineChart\Tables\Columns\InlineChart; class MetricResource extends Resource { @@ -111,7 +112,15 @@ class MetricResource extends Resource } }), TextColumn::make('help') - ->label('帮助') + ->label('帮助'), + + InlineChart::make('data') + ->label('数据') + ->chart(MetricWidgetChart::class) + ->maxWidth(350) + ->maxHeight(90) + ->description('') + ->toggleable(), ]) ->filters([ // diff --git a/management-panel/app/Livewire/MetricWidgetChart.php b/management-panel/app/Livewire/MetricWidgetChart.php new file mode 100644 index 0000000..fe35a70 --- /dev/null +++ b/management-panel/app/Livewire/MetricWidgetChart.php @@ -0,0 +1,61 @@ +record->name . '{data="real"}'; + $query2 = $this->record->name . '{data="simulate"}'; + + $start = now()->subDays(1)->timestamp; + $end = now()->timestamp; + $step = 60; + $realdata = $prometheus->queryRange($query1, $start, $end, $step); + $simulatedata = $prometheus->queryRange($query2, $start, $end, $step); + + // 将数据格式化为 InlineChartWidget 所需的格式 + $formattedRealData = $this->formatData($realdata, 'Real Data'); + $formattedSimulateData = $this->formatData($simulatedata, 'Simulate Data'); + + return [ + 'labels' => $formattedRealData['labels'], + 'datasets' => [ + $formattedRealData['datasets'], + $formattedSimulateData['datasets'], + ], + ]; + } + + /** + * 将 Prometheus 查询结果格式化为 InlineChartWidget 所需的数据格式 + * + * @param array $data Prometheus 查询结果 + * @param string $label 数据集标签 + * @return array 格式化后的数据 + */ + protected function formatData(array $data, string $label): array + { + $labels = []; + $dataset = [ + 'label' => $label, + 'data' => [], + ]; + + foreach ($data['data']['result'][0]['values'] as $point) { + $labels[] = date('H:i', $point[0]); + $dataset['data'][] = $point[1]; + } + + return [ + 'labels' => $labels, + 'datasets' => $dataset, + ]; + } +} diff --git a/management-panel/composer.json b/management-panel/composer.json index 5db7205..2d56631 100644 --- a/management-panel/composer.json +++ b/management-panel/composer.json @@ -9,6 +9,7 @@ "fakerphp/faker": "^1.23", "filament/filament": "^3.2", "filament/spatie-laravel-settings-plugin": "^3.2", + "lara-zeus/inline-chart": "^1.1", "laravel/framework": "^11.9", "laravel/octane": "^2.5", "laravel/tinker": "^2.9", diff --git a/management-panel/composer.lock b/management-panel/composer.lock index 1ff2313..311fe26 100644 --- a/management-panel/composer.lock +++ b/management-panel/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0384a2e68cafacbc7a470648824ea732", + "content-hash": "44cafcd7ddb03aa85528abd66ff2ccd7", "packages": [ { "name": "anourvalar/eloquent-serialize", @@ -2215,6 +2215,89 @@ ], "time": "2024-02-16T16:06:16+00:00" }, + { + "name": "lara-zeus/inline-chart", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/lara-zeus/inline-chart.git", + "reference": "59afabb7ea2c8326dda9384a041c98c4a3a375f2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/lara-zeus/inline-chart/zipball/59afabb7ea2c8326dda9384a041c98c4a3a375f2", + "reference": "59afabb7ea2c8326dda9384a041c98c4a3a375f2", + "shasum": "" + }, + "require": { + "filament/filament": "^3.0", + "php": "^8.1", + "spatie/laravel-package-tools": "^1.16" + }, + "require-dev": { + "laravel/pint": "^1.0", + "nunomaduro/collision": "^7.0", + "nunomaduro/larastan": "^2.0.1", + "nunomaduro/phpinsights": "^2.8", + "orchestra/testbench": "^8.0", + "phpstan/extension-installer": "^1.1" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "LaraZeus\\InlineChart\\InlineChartServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "LaraZeus\\InlineChart\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "php coder", + "email": "info@larazeus.com", + "role": "Owner" + } + ], + "description": "Zeus Inline Chart easily add a chart in filamentPHP table column", + "homepage": "https://larazeus.com/inline-chart", + "keywords": [ + "chart", + "coulmn", + "design", + "filamentphp", + "generator", + "inline", + "input", + "lara-zeus", + "laravel", + "mini", + "table", + "ui" + ], + "support": { + "issues": "https://github.com/lara-zeus/inline-chart/issues", + "source": "https://github.com/lara-zeus/inline-chart" + }, + "funding": [ + { + "url": "https://www.buymeacoffee.com/larazeus", + "type": "custom" + }, + { + "url": "https://github.com/atmonshi", + "type": "github" + } + ], + "time": "2024-02-07T10:24:01+00:00" + }, { "name": "laravel/framework", "version": "v11.21.0", @@ -5265,97 +5348,6 @@ ], "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", "version": "1.16.4", @@ -8284,80 +8276,6 @@ "source": "https://github.com/webmozarts/assert/tree/1.11.0" }, "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": [