From 9499e579b3ae174fa410033dd8c97bdbb9a36222 Mon Sep 17 00:00:00 2001 From: douxu Date: Fri, 19 Dec 2025 17:33:12 +0800 Subject: [PATCH] optimize code of sql struct and measurement node recommend api --- database/create_component.go | 19 +++++----- database/fill_identity_token_model.go | 14 ++++---- database/query_component.go | 22 +----------- database/query_component_attr.go | 27 +++++++++++++++ database/query_node_inof.go | 3 +- database/update_component.go | 19 +++++----- handler/measurement_recommend.go | 4 +-- main.go | 42 ++++++----------------- model/attribute_group_model.go | 34 ++++++++++++------ model/identity_token_model.go | 30 ++++++++-------- network/circuit_diagram_create_request.go | 40 ++++++++++----------- network/circuit_diagram_update_request.go | 20 +++++------ network/response.go | 1 + orm/attribute_group.go | 8 ++--- orm/circuit_diagram_component.go | 37 ++++++++++---------- 15 files changed, 159 insertions(+), 161 deletions(-) create mode 100644 database/query_component_attr.go diff --git a/database/create_component.go b/database/create_component.go index 778ed4b..1c288c0 100644 --- a/database/create_component.go +++ b/database/create_component.go @@ -4,7 +4,6 @@ package database import ( "context" "fmt" - "strconv" "time" "modelRT/common/errcode" @@ -26,15 +25,15 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo } component := orm.Component{ - GlobalUUID: globalUUID, - GridTag: strconv.FormatInt(componentInfo.GridID, 10), - ZoneTag: strconv.FormatInt(componentInfo.ZoneID, 10), - StationTag: strconv.FormatInt(componentInfo.StationID, 10), - Tag: componentInfo.Tag, - Name: componentInfo.Name, - Context: componentInfo.Context, - Op: componentInfo.Op, - Ts: time.Now(), + GlobalUUID: globalUUID, + GridName: componentInfo.GridName, + ZoneName: componentInfo.ZoneName, + StationName: componentInfo.StationName, + Tag: componentInfo.Tag, + Name: componentInfo.Name, + Context: componentInfo.Context, + Op: componentInfo.Op, + Ts: time.Now(), } result := tx.WithContext(cancelCtx).Create(&component) diff --git a/database/fill_identity_token_model.go b/database/fill_identity_token_model.go index d317960..78a0e48 100644 --- a/database/fill_identity_token_model.go +++ b/database/fill_identity_token_model.go @@ -16,9 +16,9 @@ import ( // FillingShortTokenModel define filling short token model info func FillingShortTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.ShortIdentityTokenModel) error { filterComponent := &orm.Component{ - GridTag: identModel.GetGridTag(), - ZoneTag: identModel.GetZoneTag(), - StationTag: identModel.GetStationTag(), + GridName: identModel.GetGridName(), + ZoneName: identModel.GetZoneName(), + StationName: identModel.GetStationName(), } 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 func FillingLongTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.LongIdentityTokenModel) error { filterComponent := &orm.Component{ - GridTag: identModel.GetGridTag(), - ZoneTag: identModel.GetZoneTag(), - StationTag: identModel.GetStationTag(), - Tag: identModel.GetComponentTag(), + GridName: identModel.GetGridName(), + ZoneName: identModel.GetZoneName(), + StationName: identModel.GetStationName(), + Tag: identModel.GetComponentTag(), } component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent) if err != nil { diff --git a/database/query_component.go b/database/query_component.go index 3ebc279..e118726 100644 --- a/database/query_component.go +++ b/database/query_component.go @@ -4,9 +4,8 @@ package database import ( "context" "fmt" - "time" - "modelRT/orm" + "time" "github.com/gofrs/uuid" "gorm.io/gorm" @@ -137,22 +136,3 @@ func QueryShortIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag s } 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 -} diff --git a/database/query_component_attr.go b/database/query_component_attr.go new file mode 100644 index 0000000..c2df2bd --- /dev/null +++ b/database/query_component_attr.go @@ -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 +} diff --git a/database/query_node_inof.go b/database/query_node_inof.go index 3f1689b..713aecc 100644 --- a/database/query_node_inof.go +++ b/database/query_node_inof.go @@ -63,7 +63,8 @@ func QueryNodeInfoByID(ctx context.Context, tx *gorm.DB, id int64, level int) (o currentNodeInfo = component if err == nil { 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 } case 5: diff --git a/database/update_component.go b/database/update_component.go index c2f9f1c..7957bc8 100644 --- a/database/update_component.go +++ b/database/update_component.go @@ -4,7 +4,6 @@ package database import ( "context" "fmt" - "strconv" "time" "modelRT/common/errcode" @@ -36,15 +35,15 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo } updateParams := orm.Component{ - GlobalUUID: globalUUID, - GridTag: strconv.FormatInt(componentInfo.GridID, 10), - ZoneTag: strconv.FormatInt(componentInfo.ZoneID, 10), - StationTag: strconv.FormatInt(componentInfo.StationID, 10), - Tag: componentInfo.Tag, - Name: componentInfo.Name, - Context: componentInfo.Context, - Op: componentInfo.Op, - Ts: time.Now(), + GlobalUUID: globalUUID, + GridName: componentInfo.GridName, + ZoneName: componentInfo.ZoneName, + StationName: componentInfo.StationName, + Tag: componentInfo.Tag, + Name: componentInfo.Name, + Context: componentInfo.Context, + Op: componentInfo.Op, + Ts: time.Now(), } result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("GLOBAL_UUID = ?", component.GlobalUUID).Updates(&updateParams) diff --git a/handler/measurement_recommend.go b/handler/measurement_recommend.go index efa9f7d..ef79916 100644 --- a/handler/measurement_recommend.go +++ b/handler/measurement_recommend.go @@ -2,12 +2,11 @@ package handler import ( - "net/http" - "modelRT/logger" "modelRT/model" "modelRT/network" "modelRT/util" + "net/http" "github.com/gin-gonic/gin" ) @@ -109,6 +108,7 @@ func MeasurementRecommendHandler(c *gin.Context) { payloads = append(payloads, network.MeasurementRecommendPayload{ Input: request.Input, Offset: finalOffset, + RecommendType: recommendResult.RecommendType.String(), RecommendedList: resultRecommends, }) } diff --git a/main.go b/main.go index 2376fda..1ad6baa 100644 --- a/main.go +++ b/main.go @@ -7,25 +7,25 @@ import ( "flag" "fmt" "log" + "modelRT/alert" + "modelRT/config" + "modelRT/database" + "modelRT/diagram" + "modelRT/logger" + "modelRT/model" + "modelRT/pool" + "modelRT/router" + "modelRT/util" "net/http" "os" "os/signal" "path/filepath" "syscall" - "modelRT/alert" - "modelRT/config" - "modelRT/database" - "modelRT/diagram" locker "modelRT/distributedlock" _ "modelRT/docs" - "modelRT/handler" - "modelRT/logger" - "modelRT/model" - "modelRT/pool" + realtimedata "modelRT/real-time-data" - "modelRT/router" - "modelRT/util" "github.com/gin-gonic/gin" "github.com/panjf2000/ants/v2" @@ -172,28 +172,6 @@ func main() { engine := gin.New() 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 engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) diff --git a/model/attribute_group_model.go b/model/attribute_group_model.go index b18eb43..20e60e1 100644 --- a/model/attribute_group_model.go +++ b/model/attribute_group_model.go @@ -5,11 +5,11 @@ import ( "context" "errors" "fmt" - + "modelRT/constants" + "modelRT/diagram" "modelRT/logger" "modelRT/orm" - "github.com/gofrs/uuid" "gorm.io/gorm" ) @@ -94,19 +94,31 @@ func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compParamMap AttributeType: attributeType, AttributeGroup: attributeGroup, } - - // TODO 将筛选后的 recordSchema 属性值加入到缓存系统中 - // TODO 增加 station tag的获取 go storeAttributeGroup(ctx, attrSet, componentUUIDStr, columnParam) } } return nil } -func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, params columnParam) { - fmt.Println(ctx) - fmt.Println(attributeSet) - uuid, err := uuid.FromString(compUUIDStr) - fmt.Println(uuid, err) - fmt.Println(params) +func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, colParams columnParam) { + rdb := diagram.GetRedisClientInstance() + pipe := rdb.Pipeline() + + // add token6 hierarchy content + 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) + } } diff --git a/model/identity_token_model.go b/model/identity_token_model.go index 088de78..e191d29 100644 --- a/model/identity_token_model.go +++ b/model/identity_token_model.go @@ -7,9 +7,9 @@ import "modelRT/orm" type IndentityTokenModelInterface interface { GetComponentInfo() *orm.Component GetMeasurementInfo() *orm.Measurement - GetGridTag() string // token1 - GetZoneTag() string // token2 - GetStationTag() string // token3 + GetGridName() string // token1 + GetZoneName() string // token2 + GetStationName() string // token3 GetNamespacePath() string // token4(COMPONENT TABLE NSPATH) GetComponentTag() string // token5(COMPONENT TABLE TAG) GetAttributeGroup() string // token6(component attribute group information) @@ -40,14 +40,14 @@ func (l *LongIdentityTokenModel) GetMeasurementInfo() *orm.Measurement { return l.MeasurementInfo } -// GetGridTag define function to return the grid tag information in the long identity token -func (l *LongIdentityTokenModel) GetGridTag() string { return l.GridTag } +// GetGridName define function to return the grid name information in the long identity token +func (l *LongIdentityTokenModel) GetGridName() string { return l.GridTag } -// GetZoneTag define function to return the zone tag information in the long identity token -func (l *LongIdentityTokenModel) GetZoneTag() string { return l.ZoneTag } +// GetZoneName define function to return the zone name information in the long identity token +func (l *LongIdentityTokenModel) GetZoneName() string { return l.ZoneTag } -// GetStationTag define function to return the station tag information in the long identity token -func (l *LongIdentityTokenModel) GetStationTag() string { return l.StationTag } +// GetStationName define function to return the station name information in the long identity token +func (l *LongIdentityTokenModel) GetStationName() string { return l.StationTag } // GetNamespacePath define function to return the namespace path information in the long identity token func (l *LongIdentityTokenModel) GetNamespacePath() string { return l.NamespacePath } @@ -90,14 +90,14 @@ func (s *ShortIdentityTokenModel) GetMeasurementInfo() *orm.Measurement { return s.MeasurementInfo } -// GetGridTag define function to return the grid tag information in the short identity token -func (s *ShortIdentityTokenModel) GetGridTag() string { return "" } +// GetGridName define function to return the grid name information in the short identity token +func (s *ShortIdentityTokenModel) GetGridName() string { return "" } -// GetZoneTag define function to return the zone tag information in the short identity token -func (s *ShortIdentityTokenModel) GetZoneTag() string { return "" } +// GetZoneName define function to return the zone name information in the short identity token +func (s *ShortIdentityTokenModel) GetZoneName() string { return "" } -// GetStationTag define function to return the station tag information in the short identity token -func (s *ShortIdentityTokenModel) GetStationTag() string { return "" } +// GetStationName define function to return the station name information in the short identity token +func (s *ShortIdentityTokenModel) GetStationName() string { return "" } // GetNamespacePath define function to return the namespace path information in the short identity token func (s *ShortIdentityTokenModel) GetNamespacePath() string { return s.NamespacePath } diff --git a/network/circuit_diagram_create_request.go b/network/circuit_diagram_create_request.go index 761cb6e..3735fab 100644 --- a/network/circuit_diagram_create_request.go +++ b/network/circuit_diagram_create_request.go @@ -25,30 +25,30 @@ type TopologicUUIDCreateInfo struct { // ComponentCreateInfo defines circuit diagram component create info type ComponentCreateInfo struct { - UUID string `json:"uuid"` - Name string `json:"name"` - Context map[string]any `json:"context"` - GridID int64 `json:"grid_id"` - ZoneID int64 `json:"zone_id"` - StationID int64 `json:"station_id"` - PageID int64 `json:"page_id"` - Tag string `json:"tag"` - Params string `json:"params"` - Op int `json:"op"` + UUID string `json:"uuid"` + Name string `json:"name"` + GridName string `json:"grid_name"` + ZoneName string `json:"zone_name"` + StationName string `json:"station_name"` + Tag string `json:"tag"` + Params string `json:"params"` + PageID int64 `json:"page_id"` + Op int `json:"op"` + Context map[string]any `json:"context"` } // MeasurementCreateInfo defines circuit diagram measurement create info type MeasurementCreateInfo struct { - UUID string `json:"uuid"` - Name string `json:"name"` - Context map[string]any `json:"context"` - GridID int64 `json:"grid_id"` - ZoneID int64 `json:"zone_id"` - StationID int64 `json:"station_id"` - PageID int64 `json:"page_id"` - Tag string `json:"tag"` - Params string `json:"params"` - Op int `json:"op"` + UUID string `json:"uuid"` + Name string `json:"name"` + Context map[string]any `json:"context"` + GridName string `json:"grid_name"` + ZoneName string `json:"zone_name"` + StationName string `json:"station_name"` + PageID int64 `json:"page_id"` + Tag string `json:"tag"` + Params string `json:"params"` + Op int `json:"op"` } // CircuitDiagramCreateRequest defines request params of circuit diagram create api diff --git a/network/circuit_diagram_update_request.go b/network/circuit_diagram_update_request.go index de010c6..06966cd 100644 --- a/network/circuit_diagram_update_request.go +++ b/network/circuit_diagram_update_request.go @@ -36,16 +36,16 @@ type TopologicUUIDChangeInfos struct { // ComponentUpdateInfo defines circuit diagram component params info type ComponentUpdateInfo struct { - ID int64 `json:"id"` - UUID string `json:"uuid"` - Name string `json:"name"` - Context map[string]any `json:"context"` - GridID int64 `json:"grid_id"` - ZoneID int64 `json:"zone_id"` - StationID int64 `json:"station_id"` - Params string `json:"params"` - Op int `json:"op"` - Tag string `json:"tag"` + ID int64 `json:"id"` + UUID string `json:"uuid"` + Name string `json:"name"` + GridName string `json:"grid_name"` + ZoneName string `json:"zone_name"` + StationName string `json:"station_name"` + Params string `json:"params"` + Tag string `json:"tag"` + Op int `json:"op"` + Context map[string]any `json:"context"` } // CircuitDiagramUpdateRequest defines request params of circuit diagram update api diff --git a/network/response.go b/network/response.go index f2a2a89..e8d775a 100644 --- a/network/response.go +++ b/network/response.go @@ -19,6 +19,7 @@ type SuccessResponse struct { type MeasurementRecommendPayload struct { Input string `json:"input" example:"transformfeeder1_220."` 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\"]"` } diff --git a/orm/attribute_group.go b/orm/attribute_group.go index 2232eb0..c0a9731 100644 --- a/orm/attribute_group.go +++ b/orm/attribute_group.go @@ -1,9 +1,9 @@ // Package orm define database data struct 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 { - Tag string - NSPath string - StationTag string + CompTag string + CompNSPath string + // StationTag string } diff --git a/orm/circuit_diagram_component.go b/orm/circuit_diagram_component.go index 968c475..df41bc5 100644 --- a/orm/circuit_diagram_component.go +++ b/orm/circuit_diagram_component.go @@ -9,24 +9,25 @@ import ( // Component structure define abstracted info set of electrical component type Component struct { - GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey"` - NSPath string `gorm:"column:nspath"` - Tag string `gorm:"column:tag"` - Name string `gorm:"column:name"` - ModelName string `gorm:"column:model_name"` - Description string `gorm:"column:description"` - GridTag string `gorm:"column:grid"` - ZoneTag string `gorm:"column:zone"` - StationTag string `gorm:"column:station"` - Type int `gorm:"column:type"` - InService bool `gorm:"column:in_service"` - State int `gorm:"column:state"` - Status int `gorm:"column:status"` - Connection JSONMap `gorm:"column:connection;type:jsonb;default:'{}'"` - Label JSONMap `gorm:"column:label;type:jsonb;default:'{}'"` - Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"` - Op int `gorm:"column:op"` - Ts time.Time `gorm:"column:ts"` + GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey;default:gen_random_uuid()"` + NSPath string `gorm:"column:nspath;type:varchar(32);not null;default:''"` + Tag string `gorm:"column:tag;type:varchar(32);not null;uniqueIndex;default:''"` + Name string `gorm:"column:name;type:varchar(64);not null;default:''"` + ModelName string `gorm:"column:model_name;type:varchar(64);not null;default:''"` + Description string `gorm:"column:description;type:varchar(512);not null;default:''"` + GridName string `gorm:"column:grid;type:varchar(64);not null;default:''"` + ZoneName string `gorm:"column:zone;type:varchar(64);not null;default:''"` + StationName string `gorm:"column:station;type:varchar(64);not null;default:''"` + StationID int64 `gorm:"column:station_id;not null"` + Type int `gorm:"column:type;not null;default:-1"` + InService bool `gorm:"column:in_service;not null;default:false"` + State int `gorm:"column:state;not null;default:-1"` + Status int `gorm:"column:status;not null;default:-1"` + Connection JSONMap `gorm:"column:connection;type:jsonb;not null;default:'{}'"` + Label JSONMap `gorm:"column:label;type:jsonb;not null;default:'{}'"` + Context JSONMap `gorm:"column:context;type:jsonb;not null;default:'{}'"` + 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