Files
ewoooc/database/notification_models.py
ogt 1b4f3a7bbe
Some checks failed
CD Pipeline / deploy (push) Failing after 59s
feat: EwoooC 初始化 — 完整專案推版至 Gitea
- 建立 Gitea Actions CD pipeline (.gitea/workflows/cd.yaml)
- 部署模式: rsync Python 檔案至 188 → docker restart (volume mount)
- Dockerfile/requirements 變動時自動重建 Docker image
- 部署通知: Telegram (開始/成功/失敗)
- 健康檢查: https://mo.wooo.work/health (最多 5 次重試)
- 同步最新 CLAUDE.md / ADR-008 / memory (2026-04-19)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 01:21:13 +08:00

237 lines
6.8 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
通知模板資料模型
用於管理 n8n 和系統通知的訊息模板
"""
from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime, timezone, timedelta
Base = declarative_base()
TAIPEI_TZ = timezone(timedelta(hours=8))
class NotificationTemplate(Base):
"""通知模板"""
__tablename__ = 'notification_templates'
id = Column(Integer, primary_key=True)
code = Column(String(50), unique=True, nullable=False) # 模板代碼,如 'disk_alert'
name = Column(String(100), nullable=False) # 顯示名稱
category = Column(String(50), nullable=False) # 分類system, business, report
channel = Column(String(20), default='telegram') # 通知渠道telegram, line, both
# 模板內容
title = Column(String(200)) # 標題
body = Column(Text, nullable=False) # 訊息內容(支援變數)
emoji_prefix = Column(String(10)) # 前綴 emoji
# 狀態
is_active = Column(Boolean, default=True)
# 時間戳
created_at = Column(DateTime, default=lambda: datetime.now(TAIPEI_TZ).replace(tzinfo=None))
updated_at = Column(DateTime, onupdate=lambda: datetime.now(TAIPEI_TZ).replace(tzinfo=None))
def to_dict(self):
return {
'id': self.id,
'code': self.code,
'name': self.name,
'category': self.category,
'channel': self.channel,
'title': self.title,
'body': self.body,
'emoji_prefix': self.emoji_prefix,
'is_active': self.is_active,
'created_at': self.created_at.isoformat() if self.created_at else None,
'updated_at': self.updated_at.isoformat() if self.updated_at else None
}
# 預設模板
DEFAULT_TEMPLATES = [
{
'code': 'disk_warning',
'name': '磁碟空間警告',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '🟠',
'title': '磁碟空間警告',
'body': '''📊 使用率: {usage_percent}%
💾 剩餘: {free_gb} GB / {total_gb} GB
🔧 正在執行自動清理...'''
},
{
'code': 'disk_critical',
'name': '磁碟空間嚴重不足',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '🔴',
'title': '磁碟空間嚴重不足',
'body': '''📊 使用率: {usage_percent}%
💾 剩餘: {free_gb} GB / {total_gb} GB
⚠️ 立即執行緊急清理!'''
},
{
'code': 'cleanup_complete',
'name': '自動清理完成',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '',
'title': '自動清理完成',
'body': '''🧹 清理結果:
{results}
📊 清理後使用率: {new_usage_percent}%'''
},
{
'code': 'ssl_warning',
'name': 'SSL 證書即將到期',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '🟡',
'title': 'SSL 證書警告',
'body': '''{issues}
💡 執行: sudo certbot renew --nginx'''
},
{
'code': 'pod_unhealthy',
'name': 'K8s Pod 異常',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '🔴',
'title': 'K8s Pod 異常',
'body': '''📍 狀態: {status}
🗄️ 資料庫: {database}
🔧 正在嘗試重啟...'''
},
{
'code': 'pod_restart_result',
'name': 'Pod 重啟結果',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '',
'title': 'Pod 重啟完成',
'body': '''{deployment} 已重啟,等待恢復中...'''
},
{
'code': 'crawler_warning',
'name': '爬蟲執行警告',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '🟠',
'title': '爬蟲監控警告',
'body': '''{issues}
🔧 正在嘗試重啟 scheduler...'''
},
{
'code': 'harbor_unhealthy',
'name': 'Harbor Registry 異常',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '🟠',
'title': 'Harbor Registry 異常',
'body': '''📍 狀態: {status}
❌ 錯誤: {error}
💡 請檢查 Harbor 服務
執行: docker restart harbor-core harbor-nginx'''
},
{
'code': 'backup_warning',
'name': '備份監控警告',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '🟠',
'title': '備份監控警告',
'body': '''{error}
💡 請檢查備份排程'''
},
{
'code': 'cicd_success',
'name': 'CI/CD 成功',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '',
'title': 'CI/CD Pipeline SUCCESS',
'body': '''📦 專案: {project}
🌿 分支: {branch}
🆔 Pipeline: #{pipeline_id}
💬 Commit: {commit_message}
👤 作者: {author}
⏱️ 耗時: {duration}
🔗 查看詳情: {url}'''
},
{
'code': 'cicd_failed',
'name': 'CI/CD 失敗',
'category': 'system',
'channel': 'telegram',
'emoji_prefix': '',
'title': 'CI/CD Pipeline FAILED',
'body': '''📦 專案: {project}
🌿 分支: {branch}
🆔 Pipeline: #{pipeline_id}
💬 Commit: {commit_message}
👤 作者: {author}
🔗 查看詳情: {url}'''
},
{
'code': 'daily_report',
'name': '每日系統狀態報告',
'category': 'report',
'channel': 'telegram',
'emoji_prefix': '📊',
'title': '每日系統狀態報告',
'body': '''📅 日期: {date}
🖥️ 應用程式: {app_status}
💾 資料備份: {backup_status}
🕷️ 爬蟲排程: {crawler_status}
📦 最新備份: {last_backup}'''
},
{
'code': 'weekly_sales',
'name': '每週業績摘要',
'category': 'business',
'channel': 'telegram',
'emoji_prefix': '📈',
'title': '每週業績摘要',
'body': '''📅 週期: {week_start} ~ {week_end}
💰 總業績: ${total_sales}
📦 訂單數: {order_count}
📈 成長率: {growth_rate}%
詳細報表請登入系統查看'''
},
{
'code': 'monthly_reminder',
'name': '月初作業提醒',
'category': 'business',
'channel': 'telegram',
'emoji_prefix': '📅',
'title': '{month}月初作業提醒',
'body': '''✅ 待辦事項:
1⃣ 匯出上月 ({prev_month}) 月結報表
2⃣ 檢查 Google Drive 自動匯入設定
3⃣ 確認爬蟲排程正常運作
4⃣ 備份資料庫
5⃣ 檢查系統日誌
💡 登入系統: https://mo.wooo.work'''
}
]