超表改造

This commit is contained in:
2025-09-30 21:44:03 +08:00
parent 5022a2be1f
commit 108d496346
3 changed files with 17 additions and 7 deletions

View File

@@ -156,8 +156,8 @@ func (ps *PostgresStorage) creatingHyperTable() error {
}{ }{
{models.SensorData{}, "time"}, {models.SensorData{}, "time"},
{models.DeviceCommandLog{}, "sent_at"}, {models.DeviceCommandLog{}, "sent_at"},
{models.PlanExecutionLog{}, "started_at"}, {models.PlanExecutionLog{}, "created_at"},
{models.TaskExecutionLog{}, "started_at"}, {models.TaskExecutionLog{}, "created_at"},
{models.PendingCollection{}, "created_at"}, {models.PendingCollection{}, "created_at"},
{models.UserActionLog{}, "time"}, {models.UserActionLog{}, "time"},
} }

View File

@@ -25,8 +25,13 @@ const (
) )
// PlanExecutionLog 记录整个计划的一次执行历史 // PlanExecutionLog 记录整个计划的一次执行历史
type PlanExecutionLog struct { type PlanExecutionLog struct {
gorm.Model ID uint `gorm:"primaryKey"`
CreatedAt time.Time `gorm:"primaryKey"` // 作为联合主键方便只查询热点数据
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
PlanID uint `gorm:"index"` PlanID uint `gorm:"index"`
Status ExecutionStatus Status ExecutionStatus
StartedAt time.Time StartedAt time.Time
@@ -41,7 +46,11 @@ func (PlanExecutionLog) TableName() string {
// TaskExecutionLog 记录单个任务的一次执行历史 // TaskExecutionLog 记录单个任务的一次执行历史
type TaskExecutionLog struct { type TaskExecutionLog struct {
gorm.Model ID uint `gorm:"primaryKey"`
CreatedAt time.Time `gorm:"primaryKey"` // 作为联合主键方便只查询热点数据
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
PlanExecutionLogID uint `gorm:"index"` // 关联到某次计划执行 PlanExecutionLogID uint `gorm:"index"` // 关联到某次计划执行
// TaskID 使用 int 类型以容纳特殊的负数ID代表系统任务 // TaskID 使用 int 类型以容纳特殊的负数ID代表系统任务
@@ -100,7 +109,7 @@ type DeviceCommandLog struct {
DeviceID uint `gorm:"not null;index" json:"device_id"` DeviceID uint `gorm:"not null;index" json:"device_id"`
// SentAt 记录下行任务最初发送的时间。 // SentAt 记录下行任务最初发送的时间。
SentAt time.Time `gorm:"not null" json:"sent_at"` SentAt time.Time `gorm:"primaryKey" json:"sent_at"`
// AcknowledgedAt 记录设备确认收到下行消息的时间。 // AcknowledgedAt 记录设备确认收到下行消息的时间。
// 如果设备未确认,则为零值或 NULL。使用指针类型 *time.Time 允许 NULL 值。 // 如果设备未确认,则为零值或 NULL。使用指针类型 *time.Time 允许 NULL 值。
@@ -136,7 +145,7 @@ type PendingCollection struct {
FulfilledAt *time.Time FulfilledAt *time.Time
// CreatedAt 是 GORM 的标准字段,记录请求创建时间。 // CreatedAt 是 GORM 的标准字段,记录请求创建时间。
CreatedAt time.Time CreatedAt time.Time `gorm:"primaryKey"`
} }
// TableName 自定义 GORM 使用的数据库表名 // TableName 自定义 GORM 使用的数据库表名
@@ -175,7 +184,7 @@ type UserActionLog struct {
Time time.Time `gorm:"primaryKey" json:"time"` Time time.Time `gorm:"primaryKey" json:"time"`
// --- Who (谁) --- // --- Who (谁) ---
UserID uint `gorm:"index" json:"user_id,omitempty"` UserID uint `gorm:"primaryKey" json:"user_id,omitempty"`
Username string `json:"username,omitempty"` // 操作发生时用户名的快照 Username string `json:"username,omitempty"` // 操作发生时用户名的快照
// --- Where (何地) --- // --- Where (何地) ---

View File

@@ -25,6 +25,7 @@ func GetAllModels() []interface{} {
&PendingCollection{}, &PendingCollection{},
&AreaController{}, &AreaController{},
&DeviceTemplate{}, &DeviceTemplate{},
&UserActionLog{},
} }
} }