1. 建立警報條件 (Alert Rule)
進入 Grafana:
(1) 點左邊選單的 Alerting → Alert Rules
(2) 點右上角「Create alert rule」
● 設定範例
Name: AFP 高於安全範圍
Data source: PostgreSQL
查詢語法:
SELECT
record_date AS "time",
afp
FROM cancer_patients
WHERE patient_id = '$patient_id'
ORDER BY record_date DESC
LIMIT 1;
Condition:
● WHEN last() OF afp
● IS ABOVE 400
代表意思:
當最新的 AFP 數值大於 400,就觸發警報。
2. 設定通知通道 (Contact Point)
在 Grafana 左側選單選:
Alerting → Contact points → New contact point
(選項 1) mail 通知
(1) 點「Add contact point」
(2) 選擇類型:Email
(3) 輸入收件者信箱
(4) 點「Test」看看能不能收到測試信
(選項 2) Line Notify
● 前往 https://notify-bot.line.me/my/
● 登入後點「發行權杖」
● 給權杖名稱(如:AFP Alert)並選「1對1聊天」
● 複製 Token
回到 Grafana:
Contact point 類型選「Webhook」
URL 輸入:
https://notify-api.line.me/api/notify
Header 加入:
Authorization: Bearer <你的TOKEN>
Body:
{"message": " 病人 $patient_id AFP 值超過安全範圍!"}
(選項 3) Slack 通知
若你的團隊使用 Slack,可以用「Incoming Webhook」整合:
(1) 到 Slack App 設定頁,建立 Webhook URL
(2) 在 Grafana 的 Contact point 加入 Webhook
(3) Body 內容可設定為:
{"text": " 病人 $patient_id AFP 超過 400!"}
3. 設定 Notification Policy
接著到 Grafana:
Alerting → Notification policies → New policy
設定:
● If alert name = "AFP 高於安全範圍"
● Send to → 你剛剛設定的 Email / Line / Slack
● Optionally:Add delay = 1m(避免瞬間誤報)
4. 測試警報
回到你的資料庫,把某筆 AFP 值調高看看:
UPDATE cancer_patients
SET afp = 800
WHERE patient_id = '001'
AND record_date = '2025-09-15';
等一兩分鐘後,Grafana 應該會:
● 偵測異常
● 觸發警示
● 自動發送通知
5. 儀表板警示顯示
在 Dashboard 的折線圖 Panel 裡,你也可以開啟警示視覺化:
● 點「Edit panel」
● 切換到「Alert」分頁
● 勾選「Show alert state on graph」
這樣當警報觸發時,圖上會出現紅線區域或「Alert」標記,讓異常一目了然。
6. 成果展示
現在系統能做到:
● 自動分析 AFP / ALT
● 超過門檻即發出警示
● 支援 Email / Line / Slack 通知
● Grafana Dashboard 同步顯示異常狀態
這樣的平台不僅能視覺化,也能「主動關心病人狀況」
是從資料觀察 → 智慧提醒 的完整轉變!