Add Grafana monitoring dashboard for THQ tracking system#26
Add Grafana monitoring dashboard for THQ tracking system#26TinyKitten wants to merge 6 commits intomainfrom
Conversation
Introduces Grafana as a new Docker Compose service to visualize data already stored in PostgreSQL. Includes provisioned datasource config and a pre-built dashboard with panels for location logs, log events, accuracy, speed, battery level, and device activity. https://claude.ai/code/session_01RADeEtKpYSQno63R2Cd5Ce
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughdocker-compose に Grafana サービスと永続ボリューム、Grafana のプロビジョニング(ダッシュボード・データソース)および THQ 概要ダッシュボード定義を追加し、Grafana を db に依存させてポート3000で公開。さらにストレージ準備に2つのインデックスを追加し、 Changes
Sequence Diagram(s)sequenceDiagram
participant Browser
participant Grafana
participant FS as "Filesystem (provisioning & dashboards)"
participant Postgres as "PostgreSQL (db)"
Browser->>Grafana: HTTP GET / (dashboard UI)
Grafana->>FS: Load provisioning (datasources, dashboards)
Grafana->>Postgres: Connect via datasource `thq-postgres` (SQL queries)
Postgres-->>Grafana: Query results
Grafana-->>Browser: Render dashboard (panels)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
grafana/dashboards/thq-overview.json (1)
13-13: データソース UID が空に設定されています。すべてのパネルで
"uid": ""が設定されています。これはpostgres.ymlでisDefault: trueが設定されているため動作しますが、将来複数のデータソースが追加された場合に明確性が低下する可能性があります。現時点ではこの設定で問題ありませんが、明示的な UID(例:
"uid": "thq-postgres")を使用し、postgres.ymlでも同じ UID を設定することで、より堅牢な構成になります。🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@grafana/dashboards/thq-overview.json` at line 13, The panels currently set the datasource uid to an empty string ("datasource": { "type": "postgres", "uid": "" }), which relies on postgres.yml's isDefault and is fragile; update each panel's datasource UID to a specific identifier (e.g., "thq-postgres") and ensure the same UID is defined in your postgres.yml datasource declaration so Grafana resolves the correct datasource even if multiple postgres datasources are added later.docker-compose.yml (1)
41-42: デフォルトの管理者パスワードが弱いです。
GF_ADMIN_PASSWORDのデフォルト値がadminに設定されています。開発環境では許容範囲ですが、本番環境やインターネットに公開された環境では、この弱いデフォルトパスワードはセキュリティリスクとなります。
.env.exampleファイルにGF_ADMIN_PASSWORDの設定例を追加し、ドキュメントで強力なパスワードの設定を促すことを検討してください。🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@docker-compose.yml` around lines 41 - 42, 現在の docker-compose.yml は GF_ADMIN_PASSWORD に弱いデフォルト値 "admin" を与えておりセキュリティリスクがあるため、GF_SECURITY_ADMIN_PASSWORD: ${GF_ADMIN_PASSWORD:-admin} を変更してデフォルトを削除(例: GF_SECURITY_ADMIN_PASSWORD: ${GF_ADMIN_PASSWORD})し、必須の環境変数にするか、安全なランダム値を生成する仕組みを導入してください。合わせて .env.example に GF_ADMIN_PASSWORD のエントリを追加し(例として強力なパスワードのフォーマットやパスワード生成推奨を記載)、ドキュメントに本番環境では必ず強力なパスワードを設定する旨を追記してください。grafana/provisioning/datasources/postgres.yml (1)
8-13: ハードコードされた認証情報をセキュリティの観点から検討してください。パスワード
thqがファイルに直接記述されています。docker-compose.yml のGF_ADMIN_PASSWORDのように環境変数を使用することを検討してください。Grafana は$__env{VAR_NAME}または${VAR_NAME}構文で環境変数の参照をサポートしています。現在の開発環境設定では許容範囲ですが、本番環境では認証情報を外部化することを推奨します。
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@grafana/provisioning/datasources/postgres.yml` around lines 8 - 13, The postgres datasource currently hardcodes credentials (user: thq and secureJsonData.password: thq); replace these with environment-variable references so secrets aren't stored in the YAML. Update the postgres.yml entries for user and secureJsonData.password to use Grafana's env var syntax (e.g. ${VAR_NAME} or $__env{VAR_NAME}) and document the new env names (e.g. GRAFANA_PG_USER, GRAFANA_PG_PASSWORD) so docker-compose or the deployment injects them instead of the literal "thq".
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@docker-compose.yml`:
- Around line 41-42: 現在の docker-compose.yml は GF_ADMIN_PASSWORD に弱いデフォルト値
"admin" を与えておりセキュリティリスクがあるため、GF_SECURITY_ADMIN_PASSWORD:
${GF_ADMIN_PASSWORD:-admin} を変更してデフォルトを削除(例: GF_SECURITY_ADMIN_PASSWORD:
${GF_ADMIN_PASSWORD})し、必須の環境変数にするか、安全なランダム値を生成する仕組みを導入してください。合わせて .env.example に
GF_ADMIN_PASSWORD
のエントリを追加し(例として強力なパスワードのフォーマットやパスワード生成推奨を記載)、ドキュメントに本番環境では必ず強力なパスワードを設定する旨を追記してください。
In `@grafana/dashboards/thq-overview.json`:
- Line 13: The panels currently set the datasource uid to an empty string
("datasource": { "type": "postgres", "uid": "" }), which relies on
postgres.yml's isDefault and is fragile; update each panel's datasource UID to a
specific identifier (e.g., "thq-postgres") and ensure the same UID is defined in
your postgres.yml datasource declaration so Grafana resolves the correct
datasource even if multiple postgres datasources are added later.
In `@grafana/provisioning/datasources/postgres.yml`:
- Around line 8-13: The postgres datasource currently hardcodes credentials
(user: thq and secureJsonData.password: thq); replace these with
environment-variable references so secrets aren't stored in the YAML. Update the
postgres.yml entries for user and secureJsonData.password to use Grafana's env
var syntax (e.g. ${VAR_NAME} or $__env{VAR_NAME}) and document the new env names
(e.g. GRAFANA_PG_USER, GRAFANA_PG_PASSWORD) so docker-compose or the deployment
injects them instead of the literal "thq".
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9ae5c1fa-dfe1-45d9-a184-283e2a005a1e
📒 Files selected for processing (4)
docker-compose.ymlgrafana/dashboards/thq-overview.jsongrafana/provisioning/dashboards/default.ymlgrafana/provisioning/datasources/postgres.yml
…e env vars for PG creds
- GF_ADMIN_PASSWORD is now required (no weak default)
- Datasource provisioning uses $__env{} for PG user/password
- All dashboard panels reference explicit UID "thq-postgres"
- .env.example updated with GF_ADMIN_PASSWORD entry
https://claude.ai/code/session_01RADeEtKpYSQno63R2Cd5Ce
Grafana's "m" unit maps to minutes; "lengthmeter" is the correct unit ID for meters. https://claude.ai/code/session_01RADeEtKpYSQno63R2Cd5Ce
…abels Without the transformation, piechart only shows "count" as a single series instead of breaking it out by log_level. https://claude.ai/code/session_01RADeEtKpYSQno63R2Cd5Ce
"lengthmeter" is not a recognized Grafana unit and was displayed literally. Use "suffix: m" to show e.g. "33.2 m". https://claude.ai/code/session_01RADeEtKpYSQno63R2Cd5Ce
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
grafana/dashboards/thq-overview.json (1)
129-133: 「Active Devices (24h)」パネルが時間ピッカーと非連動です。Line 131 の固定 24 時間窓により、ダッシュボードの時間範囲を変更しても他のパネルと比較条件がずれます。検証結果、このパネルのみが固定窓を使用しており、残り 9 つのパネルはすべて
$__timeFrom()と$__timeTo()変数を使用しています。一貫性のため、同じ時間変数に揃えることを推奨します。修正案
- "rawSql": "SELECT COUNT(DISTINCT device) AS \"Devices\" FROM location_logs WHERE recorded_at >= NOW() - INTERVAL '24 hours'", + "rawSql": "SELECT COUNT(DISTINCT device) AS \"Devices\" FROM location_logs WHERE recorded_at >= $__timeFrom() AND recorded_at <= $__timeTo()",🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@grafana/dashboards/thq-overview.json` around lines 129 - 133, The "Active Devices (24h)" panel is hard-coded to a 24-hour window; update its rawSql (the panel with refId "A" / the rawSql property) to use Grafana time range variables instead of NOW() - INTERVAL '24 hours' so it follows the dashboard time picker; replace the WHERE clause with a time-range filter using $__timeFrom() and $__timeTo() (e.g. recorded_at >= $__timeFrom() AND recorded_at <= $__timeTo() or BETWEEN) while keeping the same datasource and result formatting.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@grafana/dashboards/thq-overview.json`:
- Around line 26-299: Add time-series indexes for recorded_at to avoid full
table scans: create an index on location_logs(recorded_at) (or composite
location_logs(recorded_at, device)) and on log_events(recorded_at) (or composite
log_events(recorded_at, log_level)); update the schema/migration definitions
where existing indexes are created (currently idx_location_logs_device,
idx_location_logs_segment, idx_log_events_device) to add
idx_location_logs_recorded_at and idx_log_events_recorded_at (or their composite
variants) so all queries using WHERE recorded_at >= $__timeFrom() AND
recorded_at <= $__timeTo() can use the index.
---
Nitpick comments:
In `@grafana/dashboards/thq-overview.json`:
- Around line 129-133: The "Active Devices (24h)" panel is hard-coded to a
24-hour window; update its rawSql (the panel with refId "A" / the rawSql
property) to use Grafana time range variables instead of NOW() - INTERVAL '24
hours' so it follows the dashboard time picker; replace the WHERE clause with a
time-range filter using $__timeFrom() and $__timeTo() (e.g. recorded_at >=
$__timeFrom() AND recorded_at <= $__timeTo() or BETWEEN) while keeping the same
datasource and result formatting.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d3543c8f-d61a-4aca-acc2-5063fe696d87
📒 Files selected for processing (4)
.env.exampledocker-compose.ymlgrafana/dashboards/thq-overview.jsongrafana/provisioning/datasources/postgres.yml
🚧 Files skipped from review as they are similar to previous changes (2)
- grafana/provisioning/datasources/postgres.yml
- docker-compose.yml
All dashboard panels filter on recorded_at with time range macros. Without an index on recorded_at, every query does a full table scan. - idx_location_logs_recorded_at (recorded_at, device) - idx_log_events_recorded_at (recorded_at, log_level) https://claude.ai/code/session_01RADeEtKpYSQno63R2Cd5Ce
Summary
This PR adds comprehensive Grafana monitoring and visualization capabilities to the THQ tracking system. It includes a new Grafana service in the Docker Compose setup, a pre-configured PostgreSQL datasource, and a detailed overview dashboard displaying location logs, log events, device metrics, and system statistics.
Key Changes
postgres.yml) that automatically connects Grafana to the THQ database with proper SSL and version settingsdefault.yml) to enable automatic dashboard loading from the filesystemthq-overview.jsondashboard with 12 panels including:Implementation Details
https://claude.ai/code/session_01RADeEtKpYSQno63R2Cd5Ce
Summary by CodeRabbit
新機能
修正/改善