Skip to content

Commit

Permalink
Merge pull request kubeedge#5860 from wbc6080/change-deviceID-database
Browse files Browse the repository at this point in the history
Change deviceName to deviceID in mapper data panel
  • Loading branch information
kubeedge-bot authored Sep 24, 2024
2 parents 69e039d + 95c383e commit 47b159a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ func (d *DataBaseConfig) CloseSession() {
}

func (d *DataBaseConfig) AddData(data *common.DataModel) error {
deviceName := data.DeviceName
propertyName := data.PropertyName
namespace := data.Namespace
tableName := namespace + "/" + deviceName + "/" + propertyName
tableName := data.Namespace + "/" + data.DeviceName + "/" + data.PropertyName
datatime := time.Unix(data.TimeStamp/1e3, 0).Format("2006-01-02 15:04:05")

createTable := fmt.Sprintf("CREATE TABLE IF NOT EXISTS `%s` (id INT AUTO_INCREMENT PRIMARY KEY, ts DATETIME NOT NULL,field TEXT)", tableName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ func (d *DataBaseConfig) CloseSession() {

func (d *DataBaseConfig) AddData(data *common.DataModel) error {
ctx := context.Background()
// The key to construct the ordered set, here DeviceName is used as the key
klog.V(1).Infof("deviceName:%s", data.DeviceName)
tableName := data.Namespace + "/" + data.DeviceName
// The key to construct the ordered set, here DeviceID is used as the key
klog.V(4).Infof("tableName:%s", tableName)
// Check if the current ordered set exists
deviceData := "TimeStamp: " + strconv.FormatInt(data.TimeStamp, 10) + " PropertyName: " + data.PropertyName + " data: " + data.Value
// Add data to ordered set. If the ordered set does not exist, it will be created.
_, err := RedisCli.ZAdd(ctx, data.DeviceName, &redis.Z{
_, err := RedisCli.ZAdd(ctx, tableName, &redis.Z{
Score: float64(data.TimeStamp),
Member: deviceData,
}).Result()
Expand All @@ -81,10 +82,10 @@ func (d *DataBaseConfig) AddData(data *common.DataModel) error {
return nil
}

func (d *DataBaseConfig) GetDataByDeviceName(deviceName string) ([]*common.DataModel, error) {
func (d *DataBaseConfig) GetDataByDeviceID(deviceID string) ([]*common.DataModel, error) {
ctx := context.Background()

dataJSON, err := RedisCli.ZRevRange(ctx, deviceName, 0, -1).Result()
dataJSON, err := RedisCli.ZRevRange(ctx, deviceID, 0, -1).Result()
if err != nil {
klog.V(4).Infof("fail query data for deviceName,err:%v", err)
}
Expand All @@ -103,7 +104,7 @@ func (d *DataBaseConfig) GetDataByDeviceName(deviceName string) ([]*common.DataM
return dataModels, nil
}

func (d *DataBaseConfig) GetPropertyDataByDeviceName(deviceName string, propertyData string) ([]*common.DataModel, error) {
func (d *DataBaseConfig) GetPropertyDataByDeviceID(deviceID string, propertyData string) ([]*common.DataModel, error) {
//TODO implement me
return nil, errors.New("implement me")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@ func (d *DataBaseConfig) CloseSessio() {

func (d *DataBaseConfig) AddData(data *common.DataModel) error {

legal_table := strings.Replace(data.DeviceName, "-", "_", -1)
legal_tag := strings.Replace(data.PropertyName, "-", "_", -1)
tableName := data.Namespace + "/" + data.DeviceName
legalTable := strings.Replace(tableName, "-", "_", -1)
legalTag := strings.Replace(data.PropertyName, "-", "_", -1)

stable_name := fmt.Sprintf("SHOW STABLES LIKE '%s'", legal_table)
stabel := fmt.Sprintf("CREATE STABLE %s (ts timestamp, devicename binary(64), propertyname binary(64), data binary(64),type binary(64)) TAGS (localtion binary(64));", legal_table)
stableName := fmt.Sprintf("SHOW STABLES LIKE '%s'", legalTable)
stabel := fmt.Sprintf("CREATE STABLE %s (ts timestamp, deviceid binary(64), propertyname binary(64), data binary(64),type binary(64)) TAGS (localtion binary(64));", legalTable)

datatime := time.Unix(data.TimeStamp/1e3, 0).Format("2006-01-02 15:04:05")
insertSQL := fmt.Sprintf("INSERT INTO %s USING %s TAGS ('%s') VALUES('%v','%s', '%s', '%s', '%s');",
legal_tag, legal_table, legal_tag, datatime, data.DeviceName, data.PropertyName, data.Value, data.Type)
legalTag, legalTable, legalTag, datatime, tableName, data.PropertyName, data.Value, data.Type)

rows, _ := DB.Query(stable_name)
rows, _ := DB.Query(stableName)
defer rows.Close()

if err := rows.Err(); err != nil {
Expand Down Expand Up @@ -97,8 +98,8 @@ func (d *DataBaseConfig) AddData(data *common.DataModel) error {

return nil
}
func (d *DataBaseConfig) GetDataByDeviceName(deviceName string) ([]*common.DataModel, error) {
querySql := fmt.Sprintf("SELECT ts, devicename, propertyname, data, type FROM %s", deviceName)
func (d *DataBaseConfig) GetDataByDeviceID(deviceID string) ([]*common.DataModel, error) {
querySql := fmt.Sprintf("SELECT ts, deviceid, propertyname, data, type FROM %s", deviceID)
rows, err := DB.Query(querySql)
if err != nil {
return nil, err
Expand All @@ -119,17 +120,17 @@ func (d *DataBaseConfig) GetDataByDeviceName(deviceName string) ([]*common.DataM
}
return dataModel, nil
}
func (d *DataBaseConfig) GetPropertyDataByDeviceName(deviceName string, propertyData string) ([]*common.DataModel, error) {
func (d *DataBaseConfig) GetPropertyDataByDeviceID(deviceID string, propertyData string) ([]*common.DataModel, error) {
//TODO implement me
panic("implement me")
return nil, errors.New("implement me")
}
func (d *DataBaseConfig) GetDataByTimeRange(deviceName string, start int64, end int64) ([]*common.DataModel, error) {
func (d *DataBaseConfig) GetDataByTimeRange(deviceID string, start int64, end int64) ([]*common.DataModel, error) {

legal_table := strings.Replace(deviceName, "-", "_", -1)
legalTable := strings.Replace(deviceID, "-", "_", -1)
startTime := time.Unix(start, 0).UTC().Format("2006-01-02 15:04:05")
endTime := time.Unix(end, 0).UTC().Format("2006-01-02 15:04:05")
//Query data within a specified time range
querySQL := fmt.Sprintf("SELECT ts, devicename, propertyname, data, type FROM %s WHERE ts >= '%s' AND ts <= '%s'", legal_table, startTime, endTime)
querySQL := fmt.Sprintf("SELECT ts, deviceid, propertyname, data, type FROM %s WHERE ts >= '%s' AND ts <= '%s'", legalTable, startTime, endTime)
fmt.Println(querySQL)
rows, err := DB.Query(querySQL)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ type DataBaseClient interface {

AddData(data *common.DataModel)

GetDataByDeviceName(deviceName string) ([]*common.DataModel, error)
GetPropertyDataByDeviceName(deviceName string, propertyData string) ([]*common.DataModel, error)
GetDataByDeviceID(deviceID string) ([]*common.DataModel, error)
GetPropertyDataByDeviceID(deviceID string, propertyData string) ([]*common.DataModel, error)
GetDataByTimeRange(start int64, end int64) ([]*common.DataModel, error)

DeleteDataByTimeRange(start int64, end int64) ([]*common.DataModel, error)
Expand Down

0 comments on commit 47b159a

Please sign in to comment.