mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
优化
This commit is contained in:
@@ -20,11 +20,3 @@ type TableDataInfo struct {
|
||||
Filed []string `json:"filed" description:"字段"`
|
||||
Info []map[string]interface{} `json:"info" description:"数据"`
|
||||
}
|
||||
|
||||
// 日志 TDengine
|
||||
type TdLog struct {
|
||||
Ts string `json:"ts" dc:"时间"`
|
||||
Device string `json:"device" dc:"设备标识"`
|
||||
Type string `json:"type" dc:"日志类型"`
|
||||
Content string `json:"content" dc:"日志内容"`
|
||||
}
|
||||
|
||||
@@ -4,22 +4,20 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const connectTableName = "device_connect"
|
||||
const connectTableName = "events"
|
||||
|
||||
type ConnectInfo struct {
|
||||
Ts string `json:"ts"`
|
||||
ClientID string `json:"clientId"`
|
||||
Type string `json:"type"` // 连接类型
|
||||
PeerHost string `json:"peerHost"`
|
||||
SocketPort string `json:"sockPort"`
|
||||
Protocol string `json:"protocol"`
|
||||
DeviceId string `json:"deviceId"`
|
||||
type Events struct {
|
||||
Ts string `json:"ts"`
|
||||
Name string `json:"name"` //标识 connet
|
||||
Type string `json:"type"` // 事件类型 info alarm fault
|
||||
Content string `json:"content"` // 事件描述
|
||||
DeviceId string `json:"deviceId"`
|
||||
}
|
||||
|
||||
// CreateEventTable 创建设备连接事件表
|
||||
func (s *TdEngine) CreateEventTable() (err error) {
|
||||
sql := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s.%s (ts TIMESTAMP,deviceId NCHAR(64),
|
||||
type NCHAR(64),clientId NCHAR(64),peerHost NCHAR(64),sockPort NCHAR(64),protocol NCHAR(64))`, s.dbName, connectTableName)
|
||||
sql := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s.%s (ts TIMESTAMP,deviceId NCHAR(64),name NCHAR(64),
|
||||
type NCHAR(64),content NCHAR(255))`, s.dbName, connectTableName)
|
||||
_, err = s.db.Exec(sql)
|
||||
return
|
||||
}
|
||||
@@ -27,3 +25,25 @@ func (s *TdEngine) CreateEventTable() (err error) {
|
||||
func (s *TdEngine) InsertEvent(data map[string]any) (err error) {
|
||||
return s.InsertDevice(connectTableName, data)
|
||||
}
|
||||
|
||||
func (s *TdEngine) GetAllEvents(sql string, args ...any) (list []Events, err error) {
|
||||
rows, err := s.db.Query(sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var event Events
|
||||
|
||||
err = rows.Scan(&event.Ts, &event.DeviceId, &event.Name, &event.Type, &event.Content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Ts = s.Time(event.Ts)
|
||||
|
||||
list = append(list, event)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,21 +1,37 @@
|
||||
package tdengine
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
const logTableName = "device_log"
|
||||
"github.com/kakuilan/kgo"
|
||||
)
|
||||
|
||||
const logTableName = "logs"
|
||||
|
||||
// 日志 TDengine
|
||||
type TdLog struct {
|
||||
Ts string `json:"ts" dc:"时间"`
|
||||
DeviceId string `json:"deviceId" dc:"设备标识"`
|
||||
TraceId string `json:"traceId" dc:"追踪"`
|
||||
Type string `json:"type" dc:"日志类型"` // 命令调用 上行 下行
|
||||
Content string `json:"content" dc:"日志内容"`
|
||||
}
|
||||
|
||||
// CreateLogStable 添加LOG超级表
|
||||
func (s *TdEngine) CreateLogStable() (err error) {
|
||||
sql := "CREATE STABLE IF NOT EXISTS ? (ts TIMESTAMP, type VARCHAR(20), content VARCHAR(1000)) TAGS (device VARCHAR(255))"
|
||||
sql := "CREATE STABLE IF NOT EXISTS ? (ts TIMESTAMP,deviceId NCHAR(64),traceId NCHAR(64),type NCHAR(20), content VARCHAR(1000))"
|
||||
_, err = s.db.Exec(sql, logTableName)
|
||||
return
|
||||
}
|
||||
|
||||
// InsertLog 写入数据
|
||||
func (s *TdEngine) InsertLog(log *TdLog) (err error) {
|
||||
sql := "INSERT INTO log_? USING device_log TAGS (?) VALUES (?, ?, ?)"
|
||||
_, err = s.db.Exec(sql, log.Device, log.Ts, log.Type, log.Content)
|
||||
|
||||
logs, err := kgo.KConv.Struct2Map(*log, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = s.InsertDevice(logTableName, logs)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -23,7 +39,7 @@ func (s *TdEngine) InsertLog(log *TdLog) (err error) {
|
||||
func (s *TdEngine) ClearLog() (err error) {
|
||||
ts := time.Now().Add(-7 * 24 * time.Hour).Format("2006-01-02")
|
||||
|
||||
sql := "DELETE FROM device_log WHERE ts < ?"
|
||||
sql := fmt.Sprintf("DELETE FROM %s WHERE ts < ?", logTableName)
|
||||
_, err = s.db.Exec(sql, ts)
|
||||
|
||||
return
|
||||
@@ -40,7 +56,7 @@ func (s *TdEngine) GetAllLog(sql string, args ...any) (list []TdLog, err error)
|
||||
for rows.Next() {
|
||||
var log TdLog
|
||||
|
||||
err = rows.Scan(&log.Ts, &log.Type, &log.Content, &log.Device)
|
||||
err = rows.Scan(&log.Ts, &log.DeviceId, &log.TraceId, &log.Type, &log.Content)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user