Skip to content

Commit

Permalink
fix:插入时序数据库时概率报错的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ZyRiven committed Apr 9, 2024
1 parent eca1e5b commit 4ef99d8
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pkg/tsd/internal/tdengine/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gogf/gf/v2/os/gtime"
"sagooiot/pkg/iotModel"
"sagooiot/pkg/tsd/comm"
"sort"
"time"

"strings"
Expand Down Expand Up @@ -45,14 +46,15 @@ func (m *TdEngine) InsertDeviceData(deviceKey string, data iotModel.ReportProper

// getDeviceField 获取设备数据字段
func getDeviceField(data iotModel.ReportPropertyData) []string {
var field = []string{"ts"}
var field []string

for k := range data {
k = comm.TsdColumnName(k)
field = append(field, k)
// 属性上报时间
field = append(field, k+"_time")
}
sort.Strings(field)
return field
}

Expand All @@ -61,7 +63,17 @@ func getDeviceValue(data iotModel.ReportPropertyData) []string {
//ts := time.Now().Format("Y-m-d H:i:s")
//var value = []string{"'" + ts + "'"}
var value []string
for _, v := range data {

var keys []string
// 提取map中的键
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)

// 给key排序后,从map取值
for _, k := range keys {
v := data[k]
value = append(value, "'"+gvar.New(v.Value).String()+"'")
value = append(value, "'"+gtime.New(v.CreateTime).Format("Y-m-d H:i:s")+"'")
}
Expand Down Expand Up @@ -145,7 +157,8 @@ func (m *TdEngine) BatchInsertMultiDeviceData(multiDeviceDataList map[string][]i
for deviceKey, deviceData := range multiDeviceDataList {

table := comm.DeviceTableName(deviceKey)
field := getDeviceField(deviceData[0])
var field = []string{"ts"}
field = append(field, getDeviceField(deviceData[0])...)

ts++

Expand Down

0 comments on commit 4ef99d8

Please sign in to comment.