metric_sample.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. """系统资源监控采样模型(契约1.23)。
  2. MetricSampler 后台线程按固定间隔采集 CPU/内存写入本表,
  3. 支撑系统管理页签的资源趋势图;磁盘分区为实时查询不采样。
  4. 纯时序追加数据:无软删/行版本字段,按保留窗口直接物理删除。
  5. """
  6. from __future__ import annotations
  7. from datetime import datetime
  8. from sqlalchemy import BigInteger, Float, Index, text
  9. from sqlalchemy.dialects.mysql import DATETIME
  10. from sqlalchemy.orm import Mapped, mapped_column
  11. from dms.extensions import db
  12. class MetricSample(db.Model):
  13. __tablename__ = "sys_metric_sample"
  14. __table_args__ = (Index("ix_sys_metric_sample_time", "sampled_at"),)
  15. id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
  16. sampled_at: Mapped[datetime] = mapped_column(
  17. DATETIME(fsp=3),
  18. nullable=False,
  19. server_default=text("CURRENT_TIMESTAMP(3)"),
  20. )
  21. # CPU 使用率(0-100,NULL=本次采集失败)
  22. cpu_percent: Mapped[float | None] = mapped_column(Float, nullable=True)
  23. mem_used_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)
  24. mem_total_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)