feat: initial TaskPulse release

AI Agent 定时任务跟踪、监控、状态看板系统。

- FastAPI + Vue3 单体应用
- AI Agent 一键接入(批量注册 API)
- 暗色主题 Dashboard
- 后台调度器 + 超时告警
- 飞书/邮件/Webhook 多渠道通知
- Base URL 可配置
- Gunicorn 生产部署
This commit is contained in:
2026-06-14 22:39:23 +08:00
commit 521cf0269c
54 changed files with 6328 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
"""Model registry — all models are imported here so Alembic can discover them."""
from app.models.agent import Agent # noqa: F401
from app.models.task import ScheduledTask # noqa: F401
from app.models.execution import TaskExecution # noqa: F401
from app.models.notification import Alert, AlertRule, NotificationChannel # noqa: F401
from app.models.system_config import SystemConfig # noqa: F401
__all__ = ["Agent", "ScheduledTask", "TaskExecution", "NotificationChannel", "AlertRule", "Alert", "SystemConfig"]
+31
View File
@@ -0,0 +1,31 @@
"""SQLAlchemy models — Agent."""
import uuid
from datetime import datetime
from sqlalchemy import Boolean, DateTime, Float, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
def _gen_api_key() -> str:
return f"tp_{uuid.uuid4().hex}"
class Agent(Base):
__tablename__ = "agents"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(128), unique=True, nullable=False, comment="Agent 名称")
description: Mapped[str] = mapped_column(Text, default="", comment="描述")
api_key: Mapped[str] = mapped_column(String(128), unique=True, default=_gen_api_key, comment="API Key")
status: Mapped[str] = mapped_column(String(32), default="active", comment="active / inactive")
last_heartbeat_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="最后心跳时间")
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
tasks = relationship("ScheduledTask", back_populates="agent")
def __repr__(self) -> str:
return f"<Agent {self.name}>"
+30
View File
@@ -0,0 +1,30 @@
"""SQLAlchemy models — TaskExecution."""
from datetime import datetime
from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class TaskExecution(Base):
__tablename__ = "task_executions"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
task_id: Mapped[int] = mapped_column(ForeignKey("scheduled_tasks.id", ondelete="CASCADE"), nullable=False)
agent_id: Mapped[int] = mapped_column(ForeignKey("agents.id", ondelete="CASCADE"), nullable=False)
status: Mapped[str] = mapped_column(String(32), default="running", comment="running / success / failed")
started_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), comment="开始时间")
finished_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="结束时间")
duration_ms: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="耗时(毫秒)")
result: Mapped[str | None] = mapped_column(Text, nullable=True, comment="执行结果摘要 (JSON)")
log: Mapped[str | None] = mapped_column(Text, nullable=True, comment="执行日志")
error_message: Mapped[str | None] = mapped_column(Text, nullable=True, comment="错误信息")
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
task = relationship("ScheduledTask", back_populates="executions")
agent = relationship("Agent")
def __repr__(self) -> str:
return f"<TaskExecution #{self.id} task={self.task_id} status={self.status}>"
+60
View File
@@ -0,0 +1,60 @@
"""SQLAlchemy models — NotificationChannel & AlertRule & AlertHistory."""
from datetime import datetime
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class NotificationChannel(Base):
"""用户绑定的通知渠道"""
__tablename__ = "notification_channels"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(128), nullable=False, comment="渠道名称, e.g. 我的飞书")
channel_type: Mapped[str] = mapped_column(String(32), nullable=False, comment="feishu_cli / email / webhook")
config: Mapped[str] = mapped_column(Text, nullable=False, comment="JSON 配置")
enabled: Mapped[bool] = mapped_column(default=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
alert_rules = relationship("AlertRule", back_populates="channel", cascade="all, delete-orphan")
def __repr__(self) -> str:
return f"<NotificationChannel {self.name} ({self.channel_type})>"
class AlertRule(Base):
"""告警规则:哪些任务触发哪些通知渠道"""
__tablename__ = "alert_rules"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
task_id: Mapped[int] = mapped_column(ForeignKey("scheduled_tasks.id", ondelete="CASCADE"), nullable=False)
channel_id: Mapped[int] = mapped_column(ForeignKey("notification_channels.id", ondelete="CASCADE"), nullable=False)
alert_type: Mapped[str] = mapped_column(String(32), default="missed_run", comment="missed_run / failure")
enabled: Mapped[bool] = mapped_column(default=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
task = relationship("ScheduledTask", back_populates="alert_rules")
channel = relationship("NotificationChannel", back_populates="alert_rules")
def __repr__(self) -> str:
return f"<AlertRule task={self.task_id} → channel={self.channel_id}>"
class Alert(Base):
"""告警历史"""
__tablename__ = "alerts"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
task_id: Mapped[int] = mapped_column(ForeignKey("scheduled_tasks.id", ondelete="SET NULL"), nullable=True)
alert_type: Mapped[str] = mapped_column(String(32), nullable=False, comment="missed_run / failure / timeout")
message: Mapped[str] = mapped_column(Text, nullable=False, comment="告警内容")
status: Mapped[str] = mapped_column(String(32), default="pending", comment="pending / acknowledged / resolved")
acknowledged_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="确认时间")
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
def __repr__(self) -> str:
return f"<Alert #{self.id} type={self.alert_type} status={self.status}>"
+21
View File
@@ -0,0 +1,21 @@
"""SQLAlchemy models — SystemConfig (key-value store for system settings)."""
from datetime import datetime
from sqlalchemy import DateTime, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column
from app.database import Base
class SystemConfig(Base):
"""System-level configuration stored as key-value pairs."""
__tablename__ = "system_config"
key: Mapped[str] = mapped_column(String(128), primary_key=True, comment="配置键名")
value: Mapped[str] = mapped_column(Text, nullable=False, comment="配置值 (JSON)")
description: Mapped[str] = mapped_column(String(256), default="", comment="描述")
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
def __repr__(self) -> str:
return f"<SystemConfig {self.key}={self.value}>"
+36
View File
@@ -0,0 +1,36 @@
"""SQLAlchemy models — ScheduledTask."""
from datetime import datetime
from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
class ScheduledTask(Base):
__tablename__ = "scheduled_tasks"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
agent_id: Mapped[int] = mapped_column(ForeignKey("agents.id", ondelete="CASCADE"), nullable=False)
name: Mapped[str] = mapped_column(String(256), nullable=False, comment="任务名称")
description: Mapped[str] = mapped_column(Text, default="", comment="任务描述")
cron_expression: Mapped[str] = mapped_column(String(64), nullable=False, comment="Cron 表达式, e.g. */5 * * * *")
grace_period: Mapped[int] = mapped_column(Integer, default=300, comment="容忍秒数, 超时未执行则告警")
status: Mapped[str] = mapped_column(String(32), default="active", comment="active / paused / stopped")
# 冗余字段方便查询
last_run_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="最近一次执行时间")
last_run_result: Mapped[str | None] = mapped_column(String(32), nullable=True, comment="最近一次执行结果 success/failed")
next_run_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True, comment="预计下次执行时间")
total_run_count: Mapped[int] = mapped_column(Integer, default=0, comment="累计执行次数")
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now())
agent = relationship("Agent", back_populates="tasks")
executions = relationship("TaskExecution", back_populates="task", cascade="all, delete-orphan")
alert_rules = relationship("AlertRule", back_populates="task", cascade="all, delete-orphan")
def __repr__(self) -> str:
return f"<ScheduledTask {self.name}>"