定义一个配置记录是不是timescaledb

This commit is contained in:
2025-09-24 16:06:05 +08:00
parent 6520f2e9d7
commit b668f3fbb5
7 changed files with 46 additions and 1 deletions

View File

@@ -1,5 +1,11 @@
# 猪场管理系统
## 安装说明
### 推荐使用 TimescaleDB
TimescaleDB 是基于 PostgreSQL 的开源数据库, 专门为处理时序数据而设计的。可以应对后续传海量传感器数据
## 功能介绍
### 一. 猪舍控制

View File

@@ -29,6 +29,7 @@ database:
password: "pig-farm-controller"
dbname: "pig-farm-controller"
sslmode: "disable" # 在生产环境中建议使用 "require"
is_timescaledb: false
max_open_conns: 25 # 最大开放连接数
max_idle_conns: 10 # 最大空闲连接数
conn_max_lifetime: 600 # 连接最大生命周期(秒)

View File

@@ -81,6 +81,9 @@ type DatabaseConfig struct {
// SSLMode SSL模式
SSLMode string `yaml:"sslmode"`
// IsTimescaleDB is timescaledb
IsTimescaleDB bool `yaml:"is_timescaledb"`
// MaxOpenConns 最大开放连接数
MaxOpenConns int `yaml:"max_open_conns"`

View File

@@ -16,6 +16,7 @@ import (
// 使用GORM作为ORM库
type PostgresStorage struct {
db *gorm.DB
isTimescaleDB bool
connectionString string
maxOpenConns int
maxIdleConns int
@@ -25,9 +26,10 @@ type PostgresStorage struct {
// NewPostgresStorage 创建并返回一个新的PostgreSQL存储实例
// 它接收一个 logger 实例,而不是自己创建
func NewPostgresStorage(connectionString string, maxOpenConns, maxIdleConns, connMaxLifetime int, logger *logs.Logger) *PostgresStorage {
func NewPostgresStorage(connectionString string, isTimescaleDB bool, maxOpenConns, maxIdleConns, connMaxLifetime int, logger *logs.Logger) *PostgresStorage {
return &PostgresStorage{
connectionString: connectionString,
isTimescaleDB: isTimescaleDB,
maxOpenConns: maxOpenConns,
maxIdleConns: maxIdleConns,
connMaxLifetime: connMaxLifetime,

View File

@@ -42,9 +42,11 @@ func NewStorage(cfg config.DatabaseConfig, logger *logs.Logger) Storage {
cfg.SSLMode,
)
// 当前默认返回PostgreSQL存储实现并将 logger 注入
// 当前默认返回PostgreSQL存储实现并将 logger 注入
return NewPostgresStorage(
connectionString,
cfg.IsTimescaleDB, // <--- 添加 IsTimescaleDB
cfg.MaxOpenConns,
cfg.MaxIdleConns,
cfg.ConnMaxLifetime,

View File

@@ -0,0 +1,30 @@
package models
import (
"time"
"gorm.io/datatypes"
)
// SensorData 存储所有类型的传感器数据,对应数据库中的 'sensor_data' 表。
type SensorData struct {
// Time 是数据记录的时间戳,作为复合主键的一部分。
// GORM 会将其映射到 'time' TIMESTAMPTZ 列。
Time time.Time `gorm:"primaryKey"`
// DeviceID 是传感器的唯一标识符,作为复合主键的另一部分。
// GORM 会将其映射到 'device_id' VARCHAR(50) 列。
DeviceID string `gorm:"primaryKey;size:50"`
// RegionalControllerID 是上报此数据的区域主控的ID。
// 我们为其添加了数据库索引以优化按区域查询的性能。
RegionalControllerID string `gorm:"size:50;index"`
// Data 存储一个或多个传感器读数,格式为 JSON。
// GORM 会使用 'jsonb' 类型来创建此列。
Data datatypes.JSON `gorm:"type:jsonb"`
}
func (SensorData) TableName() string {
return "sensor_data"
}

View File

@@ -12,5 +12,6 @@ func GetAllModels() []interface{} {
&PlanExecutionLog{},
&TaskExecutionLog{},
&PendingTask{},
&SensorData{},
}
}