optimize code of sql struct and measurement node recommend api

This commit is contained in:
douxu 2025-12-19 17:33:12 +08:00
parent 70bcb00062
commit 9499e579b3
15 changed files with 159 additions and 161 deletions

View File

@ -4,7 +4,6 @@ package database
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"time" "time"
"modelRT/common/errcode" "modelRT/common/errcode"
@ -26,15 +25,15 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
} }
component := orm.Component{ component := orm.Component{
GlobalUUID: globalUUID, GlobalUUID: globalUUID,
GridTag: strconv.FormatInt(componentInfo.GridID, 10), GridName: componentInfo.GridName,
ZoneTag: strconv.FormatInt(componentInfo.ZoneID, 10), ZoneName: componentInfo.ZoneName,
StationTag: strconv.FormatInt(componentInfo.StationID, 10), StationName: componentInfo.StationName,
Tag: componentInfo.Tag, Tag: componentInfo.Tag,
Name: componentInfo.Name, Name: componentInfo.Name,
Context: componentInfo.Context, Context: componentInfo.Context,
Op: componentInfo.Op, Op: componentInfo.Op,
Ts: time.Now(), Ts: time.Now(),
} }
result := tx.WithContext(cancelCtx).Create(&component) result := tx.WithContext(cancelCtx).Create(&component)

View File

@ -16,9 +16,9 @@ import (
// FillingShortTokenModel define filling short token model info // FillingShortTokenModel define filling short token model info
func FillingShortTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.ShortIdentityTokenModel) error { func FillingShortTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.ShortIdentityTokenModel) error {
filterComponent := &orm.Component{ filterComponent := &orm.Component{
GridTag: identModel.GetGridTag(), GridName: identModel.GetGridName(),
ZoneTag: identModel.GetZoneTag(), ZoneName: identModel.GetZoneName(),
StationTag: identModel.GetStationTag(), StationName: identModel.GetStationName(),
} }
component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent) component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent)
@ -34,10 +34,10 @@ func FillingShortTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.
// FillingLongTokenModel define filling long token model info // FillingLongTokenModel define filling long token model info
func FillingLongTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.LongIdentityTokenModel) error { func FillingLongTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.LongIdentityTokenModel) error {
filterComponent := &orm.Component{ filterComponent := &orm.Component{
GridTag: identModel.GetGridTag(), GridName: identModel.GetGridName(),
ZoneTag: identModel.GetZoneTag(), ZoneName: identModel.GetZoneName(),
StationTag: identModel.GetStationTag(), StationName: identModel.GetStationName(),
Tag: identModel.GetComponentTag(), Tag: identModel.GetComponentTag(),
} }
component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent) component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent)
if err != nil { if err != nil {

View File

@ -4,9 +4,8 @@ package database
import ( import (
"context" "context"
"fmt" "fmt"
"time"
"modelRT/orm" "modelRT/orm"
"time"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
"gorm.io/gorm" "gorm.io/gorm"
@ -137,22 +136,3 @@ func QueryShortIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag s
} }
return &resultComp, &meauserment, nil return &resultComp, &meauserment, nil
} }
// GetGlobalUUIDTagStringMap define func to query global_uuid、tag、nspath field
func GetGlobalUUIDTagStringMap(db *gorm.DB) (map[string]orm.AttributeSet, error) {
var results []orm.Component
resMap := make(map[string]orm.AttributeSet)
err := db.Model(&orm.Component{}).Select("global_uuid", "station", "tag", "nspath").Find(&results).Error
if err != nil {
return nil, err
}
for _, r := range results {
resMap[r.GlobalUUID.String()] = orm.AttributeSet{
Tag: r.Tag,
NSPath: r.NSPath,
}
}
return resMap, nil
}

View File

@ -0,0 +1,27 @@
// Package database define database operation functions
package database
import (
"modelRT/orm"
"gorm.io/gorm"
)
// GenAllAttributeMap define func to query global_uuid、component tag、component nspath field for attribute group
func GenAllAttributeMap(db *gorm.DB) (map[string]orm.AttributeSet, error) {
var compResults []orm.Component
resMap := make(map[string]orm.AttributeSet)
err := db.Model(&orm.Component{}).Select("global_uuid", "station_id", "tag", "nspath").Find(&compResults).Error
if err != nil {
return nil, err
}
for _, r := range compResults {
resMap[r.GlobalUUID.String()] = orm.AttributeSet{
CompTag: r.Tag,
CompNSPath: r.NSPath,
}
}
return resMap, nil
}

View File

@ -63,7 +63,8 @@ func QueryNodeInfoByID(ctx context.Context, tx *gorm.DB, id int64, level int) (o
currentNodeInfo = component currentNodeInfo = component
if err == nil { if err == nil {
var station orm.Station var station orm.Station
err = queryFirstByTag(cancelCtx, tx, component.StationTag, &station) // TODO 修改staion name为通过 station id 查询
err = queryFirstByTag(cancelCtx, tx, component.StationName, &station)
previousNodeInfo = station previousNodeInfo = station
} }
case 5: case 5:

View File

@ -4,7 +4,6 @@ package database
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"time" "time"
"modelRT/common/errcode" "modelRT/common/errcode"
@ -36,15 +35,15 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
} }
updateParams := orm.Component{ updateParams := orm.Component{
GlobalUUID: globalUUID, GlobalUUID: globalUUID,
GridTag: strconv.FormatInt(componentInfo.GridID, 10), GridName: componentInfo.GridName,
ZoneTag: strconv.FormatInt(componentInfo.ZoneID, 10), ZoneName: componentInfo.ZoneName,
StationTag: strconv.FormatInt(componentInfo.StationID, 10), StationName: componentInfo.StationName,
Tag: componentInfo.Tag, Tag: componentInfo.Tag,
Name: componentInfo.Name, Name: componentInfo.Name,
Context: componentInfo.Context, Context: componentInfo.Context,
Op: componentInfo.Op, Op: componentInfo.Op,
Ts: time.Now(), Ts: time.Now(),
} }
result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("GLOBAL_UUID = ?", component.GlobalUUID).Updates(&updateParams) result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("GLOBAL_UUID = ?", component.GlobalUUID).Updates(&updateParams)

View File

@ -2,12 +2,11 @@
package handler package handler
import ( import (
"net/http"
"modelRT/logger" "modelRT/logger"
"modelRT/model" "modelRT/model"
"modelRT/network" "modelRT/network"
"modelRT/util" "modelRT/util"
"net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -109,6 +108,7 @@ func MeasurementRecommendHandler(c *gin.Context) {
payloads = append(payloads, network.MeasurementRecommendPayload{ payloads = append(payloads, network.MeasurementRecommendPayload{
Input: request.Input, Input: request.Input,
Offset: finalOffset, Offset: finalOffset,
RecommendType: recommendResult.RecommendType.String(),
RecommendedList: resultRecommends, RecommendedList: resultRecommends,
}) })
} }

42
main.go
View File

@ -7,25 +7,25 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"modelRT/alert"
"modelRT/config"
"modelRT/database"
"modelRT/diagram"
"modelRT/logger"
"modelRT/model"
"modelRT/pool"
"modelRT/router"
"modelRT/util"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"syscall" "syscall"
"modelRT/alert"
"modelRT/config"
"modelRT/database"
"modelRT/diagram"
locker "modelRT/distributedlock" locker "modelRT/distributedlock"
_ "modelRT/docs" _ "modelRT/docs"
"modelRT/handler"
"modelRT/logger"
"modelRT/model"
"modelRT/pool"
realtimedata "modelRT/real-time-data" realtimedata "modelRT/real-time-data"
"modelRT/router"
"modelRT/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/panjf2000/ants/v2" "github.com/panjf2000/ants/v2"
@ -172,28 +172,6 @@ func main() {
engine := gin.New() engine := gin.New()
router.RegisterRoutes(engine, serviceToken) router.RegisterRoutes(engine, serviceToken)
// real time data api
engine.GET("/ws/rtdatas", handler.RealTimeDataReceivehandler)
// anchor api
engine.POST("/model/anchor_replace", handler.ComponentAnchorReplaceHandler)
// alert api
engine.GET("/alert/events/query", handler.QueryAlertEventHandler)
// real time data api
engine.GET("/rt/datas/query", handler.QueryRealTimeDataHandler)
// dashborad api
dashboard := engine.Group("/dashboard")
{
dashboard.GET("/load", nil)
dashboard.GET("/query", nil)
dashboard.POST("/create", nil)
dashboard.POST("/update", nil)
dashboard.POST("/delete", nil)
}
// Swagger UI // Swagger UI
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

View File

@ -5,11 +5,11 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"modelRT/constants"
"modelRT/diagram"
"modelRT/logger" "modelRT/logger"
"modelRT/orm" "modelRT/orm"
"github.com/gofrs/uuid"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -94,19 +94,31 @@ func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compParamMap
AttributeType: attributeType, AttributeType: attributeType,
AttributeGroup: attributeGroup, AttributeGroup: attributeGroup,
} }
// TODO 将筛选后的 recordSchema 属性值加入到缓存系统中
// TODO 增加 station tag的获取
go storeAttributeGroup(ctx, attrSet, componentUUIDStr, columnParam) go storeAttributeGroup(ctx, attrSet, componentUUIDStr, columnParam)
} }
} }
return nil return nil
} }
func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, params columnParam) { func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, colParams columnParam) {
fmt.Println(ctx) rdb := diagram.GetRedisClientInstance()
fmt.Println(attributeSet) pipe := rdb.Pipeline()
uuid, err := uuid.FromString(compUUIDStr)
fmt.Println(uuid, err) // add token6 hierarchy content
fmt.Println(params) pipe.SAdd(ctx, constants.RedisAllConfigSetKey, colParams.AttributeType)
// add token5-token7 hierarchy collaboration content
specCompMeasKey := fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, attributeSet.CompTag)
attrNameMembers := make([]string, 0, len(colParams.AttributeGroup))
for attrName, attrValue := range colParams.AttributeGroup {
// TODO 增加元件属性组表的列与对应值的缓存存储
fmt.Println(attrValue)
fmt.Println(compUUIDStr)
attrNameMembers = append(attrNameMembers, attrName)
}
pipe.SAdd(ctx, specCompMeasKey, attrNameMembers)
_, err := pipe.Exec(ctx)
if err != nil {
logger.Error(ctx, "init component attribute group recommend content failed", "error", err)
}
} }

View File

@ -7,9 +7,9 @@ import "modelRT/orm"
type IndentityTokenModelInterface interface { type IndentityTokenModelInterface interface {
GetComponentInfo() *orm.Component GetComponentInfo() *orm.Component
GetMeasurementInfo() *orm.Measurement GetMeasurementInfo() *orm.Measurement
GetGridTag() string // token1 GetGridName() string // token1
GetZoneTag() string // token2 GetZoneName() string // token2
GetStationTag() string // token3 GetStationName() string // token3
GetNamespacePath() string // token4(COMPONENT TABLE NSPATH) GetNamespacePath() string // token4(COMPONENT TABLE NSPATH)
GetComponentTag() string // token5(COMPONENT TABLE TAG) GetComponentTag() string // token5(COMPONENT TABLE TAG)
GetAttributeGroup() string // token6(component attribute group information) GetAttributeGroup() string // token6(component attribute group information)
@ -40,14 +40,14 @@ func (l *LongIdentityTokenModel) GetMeasurementInfo() *orm.Measurement {
return l.MeasurementInfo return l.MeasurementInfo
} }
// GetGridTag define function to return the grid tag information in the long identity token // GetGridName define function to return the grid name information in the long identity token
func (l *LongIdentityTokenModel) GetGridTag() string { return l.GridTag } func (l *LongIdentityTokenModel) GetGridName() string { return l.GridTag }
// GetZoneTag define function to return the zone tag information in the long identity token // GetZoneName define function to return the zone name information in the long identity token
func (l *LongIdentityTokenModel) GetZoneTag() string { return l.ZoneTag } func (l *LongIdentityTokenModel) GetZoneName() string { return l.ZoneTag }
// GetStationTag define function to return the station tag information in the long identity token // GetStationName define function to return the station name information in the long identity token
func (l *LongIdentityTokenModel) GetStationTag() string { return l.StationTag } func (l *LongIdentityTokenModel) GetStationName() string { return l.StationTag }
// GetNamespacePath define function to return the namespace path information in the long identity token // GetNamespacePath define function to return the namespace path information in the long identity token
func (l *LongIdentityTokenModel) GetNamespacePath() string { return l.NamespacePath } func (l *LongIdentityTokenModel) GetNamespacePath() string { return l.NamespacePath }
@ -90,14 +90,14 @@ func (s *ShortIdentityTokenModel) GetMeasurementInfo() *orm.Measurement {
return s.MeasurementInfo return s.MeasurementInfo
} }
// GetGridTag define function to return the grid tag information in the short identity token // GetGridName define function to return the grid name information in the short identity token
func (s *ShortIdentityTokenModel) GetGridTag() string { return "" } func (s *ShortIdentityTokenModel) GetGridName() string { return "" }
// GetZoneTag define function to return the zone tag information in the short identity token // GetZoneName define function to return the zone name information in the short identity token
func (s *ShortIdentityTokenModel) GetZoneTag() string { return "" } func (s *ShortIdentityTokenModel) GetZoneName() string { return "" }
// GetStationTag define function to return the station tag information in the short identity token // GetStationName define function to return the station name information in the short identity token
func (s *ShortIdentityTokenModel) GetStationTag() string { return "" } func (s *ShortIdentityTokenModel) GetStationName() string { return "" }
// GetNamespacePath define function to return the namespace path information in the short identity token // GetNamespacePath define function to return the namespace path information in the short identity token
func (s *ShortIdentityTokenModel) GetNamespacePath() string { return s.NamespacePath } func (s *ShortIdentityTokenModel) GetNamespacePath() string { return s.NamespacePath }

View File

@ -25,30 +25,30 @@ type TopologicUUIDCreateInfo struct {
// ComponentCreateInfo defines circuit diagram component create info // ComponentCreateInfo defines circuit diagram component create info
type ComponentCreateInfo struct { type ComponentCreateInfo struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
Name string `json:"name"` Name string `json:"name"`
Context map[string]any `json:"context"` GridName string `json:"grid_name"`
GridID int64 `json:"grid_id"` ZoneName string `json:"zone_name"`
ZoneID int64 `json:"zone_id"` StationName string `json:"station_name"`
StationID int64 `json:"station_id"` Tag string `json:"tag"`
PageID int64 `json:"page_id"` Params string `json:"params"`
Tag string `json:"tag"` PageID int64 `json:"page_id"`
Params string `json:"params"` Op int `json:"op"`
Op int `json:"op"` Context map[string]any `json:"context"`
} }
// MeasurementCreateInfo defines circuit diagram measurement create info // MeasurementCreateInfo defines circuit diagram measurement create info
type MeasurementCreateInfo struct { type MeasurementCreateInfo struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
Name string `json:"name"` Name string `json:"name"`
Context map[string]any `json:"context"` Context map[string]any `json:"context"`
GridID int64 `json:"grid_id"` GridName string `json:"grid_name"`
ZoneID int64 `json:"zone_id"` ZoneName string `json:"zone_name"`
StationID int64 `json:"station_id"` StationName string `json:"station_name"`
PageID int64 `json:"page_id"` PageID int64 `json:"page_id"`
Tag string `json:"tag"` Tag string `json:"tag"`
Params string `json:"params"` Params string `json:"params"`
Op int `json:"op"` Op int `json:"op"`
} }
// CircuitDiagramCreateRequest defines request params of circuit diagram create api // CircuitDiagramCreateRequest defines request params of circuit diagram create api

View File

@ -36,16 +36,16 @@ type TopologicUUIDChangeInfos struct {
// ComponentUpdateInfo defines circuit diagram component params info // ComponentUpdateInfo defines circuit diagram component params info
type ComponentUpdateInfo struct { type ComponentUpdateInfo struct {
ID int64 `json:"id"` ID int64 `json:"id"`
UUID string `json:"uuid"` UUID string `json:"uuid"`
Name string `json:"name"` Name string `json:"name"`
Context map[string]any `json:"context"` GridName string `json:"grid_name"`
GridID int64 `json:"grid_id"` ZoneName string `json:"zone_name"`
ZoneID int64 `json:"zone_id"` StationName string `json:"station_name"`
StationID int64 `json:"station_id"` Params string `json:"params"`
Params string `json:"params"` Tag string `json:"tag"`
Op int `json:"op"` Op int `json:"op"`
Tag string `json:"tag"` Context map[string]any `json:"context"`
} }
// CircuitDiagramUpdateRequest defines request params of circuit diagram update api // CircuitDiagramUpdateRequest defines request params of circuit diagram update api

View File

@ -19,6 +19,7 @@ type SuccessResponse struct {
type MeasurementRecommendPayload struct { type MeasurementRecommendPayload struct {
Input string `json:"input" example:"transformfeeder1_220."` Input string `json:"input" example:"transformfeeder1_220."`
Offset int `json:"offset" example:"21"` Offset int `json:"offset" example:"21"`
RecommendType string `json:"recommended_type" example:"grid_tag"`
RecommendedList []string `json:"recommended_list" example:"[\"I_A_rms\", \"I_B_rms\",\"I_C_rms\"]"` RecommendedList []string `json:"recommended_list" example:"[\"I_A_rms\", \"I_B_rms\",\"I_C_rms\"]"`
} }

View File

@ -1,9 +1,9 @@
// Package orm define database data struct // Package orm define database data struct
package orm package orm
// AttributeSet define struct to return station tag、component Tag、 NSPath field // AttributeSet define struct to return component Tag、 NSPath field
type AttributeSet struct { type AttributeSet struct {
Tag string CompTag string
NSPath string CompNSPath string
StationTag string // StationTag string
} }

View File

@ -9,24 +9,25 @@ import (
// Component structure define abstracted info set of electrical component // Component structure define abstracted info set of electrical component
type Component struct { type Component struct {
GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey"` GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey;default:gen_random_uuid()"`
NSPath string `gorm:"column:nspath"` NSPath string `gorm:"column:nspath;type:varchar(32);not null;default:''"`
Tag string `gorm:"column:tag"` Tag string `gorm:"column:tag;type:varchar(32);not null;uniqueIndex;default:''"`
Name string `gorm:"column:name"` Name string `gorm:"column:name;type:varchar(64);not null;default:''"`
ModelName string `gorm:"column:model_name"` ModelName string `gorm:"column:model_name;type:varchar(64);not null;default:''"`
Description string `gorm:"column:description"` Description string `gorm:"column:description;type:varchar(512);not null;default:''"`
GridTag string `gorm:"column:grid"` GridName string `gorm:"column:grid;type:varchar(64);not null;default:''"`
ZoneTag string `gorm:"column:zone"` ZoneName string `gorm:"column:zone;type:varchar(64);not null;default:''"`
StationTag string `gorm:"column:station"` StationName string `gorm:"column:station;type:varchar(64);not null;default:''"`
Type int `gorm:"column:type"` StationID int64 `gorm:"column:station_id;not null"`
InService bool `gorm:"column:in_service"` Type int `gorm:"column:type;not null;default:-1"`
State int `gorm:"column:state"` InService bool `gorm:"column:in_service;not null;default:false"`
Status int `gorm:"column:status"` State int `gorm:"column:state;not null;default:-1"`
Connection JSONMap `gorm:"column:connection;type:jsonb;default:'{}'"` Status int `gorm:"column:status;not null;default:-1"`
Label JSONMap `gorm:"column:label;type:jsonb;default:'{}'"` Connection JSONMap `gorm:"column:connection;type:jsonb;not null;default:'{}'"`
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"` Label JSONMap `gorm:"column:label;type:jsonb;not null;default:'{}'"`
Op int `gorm:"column:op"` Context JSONMap `gorm:"column:context;type:jsonb;not null;default:'{}'"`
Ts time.Time `gorm:"column:ts"` Op int `gorm:"column:op;not null;default:-1"`
Ts time.Time `gorm:"column:ts;type:timestamptz;not null;default:current_timestamp;autoCreateTime"`
} }
// TableName func respresent return table name of Component // TableName func respresent return table name of Component